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

Makes Greentea TCP test cases to timeout less in connection errors #7578

Merged
merged 1 commit into from Jul 26, 2018
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
30 changes: 23 additions & 7 deletions TESTS/netsocket/tcp/main.cpp
Expand Up @@ -32,6 +32,7 @@ using namespace utest::v1;
namespace
{
NetworkInterface* net;
Timer tc_bucket; // Timer to limit a test cases run time
}

char tcp_global::rx_buffer[RX_BUFF_SIZE];
Expand Down Expand Up @@ -66,24 +67,32 @@ static void _ifdown() {
printf("MBED: ifdown\n");
}

void tcpsocket_connect_to_echo_srv(TCPSocket& sock) {
nsapi_error_t tcpsocket_connect_to_echo_srv(TCPSocket& sock) {
SocketAddress tcp_addr;

get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &tcp_addr);
tcp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT);

TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(get_interface()));
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.connect(tcp_addr));
nsapi_error_t err = sock.open(get_interface());
if (err != NSAPI_ERROR_OK) {
return err;
}

return sock.connect(tcp_addr);
}

void tcpsocket_connect_to_discard_srv(TCPSocket& sock) {
nsapi_error_t tcpsocket_connect_to_discard_srv(TCPSocket& sock) {
SocketAddress tcp_addr;

get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &tcp_addr);
tcp_addr.set_port(9);

TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(get_interface()));
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.connect(tcp_addr));
nsapi_error_t err = sock.open(get_interface());
if (err != NSAPI_ERROR_OK) {
return err;
}

return sock.connect(tcp_addr);
}

void fill_tx_buffer_ascii(char *buff, size_t len)
Expand All @@ -93,16 +102,23 @@ void fill_tx_buffer_ascii(char *buff, size_t len)
}
}

int split2half_rmng_tcp_test_time()
{
return (tcp_global::TESTS_TIMEOUT-tc_bucket.read())/2;
}

// Test setup
utest::v1::status_t greentea_setup(const size_t number_of_cases)
{
GREENTEA_SETUP(480, "default_auto");
GREENTEA_SETUP(tcp_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);
}
Expand Down
10 changes: 8 additions & 2 deletions TESTS/netsocket/tcp/tcp_tests.h
Expand Up @@ -21,11 +21,17 @@
NetworkInterface* get_interface();
void drop_bad_packets(TCPSocket& sock, int orig_timeout);
void fill_tx_buffer_ascii(char *buff, size_t len);
void tcpsocket_connect_to_echo_srv(TCPSocket& sock);
void tcpsocket_connect_to_discard_srv(TCPSocket& sock);
nsapi_error_t tcpsocket_connect_to_echo_srv(TCPSocket& sock);
nsapi_error_t tcpsocket_connect_to_discard_srv(TCPSocket& sock);

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

namespace tcp_global
{
static const int TESTS_TIMEOUT = 480;
static const int TCP_OS_STACK_SIZE = 1024;

static const int RX_BUFF_SIZE = 1220;
Expand Down
35 changes: 32 additions & 3 deletions TESTS/netsocket/tcp/tcpsocket_echotest.cpp
Expand Up @@ -37,6 +37,9 @@ namespace
1100,1200};
TCPSocket sock;
Semaphore tx_sem(0, 1);

Timer tc_exec_time;
int time_allotted;
}

static void _sigio_handler(osThreadId id) {
Expand All @@ -45,7 +48,10 @@ static void _sigio_handler(osThreadId id) {

void TCPSOCKET_ECHOTEST()
{
tcpsocket_connect_to_echo_srv(sock);
if (tcpsocket_connect_to_echo_srv(sock) != NSAPI_ERROR_OK) {
TEST_FAIL();
return;
}

int recvd;
int sent;
Expand All @@ -57,6 +63,8 @@ void TCPSOCKET_ECHOTEST()
if (sent < 0) {
printf("[Round#%02d] network error %d\n", x, sent);
TEST_FAIL();
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
return;
}

int bytes2recv = sent;
Expand All @@ -65,6 +73,8 @@ void TCPSOCKET_ECHOTEST()
if (recvd < 0) {
printf("[Round#%02d] network error %d\n", x, recvd);
TEST_FAIL();
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
return;
}
bytes2recv -= recvd;
}
Expand All @@ -80,10 +90,15 @@ void tcpsocket_echotest_nonblock_receiver(void *receive_bytes)
while (bytes2recv) {
recvd = sock.recv(&(tcp_global::rx_buffer[*(int*)receive_bytes-bytes2recv]), bytes2recv);
if (recvd == NSAPI_ERROR_WOULD_BLOCK) {
if (tc_exec_time.read() >= time_allotted) {
TEST_FAIL();
break;
}
wait(1);
continue;
} else if (recvd < 0) {
TEST_FAIL();
break;
}
bytes2recv -= recvd;
}
Expand All @@ -99,6 +114,9 @@ void tcpsocket_echotest_nonblock_receiver(void *receive_bytes)

void TCPSOCKET_ECHOTEST_NONBLOCK()
{
tc_exec_time.start();
time_allotted = split2half_rmng_tcp_test_time(); // [s]

tcpsocket_connect_to_echo_srv(sock);
sock.set_blocking(false);
sock.sigio(callback(_sigio_handler, Thread::gettid()));
Expand All @@ -124,19 +142,30 @@ void TCPSOCKET_ECHOTEST_NONBLOCK()
while (bytes2send > 0) {
sent = sock.send(&(tcp_global::tx_buffer[pkt_s-bytes2send]), bytes2send);
if (sent == NSAPI_ERROR_WOULD_BLOCK) {
TEST_ASSERT_NOT_EQUAL(osEventTimeout, osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status);
if (tc_exec_time.read() >= time_allotted ||
osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
thread->terminate();
delete thread;
TEST_FAIL();
goto END;
}
continue;
} else if (sent <= 0) {
printf("[Sender#%02d] network error %d\n", s_idx, sent);
thread->terminate();
delete thread;
TEST_FAIL();
goto END;
}
bytes2send -= sent;
}
printf("[Sender#%02d] bytes sent: %d\n", s_idx, pkt_s);
tx_sem.wait();
tx_sem.wait(split2half_rmng_tcp_test_time());
thread->join();
delete thread;
}
END:
tc_exec_time.stop();
free(stack_mem);
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
}
13 changes: 12 additions & 1 deletion TESTS/netsocket/tcp/tcpsocket_echotest_burst.cpp
Expand Up @@ -57,11 +57,13 @@ void TCPSOCKET_ECHOTEST_BURST()
if (sent == NSAPI_ERROR_WOULD_BLOCK) {
if(osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
TEST_FAIL();
goto END;
}
continue;
} else if (sent < 0) {
printf("[%02d] network error %d\n", i, sent);
TEST_FAIL();
goto END;
}
bt_left -= sent;
}
Expand All @@ -79,10 +81,12 @@ void TCPSOCKET_ECHOTEST_BURST()
if (bt_left != 0) {
drop_bad_packets(sock, 0);
TEST_FAIL();
goto END;
}

TEST_ASSERT_EQUAL(0, memcmp(tcp_global::tx_buffer, tcp_global::rx_buffer, BURST_SIZE));
}
END:
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
}

Expand All @@ -106,15 +110,20 @@ void TCPSOCKET_ECHOTEST_BURST_NONBLOCK()
if (sent == NSAPI_ERROR_WOULD_BLOCK) {
if(osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
TEST_FAIL();
goto END;
}
continue;
} else if (sent < 0) {
printf("[%02d] network error %d\n", i, sent);
TEST_FAIL();
goto END;
}
bt_left -= sent;
}
TEST_ASSERT_EQUAL(0, bt_left);
if (bt_left != 0) {
TEST_FAIL();
goto END;
}

bt_left = BURST_SIZE;
while (bt_left > 0) {
Expand All @@ -136,9 +145,11 @@ void TCPSOCKET_ECHOTEST_BURST_NONBLOCK()
printf("network error %d, missing %d bytes from a burst\n", recvd, bt_left);
drop_bad_packets(sock, -1);
TEST_FAIL();
goto END;
}

TEST_ASSERT_EQUAL(0, memcmp(tcp_global::tx_buffer, tcp_global::rx_buffer, BURST_SIZE));
}
END:
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
}
26 changes: 20 additions & 6 deletions TESTS/netsocket/tcp/tcpsocket_endpoint_close.cpp
Expand Up @@ -35,24 +35,34 @@ static void _sigio_handler(osThreadId id) {
osSignalSet(id, SIGNAL_SIGIO);
}

static void _tcpsocket_connect_to_daytime_srv(TCPSocket& sock) {
static nsapi_error_t _tcpsocket_connect_to_daytime_srv(TCPSocket& sock) {
SocketAddress tcp_addr;

get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &tcp_addr);
tcp_addr.set_port(13);

TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(get_interface()));
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.connect(tcp_addr));
nsapi_error_t err = sock.open(get_interface());
if (err != NSAPI_ERROR_OK) {
return err;
}

return sock.connect(tcp_addr);
}


void TCPSOCKET_ENDPOINT_CLOSE()
{
static const int MORE_THAN_AVAILABLE = 30;
char buff[MORE_THAN_AVAILABLE];
int time_allotted = split2half_rmng_tcp_test_time(); // [s]
Timer tc_exec_time;
tc_exec_time.start();

TCPSocket sock;
_tcpsocket_connect_to_daytime_srv(sock);
if (_tcpsocket_connect_to_daytime_srv(sock) != NSAPI_ERROR_OK) {
TEST_FAIL();
return;
}
sock.sigio(callback(_sigio_handler, Thread::gettid()));

int recvd = 0;
Expand All @@ -61,16 +71,20 @@ void TCPSOCKET_ENDPOINT_CLOSE()
recvd = sock.recv(&(buff[recvd_total]), MORE_THAN_AVAILABLE);
if (recvd_total > 0 && recvd == 0) {
break; // Endpoint closed socket, success
} else if (recvd == 0) {
} else if (recvd <= 0) {
TEST_FAIL();
break;
} else if (recvd == 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) {
TEST_FAIL();
break;
}
continue;
}
recvd_total += recvd;
TEST_ASSERT(recvd_total < MORE_THAN_AVAILABLE);
}
tc_exec_time.stop();
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
}