Skip to content

Commit 2b934a2

Browse files
committed
Fix tests-netsocket-udp
Use std::chrono literals and variables where it makes sense
1 parent a3f4cf2 commit 2b934a2

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

TESTS/netsocket/udp/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ void fill_tx_buffer_ascii(char *buff, size_t len)
118118
}
119119
}
120120

121-
int split2half_rmng_udp_test_time()
121+
microseconds split2half_rmng_udp_test_time()
122122
{
123-
return (udp_global::TESTS_TIMEOUT - tc_bucket.read()) / 2;
123+
return (udp_global::TESTS_TIMEOUT - tc_bucket.elapsed_time()) / 2;
124124
}
125125

126126
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
@@ -133,7 +133,7 @@ int fetch_stats()
133133
// Test setup
134134
utest::v1::status_t greentea_setup(const size_t number_of_cases)
135135
{
136-
GREENTEA_SETUP(udp_global::TESTS_TIMEOUT, "default_auto");
136+
GREENTEA_SETUP(seconds(udp_global::TESTS_TIMEOUT).count(), "default_auto");
137137
_ifup();
138138
tc_bucket.start();
139139
return greentea_test_setup_handler(number_of_cases);

TESTS/netsocket/udp/udp_tests.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "../test_params.h"
2222
#include "mbed_trace.h"
2323

24+
using namespace std::chrono;
25+
2426
#define TRACE_GROUP "GRNT"
2527

2628
NetworkInterface *get_interface();
@@ -37,18 +39,18 @@ int fetch_stats(void);
3739
/**
3840
* Single testcase might take only half of the remaining execution time
3941
*/
40-
int split2half_rmng_udp_test_time(); // [s]
42+
microseconds split2half_rmng_udp_test_time();
4143

4244
namespace udp_global {
4345
#ifdef MBED_GREENTEA_TEST_UDPSOCKET_TIMEOUT_S
44-
static const int TESTS_TIMEOUT = MBED_GREENTEA_TEST_UDPSOCKET_TIMEOUT_S;
46+
static constexpr seconds TESTS_TIMEOUT(MBED_GREENTEA_TEST_UDPSOCKET_TIMEOUT_S);
4547
#else
4648
#define MESH 3
4749
#define WISUN 0x2345
4850
#if MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == MESH && MBED_CONF_NSAPI_DEFAULT_MESH_TYPE == WISUN
49-
static const int TESTS_TIMEOUT = (25 * 60);
51+
static constexpr seconds TESTS_TIMEOUT = 25min;
5052
#else
51-
static const int TESTS_TIMEOUT = (20 * 60);
53+
static constexpr seconds TESTS_TIMEOUT = 20min;
5254
#endif
5355
#endif
5456

TESTS/netsocket/udp/udpsocket_echotest.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static bool pkt_received[PKTS] = {false, false, false, false, false, false, fals
5454
};
5555

5656
Timer tc_exec_time;
57-
int time_allotted;
57+
microseconds time_allotted;
5858
}
5959

6060
static void _sigio_handler()
@@ -76,7 +76,7 @@ void UDPSOCKET_ECHOTEST_impl(bool use_sendto)
7676
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.connect(udp_addr));
7777
}
7878

79-
sock.set_timeout(SOCKET_TIMEOUT);
79+
sock.set_timeout(milliseconds(SOCKET_TIMEOUT).count());
8080
int recvd;
8181
int sent;
8282
int packets_sent = 0;
@@ -170,7 +170,7 @@ void UDPSOCKET_ECHOTEST_CONNECT_SEND_RECV()
170170
void UDPSOCKET_ECHOTEST_NONBLOCK_impl(bool use_sendto)
171171
{
172172
tc_exec_time.start();
173-
time_allotted = split2half_rmng_udp_test_time(); // [s]
173+
time_allotted = split2half_rmng_udp_test_time();
174174

175175
SocketAddress udp_addr;
176176
NetworkInterface::get_default_instance()->gethostbyname(ECHO_SERVER_ADDR, &udp_addr);
@@ -212,8 +212,12 @@ void UDPSOCKET_ECHOTEST_NONBLOCK_impl(bool use_sendto)
212212
} else if (sent == pkt_s) {
213213
packets_sent++;
214214
} else if (sent == NSAPI_ERROR_WOULD_BLOCK) {
215-
if (tc_exec_time.read() >= time_allotted ||
216-
signals.wait_all(SIGNAL_SIGIO_TX, SIGIO_TIMEOUT) == osFlagsErrorTimeout) {
215+
if (
216+
(tc_exec_time.elapsed_time() >= time_allotted)
217+
|| signals.wait_all(
218+
SIGNAL_SIGIO_TX, milliseconds(SIGIO_TIMEOUT).count()
219+
) == osFlagsErrorTimeout
220+
) {
217221
continue;
218222
}
219223
--retry_cnt;
@@ -231,10 +235,13 @@ void UDPSOCKET_ECHOTEST_NONBLOCK_impl(bool use_sendto)
231235
}
232236

233237
if (recvd == NSAPI_ERROR_WOULD_BLOCK) {
234-
if (tc_exec_time.read() >= time_allotted) {
238+
if (tc_exec_time.elapsed_time() >= time_allotted) {
235239
break;
236240
}
237-
signals.wait_all(SIGNAL_SIGIO_RX, SIGIO_TIMEOUT);
241+
signals.wait_all(
242+
SIGNAL_SIGIO_RX,
243+
milliseconds(SIGIO_TIMEOUT).count()
244+
);
238245
--retry_recv;
239246
continue;
240247
} else if (recvd < 0) {

0 commit comments

Comments
 (0)