qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acon...@apache.org
Subject [02/10] qpid-dispatch git commit: DISPATCH-390: Convert dispatch to use pn_proactor_t
Date Thu, 27 Apr 2017 17:38:27 GMT
http://git-wip-us.apache.org/repos/asf/qpid-dispatch/blob/6f56e289/tests/timer_test.c
----------------------------------------------------------------------
diff --git a/tests/timer_test.c b/tests/timer_test.c
index 510ac95..355d8fc 100644
--- a/tests/timer_test.c
+++ b/tests/timer_test.c
@@ -6,9 +6,9 @@
  * 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
@@ -18,6 +18,7 @@
  */
 
 #include <stdio.h>
+#include <limits.h>
 #include <qpid/dispatch/timer.h>
 #include "dispatch_private.h"
 #include "alloc.h"
@@ -27,68 +28,45 @@
 
 
 static unsigned long    fire_mask;
-static qd_timer_list_t  pending_timers;
-static sys_mutex_t     *lock;
+static unsigned long    fired;
+static qd_duration_t    timeout;
 static long             time_value;
 static qd_timer_t      *timers[16];
 
 
-void qd_server_timer_pending_LH(qd_timer_t *timer)
-{
-    DEQ_INSERT_TAIL(pending_timers, timer);
+/* Dummy out the now and timeout functions */
+qd_timestamp_t qd_timer_now() {
+    return time_value;
 }
 
-
-void qd_server_timer_cancel_LH(qd_timer_t *timer)
-{
-    if (timer->state == TIMER_PENDING)
-        DEQ_REMOVE(pending_timers, timer);
+void qd_server_timeout(qd_server_t *server, qd_duration_t duration) {
+    timeout = duration;
 }
 
-
-static int fire_head()
+static void on_timer(void *context)
 {
-    sys_mutex_lock(lock);
-    int         result = DEQ_SIZE(pending_timers);
-    qd_timer_t *timer  = DEQ_HEAD(pending_timers);
-    if (timer) {
-        DEQ_REMOVE_HEAD(pending_timers);
-        qd_timer_idle_LH(timer);
-        fire_mask |= (unsigned long) timer->context;
-    }
-    sys_mutex_unlock(lock);
-    return result;
+    fire_mask |= (unsigned long) context;
+    ++fired;
 }
 
 
 static char* test_quiet(void *context)
 {
-    fire_mask = 0;
-
-    sys_mutex_lock(lock);
-    qd_timer_visit_LH(time_value++);
-    qd_timer_visit_LH(time_value++);
-    qd_timer_visit_LH(time_value++);
-    qd_timer_visit_LH(time_value++);
-    qd_timer_visit_LH(time_value++);
-    sys_mutex_unlock(lock);
-
-    while(fire_head());
-
-    if (fire_mask != 0)
+    fire_mask = fired = 0;
+    qd_timer_visit();
+    qd_timer_visit();
+    if (fired != 0 && fire_mask != 0)
         return "Expected zero timers fired";
     return 0;
 }
 
 static char* test_immediate(void *context)
 {
-    while(fire_head());
-    fire_mask = 0;
-
+    fire_mask = fired = 0;
     qd_timer_schedule(timers[0], 0);
-
-    if (fire_mask != 0)  return "Premature firing";
-    if (fire_head() > 1) return "Too many firings";
+    if (fired != 0)  return "Premature firing";
+    qd_timer_visit();
+    if (fired != 1)  return "Expected 1 firing";
     if (fire_mask != 1)  return "Incorrect fire mask";
 
     return 0;
@@ -97,22 +75,20 @@ static char* test_immediate(void *context)
 
 static char* test_immediate_reschedule(void *context)
 {
-    while(fire_head());
-    fire_mask = 0;
-
+    fire_mask = fired = 0;
     qd_timer_schedule(timers[0], 0);
     qd_timer_schedule(timers[0], 0);
+    qd_timer_visit();
 
-    if (fire_mask != 0)  return "pass 1 - Premature firing";
-    if (fire_head() > 1) return "pass 1 - Too many firings";
-    if (fire_mask != 1)  return "pass 1 - Incorrect fire mask";
+    if (fired > 1) return "pass 1 - Too many firings";
+    if (fire_mask != 1) return "pass 1 - Incorrect fire mask";
 
-    fire_mask = 0;
+    fire_mask = fired = 0;
     qd_timer_schedule(timers[0], 0);
     qd_timer_schedule(timers[0], 0);
+    qd_timer_visit();
 
-    if (fire_mask != 0)  return "pass 2 - Premature firing";
-    if (fire_head() > 1) return "pass 2 - Too many firings";
+    if (fired > 1) return "pass 2 - Too many firings";
     if (fire_mask != 1)  return "pass 2 - Incorrect fire mask";
 
     return 0;
@@ -121,23 +97,18 @@ static char* test_immediate_reschedule(void *context)
 
 static char* test_immediate_plus_delayed(void *context)
 {
-    while(fire_head());
-    fire_mask = 0;
-
+    fire_mask = fired = 0;
     qd_timer_schedule(timers[0], 0);
     qd_timer_schedule(timers[1], 5);
+    qd_timer_visit();
 
-    if (fire_mask != 0)  return "Premature firing";
-    if (fire_head() > 1) return "Too many firings";
+    if (fired > 1) return "Too many firings";
     if (fire_mask != 1)  return "Incorrect fire mask 1";
 
-    sys_mutex_lock(lock);
-    qd_timer_visit_LH(time_value++);
     time_value += 8;
-    qd_timer_visit_LH(time_value++);
-    sys_mutex_unlock(lock);
+    qd_timer_visit();
 
-    if (fire_head() < 1) return "Delayed Failed to fire";
+    if (fired < 1) return "Delayed Failed to fire";
     if (fire_mask != 3)  return "Incorrect fire mask 3";
 
     return 0;
@@ -146,31 +117,25 @@ static char* test_immediate_plus_delayed(void *context)
 
 static char* test_single(void *context)
 {
-    while(fire_head());
-    fire_mask = 0;
+    fire_mask = fired = 0;
 
     qd_timer_schedule(timers[0], 2);
-    if (fire_head() > 0) return "Premature firing 1";
-
-    sys_mutex_lock(lock);
-    qd_timer_visit_LH(time_value++);
-    sys_mutex_unlock(lock);
-    if (fire_head() > 0) return "Premature firing 2";
-
-    sys_mutex_lock(lock);
-    qd_timer_visit_LH(time_value++);
-    sys_mutex_unlock(lock);
-    if (fire_head() < 1) return "Failed to fire";
-
-    sys_mutex_lock(lock);
-    qd_timer_visit_LH(time_value++);
-    qd_timer_visit_LH(time_value++);
-    qd_timer_visit_LH(time_value++);
-    sys_mutex_unlock(lock);
-    if (fire_head() != 0) return "Spurious fires";
-
+    if (timeout != 2) return "Incorrect timeout";
+    qd_timer_visit();
+    if (fired > 0) return "Premature firing";
+    time_value++;
+    qd_timer_visit();
+    if (fired > 0) return "Premature firing 2";
+    time_value++;
+    qd_timer_visit();
     if (fire_mask != 1)  return "Incorrect fire mask";
-    if (timers[0]->state != TIMER_IDLE) return "Expected idle timer state";
+
+    fire_mask = fired = 0;
+    time_value++;
+    qd_timer_visit();
+    time_value++;
+    qd_timer_visit();
+    if (fired != 0) return "Spurious fires";
 
     return 0;
 }
@@ -178,26 +143,21 @@ static char* test_single(void *context)
 
 static char* test_two_inorder(void *context)
 {
-    while(fire_head());
-    fire_mask = 0;
+    fire_mask = fired = 0;
 
     qd_timer_schedule(timers[0], 2);
+    if (timeout != 2) return "bad timeout 2";
     qd_timer_schedule(timers[1], 4);
+    if (timeout != 2) return "bad timeout still 2";
+    time_value += 2;
+    qd_timer_visit();
 
-    sys_mutex_lock(lock);
-    qd_timer_visit_LH(time_value++);
-    qd_timer_visit_LH(time_value++);
-    sys_mutex_unlock(lock);
-    int count = fire_head();
-    if (count < 1) return "First failed to fire";
-    if (count > 1) return "Second fired prematurely";
+    if (fire_mask & 2) return "Second fired prematurely";
     if (fire_mask != 1) return "Incorrect fire mask 1";
 
-    sys_mutex_lock(lock);
-    qd_timer_visit_LH(time_value++);
-    qd_timer_visit_LH(time_value++);
-    sys_mutex_unlock(lock);
-    if (fire_head() < 1) return "Second failed to fire";
+    time_value += 2;
+    qd_timer_visit();
+
     if (fire_mask != 3)  return "Incorrect fire mask 3";
 
     return 0;
@@ -206,26 +166,21 @@ static char* test_two_inorder(void *context)
 
 static char* test_two_reverse(void *context)
 {
-    while(fire_head());
-    fire_mask = 0;
+    fire_mask = fired = 0;
 
     qd_timer_schedule(timers[0], 4);
     qd_timer_schedule(timers[1], 2);
+    time_value += 2;
+    qd_timer_visit();
 
-    sys_mutex_lock(lock);
-    qd_timer_visit_LH(time_value++);
-    qd_timer_visit_LH(time_value++);
-    sys_mutex_unlock(lock);
-    int count = fire_head();
-    if (count < 1) return "First failed to fire";
-    if (count > 1) return "Second fired prematurely";
+    if (fired < 1) return "First failed to fire";
+    if (fired > 1) return "Second fired prematurely";
     if (fire_mask != 2) return "Incorrect fire mask 2";
 
-    sys_mutex_lock(lock);
-    qd_timer_visit_LH(time_value++);
-    qd_timer_visit_LH(time_value++);
-    sys_mutex_unlock(lock);
-    if (fire_head() < 1) return "Second failed to fire";
+    time_value += 2;
+    qd_timer_visit();
+
+    if (fired < 1) return "Second failed to fire";
     if (fire_mask != 3)  return "Incorrect fire mask 3";
 
     return 0;
@@ -234,26 +189,19 @@ static char* test_two_reverse(void *context)
 
 static char* test_two_duplicate(void *context)
 {
-    while(fire_head());
-    fire_mask = 0;
+    fire_mask = fired = 0;
 
     qd_timer_schedule(timers[0], 2);
     qd_timer_schedule(timers[1], 2);
-
-    sys_mutex_lock(lock);
-    qd_timer_visit_LH(time_value++);
-    qd_timer_visit_LH(time_value++);
-    sys_mutex_unlock(lock);
-    int count = fire_head();
-    if (count != 2) return "Expected two firings";
-    fire_head();
+    time_value += 2;
+    qd_timer_visit();
+    if (fired != 2) return "Expected two firings";
     if (fire_mask != 3) return "Incorrect fire mask 3";
 
-    sys_mutex_lock(lock);
-    qd_timer_visit_LH(time_value++);
-    qd_timer_visit_LH(time_value++);
-    sys_mutex_unlock(lock);
-    if (fire_head() > 0) return "Spurious timer fires";
+    fire_mask = fired = 0;
+    time_value += 2;
+    qd_timer_visit();
+    if (fired > 0) return "Spurious timer fires";
 
     return 0;
 }
@@ -261,64 +209,39 @@ static char* test_two_duplicate(void *context)
 
 static char* test_separated(void *context)
 {
-    int count;
-
-    while(fire_head());
-    fire_mask = 0;
-
+    fire_mask = fired = 0;
     qd_timer_schedule(timers[0], 2);
     qd_timer_schedule(timers[1], 4);
+    time_value +=  2;
+    qd_timer_visit();
 
-    sys_mutex_lock(lock);
-    qd_timer_visit_LH(time_value++);
-    qd_timer_visit_LH(time_value++);
-    sys_mutex_unlock(lock);
-    count = fire_head();
-    if (count < 1) return "First failed to fire";
-    if (count > 1) return "Second fired prematurely";
+    if (fired < 1) return "First failed to fire";
+    if (fired > 1) return "Second fired prematurely";
     if (fire_mask != 1) return "Incorrect fire mask 1";
 
+    fired = 0;
     qd_timer_schedule(timers[2], 2);
     qd_timer_schedule(timers[3], 4);
+    time_value +=  2;
+    qd_timer_visit();
 
-    sys_mutex_lock(lock);
-    qd_timer_visit_LH(time_value++);
-    qd_timer_visit_LH(time_value++);
-    sys_mutex_unlock(lock);
-    count = fire_head();
-    fire_head();
-    if (count < 1) return "Second failed to fire";
-    if (count < 2) return "Third failed to fire";
+    if (fired < 1) return "Second failed to fire";
+    if (fired < 2) return "Third failed to fire";
     if (fire_mask != 7)  return "Incorrect fire mask 7";
 
-    sys_mutex_lock(lock);
-    qd_timer_visit_LH(time_value++);
-    qd_timer_visit_LH(time_value++);
-    sys_mutex_unlock(lock);
-    count = fire_head();
-    if (count < 1) return "Fourth failed to fire";
+    fired = 0;
+    time_value +=  2;
+    qd_timer_visit();
+    if (fired < 1) return "Fourth failed to fire";
     if (fire_mask != 15) return "Incorrect fire mask 15";
 
-    sys_mutex_lock(lock);
-    qd_timer_visit_LH(time_value++);
-    qd_timer_visit_LH(time_value++);
-    qd_timer_visit_LH(time_value++);
-    qd_timer_visit_LH(time_value++);
-    qd_timer_visit_LH(time_value++);
-    qd_timer_visit_LH(time_value++);
-    sys_mutex_unlock(lock);
-    count = fire_head();
-    if (count > 0) return "Spurious fire";
-
     return 0;
 }
 
 
 static char* test_big(void *context)
 {
-    while(fire_head());
-    fire_mask = 0;
-
+    fire_mask = fired = 0;
     long durations[16] =
         { 5,  8,  7,  6,
          14, 10, 16, 15,
@@ -349,10 +272,8 @@ static char* test_big(void *context)
     for (i = 0; i < 16; i++)
         qd_timer_schedule(timers[i], durations[i]);
     for (i = 0; i < 18; i++) {
-        sys_mutex_lock(lock);
-        qd_timer_visit_LH(time_value++);
-        sys_mutex_unlock(lock);
-        while(fire_head());
+        ++time_value;
+        qd_timer_visit();
         if (fire_mask != masks[i]) {
             static char error[100];
             sprintf(error, "Iteration %d: expected mask %04lx, got %04lx", i, masks[i], fire_mask);
@@ -364,32 +285,29 @@ static char* test_big(void *context)
 }
 
 
-int timer_tests(void)
+int timer_tests()
 {
-    int result = 0;
     char *test_group = "timer_tests";
-
-    fire_mask = 0;
-    DEQ_INIT(pending_timers);
-    lock = qd_timer_lock();
+    int result = 0;
+    fire_mask = fired = 0;
     time_value = 1;
 
-    timers[0]  = qd_timer(0, 0, (void*) 0x00000001);
-    timers[1]  = qd_timer(0, 0, (void*) 0x00000002);
-    timers[2]  = qd_timer(0, 0, (void*) 0x00000004);
-    timers[3]  = qd_timer(0, 0, (void*) 0x00000008);
-    timers[4]  = qd_timer(0, 0, (void*) 0x00000010);
-    timers[5]  = qd_timer(0, 0, (void*) 0x00000020);
-    timers[6]  = qd_timer(0, 0, (void*) 0x00000040);
-    timers[7]  = qd_timer(0, 0, (void*) 0x00000080);
-    timers[8]  = qd_timer(0, 0, (void*) 0x00000100);
-    timers[9]  = qd_timer(0, 0, (void*) 0x00000200);
-    timers[10] = qd_timer(0, 0, (void*) 0x00000400);
-    timers[11] = qd_timer(0, 0, (void*) 0x00000800);
-    timers[12] = qd_timer(0, 0, (void*) 0x00001000);
-    timers[13] = qd_timer(0, 0, (void*) 0x00002000);
-    timers[14] = qd_timer(0, 0, (void*) 0x00004000);
-    timers[15] = qd_timer(0, 0, (void*) 0x00008000);
+    timers[0]  = qd_timer(0, on_timer, (void*) 0x00000001);
+    timers[1]  = qd_timer(0, on_timer, (void*) 0x00000002);
+    timers[2]  = qd_timer(0, on_timer, (void*) 0x00000004);
+    timers[3]  = qd_timer(0, on_timer, (void*) 0x00000008);
+    timers[4]  = qd_timer(0, on_timer, (void*) 0x00000010);
+    timers[5]  = qd_timer(0, on_timer, (void*) 0x00000020);
+    timers[6]  = qd_timer(0, on_timer, (void*) 0x00000040);
+    timers[7]  = qd_timer(0, on_timer, (void*) 0x00000080);
+    timers[8]  = qd_timer(0, on_timer, (void*) 0x00000100);
+    timers[9]  = qd_timer(0, on_timer, (void*) 0x00000200);
+    timers[10] = qd_timer(0, on_timer, (void*) 0x00000400);
+    timers[11] = qd_timer(0, on_timer, (void*) 0x00000800);
+    timers[12] = qd_timer(0, on_timer, (void*) 0x00001000);
+    timers[13] = qd_timer(0, on_timer, (void*) 0x00002000);
+    timers[14] = qd_timer(0, on_timer, (void*) 0x00004000);
+    timers[15] = qd_timer(0, on_timer, (void*) 0x00008000);
 
     TEST_CASE(test_quiet, 0);
     TEST_CASE(test_immediate, 0);
@@ -408,4 +326,3 @@ int timer_tests(void)
 
     return result;
 }
-


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


Mime
View raw message