[PATCH rtems-libbsd v3 3/3] testsuite: Wait for the link to be UP

Chris Johns chrisj at rtems.org
Mon Aug 9 01:21:53 UTC 2021


- Wait for a slow PHY to bring the link UP. If the IP address is
  static the test can start before the link is up and the test
  fails.

- Make 2 tests wait. Others will need to be added.
---
 .../include/rtems/bsd/test/default-init.h     | 29 +++++++++++++++++++
 .../rtems/bsd/test/default-network-init.h     | 28 ++++++++++++++++++
 testsuite/nfs01/test_main.c                   |  1 +
 testsuite/ping01/test_main.c                  |  1 +
 4 files changed, 59 insertions(+)

diff --git a/testsuite/include/rtems/bsd/test/default-init.h b/testsuite/include/rtems/bsd/test/default-init.h
index f8ea3acd..ea502f94 100644
--- a/testsuite/include/rtems/bsd/test/default-init.h
+++ b/testsuite/include/rtems/bsd/test/default-init.h
@@ -9,11 +9,31 @@
 #include <assert.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <unistd.h>
+
 #include <rtems/printer.h>
 #include <rtems/test-info.h>
 #include <rtems/stackchk.h>
 #include <rtems/bsd/bsd.h>
 
+static void default_wait_for_link_up( const char *name )
+{
+  size_t seconds = 0;
+  while ( true ) {
+    bool link_active = false;
+    assert(rtems_bsd_iface_link_state( name, &link_active ) == 0);
+    if (link_active) {
+      return;
+    }
+    sleep( 1 );
+    ++seconds;
+    if (seconds > 10) {
+      printf("error: %s: no active link\n", name);
+      assert(seconds < 10);
+    }
+  }
+}
+
 static void default_set_self_prio( rtems_task_priority prio )
 {
   rtems_status_code sc;
@@ -68,6 +88,15 @@ rtems_task Init(
   sc = rtems_task_wake_after( 2 );
   assert(sc == RTEMS_SUCCESSFUL);
 
+#if defined(TEST_WAIT_FOR_LINK)
+  /*
+   * Per test option to wait for the network interface. If the address
+   * is static the PHY may take a while to connect and bring the
+   * interface online.
+   */
+  default_wait_for_link_up( TEST_WAIT_FOR_LINK );
+#endif
+
   test_main();
   /* should not return */
 
diff --git a/testsuite/include/rtems/bsd/test/default-network-init.h b/testsuite/include/rtems/bsd/test/default-network-init.h
index ce1fc015..ba995910 100644
--- a/testsuite/include/rtems/bsd/test/default-network-init.h
+++ b/testsuite/include/rtems/bsd/test/default-network-init.h
@@ -40,6 +40,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <sysexits.h>
+#include <unistd.h>
 
 #include <machine/rtems-bsd-commands.h>
 
@@ -175,6 +176,25 @@ default_network_on_exit(int exit_code, void *arg)
 	}
 }
 
+static void
+default_wait_for_link_up( const char *name )
+{
+	size_t seconds = 0;
+	while ( true ) {
+		bool link_active = false;
+		assert(rtems_bsd_iface_link_state( name, &link_active ) == 0);
+		if (link_active) {
+			return;
+		}
+		sleep( 1 );
+		++seconds;
+		if (seconds > 10) {
+			printf("error: %s: no active link\n", name);
+			assert(seconds < 10);
+		}
+	}
+}
+
 static void
 Init(rtems_task_argument arg)
 {
@@ -238,6 +258,14 @@ Init(rtems_task_argument arg)
 #endif
 	default_network_dhcpcd();
 
+#if defined(TEST_WAIT_FOR_LINK)
+	/*
+	 * Per test option to wait for the network interface. If the address
+	 * is static the PHY may take a while to connect and bring the
+	 * interface online.
+	 */
+	default_wait_for_link_up( TEST_WAIT_FOR_LINK );
+#endif
 	test_main();
 
 	assert(0);
diff --git a/testsuite/nfs01/test_main.c b/testsuite/nfs01/test_main.c
index 2312040a..170cd484 100644
--- a/testsuite/nfs01/test_main.c
+++ b/testsuite/nfs01/test_main.c
@@ -46,6 +46,7 @@
 #include <rtems/bsd/test/network-config.h>
 
 #define TEST_NAME "LIBBSD NFS 1"
+#define TEST_WAIT_FOR_LINK NET_CFG_INTERFACE_0
 #define TEST_STATE_USER_INPUT 1
 
 static void
diff --git a/testsuite/ping01/test_main.c b/testsuite/ping01/test_main.c
index 5702cee2..8b9a42ce 100644
--- a/testsuite/ping01/test_main.c
+++ b/testsuite/ping01/test_main.c
@@ -46,6 +46,7 @@
 #include <rtems/bsd/test/network-config.h>
 
 #define TEST_NAME "LIBBSD PING 1"
+#define TEST_WAIT_FOR_LINK NET_CFG_INTERFACE_0
 
 static void
 test_ping(void)
-- 
2.24.1



More information about the devel mailing list