tests: net: socket: tcp: Make test_open_close_immediately more robust

The close function on a TCP socket can return before the complete
socket has been closed, as there might be packets still in flight.
Modify the test to wait briefly (less then the retransmit time), before
counting the number of still open net_contexts. This makes the test
outcome not dependent on the scheduling order of the different tasks.

Secondly the test actually checks the number of open contexts to be zero
so there is no need to wait for any open contexts to still close.

Signed-off-by: Sjors Hettinga <s.a.hettinga@gmail.com>
This commit is contained in:
Sjors Hettinga 2022-05-02 20:04:58 +02:00 committed by Maureen Helm
parent 40ee401dd2
commit cf3466ff74

View file

@ -709,18 +709,29 @@ void test_open_close_immediately(void)
zassert_not_equal(connect(c_sock, (struct sockaddr *)&s_saddr,
sizeof(s_saddr)),
0, "connect succeed");
test_close(c_sock);
/* Allow for the close communication to finish,
* this makes the test success, no longer scheduling dependent
*/
k_sleep(K_MSEC(CONFIG_NET_TCP_INIT_RETRANSMISSION_TIMEOUT / 2));
/* After the client socket closing, the context count should be 1 */
net_context_foreach(calc_net_context, &count_after);
test_close(s_sock);
/* Although closing a server socket does not require communication,
* wait a little to make the test robust to scheduling order
*/
k_sleep(K_MSEC(CONFIG_NET_TCP_INIT_RETRANSMISSION_TIMEOUT / 2));
zassert_equal(count_before - 1, count_after,
"net_context still in use (before %d vs after %d)",
count_before - 1, count_after);
k_sleep(TCP_TEARDOWN_TIMEOUT);
/* No need to wait here, as the test success depends on the socket being closed */
}
void test_connect_timeout(void)