Browse Source

define UA_realsleep for tests that wait for threads (no fake-sleep)

Julius Pfrommer 7 years ago
parent
commit
ebd9b6a7b7
4 changed files with 20 additions and 1 deletions
  1. 1 1
      tests/check_server_jobs.c
  2. 1 0
      tests/check_services_subscriptions.c
  3. 14 0
      tests/testing_clock.c
  4. 4 0
      tests/testing_clock.h

+ 1 - 1
tests/check_server_jobs.c

@@ -43,7 +43,7 @@ START_TEST(Server_addRemoveRepeatedCallback) {
     UA_Server_run_iterate(server, false);
 
     /* Wait a bit longer until the workers have picked up the dispatched callback */
-    UA_sleep(15);
+    UA_realsleep(100);
     ck_assert_uint_eq(*executed, true);
 
     UA_Server_removeRepeatedCallback(server, id);

+ 1 - 0
tests/check_services_subscriptions.c

@@ -169,6 +169,7 @@ START_TEST(Server_publishCallback) {
     /* Sleep until the publishing interval times out */
     UA_sleep((UA_UInt32)publishingInterval + 1);
     UA_Server_run_iterate(server, false);
+    UA_realsleep(100);
 
     LIST_FOREACH(sub, &adminSession.serverSubscriptions, listEntry)
         ck_assert_uint_eq(sub->currentKeepAliveCount, sub->maxKeepAliveCount+1);

+ 14 - 0
tests/testing_clock.c

@@ -3,6 +3,9 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "testing_clock.h"
+#ifdef UA_ENABLE_MULTITHREADING
+#include <time.h>
+#endif
 
 UA_DateTime testingClock = 0;
 
@@ -18,3 +21,14 @@ void
 UA_sleep(UA_UInt32 duration) {
     testingClock += duration * UA_MSEC_TO_DATETIME;
 }
+
+#define NANO_SECOND_MULTIPLIER 1000000 // 1 millisecond = 1,000,000 Nanoseconds
+void
+UA_realsleep(UA_UInt32 duration) {
+#ifdef UA_ENABLE_MULTITHREADING
+    struct timespec sleepValue;
+    sleepValue.tv_sec = 0;
+    sleepValue.tv_nsec = duration * NANO_SECOND_MULTIPLIER;
+    nanosleep(&sleepValue, NULL);
+#endif
+}

+ 4 - 0
tests/testing_clock.h

@@ -17,4 +17,8 @@
 /* Forwards the testing clock by the given duration in ms */
 void UA_sleep(UA_UInt32 duration);
 
+/* Sleep for the duration in milliseconds. Used to wait for workers to complete.
+ * Does not do anything in single-threaded mode. */
+void UA_realsleep(UA_UInt32 duration);
+
 #endif /* TESTING_CLOCK_H_ */