Skip to content

Commit b4025e8

Browse files
JordanYatesjhedberg
authored andcommitted
tests: net: conn_mgr_nsos: test idle timeout
Test the behaviour of the idle timeout on native sockets. Only tests a subset of the "active" paths to not require any external services. Signed-off-by: Jordan Yates <[email protected]>
1 parent 7521782 commit b4025e8

File tree

1 file changed

+61
-0
lines changed
  • tests/net/conn_mgr_nsos/src

1 file changed

+61
-0
lines changed

tests/net/conn_mgr_nsos/src/main.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <zephyr/net/conn_mgr_monitor.h>
1313
#include <zephyr/net/conn_mgr_connectivity.h>
1414

15+
#include <zephyr/net/socket.h>
16+
1517
K_SEM_DEFINE(l4_connected, 0, 1);
1618
K_SEM_DEFINE(l4_disconnected, 0, 1);
1719

@@ -125,10 +127,69 @@ ZTEST(conn_mgr_nsos, test_conn_mgr_nsos)
125127
0, conn_mgr_if_set_opt(iface, 0, &conn_delay_default, sizeof(conn_delay_default)));
126128
}
127129

130+
ZTEST(conn_mgr_nsos, test_conn_mgr_nsos_idle)
131+
{
132+
struct net_if *iface = net_if_get_default();
133+
struct sockaddr_in v4addr;
134+
int sock, rc;
135+
136+
/* 2 second idle timeout */
137+
conn_mgr_if_set_idle_timeout(iface, 2);
138+
139+
/* Trigger the connection */
140+
zassert_equal(0, conn_mgr_if_connect(iface));
141+
zassert_equal(0, k_sem_take(&l4_connected, K_SECONDS(2)));
142+
143+
/* Connection should terminate after 2 seconds due to inactivity */
144+
zassert_equal(-EAGAIN, k_sem_take(&l4_disconnected, K_MSEC(1900)));
145+
zassert_equal(0, k_sem_take(&l4_disconnected, K_MSEC(500)));
146+
147+
/* Connect again */
148+
zassert_equal(0, conn_mgr_if_connect(iface));
149+
zassert_equal(0, k_sem_take(&l4_connected, K_SECONDS(2)));
150+
151+
/* Send data after a second (to localhost) */
152+
rc = zsock_inet_pton(AF_INET, "127.0.0.1", (void *)&v4addr);
153+
zassert_equal(1, rc);
154+
v4addr.sin_family = AF_INET;
155+
v4addr.sin_port = htons(1234);
156+
157+
sock = zsock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
158+
rc = zsock_sendto(sock, "TEST", 4, 0, (const struct sockaddr *)&v4addr, sizeof(v4addr));
159+
zassert_equal(4, rc);
160+
161+
/* Should have reset the idle timeout */
162+
zassert_equal(-EAGAIN, k_sem_take(&l4_disconnected, K_MSEC(1900)));
163+
zassert_equal(0, k_sem_take(&l4_disconnected, K_MSEC(500)));
164+
165+
/* Set the interface to persistent */
166+
conn_mgr_if_set_flag(iface, CONN_MGR_IF_PERSISTENT, true);
167+
168+
/* Trigger the connection */
169+
zassert_equal(0, conn_mgr_if_connect(iface));
170+
zassert_equal(0, k_sem_take(&l4_connected, K_SECONDS(2)));
171+
172+
/* Interface should disconnect due to idle */
173+
zassert_equal(0, k_sem_take(&l4_disconnected, K_MSEC(2100)));
174+
/* But it should also come back up automatically */
175+
zassert_equal(0, k_sem_take(&l4_connected, K_SECONDS(2)));
176+
177+
/* Clear the persistent flag, times out and doesn't reconnect */
178+
conn_mgr_if_set_flag(iface, CONN_MGR_IF_PERSISTENT, false);
179+
zassert_equal(0, k_sem_take(&l4_disconnected, K_MSEC(2100)));
180+
zassert_equal(-EAGAIN, k_sem_take(&l4_connected, K_MSEC(2100)));
181+
182+
/* Cleanup socket */
183+
zsock_close(sock);
184+
}
185+
128186
static void test_init(void *state)
129187
{
188+
struct net_if *iface = net_if_get_default();
189+
130190
k_sem_take(&l4_connected, K_NO_WAIT);
131191
k_sem_take(&l4_disconnected, K_NO_WAIT);
192+
conn_mgr_if_set_idle_timeout(iface, CONN_MGR_IF_NO_TIMEOUT);
132193
}
133194

134195
static void test_after(void *fixture)

0 commit comments

Comments
 (0)