[at-spi2-atk: 2/4] tests: use sub-second sleeps to wait for application startup



commit 18922d25f2c6ed37ecbf893e6062e87ebc3e66ff
Author: Samuel Thibault <samuel thibault ens-lyon org>
Date:   Sun Aug 25 20:46:30 2019 +0200

    tests: use sub-second sleeps to wait for application startup
    
    This allows to reduce the testsuite completion time from almost 3 minutes to
    about 3s.
    
    This however showed that we were not waiting for the application to
    terminate. Add terminate_app() which ensures this, to be used instead of
    only killing the application.

 tests/atk_test_accessible.c    |  2 +-
 tests/atk_test_action.c        |  2 +-
 tests/atk_test_collection.c    |  2 +-
 tests/atk_test_component.c     |  2 +-
 tests/atk_test_document.c      |  2 +-
 tests/atk_test_editable_text.c |  2 +-
 tests/atk_test_hyperlink.c     |  2 +-
 tests/atk_test_hypertext.c     |  2 +-
 tests/atk_test_image.c         |  2 +-
 tests/atk_test_selection.c     |  2 +-
 tests/atk_test_state_set.c     |  2 +-
 tests/atk_test_table.c         |  2 +-
 tests/atk_test_table_cell.c    |  2 +-
 tests/atk_test_text.c          |  2 +-
 tests/atk_test_util.c          | 72 ++++++++++++++++++++++++++++++++++--------
 tests/atk_test_util.h          |  1 +
 tests/atk_test_value.c         |  2 +-
 tests/meson.build              |  2 +-
 18 files changed, 75 insertions(+), 30 deletions(-)
---
diff --git a/tests/atk_test_accessible.c b/tests/atk_test_accessible.c
index 1028739..89d1575 100644
--- a/tests/atk_test_accessible.c
+++ b/tests/atk_test_accessible.c
@@ -28,7 +28,7 @@
 static void
 teardown_accessible_test (gpointer fixture, gconstpointer user_data)
 {
-  kill (child_pid, SIGTERM);
+  terminate_app ();
 }
 
 static void
diff --git a/tests/atk_test_action.c b/tests/atk_test_action.c
index fa79d1e..2c6a378 100644
--- a/tests/atk_test_action.c
+++ b/tests/atk_test_action.c
@@ -28,7 +28,7 @@
 static void
 teardown_action_test (gpointer fixture, gconstpointer user_data)
 {
-  kill (child_pid, SIGTERM);
+  terminate_app ();
 }
 
 static void
diff --git a/tests/atk_test_collection.c b/tests/atk_test_collection.c
index 9ac69d7..219113a 100644
--- a/tests/atk_test_collection.c
+++ b/tests/atk_test_collection.c
@@ -29,7 +29,7 @@
 static void
 teardown_collection_test (gpointer fixture, gconstpointer user_data)
 {
-  kill (child_pid, SIGTERM);
+  terminate_app ();
 }
 
 static void
diff --git a/tests/atk_test_component.c b/tests/atk_test_component.c
index f3ea30b..2aa4457 100644
--- a/tests/atk_test_component.c
+++ b/tests/atk_test_component.c
@@ -28,7 +28,7 @@
 static void
 teardown_component_test (gpointer fixture, gconstpointer user_data)
 {
-  kill (child_pid, SIGTERM);
+  terminate_app ();
 }
 
 static void
diff --git a/tests/atk_test_document.c b/tests/atk_test_document.c
index 4a2342d..12fa5c7 100644
--- a/tests/atk_test_document.c
+++ b/tests/atk_test_document.c
@@ -28,7 +28,7 @@
 static void
 teardown_document_test (gpointer fixture, gconstpointer user_data)
 {
-  kill (child_pid, SIGTERM);
+  terminate_app ();
 }
 
 static void
diff --git a/tests/atk_test_editable_text.c b/tests/atk_test_editable_text.c
index e0c0701..e529df5 100644
--- a/tests/atk_test_editable_text.c
+++ b/tests/atk_test_editable_text.c
@@ -28,7 +28,7 @@
 static void
 teardown_editable_text_test (gpointer fixture, gconstpointer user_data)
 {
-  kill (child_pid, SIGTERM);
+  terminate_app ();
 }
 
 static void
diff --git a/tests/atk_test_hyperlink.c b/tests/atk_test_hyperlink.c
index b0b2821..b6e2c01 100644
--- a/tests/atk_test_hyperlink.c
+++ b/tests/atk_test_hyperlink.c
@@ -142,7 +142,7 @@ atk_test_hyperlink_is_valid (gpointer fixture, gconstpointer user_data)
 static void
 teardown_hyperlink_test (gpointer fixture, gconstpointer user_data)
 {
-  kill (child_pid, SIGTERM);
+  terminate_app ();
 }
 
 void
diff --git a/tests/atk_test_hypertext.c b/tests/atk_test_hypertext.c
index 4696a06..a316874 100644
--- a/tests/atk_test_hypertext.c
+++ b/tests/atk_test_hypertext.c
@@ -80,7 +80,7 @@ atk_test_hypertext_get_link_index (gpointer fixture, gconstpointer user_data)
 static void
 teardown_hypertext_test (gpointer fixture, gconstpointer user_data)
 {
-  kill (child_pid, SIGTERM);
+  terminate_app ();
 }
 
 void
diff --git a/tests/atk_test_image.c b/tests/atk_test_image.c
index ce3bc3e..71d5af7 100644
--- a/tests/atk_test_image.c
+++ b/tests/atk_test_image.c
@@ -28,7 +28,7 @@
 static void
 teardown_image_test (gpointer fixture, gconstpointer user_data)
 {
-  kill (child_pid, SIGTERM);
+  terminate_app ();
 }
 
 static void
diff --git a/tests/atk_test_selection.c b/tests/atk_test_selection.c
index 1f89b57..8a60ee6 100644
--- a/tests/atk_test_selection.c
+++ b/tests/atk_test_selection.c
@@ -28,7 +28,7 @@
 static void
 teardown_selection_test (gpointer fixture, gconstpointer user_data)
 {
-  kill (child_pid, SIGTERM);
+  terminate_app ();
 }
 
 static void
diff --git a/tests/atk_test_state_set.c b/tests/atk_test_state_set.c
index ea0e9ce..f895c68 100644
--- a/tests/atk_test_state_set.c
+++ b/tests/atk_test_state_set.c
@@ -28,7 +28,7 @@
 static void
 teardown_state_set_test (gpointer fixture, gconstpointer user_data)
 {
-  kill (child_pid, SIGTERM);
+  terminate_app ();
 }
 
 static void
diff --git a/tests/atk_test_table.c b/tests/atk_test_table.c
index e22db4c..c0330ed 100644
--- a/tests/atk_test_table.c
+++ b/tests/atk_test_table.c
@@ -417,7 +417,7 @@ atk_test_table_is_selected (gpointer fixture, gconstpointer user_data)
 static void
 teardown_table_test (gpointer fixture, gconstpointer user_data)
 {
-  kill (child_pid, SIGTERM);
+  terminate_app ();
 }
 
 void
diff --git a/tests/atk_test_table_cell.c b/tests/atk_test_table_cell.c
index 6b629b8..d3051ee 100644
--- a/tests/atk_test_table_cell.c
+++ b/tests/atk_test_table_cell.c
@@ -181,7 +181,7 @@ atk_test_table_cell_get_table (gpointer fixture, gconstpointer user_data)
 static void
 teardown_table_cell_test (gpointer fixture, gconstpointer user_data)
 {
-  kill (child_pid, SIGTERM);
+  terminate_app ();
 }
 
 void
diff --git a/tests/atk_test_text.c b/tests/atk_test_text.c
index b9e29dc..14a33dd 100644
--- a/tests/atk_test_text.c
+++ b/tests/atk_test_text.c
@@ -432,7 +432,7 @@ atk_test_text_get_bounded_ranges (gpointer fixture, gconstpointer user_data)
 static void
 teardown_text_test (gpointer fixture, gconstpointer user_data)
 {
-  kill (child_pid, SIGTERM);
+  terminate_app ();
 }
 
 void
diff --git a/tests/atk_test_util.c b/tests/atk_test_util.c
index fd4ee8b..c932df8 100644
--- a/tests/atk_test_util.c
+++ b/tests/atk_test_util.c
@@ -47,33 +47,77 @@ run_app (const char *file_name)
   }
 }
 
-AtspiAccessible * get_root_obj (const char *file_name)
+static AtspiAccessible *try_get_root_obj (AtspiAccessible *obj)
 {
   int i;
-  AtspiAccessible *obj = NULL;
-
-  run_app (file_name);
-
-  /* sleep is needed to wait for fored test application*/
-  sleep (1);
 
-  obj = atspi_get_desktop (0);
   gint child_count = atspi_accessible_get_child_count (obj, NULL);
   if (child_count < 1) {
-    g_test_message ("Fail, test application not found\n");
-    g_test_fail ();
-    kill (child_pid, SIGTERM);
     return NULL;
   }
 
   for (i=0; i<child_count; i++) {
     AtspiAccessible *child = atspi_accessible_get_child_at_index (obj,i, NULL);
-    if (!strcmp (atspi_accessible_get_name (child, NULL), "root_object"))
+    if (child && !strcmp (atspi_accessible_get_name (child, NULL), "root_object"))
       return child;
   }
-  g_test_message ("test object not found\n");
-  g_test_fail ();
 
+  return NULL;
+}
+
+AtspiAccessible * get_root_obj (const char *file_name)
+{
+  int tries = 0;
+  AtspiAccessible *child;
+  struct timespec timeout = { .tv_sec = 0, .tv_nsec = 10 * 1000000 };
+  AtspiAccessible *obj = NULL;
+
+  run_app (file_name);
+
+  obj = atspi_get_desktop (0);
+
+  /* Wait for application to start, up to 100 times 10ms.  */
+  while (++tries <= 100)
+  {
+    child = try_get_root_obj (obj);
+    if (child)
+      return child;
+
+    nanosleep(&timeout, NULL);
+  }
+
+  if (atspi_accessible_get_child_count (obj, NULL) < 1) {
+    g_test_message ("Fail, test application not found\n");
+  } else {
+    g_test_message ("test object not found\n");
+  }
+  g_test_fail ();
   kill (child_pid, SIGTERM);
   return NULL;
 }
+
+void terminate_app (void)
+{
+  int tries = 0;
+
+  AtspiAccessible *child;
+  struct timespec timeout = { .tv_sec = 0, .tv_nsec = 10 * 1000000 };
+  AtspiAccessible *obj = NULL;
+
+  kill (child_pid, SIGTERM);
+
+  obj = atspi_get_desktop (0);
+
+  /* Wait for application to stop, up to 100 times 10ms.  */
+  while (++tries <= 100)
+  {
+    child = try_get_root_obj (obj);
+    if (child == NULL)
+      return;
+
+    nanosleep(&timeout, NULL);
+  }
+
+  g_test_message ("Fail, test application still running\n");
+  g_test_fail ();
+}
diff --git a/tests/atk_test_util.h b/tests/atk_test_util.h
index b2376d5..9fb7480 100644
--- a/tests/atk_test_util.h
+++ b/tests/atk_test_util.h
@@ -40,6 +40,7 @@ pid_t child_pid;
 
 void run_app (const char *file_name);
 AtspiAccessible *get_root_obj (const char *file_name);
+void terminate_app (void);
 void clean_exit_on_fail ();
 
 #endif /* _ATK_TEST_UTIL_H */
diff --git a/tests/atk_test_value.c b/tests/atk_test_value.c
index c5f2c06..4638293 100644
--- a/tests/atk_test_value.c
+++ b/tests/atk_test_value.c
@@ -98,7 +98,7 @@ atk_test_value_get_minimum_increment (gpointer fixture, gconstpointer user_data)
 static void
 teardown_value_test (gpointer fixture, gconstpointer user_data)
 {
-  kill (child_pid, SIGTERM);
+  terminate_app ();
 }
 
 void
diff --git a/tests/meson.build b/tests/meson.build
index dfe48bf..64ed994 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -79,4 +79,4 @@ foreach t: tests
   endif
 endforeach
 
-test('atk-test', atk_test_bin, timeout: 300)
+test('atk-test', atk_test_bin, timeout: 120)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]