Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests-netsocket-udp: UDPSOCKET_ECHOTEST_NONBLOCK fixes #9570

Merged
merged 2 commits into from Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 27 additions & 17 deletions TESTS/netsocket/udp/main.cpp
Expand Up @@ -33,10 +33,26 @@

using namespace utest::v1;

namespace {
Timer tc_bucket; // Timer to limit a test cases run time
}

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
mbed_stats_socket_t udp_stats[MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT];
#endif

void drop_bad_packets(UDPSocket &sock, int orig_timeout)
{
nsapi_error_t err;
sock.set_timeout(0);
while (true) {
err = sock.recv(NULL, 0);
if (err == NSAPI_ERROR_WOULD_BLOCK) {
break;
}
}
sock.set_timeout(orig_timeout);
}
static void _ifup()
{
NetworkInterface *net = NetworkInterface::get_default_instance();
Expand All @@ -51,18 +67,6 @@ static void _ifdown()
printf("MBED: ifdown\n");
}

void drop_bad_packets(UDPSocket &sock, int orig_timeout)
{
nsapi_error_t err;
sock.set_timeout(0);
while (true) {
err = sock.recvfrom(NULL, 0, 0);
if (err == NSAPI_ERROR_WOULD_BLOCK) {
break;
}
}
sock.set_timeout(orig_timeout);
}

void fill_tx_buffer_ascii(char *buff, size_t len)
{
Expand All @@ -71,6 +75,11 @@ void fill_tx_buffer_ascii(char *buff, size_t len)
}
}

int split2half_rmng_udp_test_time()
{
return (udp_global::TESTS_TIMEOUT - tc_bucket.read()) / 2;
}

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
int fetch_stats()
{
Expand All @@ -81,20 +90,20 @@ int fetch_stats()
// Test setup
utest::v1::status_t greentea_setup(const size_t number_of_cases)
{
GREENTEA_SETUP(480, "default_auto");
GREENTEA_SETUP(udp_global::TESTS_TIMEOUT, "default_auto");
_ifup();
tc_bucket.start();
return greentea_test_setup_handler(number_of_cases);
}

void greentea_teardown(const size_t passed, const size_t failed, const failure_t failure)
{
tc_bucket.stop();
_ifdown();
return greentea_test_teardown_handler(passed, failed, failure);
}

Case cases[] = {
Case("UDPSOCKET_ECHOTEST", UDPSOCKET_ECHOTEST),
Case("UDPSOCKET_ECHOTEST_NONBLOCK", UDPSOCKET_ECHOTEST_NONBLOCK),
Case("UDPSOCKET_OPEN_CLOSE_REPEAT", UDPSOCKET_OPEN_CLOSE_REPEAT),
Case("UDPSOCKET_OPEN_LIMIT", UDPSOCKET_OPEN_LIMIT),
Case("UDPSOCKET_RECV_TIMEOUT", UDPSOCKET_RECV_TIMEOUT),
Expand All @@ -110,10 +119,11 @@ Case cases[] = {
Case("UDPSOCKET_BIND_WRONG_TYPE", UDPSOCKET_BIND_WRONG_TYPE),
Case("UDPSOCKET_BIND_UNOPENED", UDPSOCKET_BIND_UNOPENED),
Case("UDPSOCKET_SENDTO_INVALID", UDPSOCKET_SENDTO_INVALID),
Case("UDPSOCKET_ECHOTEST", UDPSOCKET_ECHOTEST),
Case("UDPSOCKET_ECHOTEST_BURST", UDPSOCKET_ECHOTEST_BURST),
Case("UDPSOCKET_ECHOTEST_NONBLOCK", UDPSOCKET_ECHOTEST_NONBLOCK),
Case("UDPSOCKET_ECHOTEST_BURST_NONBLOCK", UDPSOCKET_ECHOTEST_BURST_NONBLOCK),
Case("UDPSOCKET_SENDTO_REPEAT", UDPSOCKET_SENDTO_REPEAT),
Case("UDPSOCKET_ECHOTEST", UDPSOCKET_ECHOTEST),
Case("UDPSOCKET_ECHOTEST_BURST", UDPSOCKET_ECHOTEST_BURST),
};

Specification specification(greentea_setup, cases, greentea_teardown, greentea_continue_handlers);
Expand Down
9 changes: 9 additions & 0 deletions TESTS/netsocket/udp/udp_tests.h
Expand Up @@ -27,6 +27,15 @@ extern mbed_stats_socket_t udp_stats[MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT];
int fetch_stats(void);
#endif

/**
* Single testcase might take only half of the remaining execution time
*/
int split2half_rmng_udp_test_time(); // [s]

namespace udp_global {
static const int TESTS_TIMEOUT = 480;
}

/*
* Test cases
*/
Expand Down
13 changes: 11 additions & 2 deletions TESTS/netsocket/udp/udpsocket_echotest.cpp
Expand Up @@ -45,6 +45,8 @@ static const int pkt_sizes[PKTS] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, \
100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, \
1100, 1200
};
Timer tc_exec_time;
int time_allotted;
}

static void _sigio_handler(osThreadId id)
Expand Down Expand Up @@ -106,6 +108,9 @@ void udpsocket_echotest_nonblock_receiver(void *receive_bytes)
for (int retry_cnt = 0; retry_cnt <= RETRIES; retry_cnt++) {
recvd = sock.recvfrom(NULL, rx_buffer, expt2recv);
if (recvd == NSAPI_ERROR_WOULD_BLOCK) {
if (tc_exec_time.read() >= time_allotted) {
break;
}
wait_ms(WAIT2RECV_TIMEOUT);
--retry_cnt;
continue;
Expand All @@ -118,7 +123,7 @@ void udpsocket_echotest_nonblock_receiver(void *receive_bytes)
}
}

drop_bad_packets(sock, -1); // timeout equivalent to set_blocking(false)
drop_bad_packets(sock, 0); // timeout equivalent to set_blocking(false)

tx_sem.release();
}
Expand All @@ -132,6 +137,8 @@ void UDPSOCKET_ECHOTEST_NONBLOCK()
TEST_ASSERT_EQUAL(SOCK_CLOSED, udp_stats[j].state);
}
#endif
tc_exec_time.start();
time_allotted = split2half_rmng_udp_test_time(); // [s]

SocketAddress udp_addr;
NetworkInterface::get_default_instance()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &udp_addr);
Expand Down Expand Up @@ -166,7 +173,8 @@ void UDPSOCKET_ECHOTEST_NONBLOCK()
packets_sent++;
}
if (sent == NSAPI_ERROR_WOULD_BLOCK) {
if (osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
if (tc_exec_time.read() >= time_allotted ||
osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
continue;
}
--retry_cnt;
Expand Down Expand Up @@ -209,4 +217,5 @@ void UDPSOCKET_ECHOTEST_NONBLOCK()
#endif
}
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
tc_exec_time.stop();
}