1 /*-----------------------------------------------------------------------------
  2 Name:      TestTimeout.cpp
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Testing the Timeout Features
  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 TestTimeout : public I_Timeout {
 25    
 26 private:
 27    string ME;
 28    Timeout *timeoutObject;
 29    Global& global_;
 30    I_Log&  log_;
 31    int     count_;
 32    int     max_;
 33 public:
 34    TestTimeout(Global& global, string name) 
 35       : ME(name), 
 36         global_(global),
 37         log_(global.getLog("test")) 
 38    {
 39       count_ = 0;
 40       max_ = 10;
 41    }
 42 
 43    virtual ~TestTimeout()
 44    {
 45       delete timeoutObject;
 46    }
 47 
 48    void timeout(void *userData) {
 49       log_.info(ME, "this is the timeout for the test count_=" + lexical_cast<string>(count_));
 50       if (userData == NULL) return;
 51       Timeout *to = static_cast<Timeout*>(userData);
 52       if (count_ < max_) {
 53          to->addTimeoutListener(this, 1000, to);
 54          log_.info(ME, "next timeout will occur in about 1 s");
 55          count_++;
 56       }
 57    }
 58 
 59    void testTimeout() 
 60    {
 61       log_.info(ME, "testTimeout(): the timeout will now be triggered");
 62       timeoutObject->addTimeoutListener(this, 2000, timeoutObject);
 63       log_.info(ME, "testTimeout: timeout triggered. Waiting to be fired (should happen in 2 seconds");
 64 
 65       // waiting some time ... (you can't use join because the timeout thread
 66       // never stops ...
 67       log_.info(ME, "main thread is sleeping now");
 68       Thread::sleepSecs(max_+6);
 69       log_.info(ME, "after waiting to complete");
 70    }
 71 
 72    void testLifecycle() 
 73    {
 74       log_.info(ME, "testLifecycle(): the timeout will now be triggered");
 75       Timeout* timeout = new Timeout(global_);
 76       timeout->addTimeoutListener(this, 10000, timeout);
 77       log_.info(ME, "testLifecycle: timeout triggered. Now destroying it again");
 78       timeout->shutdown();
 79       delete timeout;
 80    }
 81 
 82    void setUp(int args=0, char *argc[]=0) {
 83       if (log_.trace()) {
 84          for (int i=0; i < args; i++) {
 85             log_.trace(ME, string(" setUp invoked with argument ") + string(argc[i]));
 86          }
 87       }
 88       log_.info(ME, "setUp(): creating the timeout object");
 89       timeoutObject = new Timeout(global_);
 90       log_.info(ME, "setUp(): timeout object created");
 91    }
 92 
 93    void tearDown() {
 94       log_.info(ME, "tearDown(): will delete now");
 95       timeoutObject->shutdown();
 96       // delete TimestampFactory::getInstance();
 97       log_.info(ME, "tearDown(): has deleted now");
 98    }
 99 };
100    
101 }}} // namespace
102 
103 using namespace org::xmlBlaster::test;
104 
105 int main(int args, char *argc[]) 
106 {
107    Global& glob = Global::getInstance();
108    glob.initialize(args, argc);
109    try {
110       TestTimeout testObj(glob, "TestTimeout");
111       testObj.setUp(args, argc);
112       testObj.testLifecycle();
113       testObj.testTimeout();
114       testObj.tearDown();
115    } catch(...) {
116       std::cout << "UNEXPECTED EXCEPTION" << std::endl;
117    }
118    org::xmlBlaster::util::Object_Lifetime_Manager::fini();
119    return 0;
120 }


syntax highlighted by Code2HTML, v. 0.9.1