Back to index

nordugrid-arc-nox  1.1.0~rc6
ThreadTest.cpp
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #include <string>
00004 
00005 #include <cppunit/extensions/HelperMacros.h>
00006 
00007 #include <arc/Thread.h>
00008 
00009 class ThreadTest
00010   : public CppUnit::TestFixture {
00011 
00012   CPPUNIT_TEST_SUITE(ThreadTest);
00013   CPPUNIT_TEST(TestThread);
00014   CPPUNIT_TEST_SUITE_END();
00015 
00016 public:
00017   void setUp();
00018   void tearDown();
00019   void TestThread();
00020 
00021 private:
00022   static void func(void*);
00023   static int counter;
00024   static Glib::Mutex* lock;
00025 };
00026 
00027 
00028 void ThreadTest::setUp() {
00029   counter = 0;
00030   lock = new Glib::Mutex;
00031 }
00032 
00033 
00034 void ThreadTest::tearDown() {
00035   if(lock) delete lock;
00036 }
00037 
00038 void ThreadTest::TestThread() {
00039   // Simply run 500 threads and see if executable crashes
00040   for(int n = 0;n<500;++n) {
00041     CPPUNIT_ASSERT(Arc::CreateThreadFunction(&func,NULL));
00042   }
00043   sleep(30);
00044   CPPUNIT_ASSERT_EQUAL(500,counter);
00045 }
00046 
00047 void ThreadTest::func(void*) {
00048   sleep(1);
00049   lock->lock();
00050   ++counter;
00051   lock->unlock();
00052 }
00053 
00054 int ThreadTest::counter = 0;
00055 Glib::Mutex* ThreadTest::lock = NULL;
00056 
00057 CPPUNIT_TEST_SUITE_REGISTRATION(ThreadTest);
00058