1 /*-----------------------------------------------------------------------------
  2 Name:      TestThread.cpp
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Testing the Thread cleanup
  6 -----------------------------------------------------------------------------*/
  7 #include "TestSuite.h"

  8 #include <iostream>

  9 
 10 using namespace std;
 11 using namespace org::xmlBlaster::util;
 12 using namespace org::xmlBlaster::util::thread;
 13 
 14 /**
 15  * This client tests the synchronous method get() with its different qos
 16  * variants.<p>
 17  * This client may be invoked multiple time on the same xmlBlaster server,
 18  * as it cleans up everything after his tests are done.
 19  * <p>
 20  */
 21 
 22 namespace org { namespace xmlBlaster { namespace test {
 23 
 24 class TestThread : public Thread {
 25    
 26 private:
 27    string ME;
 28    Global& global_;
 29    I_Log&  log_;
 30    bool    blocking_;
 31    bool    doRun_;
 32    org::xmlBlaster::util::thread::Mutex *invocationMutex_;
 33 public:
 34    TestThread(Global& global, string name, bool blocking) 
 35       : ME(name), 
 36         global_(global),
 37         log_(global.getLog("test")),
 38         invocationMutex_(0)
 39    {
 40       blocking_ = blocking;
 41       doRun_    = true;
 42    }
 43 
 44    ~TestThread()
 45    {
 46       if (log_.call()) log_.call(ME, "destructor");
 47       if (!blocking_) {
 48          doRun_ = false;
 49          join();
 50       }
 51    }
 52 
 53    void run() 
 54    {
 55       log_.info(ME, "start run");
 56       if (blocking_) {
 57          sleepSecs(30);
 58       }
 59       else {
 60          while (doRun_) {
 61             if (log_.trace()) log_.trace(ME, "run: going to sleep");
 62             sleep(20);
 63          }
 64          if (log_.trace()) log_.trace(ME, "run: coming out of non-blocking run loop");
 65       }
 66       log_.info(ME, "stopped run");
 67    }
 68 
 69    void testThread()
 70    {
 71       log_.info(ME, "testThread() start");
 72       const bool detached = false;
 73       start(detached);
 74       sleepSecs(2);
 75       log_.info(ME, "testThread() end");
 76    }
 77 
 78    void testRecursiveThread()
 79    {
 80       log_.info(ME, "testRecursiveThread() start");
 81       int count = 0;
 82       bool recursive = global_.getProperty().get("xmlBlaster/invocationMutex/recursive", true);
 83       invocationMutex_ = new Mutex(recursive);
 84       callRecursive(count);
 85       delete invocationMutex_;
 86       log_.info(ME, "testRecursiveThread() end");
 87    }
 88 
 89    void callRecursive(int count) {
 90       Lock lock(*invocationMutex_);
 91       count++;
 92       log_.info(ME, "testRecursiveThread() count=" + lexical_cast<string>(count));
 93       if (count > 4) {
 94          return;
 95       }
 96       callRecursive(count);
 97    }
 98 
 99    void setUp(int args=0, char *argc[]=0) {
100       if (log_.trace()) {
101          for (int i=0; i < args; i++) {
102             log_.trace(ME, string(" setUp invoked with argument ") + string(argc[i]));
103          }
104       }
105    }
106 
107    void tearDown() {
108    }
109 };
110    
111 }}} // namespace

112 
113 using namespace org::xmlBlaster::test;
114 
115 int main(int args, char *argc[]) 
116 {
117    org::xmlBlaster::util::Object_Lifetime_Manager::init();
118 
119    Global& glob = Global::getInstance();
120    glob.initialize(args, argc);
121 
122    TestThread *testObj = new TestThread(glob, "TestThread", false);
123    testObj->setUp(args, argc);
124    testObj->testRecursiveThread();
125    testObj->testThread();
126    testObj->tearDown();
127    delete testObj;
128    
129    testObj = new TestThread(glob, "TestThread", true);
130    testObj->setUp(args, argc);
131    testObj->testThread();
132    testObj->tearDown();
133    delete testObj;
134    testObj = NULL;
135    
136    org::xmlBlaster::util::Object_Lifetime_Manager::fini();
137    return 0;
138 }
139 
140 


syntax highlighted by Code2HTML, v. 0.9.1