testsuite/src/c++/TestTimeout.cpp

Go to the documentation of this file.
00001 /*-----------------------------------------------------------------------------
00002 Name:      TestTimeout.cpp
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Testing the Timeout Features
00006 -----------------------------------------------------------------------------*/
00007 #include "TestSuite.h"
00008 #include <iostream>
00009 
00010 using namespace std;
00011 using namespace org::xmlBlaster::util;
00012 using namespace org::xmlBlaster::util::thread;
00013 
00022 namespace org { namespace xmlBlaster { namespace test {
00023 
00024 class TestTimeout : public I_Timeout {
00025    
00026 private:
00027    string ME;
00028    Timeout *timeoutObject;
00029    Global& global_;
00030    I_Log&  log_;
00031    int     count_;
00032    int     max_;
00033 public:
00034    TestTimeout(Global& global, string name) 
00035       : ME(name), 
00036         global_(global),
00037         log_(global.getLog("test")) 
00038    {
00039       count_ = 0;
00040       max_ = 10;
00041    }
00042 
00043    virtual ~TestTimeout()
00044    {
00045       delete timeoutObject;
00046    }
00047 
00048    void timeout(void *userData) {
00049       log_.info(ME, "this is the timeout for the test count_=" + lexical_cast<string>(count_));
00050       if (userData == NULL) return;
00051       Timeout *to = static_cast<Timeout*>(userData);
00052       if (count_ < max_) {
00053          to->addTimeoutListener(this, 1000, to);
00054          log_.info(ME, "next timeout will occur in about 1 s");
00055          count_++;
00056       }
00057    }
00058 
00059    void testTimeout() 
00060    {
00061       log_.info(ME, "testTimeout(): the timeout will now be triggered");
00062       timeoutObject->addTimeoutListener(this, 2000, timeoutObject);
00063       log_.info(ME, "testTimeout: timeout triggered. Waiting to be fired (should happen in 2 seconds");
00064 
00065       // waiting some time ... (you can't use join because the timeout thread
00066       // never stops ...
00067       log_.info(ME, "main thread is sleeping now");
00068       Thread::sleepSecs(max_+6);
00069       log_.info(ME, "after waiting to complete");
00070    }
00071 
00072    void testLifecycle() 
00073    {
00074       log_.info(ME, "testLifecycle(): the timeout will now be triggered");
00075       Timeout* timeout = new Timeout(global_);
00076       timeout->addTimeoutListener(this, 10000, timeout);
00077       log_.info(ME, "testLifecycle: timeout triggered. Now destroying it again");
00078       timeout->shutdown();
00079       delete timeout;
00080    }
00081 
00082    void setUp(int args=0, char *argc[]=0) {
00083       if (log_.trace()) {
00084          for (int i=0; i < args; i++) {
00085             log_.trace(ME, string(" setUp invoked with argument ") + string(argc[i]));
00086          }
00087       }
00088       log_.info(ME, "setUp(): creating the timeout object");
00089       timeoutObject = new Timeout(global_);
00090       log_.info(ME, "setUp(): timeout object created");
00091    }
00092 
00093    void tearDown() {
00094       log_.info(ME, "tearDown(): will delete now");
00095       timeoutObject->shutdown();
00096       // delete TimestampFactory::getInstance();
00097       log_.info(ME, "tearDown(): has deleted now");
00098    }
00099 };
00100    
00101 }}} // namespace
00102 
00103 using namespace org::xmlBlaster::test;
00104 
00105 int main(int args, char *argc[]) 
00106 {
00107    Global& glob = Global::getInstance();
00108    glob.initialize(args, argc);
00109    try {
00110       TestTimeout testObj(glob, "TestTimeout");
00111       testObj.setUp(args, argc);
00112       testObj.testLifecycle();
00113       testObj.testTimeout();
00114       testObj.tearDown();
00115    } catch(...) {
00116       std::cout << "UNEXPECTED EXCEPTION" << std::endl;
00117    }
00118    org::xmlBlaster::util::Object_Lifetime_Manager::fini();
00119    return 0;
00120 }
00121 
00122