Skip to content

Commit 08e2edd

Browse files
committed
[nrf fromtree] tests: coap_client: Proper slow-down
Use real-time scheduler with 100x speedup, so timeouts are accurate enough, but still fast for tests to run. Signed-off-by: Seppo Takalo <[email protected]> (cherry picked from commit 34a6d5a)
1 parent 4d0e800 commit 08e2edd

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

tests/net/lib/coap_client/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ add_compile_definitions(CONFIG_COAP_CLIENT_MESSAGE_HEADER_SIZE=48)
2626
add_compile_definitions(CONFIG_COAP_CLIENT_STACK_SIZE=1024)
2727
add_compile_definitions(CONFIG_COAP_CLIENT_THREAD_PRIORITY=10)
2828
add_compile_definitions(CONFIG_COAP_LOG_LEVEL=4)
29-
add_compile_definitions(CONFIG_COAP_INIT_ACK_TIMEOUT_MS=100)
29+
add_compile_definitions(CONFIG_COAP_INIT_ACK_TIMEOUT_MS=1000)
3030
add_compile_definitions(CONFIG_COAP_CLIENT_MAX_REQUESTS=2)
3131
add_compile_definitions(CONFIG_COAP_CLIENT_MAX_INSTANCES=2)
3232
add_compile_definitions(CONFIG_COAP_MAX_RETRANSMIT=4)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CONFIG_NATIVE_SIM_SLOWDOWN_TO_REAL_TIME=y

tests/net/lib/coap_client/src/main.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,22 @@
88
#include <zephyr/logging/log.h>
99
#include <zephyr/misc/lorem_ipsum.h>
1010
#include <zephyr/ztest.h>
11-
11+
#if defined(CONFIG_NATIVE_SIM_SLOWDOWN_TO_REAL_TIME)
12+
#include "timer_model.h"
13+
#endif
1214
#include "stubs.h"
1315

1416
LOG_MODULE_REGISTER(coap_client_test, LOG_LEVEL_DBG);
1517

1618
DEFINE_FFF_GLOBALS;
1719
#define FFF_FAKES_LIST(FAKE)
1820

19-
#define LONG_ACK_TIMEOUT_MS 200
21+
#define LONG_ACK_TIMEOUT_MS (2 * CONFIG_COAP_INIT_ACK_TIMEOUT_MS)
2022
#define MORE_THAN_EXCHANGE_LIFETIME_MS 4 * CONFIG_COAP_INIT_ACK_TIMEOUT_MS
2123
#define MORE_THAN_LONG_EXCHANGE_LIFETIME_MS 4 * LONG_ACK_TIMEOUT_MS
2224
#define MORE_THAN_ACK_TIMEOUT_MS \
2325
(CONFIG_COAP_INIT_ACK_TIMEOUT_MS + CONFIG_COAP_INIT_ACK_TIMEOUT_MS / 2)
24-
#define COAP_SEPARATE_TIMEOUT (6000 * 3) /* Needs a safety marging, tests run faster than -rt */
26+
#define COAP_SEPARATE_TIMEOUT (6000 * 2) /* Needs a safety marging, tests run faster than -rt */
2527
#define VALID_MESSAGE_ID BIT(31)
2628
#define TOKEN_OFFSET 4
2729

@@ -425,6 +427,13 @@ extern void net_coap_init(void);
425427

426428
static void *suite_setup(void)
427429
{
430+
#if defined(CONFIG_NATIVE_SIM_SLOWDOWN_TO_REAL_TIME)
431+
/* It is enough that some slow-down is happening on sleeps, it does not have to be
432+
* real time
433+
*/
434+
hwtimer_set_rt_ratio(100.0);
435+
k_sleep(K_MSEC(1));
436+
#endif
428437
net_coap_init();
429438
zassert_ok(coap_client_init(&client, NULL));
430439
zassert_ok(coap_client_init(&client2, NULL));

tests/net/lib/coap_client/src/stubs.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <zephyr/logging/log.h>
88
#include <stubs.h>
99

10-
LOG_MODULE_DECLARE(coap_client_test);
10+
LOG_MODULE_DECLARE(coap_client_test, LOG_LEVEL_DBG);
1111

1212
DEFINE_FAKE_VALUE_FUNC(uint32_t, z_impl_sys_rand32_get);
1313
DEFINE_FAKE_VALUE_FUNC(ssize_t, z_impl_zsock_recvfrom, int, void *, size_t, int, struct sockaddr *,
@@ -45,6 +45,9 @@ int z_impl_zsock_poll(struct zsock_pollfd *fds, int nfds, int poll_timeout)
4545
events++;
4646
}
4747
}
48+
if (events == 0) {
49+
k_sleep(K_MSEC(poll_timeout));
50+
}
4851

4952
return events;
5053
}

0 commit comments

Comments
 (0)