]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
Timer.cc: add testtimers
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Wed, 20 Oct 2010 00:02:15 +0000 (17:02 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Wed, 20 Oct 2010 19:13:06 +0000 (12:13 -0700)
Add testtimers to test the timer code.

Signed-off-by: Colin McCabe <colinm@hq.newdream.net>
src/Makefile.am
src/test/TestTimers.cc [new file with mode: 0644]

index b0829c294908b2b7412849a4ec832ff82e155950..6f438fa4a17bd9ae3c6cb1e09395508765eb44d8 100644 (file)
@@ -150,6 +150,11 @@ if WITH_DEBUG
 testceph_SOURCES = client/testceph.cc
 testceph_LDADD = libceph.la libcrush.la -lpthread -lm -lcrypto
 bin_PROGRAMS += testceph
+
+testtimers_SOURCES = test/TestTimers.cc
+testtimers_LDADD = libceph.la libcrush.la -lpthread -lm -lcrypto
+bin_PROGRAMS += testtimers
+
 endif
 
 # librados
diff --git a/src/test/TestTimers.cc b/src/test/TestTimers.cc
new file mode 100644 (file)
index 0000000..e8d5615
--- /dev/null
@@ -0,0 +1,84 @@
+#include "common/Mutex.h"
+#include "common/Timer.h"
+
+#include <iostream>
+
+/*
+ * TestTimers
+ *
+ * Tests the timer classes
+ */
+#define MAX_TEST_CONTEXTS 5
+
+class TestContext;
+
+namespace
+{
+  int array[MAX_TEST_CONTEXTS];
+  int array_idx = 0;
+  TestContext* test_contexts[MAX_TEST_CONTEXTS];
+
+  Mutex array_lock("test_timers_mutex");
+}
+
+class TestContext : public Context
+{
+public:
+  TestContext(int num_)
+    : num(num_)
+  {
+  }
+
+  virtual void finish(int r)
+  {
+    array_lock.Lock();
+    array[array_idx++] = num;
+    array_lock.Unlock();
+  }
+
+  virtual ~TestContext()
+  {
+  }
+
+private:
+  int num;
+};
+
+int main(void)
+{
+  int ret = 0;
+  memset(&array, 0, sizeof(array));
+  memset(&test_contexts, 0, sizeof(test_contexts));
+
+  for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
+    test_contexts[i] = new TestContext(i);
+  }
+
+  Timer my_timer;
+
+  for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
+    utime_t inc(2 * i, 0);
+    utime_t t = g_clock.now() + inc;
+    my_timer.add_event_at(t, test_contexts[i]);
+  }
+
+  bool done = false;
+  do {
+    sleep(1);
+    array_lock.Lock();
+    done = (array_idx == MAX_TEST_CONTEXTS);
+    array_lock.Unlock();
+  } while (!done);
+
+  for (int i = 0; i < MAX_TEST_CONTEXTS; ++i) {
+    if (array[i] != i) {
+      ret = 1;
+      cout << "error: expected array[" << i << "] = " << i
+          << "; got " << array[i] << " instead." << std::endl;
+    }
+  }
+
+  cout << ((ret == 0) ? "SUCCESS" : "FAILURE");
+  cout << std::endl;
+  return ret;
+}