]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
TestTimers: test cancelling single events
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Tue, 2 Nov 2010 19:36:52 +0000 (12:36 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Thu, 4 Nov 2010 04:40:23 +0000 (21:40 -0700)
Signed-off-by: Colin McCabe <colinm@hq.newdream.net>
src/test/TestTimers.cc

index f700f8fe50e609c6b8ec20270cedd6ef7c04c5f2..8d7859279e1ded9255c0ce16e3998156a48b31bc 100644 (file)
@@ -42,10 +42,31 @@ public:
   {
   }
 
-private:
+protected:
   int num;
 };
 
+class CancellationTestContext : public TestContext
+{
+public:
+  CancellationTestContext (int num_)
+    : TestContext(num_)
+  {
+  }
+
+  virtual void finish(int r)
+  {
+    array_lock.Lock();
+    cout << "CancellationTestContext " << num << std::endl;
+    array[num] = num;
+    array_lock.Unlock();
+  }
+
+  virtual ~CancellationTestContext()
+  {
+  }
+};
+
 static void print_status(const char *str, int ret)
 {
   cout << str << ": ";
@@ -61,6 +82,8 @@ static int basic_timer_test(T &timer, Mutex *lock)
   array_idx = 0;
   memset(&test_contexts, 0, sizeof(test_contexts));
 
+  cout << __PRETTY_FUNCTION__ << std::endl;
+
   for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
     test_contexts[i] = new TestContext(i);
   }
@@ -108,8 +131,10 @@ done:
   return ret;
 }
 
-static int safe_timer_join_test(SafeTimer &safe_timer, Mutex& safe_timer_lock)
+static int safe_timer_cancel_all_test(SafeTimer &safe_timer, Mutex& safe_timer_lock)
 {
+  cout << __PRETTY_FUNCTION__ << std::endl;
+
   int ret = 0;
   memset(&array, 0, sizeof(array));
   array_idx = 0;
@@ -132,7 +157,6 @@ static int safe_timer_join_test(SafeTimer &safe_timer, Mutex& safe_timer_lock)
   safe_timer_lock.Lock();
   safe_timer.cancel_all_events();
   safe_timer_lock.Unlock();
-  safe_timer.shutdown();
 
   for (int i = 0; i < array_idx; ++i) {
     if (array[i] != i) {
@@ -145,6 +169,51 @@ static int safe_timer_join_test(SafeTimer &safe_timer, Mutex& safe_timer_lock)
   return ret;
 }
 
+static int safe_timer_cancellation_test(SafeTimer &safe_timer, Mutex& safe_timer_lock)
+{
+  cout << __PRETTY_FUNCTION__ << std::endl;
+
+  int ret = 0;
+  memset(&array, 0, sizeof(array));
+  array_idx = 0;
+  memset(&test_contexts, 0, sizeof(test_contexts));
+
+  for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
+    test_contexts[i] = new CancellationTestContext(i);
+  }
+
+  safe_timer_lock.Lock();
+  for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
+    utime_t inc(4 * i, 0);
+    utime_t t = g_clock.now() + inc;
+    safe_timer.add_event_at(t, test_contexts[i]);
+  }
+  safe_timer_lock.Unlock();
+
+  // cancel the even-numbered events
+  for (int i = 0; i < MAX_TEST_CONTEXTS; i += 2) {
+    safe_timer_lock.Lock();
+    safe_timer.cancel_event(test_contexts[i]);
+    safe_timer_lock.Unlock();
+  }
+
+  sleep(20);
+
+  safe_timer_lock.Lock();
+  safe_timer.cancel_all_events();
+  safe_timer_lock.Unlock();
+
+  for (int i = 1; i < array_idx; i += 2) {
+    if (array[i] != i) {
+      ret = 1;
+      cout << "error: expected array[" << i << "] = " << i
+          << "; got " << array[i] << " instead." << std::endl;
+    }
+  }
+
+  return ret;
+}
+
 static int test_safe_timers(void)
 {
   int ret = 0;
@@ -155,7 +224,11 @@ static int test_safe_timers(void)
   if (ret)
     goto done;
 
-  ret = safe_timer_join_test(safe_timer, safe_timer_lock);
+  ret = safe_timer_cancel_all_test(safe_timer, safe_timer_lock);
+  if (ret)
+    goto done;
+
+  ret = safe_timer_cancellation_test(safe_timer, safe_timer_lock);
   if (ret)
     goto done;