Skip to content

Commit a90d919

Browse files
committed
tests: net: socket: tcp: Add another test case for asynchronous connect
Verify that if the application triggers asynchronous connect with non-blocking sockets, and starts to monitor the socket with poll() immediately but with POLLIN only set, the poll() is still able to report an error if the connection fails. As no POLLOUT is monitored, the application won't know when the connection is done in case of success, but it should still be notified in case of errors. To achieve this, modify the existing test case to allow to specify what events should be monitored by poll(). Additionally monitor the time spent in poll() - an error should cause poll() to exit immediately, not after the specified timeout. Signed-off-by: Robert Lubos <[email protected]>
1 parent a2e42c7 commit a90d919

File tree

1 file changed

+26
-4
lines changed
  • tests/net/socket/tcp/src

1 file changed

+26
-4
lines changed

tests/net/socket/tcp/src/main.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,14 +2288,16 @@ ZTEST(net_socket_tcp, test_connect_and_wait_for_v4_select)
22882288
* make sure poll() returns proper error for the closed
22892289
* connection.
22902290
*/
2291-
ZTEST(net_socket_tcp, test_connect_and_wait_for_v4_poll)
2291+
void test_connect_and_wait_for_v4_poll_common(uint16_t events)
22922292
{
22932293
struct sockaddr_in addr = { 0 };
22942294
struct zsock_pollfd fds[1];
22952295
struct in_addr v4addr;
22962296
int fd, flags, ret, optval;
22972297
bool closed = false;
22982298
socklen_t optlen = sizeof(optval);
2299+
int64_t start_time, time_diff;
2300+
int timeout_ms = 1000;
22992301

23002302
fd = zsock_socket(AF_INET, SOCK_STREAM, 0);
23012303

@@ -2318,14 +2320,24 @@ ZTEST(net_socket_tcp, test_connect_and_wait_for_v4_poll)
23182320
while (1) {
23192321
memset(fds, 0, sizeof(fds));
23202322
fds[0].fd = fd;
2321-
fds[0].events = ZSOCK_POLLOUT;
2323+
fds[0].events = events;
23222324

2323-
/* Check if the connection is there, this should timeout */
2324-
ret = zsock_poll(fds, 1, 10);
2325+
/* Peer should reject the connection with RST as port is closed.
2326+
* Connection error should be notified as soon as it happens,
2327+
* not after a timeout. Otherwise, blocking poll() would never
2328+
* recover.
2329+
*/
2330+
start_time = k_uptime_get();
2331+
2332+
ret = zsock_poll(fds, 1, timeout_ms);
23252333
if (ret < 0) {
23262334
break;
23272335
}
23282336

2337+
time_diff = k_uptime_get() - start_time;
2338+
2339+
zassert(time_diff < timeout_ms, "poll() should quit before a timeout");
2340+
23292341
if (fds[0].revents > 0) {
23302342
if (fds[0].revents & ZSOCK_POLLERR) {
23312343
closed = true;
@@ -2349,6 +2361,16 @@ ZTEST(net_socket_tcp, test_connect_and_wait_for_v4_poll)
23492361
zassert_equal(ret, 0, "close failed, %d", errno);
23502362
}
23512363

2364+
ZTEST(net_socket_tcp, test_connect_and_wait_for_v4_poll_pollout)
2365+
{
2366+
test_connect_and_wait_for_v4_poll_common(ZSOCK_POLLOUT);
2367+
}
2368+
2369+
ZTEST(net_socket_tcp, test_connect_and_wait_for_v4_poll_pollin)
2370+
{
2371+
test_connect_and_wait_for_v4_poll_common(ZSOCK_POLLIN);
2372+
}
2373+
23522374
ZTEST(net_socket_tcp, test_so_keepalive_opt)
23532375
{
23542376
struct sockaddr_in bind_addr4;

0 commit comments

Comments
 (0)