This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new f8a1f75aa testing/nettest: Add EPOLLET cases for TCP, UDP and packet 
sockets.
f8a1f75aa is described below

commit f8a1f75aae7e6779cbe7ac176fbe772acbebf4da
Author: Marco Casaroli <[email protected]>
AuthorDate: Wed Nov 25 19:28:27 2026 +0100

    testing/nettest: Add EPOLLET cases for TCP, UDP and packet sockets.
    
    Add cases for EPOLLET on sockets on the loopback device.
    
    cmocka_net_tcp: an EPOLLIN | EPOLLET socket is reported once for each
    new data.  An idle EPOLLOUT | EPOLLET socket is not reported when it
    receives data.  An EPOLLOUT | EPOLLET socket that got EAGAIN from send()
    is reported again when the peer reads (with write buffers).
    
    cmocka_net_udp: an idle EPOLLOUT | EPOLLET socket is not reported when
    other sockets send, and it is reported for each datagram that it sends.
    
    cmocka_net_pkt: the same for a packet socket on lo.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 testing/nettest/CMakeLists.txt       |   6 +-
 testing/nettest/Kconfig              |   9 +-
 testing/nettest/Makefile             |   3 +-
 testing/nettest/pkt/test_pkt.c       |   1 +
 testing/nettest/pkt/test_pkt.h       |   5 +-
 testing/nettest/pkt/test_pkt_lo.c    |  61 +++++++
 testing/nettest/tcp/test_tcp.c       |  11 ++
 testing/nettest/tcp/test_tcp.h       |  32 ++++
 testing/nettest/tcp/test_tcp_epoll.c | 319 +++++++++++++++++++++++++++++++++++
 testing/nettest/udp/test_udp.c       |   2 +
 testing/nettest/udp/test_udp.h       |  12 ++
 testing/nettest/udp/test_udp_epoll.c | 157 +++++++++++++++++
 12 files changed, 610 insertions(+), 8 deletions(-)

diff --git a/testing/nettest/CMakeLists.txt b/testing/nettest/CMakeLists.txt
index c6889ae3b..4f7ea08e5 100644
--- a/testing/nettest/CMakeLists.txt
+++ b/testing/nettest/CMakeLists.txt
@@ -53,7 +53,8 @@ if(CONFIG_TESTING_NET_TEST)
         ${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_iov.c
         ${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_halfclose.c
         ${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_backlog.c)
-      list(APPEND SRCS ${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_procfs.c)
+      list(APPEND SRCS ${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_procfs.c
+           ${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_epoll.c)
     endif()
 
     if(CONFIG_NET_IPv4
@@ -85,7 +86,8 @@ if(CONFIG_TESTING_NET_TEST)
         ${CMAKE_CURRENT_LIST_DIR}/udp/test_udp_common.c
         ${CMAKE_CURRENT_LIST_DIR}/udp/test_udp_v6only.c
         ${CMAKE_CURRENT_LIST_DIR}/udp/test_udp_family.c
-        ${CMAKE_CURRENT_LIST_DIR}/udp/test_udp_bind.c)
+        ${CMAKE_CURRENT_LIST_DIR}/udp/test_udp_bind.c
+        ${CMAKE_CURRENT_LIST_DIR}/udp/test_udp_epoll.c)
 
     nuttx_add_application(
       NAME
diff --git a/testing/nettest/Kconfig b/testing/nettest/Kconfig
index 31992d72d..37709a108 100644
--- a/testing/nettest/Kconfig
+++ b/testing/nettest/Kconfig
@@ -43,8 +43,8 @@ config TESTING_NET_UDP
        ---help---
                Test UDP sockets of AF_INET and AF_INET6 on one port over the
                loopback device: IPV6_V6ONLY (also on TCP sockets), the delivery
-               of IPv4 and IPv6 datagrams, and EADDRINUSE from bind()
-               (cmocka_net_udp).
+               of IPv4 and IPv6 datagrams, EADDRINUSE from bind(), and the
+               EPOLLOUT | EPOLLET events of a socket (cmocka_net_udp).
 
 config TESTING_NET_LOCAL
        bool "Enable cmocka net local test"
@@ -127,8 +127,9 @@ config TESTING_NET_PKT
        default y
        ---help---
                Test packet sockets bound to the loopback device: a frame sent 
on
-               lo comes back, and the reassembly timeout of an IPv4 fragment 
sent
-               on lo does not stop the kernel (cmocka_net_pkt).
+               lo comes back, the reassembly timeout of an IPv4 fragment sent 
on
+               lo does not stop the kernel, and the EPOLLOUT | EPOLLET events 
of
+               a socket (cmocka_net_pkt).
 
 config TESTING_NET_TUN
        bool "Enable cmocka net TUN test"
diff --git a/testing/nettest/Makefile b/testing/nettest/Makefile
index 43c8031d0..668d4d228 100644
--- a/testing/nettest/Makefile
+++ b/testing/nettest/Makefile
@@ -55,7 +55,7 @@ endif
 ifeq ($(CONFIG_NET_IPv4), y)
 CSRCS    += tcp/test_tcp_reset.c tcp/test_tcp_iov.c
 CSRCS    += tcp/test_tcp_halfclose.c tcp/test_tcp_backlog.c
-CSRCS    += tcp/test_tcp_procfs.c
+CSRCS    += tcp/test_tcp_procfs.c tcp/test_tcp_epoll.c
 endif
 
 ifeq 
($(CONFIG_NET_IPv4)$(CONFIG_NET_SOCKOPTS)$(CONFIG_NET_TCP_WRITE_BUFFERS),yyy)
@@ -69,6 +69,7 @@ MAINSRC  += udp/test_udp.c
 PROGNAME += cmocka_net_udp
 CSRCS    += udp/test_udp_common.c udp/test_udp_v6only.c
 CSRCS    += udp/test_udp_family.c udp/test_udp_bind.c
+CSRCS    += udp/test_udp_epoll.c
 endif
 
 ifeq ($(CONFIG_TESTING_NET_LOCAL),y)
diff --git a/testing/nettest/pkt/test_pkt.c b/testing/nettest/pkt/test_pkt.c
index 7e858bc1e..ef5f70521 100644
--- a/testing/nettest/pkt/test_pkt.c
+++ b/testing/nettest/pkt/test_pkt.c
@@ -43,6 +43,7 @@ int main(int argc, FAR char *argv[])
       cmocka_unit_test(test_pkt_lo_send),
       cmocka_unit_test(test_pkt_lo_frag),
       cmocka_unit_test(test_pkt_lo_close),
+      cmocka_unit_test(test_pkt_lo_epollet),
     };
 
   return cmocka_run_group_tests(pkt_tests, NULL, NULL);
diff --git a/testing/nettest/pkt/test_pkt.h b/testing/nettest/pkt/test_pkt.h
index 036dafd05..3d57c290d 100644
--- a/testing/nettest/pkt/test_pkt.h
+++ b/testing/nettest/pkt/test_pkt.h
@@ -39,12 +39,15 @@
  * Description:
  *   Packet sockets bound to the loopback device: a frame sent on lo comes
  *   back, the reassembly timeout of an IPv4 fragment sent on lo does not
- *   stop the kernel, and close() frees the frames queued on a socket.
+ *   stop the kernel, close() frees the frames queued on a socket, and an
+ *   idle EPOLLOUT | EPOLLET socket is not reported for the frames of
+ *   another socket.
  *
  ****************************************************************************/
 
 void test_pkt_lo_send(FAR void **state);
 void test_pkt_lo_frag(FAR void **state);
 void test_pkt_lo_close(FAR void **state);
+void test_pkt_lo_epollet(FAR void **state);
 
 #endif /* __APPS_TESTING_NETTEST_PKT_TEST_PKT_H */
diff --git a/testing/nettest/pkt/test_pkt_lo.c 
b/testing/nettest/pkt/test_pkt_lo.c
index 32b995572..d6c2e7eae 100644
--- a/testing/nettest/pkt/test_pkt_lo.c
+++ b/testing/nettest/pkt/test_pkt_lo.c
@@ -44,6 +44,7 @@
 #include <netinet/in.h>
 #include <netinet/ip.h>
 #include <netpacket/packet.h>
+#include <sys/epoll.h>
 #include <sys/socket.h>
 
 #include "test_pkt.h"
@@ -63,6 +64,7 @@
 #define TEST_PKT_WAIT_MS  2000    /* Time to wait for a frame */
 #define TEST_PKT_ROUNDS   4       /* Open, send and close rounds */
 #define TEST_PKT_NFRAMES  2       /* Packets sent in one round */
+#define TEST_PKT_ETROUNDS 5       /* Sends of the EPOLLET case */
 
 /****************************************************************************
  * Private Functions
@@ -363,3 +365,62 @@ void test_pkt_lo_close(FAR void **state)
   assert_int_equal(test_pkt_close_rounds(true, &nfail), 0);
   assert_int_equal(nfail, 0);
 }
+
+/****************************************************************************
+ * Name: test_pkt_lo_epollet
+ *
+ * Description:
+ *   An EPOLLOUT | EPOLLET packet socket on lo is reported once.  Frames
+ *   that another packet socket sends on lo make the device poll every
+ *   packet socket of lo, and this is no event.  A frame that the socket
+ *   sends itself is an event.
+ *
+ ****************************************************************************/
+
+void test_pkt_lo_epollet(FAR void **state)
+{
+#ifndef CONFIG_NET_PKT_WRITE_BUFFERS
+  struct epoll_event ev;
+  uint8_t frame[64];
+  size_t len;
+  int epfd;
+  int esd;
+  int ssd;
+  int i;
+
+  /* The two sockets use other protocols: neither reads the frames */
+
+  esd = test_pkt_open(TEST_PKT_TYPE + 1);
+  ssd = test_pkt_open(TEST_PKT_TYPE);
+  epfd = epoll_create1(EPOLL_CLOEXEC);
+  assert_return_code(epfd, errno);
+
+  memset(&ev, 0, sizeof(ev));
+  ev.events = EPOLLOUT | EPOLLET;
+  assert_return_code(epoll_ctl(epfd, EPOLL_CTL_ADD, esd, &ev), errno);
+  assert_int_equal(epoll_wait(epfd, &ev, 1, TEST_PKT_WAIT_MS), 1);
+  assert_true((ev.events & EPOLLOUT) != 0);
+
+  len = test_pkt_ipv4(frame, 8, 3, 0);
+  for (i = 0; i < TEST_PKT_ETROUNDS; i++)
+    {
+      assert_int_equal(send(ssd, frame, len, 0), len);
+      usleep(10 * 1000);
+      assert_int_equal(epoll_wait(epfd, &ev, 1, 0), 0);
+    }
+
+  assert_int_equal(send(esd, frame, len, 0), len);
+  assert_int_equal(epoll_wait(epfd, &ev, 1, TEST_PKT_WAIT_MS), 1);
+  assert_true((ev.events & EPOLLOUT) != 0);
+
+  close(epfd);
+  close(ssd);
+  close(esd);
+#else
+  /* With write buffers a send can fail with EAGAIN: each poll of the
+   * device reports POLLOUT, so that no edge is lost.
+   */
+
+  skip();
+#endif
+}
diff --git a/testing/nettest/tcp/test_tcp.c b/testing/nettest/tcp/test_tcp.c
index 0596a9735..333cf95fc 100644
--- a/testing/nettest/tcp/test_tcp.c
+++ b/testing/nettest/tcp/test_tcp.c
@@ -168,6 +168,17 @@ int main(int argc, FAR char *argv[])
       cmocka_unit_test_setup_teardown(test_tcp_recv_iov_nowait,
                                       test_tcp_iov_setup,
                                       test_tcp_iov_teardown),
+      cmocka_unit_test_setup_teardown(test_tcp_epollet_in,
+                                      test_tcp_epoll_setup,
+                                      test_tcp_epoll_teardown),
+      cmocka_unit_test_setup_teardown(test_tcp_epollet_out,
+                                      test_tcp_epoll_setup,
+                                      test_tcp_epoll_teardown),
+#  if defined(CONFIG_NET_TCP_WRITE_BUFFERS) && defined(CONFIG_NET_SOCKOPTS)
+      cmocka_unit_test_setup_teardown(test_tcp_epollet_eagain,
+                                      test_tcp_epoll_setup,
+                                      test_tcp_epoll_teardown),
+#  endif
 #  ifdef CONFIG_NET_TCPBACKLOG
 #    ifdef CONFIG_NET_SOLINGER
       cmocka_unit_test_setup_teardown(test_tcp_backlog_relisten_rst,
diff --git a/testing/nettest/tcp/test_tcp.h b/testing/nettest/tcp/test_tcp.h
index f7e5a839f..45a4d05a8 100644
--- a/testing/nettest/tcp/test_tcp.h
+++ b/testing/nettest/tcp/test_tcp.h
@@ -444,6 +444,38 @@ void test_tcp_recv_iov_empty(FAR void **state);
 
 void test_tcp_recv_iov_nowait(FAR void **state);
 
+/****************************************************************************
+ * Name: test_tcp_epoll_setup
+ ****************************************************************************/
+
+int test_tcp_epoll_setup(FAR void **state);
+
+/****************************************************************************
+ * Name: test_tcp_epoll_teardown
+ ****************************************************************************/
+
+int test_tcp_epoll_teardown(FAR void **state);
+
+/****************************************************************************
+ * Name: test_tcp_epollet_in
+ ****************************************************************************/
+
+void test_tcp_epollet_in(FAR void **state);
+
+/****************************************************************************
+ * Name: test_tcp_epollet_out
+ ****************************************************************************/
+
+void test_tcp_epollet_out(FAR void **state);
+
+#  if defined(CONFIG_NET_TCP_WRITE_BUFFERS) && defined(CONFIG_NET_SOCKOPTS)
+/****************************************************************************
+ * Name: test_tcp_epollet_eagain
+ ****************************************************************************/
+
+void test_tcp_epollet_eagain(FAR void **state);
+#  endif
+
 #  ifdef CONFIG_NET_TCPBACKLOG
 /****************************************************************************
  * Name: test_tcp_backlog_setup
diff --git a/testing/nettest/tcp/test_tcp_epoll.c 
b/testing/nettest/tcp/test_tcp_epoll.c
new file mode 100644
index 000000000..301a033fd
--- /dev/null
+++ b/testing/nettest/tcp/test_tcp_epoll.c
@@ -0,0 +1,319 @@
+/****************************************************************************
+ * apps/testing/nettest/tcp/test_tcp_epoll.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/* EPOLLET on a TCP connection over 127.0.0.1: an epoll set reports each
+ * new event once, and reports no event when nothing changed.
+ */
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <errno.h>
+#include <netinet/in.h>
+#include <poll.h>
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/epoll.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <cmocka.h>
+
+#include "test_tcp.h"
+#include "utils.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define TEST_EPOLL_WAIT    1000  /* ms, time for an event that must come */
+#define TEST_EPOLL_QUIET   300   /* ms, time without an event */
+#define TEST_EPOLL_ROUNDS  5
+#define TEST_EPOLL_BUFSIZE 2048  /* SO_SNDBUF and SO_RCVBUF, bytes */
+#define TEST_EPOLL_NSEND   256   /* Limit of the sends that fill a buffer */
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct tcp_epoll_state_s
+{
+  int fds[3];  /* The listener, the client, the accepted socket */
+  int epfd;
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tcp_epoll_add
+ *
+ * Description:
+ *   Add 'fd' to the epoll set with 'events'.
+ *
+ ****************************************************************************/
+
+static void tcp_epoll_add(FAR struct tcp_epoll_state_s *st, int fd,
+                          uint32_t events)
+{
+  struct epoll_event ev;
+
+  memset(&ev, 0, sizeof(ev));
+  ev.events  = events;
+  ev.data.fd = fd;
+  assert_return_code(epoll_ctl(st->epfd, EPOLL_CTL_ADD, fd, &ev), errno);
+}
+
+/****************************************************************************
+ * Name: tcp_epoll_wait
+ *
+ * Description:
+ *   Wait at most 'ms' milliseconds for one event.  Return the result of
+ *   epoll_wait() and the events in 'events'.
+ *
+ ****************************************************************************/
+
+static int tcp_epoll_wait(FAR struct tcp_epoll_state_s *st, int ms,
+                          FAR uint32_t *events)
+{
+  struct epoll_event ev;
+  int ret;
+
+  memset(&ev, 0, sizeof(ev));
+  ret = epoll_wait(st->epfd, &ev, 1, ms);
+  *events = ev.events;
+  return ret;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: test_tcp_epoll_setup
+ ****************************************************************************/
+
+int test_tcp_epoll_setup(FAR void **state)
+{
+  FAR struct tcp_epoll_state_s *st = malloc(sizeof(*st));
+  struct sockaddr_in addr;
+  socklen_t addrlen = sizeof(addr);
+  struct timeval tv;
+  struct pollfd pfd;
+
+  assert_non_null(st);
+  st->fds[0] = -1;
+  st->fds[1] = -1;
+  st->fds[2] = -1;
+  st->epfd   = -1;
+  *state = st;
+
+  st->fds[0] = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
+  assert_return_code(st->fds[0], errno);
+  nettest_lo_addr((FAR struct sockaddr *)&addr, AF_INET);
+  addr.sin_port = 0;
+  assert_return_code(bind(st->fds[0], (FAR struct sockaddr *)&addr,
+                          sizeof(addr)), errno);
+  assert_return_code(listen(st->fds[0], 1), errno);
+  assert_return_code(getsockname(st->fds[0], (FAR struct sockaddr *)&addr,
+                                 &addrlen), errno);
+
+  st->fds[1] = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
+  assert_return_code(st->fds[1], errno);
+  assert_return_code(connect(st->fds[1], (FAR struct sockaddr *)&addr,
+                             addrlen), errno);
+  pfd.fd      = st->fds[0];
+  pfd.events  = POLLIN;
+  pfd.revents = 0;
+  assert_int_equal(poll(&pfd, 1, TEST_EPOLL_WAIT), 1);
+  st->fds[2] = accept4(st->fds[0], NULL, NULL, SOCK_CLOEXEC);
+  assert_return_code(st->fds[2], errno);
+
+  /* A receive that waits by mistake returns after this time */
+
+  tv.tv_sec  = TEST_EPOLL_WAIT / 1000;
+  tv.tv_usec = (TEST_EPOLL_WAIT % 1000) * 1000;
+  assert_return_code(setsockopt(st->fds[2], SOL_SOCKET, SO_RCVTIMEO, &tv,
+                                sizeof(tv)), errno);
+
+  st->epfd = epoll_create1(EPOLL_CLOEXEC);
+  assert_return_code(st->epfd, errno);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: test_tcp_epoll_teardown
+ ****************************************************************************/
+
+int test_tcp_epoll_teardown(FAR void **state)
+{
+  FAR struct tcp_epoll_state_s *st = *state;
+  int i;
+
+  if (st->epfd >= 0)
+    {
+      close(st->epfd);
+    }
+
+  for (i = 0; i < 3; i++)
+    {
+      if (st->fds[i] >= 0)
+        {
+          close(st->fds[i]);
+        }
+    }
+
+  free(st);
+  return 0;
+}
+
+/****************************************************************************
+ * Name: test_tcp_epollet_in
+ *
+ * Description:
+ *   An EPOLLIN | EPOLLET socket is reported once for each new data, and
+ *   not again until more data comes.
+ *
+ ****************************************************************************/
+
+void test_tcp_epollet_in(FAR void **state)
+{
+  FAR struct tcp_epoll_state_s *st = *state;
+  uint32_t events;
+  char buf[8];
+  int i;
+
+  tcp_epoll_add(st, st->fds[2], EPOLLIN | EPOLLET);
+  assert_int_equal(tcp_epoll_wait(st, TEST_EPOLL_QUIET, &events), 0);
+
+  for (i = 0; i < TEST_EPOLL_ROUNDS; i++)
+    {
+      assert_int_equal(send(st->fds[1], "x", 1, 0), 1);
+      assert_int_equal(tcp_epoll_wait(st, TEST_EPOLL_WAIT, &events), 1);
+      assert_true((events & EPOLLIN) != 0);
+      assert_int_equal(recv(st->fds[2], buf, sizeof(buf), 0), 1);
+      assert_int_equal(tcp_epoll_wait(st, TEST_EPOLL_QUIET, &events), 0);
+    }
+}
+
+/****************************************************************************
+ * Name: test_tcp_epollet_out
+ *
+ * Description:
+ *   An idle EPOLLOUT | EPOLLET socket is reported once.  Data that it
+ *   receives makes the device poll the connection, and this is no event.
+ *
+ ****************************************************************************/
+
+void test_tcp_epollet_out(FAR void **state)
+{
+  FAR struct tcp_epoll_state_s *st = *state;
+  uint32_t events;
+  char buf[8];
+  int i;
+
+  tcp_epoll_add(st, st->fds[2], EPOLLOUT | EPOLLET);
+  assert_int_equal(tcp_epoll_wait(st, TEST_EPOLL_WAIT, &events), 1);
+  assert_true((events & EPOLLOUT) != 0);
+
+  for (i = 0; i < TEST_EPOLL_ROUNDS; i++)
+    {
+      assert_int_equal(send(st->fds[1], "y", 1, 0), 1);
+      assert_int_equal(recv(st->fds[2], buf, sizeof(buf), 0), 1);
+      assert_int_equal(tcp_epoll_wait(st, TEST_EPOLL_QUIET, &events), 0);
+    }
+}
+
+/****************************************************************************
+ * Name: test_tcp_epollet_eagain
+ *
+ * Description:
+ *   An EPOLLOUT | EPOLLET socket fills its send buffer until send()
+ *   fails with EAGAIN.  When the peer reads the data, the socket is
+ *   reported again.
+ *
+ ****************************************************************************/
+
+#if defined(CONFIG_NET_TCP_WRITE_BUFFERS) && defined(CONFIG_NET_SOCKOPTS)
+void test_tcp_epollet_eagain(FAR void **state)
+{
+  FAR struct tcp_epoll_state_s *st = *state;
+  int size = TEST_EPOLL_BUFSIZE;
+  uint32_t events;
+  char buf[256];
+  ssize_t ret = 0;
+  int n;
+  int i;
+
+  assert_return_code(setsockopt(st->fds[1], SOL_SOCKET, SO_SNDBUF, &size,
+                                sizeof(size)), errno);
+  assert_return_code(setsockopt(st->fds[2], SOL_SOCKET, SO_RCVBUF, &size,
+                                sizeof(size)), errno);
+
+  tcp_epoll_add(st, st->fds[1], EPOLLOUT | EPOLLET);
+  assert_int_equal(tcp_epoll_wait(st, TEST_EPOLL_WAIT, &events), 1);
+  assert_true((events & EPOLLOUT) != 0);
+
+  /* The peer does not read: the send buffer fills */
+
+  memset(buf, 'z', sizeof(buf));
+  for (i = 0; i < TEST_EPOLL_NSEND; i++)
+    {
+      ret = send(st->fds[1], buf, sizeof(buf), MSG_DONTWAIT);
+      if (ret < 0)
+        {
+          break;
+        }
+    }
+
+  assert_int_equal(ret, -1);
+  assert_int_equal(errno, EAGAIN);
+
+  /* Let the segments in flight finish, and take their events */
+
+  usleep(TEST_EPOLL_QUIET * 1000);
+  do
+    {
+      n = tcp_epoll_wait(st, 0, &events);
+    }
+  while (n > 0);
+
+  /* The peer reads: the queued data goes out, and its ACK makes room */
+
+  do
+    {
+      ret = recv(st->fds[2], buf, sizeof(buf), MSG_DONTWAIT);
+    }
+  while (ret > 0);
+
+  assert_int_equal(tcp_epoll_wait(st, TEST_EPOLL_WAIT, &events), 1);
+  assert_true((events & EPOLLOUT) != 0);
+}
+#endif
diff --git a/testing/nettest/udp/test_udp.c b/testing/nettest/udp/test_udp.c
index 4e68efd66..ba3a87344 100644
--- a/testing/nettest/udp/test_udp.c
+++ b/testing/nettest/udp/test_udp.c
@@ -74,6 +74,8 @@ int main(int argc, FAR char *argv[])
       TEST_UDP(test_udp_bind_v4_v6only),
       TEST_UDP(test_udp_bind_reuse),
       TEST_UDP(test_udp_bind_v6addr),
+      TEST_UDP(test_udp_epollet_idle),
+      TEST_UDP(test_udp_epollet_send),
     };
 
   return cmocka_run_group_tests(udp_tests, NULL, NULL);
diff --git a/testing/nettest/udp/test_udp.h b/testing/nettest/udp/test_udp.h
index 34fd8d00e..99a0be775 100644
--- a/testing/nettest/udp/test_udp.h
+++ b/testing/nettest/udp/test_udp.h
@@ -163,4 +163,16 @@ void test_udp_bind_v4_v6only(FAR void **state);
 void test_udp_bind_reuse(FAR void **state);
 void test_udp_bind_v6addr(FAR void **state);
 
+/****************************************************************************
+ * Name: test_udp_epollet_*
+ *
+ * Description:
+ *   EPOLLOUT | EPOLLET on a UDP socket: no event while the socket is idle
+ *   and other sockets send, one event for each datagram that it sends.
+ *
+ ****************************************************************************/
+
+void test_udp_epollet_idle(FAR void **state);
+void test_udp_epollet_send(FAR void **state);
+
 #endif /* __APPS_TESTING_NETTEST_UDP_TEST_UDP_H */
diff --git a/testing/nettest/udp/test_udp_epoll.c 
b/testing/nettest/udp/test_udp_epoll.c
new file mode 100644
index 000000000..f34baa26d
--- /dev/null
+++ b/testing/nettest/udp/test_udp_epoll.c
@@ -0,0 +1,157 @@
+/****************************************************************************
+ * apps/testing/nettest/udp/test_udp_epoll.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <errno.h>
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+#include <cmocka.h>
+#include <sys/epoll.h>
+#include <sys/socket.h>
+
+#include "test_udp.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define TEST_UDP_EPOLL_WAIT   1000  /* ms, time for an event that must come */
+#define TEST_UDP_EPOLL_ROUNDS 5
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: test_udp_epoll_out
+ *
+ * Description:
+ *   An epoll set, kept in 'state', with the socket 'sd' for
+ *   EPOLLOUT | EPOLLET.  The first edge (the socket is writable) is taken.
+ *
+ ****************************************************************************/
+
+static int test_udp_epoll_out(FAR void **state, int sd)
+{
+  FAR struct test_udp_state_s *udp_state = *state;
+  struct epoll_event ev;
+  int epfd;
+
+  assert_true(udp_state->nsd < TEST_UDP_NSOCKS);
+  epfd = epoll_create1(EPOLL_CLOEXEC);
+  assert_return_code(epfd, errno);
+  udp_state->sd[udp_state->nsd++] = epfd;
+
+  memset(&ev, 0, sizeof(ev));
+  ev.events = EPOLLOUT | EPOLLET;
+  assert_return_code(epoll_ctl(epfd, EPOLL_CTL_ADD, sd, &ev), errno);
+  assert_int_equal(epoll_wait(epfd, &ev, 1, TEST_UDP_EPOLL_WAIT), 1);
+  assert_true((ev.events & EPOLLOUT) != 0);
+  return epfd;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: test_udp_epollet_idle
+ *
+ * Description:
+ *   An idle EPOLLOUT | EPOLLET socket is reported once.  Datagrams that
+ *   other sockets send make the device poll every UDP socket, and this is
+ *   no event.
+ *
+ ****************************************************************************/
+
+void test_udp_epollet_idle(FAR void **state)
+{
+#ifndef CONFIG_NET_UDP_WRITE_BUFFERS
+  struct epoll_event ev;
+  int port = TEST_UDP_PORT + 30;
+  int epfd;
+  int err;
+  int sd;
+  int i;
+
+  sd = test_udp_socket(state, AF_INET, SOCK_DGRAM, -1, false);
+  assert_return_code(test_udp_bind(sd, AF_INET, port, &err), err);
+  epfd = test_udp_epoll_out(state, sd);
+
+  for (i = 0; i < TEST_UDP_EPOLL_ROUNDS; i++)
+    {
+      test_udp_send4(port + 1);
+      assert_int_equal(epoll_wait(epfd, &ev, 1, 0), 0);
+    }
+#else
+  /* With write buffers a send can fail with EAGAIN: each poll of the
+   * device reports POLLOUT, so that no edge is lost.
+   */
+
+  skip();
+#endif
+}
+
+/****************************************************************************
+ * Name: test_udp_epollet_send
+ *
+ * Description:
+ *   An EPOLLOUT | EPOLLET socket is reported again when it sends a
+ *   datagram.
+ *
+ ****************************************************************************/
+
+void test_udp_epollet_send(FAR void **state)
+{
+  struct epoll_event ev;
+  struct sockaddr_in addr;
+  int port = TEST_UDP_PORT + 32;
+  int epfd;
+  int err;
+  int sd;
+  int i;
+
+  sd = test_udp_socket(state, AF_INET, SOCK_DGRAM, -1, false);
+  assert_return_code(test_udp_bind(sd, AF_INET, port, &err), err);
+  epfd = test_udp_epoll_out(state, sd);
+
+  memset(&addr, 0, sizeof(addr));
+  addr.sin_family      = AF_INET;
+  addr.sin_port        = htons(port + 1);
+  addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+
+  for (i = 0; i < TEST_UDP_EPOLL_ROUNDS; i++)
+    {
+      assert_int_equal(sendto(sd, "et", 2, 0, (FAR struct sockaddr *)&addr,
+                              sizeof(addr)), 2);
+      assert_int_equal(epoll_wait(epfd, &ev, 1, TEST_UDP_EPOLL_WAIT), 1);
+      assert_true((ev.events & EPOLLOUT) != 0);
+    }
+}

Reply via email to