Beispiel #1
0
void Connection::setOfferingTimer(int milliSeconds)
{
    UtlString    callId;
    SipSession  session ;
    Url         urlTo ;
    UtlString    remoteAddr;

    getSession(session) ;
    session.getCallId(callId) ;
    session.getToUrl(urlTo) ;
    urlTo.toString(remoteAddr) ;

    CpMultiStringMessage* offeringExpiredMessage =
        new CpMultiStringMessage(CpCallManager::CP_OFFERING_EXPIRED,
                    callId.data(), remoteAddr.data());
    OsTimer* timer = new OsTimer((mpCallManager->getMessageQueue()),
            offeringExpiredMessage);
    // Convert from mSeconds to uSeconds
    OsTime timerTime(milliSeconds / 1000, milliSeconds % 1000);
    timer->oneshotAfter(timerTime);
#ifdef TEST_PRINT
    osPrintf("Connection::setOfferingTimer message type: %d %d",
        OsMsg::PHONE_APP, CpCallManager::CP_OFFERING_EXPIRED);
#endif

    callId.remove(0);
    remoteAddr.remove(0);
}
Beispiel #2
0
void Connection::setRingingTimer(int seconds)
{
    UtlString callId;
    mpCall->getCallId(callId);
    UtlString remoteAddr;
    getRemoteAddress(&remoteAddr);
    CpMultiStringMessage* offeringExpiredMessage =
        new CpMultiStringMessage(CpCallManager::CP_RINGING_EXPIRED,
                    callId.data(), remoteAddr.data());
    OsTimer* timer = new OsTimer((mpCallManager->getMessageQueue()),
            offeringExpiredMessage);

#ifdef TEST_PRINT
    osPrintf("Setting ringing timeout in %d seconds\n",
        seconds);
#endif

    OsTime timerTime(seconds, 0);
    timer->oneshotAfter(timerTime);
#ifdef TEST_PRINT
    osPrintf("Connection::setRingingTimer message type: %d %d",
        OsMsg::PHONE_APP, CpCallManager::CP_RINGING_EXPIRED);
#endif
    callId.remove(0);
    remoteAddr.remove(0);
}
Beispiel #3
0
    void testOneshotPeriodicComboTimer()
    {
       UtlBoolean returnValue;
       OsCallback* pNotifier;
       OsTimer* pTimer;
       pNotifier = new OsCallback((void*)this, TVCallback);
       pTimer = new OsTimer(*pNotifier);
       gCallBackCount = 0;
       gettimeofday(&startTV, NULL);
       returnValue = pTimer->periodicEvery(oneSecond, twoSeconds);
       OsTask::delay(1000 + OSTIMETOLERANCE);
       CPPUNIT_ASSERT_MESSAGE("Test oneshot & periodic timer combo - "
                              "verify return value", returnValue);
       CPPUNIT_ASSERT_MESSAGE("Timer was fired",
                              gCallBackCount == 1);
       REPORT_SKEW(("      Timing inaccuracy = %ld us;\n",
                    getTimeDeltaInUsecs() - MsecsToUsecs(1000)));

       // now wait for another 5+ seconds. The total time after starting the timer is
       // 6 seconds.
       OsTask::delay(5340);
       CPPUNIT_ASSERT_EQUAL_MESSAGE("Test oneshot/periodic combo - Verify the timer is called "
                                    "repeatadly as per the second argument", 3, gCallBackCount);
       delete pTimer;
       delete pNotifier;
    }
Beispiel #4
0
    void testTimerAccuracy()
    {
       OsCallback* pNotifier;
       OsTimer* pTimer;
       long expectedWaitUSecs;

       pNotifier = new OsCallback((void*)this, TVCallback);
       pTimer = new OsTimer(*pNotifier);

       expectedWaitUSecs=(1*OsTime::USECS_PER_SEC) + (250*OsTime::USECS_PER_MSEC);
       OsTime timeToWait(1, 250*OsTime::USECS_PER_MSEC);

       // Give a small delay so we synchronize with the timer.
       OsTask::delay(20);
       gettimeofday(&startTV, NULL);
       pTimer->oneshotAfter(timeToWait);
       // Sleep for a slightly additional time
       OsTask::delay(expectedWaitUSecs / OsTime::USECS_PER_MSEC +
                     OSTIMETOLERANCE);

       CPPUNIT_ASSERT_MESSAGE("Timer was fired",
                              gCallBackCount == 1);
       REPORT_SKEW(("      Timing inaccuracy = %8ld us; Time = %d.%03d;\n",
                    getTimeDeltaInUsecs() - expectedWaitUSecs,
                    1, 250
                      ));

       delete pTimer;
       delete pNotifier;
    }
Beispiel #5
0
    void testImmediateTimer()
    {
       // Supposedly doesn't work under XPL-101, but we need to retest.
//#ifdef _WIN32
//       KNOWN_FATAL_BUG("Fails under Win32", "XPL-101");
//#endif
       OsCallback* pNotifier;
       OsTimer* pTimer;
       OsStatus returnValue;
       pNotifier = new OsCallback((void*)this, TVCallback);
       pTimer = new OsTimer(*pNotifier);
       gCallBackCount = 0;
       gettimeofday(&startTV, NULL);
       returnValue = pTimer->oneshotAfter(OsTime::NO_WAIT);
       // Although the message is supposed to be immediate, give a
       // little extra time
       OsTask::delay(OSTIMETOLERANCE);
       CPPUNIT_ASSERT_MESSAGE("Handle timer 1 (immediate) - ReturnValue",
                              returnValue == OS_SUCCESS);
       CPPUNIT_ASSERT_MESSAGE("Handle timer 1 (immediate) - Timer was fired",
                              gCallBackCount == 1);
       REPORT_SKEW(("      Timing inaccuracy = %6ld us;\n", getTimeDeltaInUsecs()));

       delete pTimer;
       delete pNotifier;
    }
Beispiel #6
0
 void testStartDeleteAsync()
 {
    OsStatus returnValue;
    OsCallback notifier((void*) this, TVCallback);
    OsTimer* pTimer = new OsTimer(notifier);
    gCallBackCount = 0;
    returnValue = pTimer->oneshotAfter(oneSecond);
    CPPUNIT_ASSERT_MESSAGE("oneshotAfter", returnValue == OS_SUCCESS);
    OsTask::delay(500);
    // Delete the timer before it can fire using deleteAsync.
    delete pTimer;
    // Make sure it did not fire.
    OsTask::delay(5000);
    CPPUNIT_ASSERT_MESSAGE("Test start/deleteAsync", gCallBackCount == 0);
 }
Beispiel #7
0
 void testDeleteTimerBeforeExpires()
 {
    OsCallback* pNotifier;
    OsTimer* pTimer;
    pNotifier = new OsCallback((void*)this, TVCallback);
    pTimer = new OsTimer(*pNotifier);
    gCallBackCount = 0;
    pTimer->periodicEvery(oneSecond, twoSeconds);
    OsTask::delay(350);
    delete pTimer;
    // Wait for another 5 seconds. Neither the first shot nor the repeat legs
    // should ever have been called.
    OsTask::delay(5000);
    CPPUNIT_ASSERT_MESSAGE("Verify that a periodictimer can be stopped even "
                           "before the first leg is called", gCallBackCount == 0);
    delete pNotifier;
 }
Beispiel #8
0
    void testPeriodicTimer_FractionalTime()
    {
       OsCallback* pNotifier;
       OsTimer* pTimer;
       pNotifier = new OsCallback((void*)this, TVCallback);
       pTimer = new OsTimer(*pNotifier);
       gCallBackCount = 0;
       pTimer->periodicEvery(OsTime::NO_WAIT, hundredMsec);
       // Give a delay of 1+ seconds . If all went well the call back method
       // must have been called once in the begining and every 100 milliseconds thereafter
       // and hence the callbackcount must be up by 10+1.
       OsTask::delay(1010);
//       KNOWN_BUG("Intermittent failure here; not predictable", "XPL-52");
       CPPUNIT_ASSERT_EQUAL_MESSAGE("Test periodic timer - verify that the fractional timer is "
                                    "*indeed* called periodically", 11, gCallBackCount);
       delete pTimer;
       delete pNotifier;
    }
Beispiel #9
0
 void testFunctionHandler()
 {
     OsTimer* pTimer;
     UtlBoolean returnValue;
     pTimer = new OsTimer(boost::bind(&OsTimerTest::functionHandler, this, _1, _2));
     gFunctionHandlerCount = 0;
     returnValue = pTimer->periodicEvery(boost::posix_time::seconds(2), boost::posix_time::seconds(2));
     // Give a delay of 10+ seconds . If all went well the call back method
     // must have been called once every 2 seconds and hence the callbackcount
     // must be up by 5.
     OsTimer::wait(boost::posix_time::milliseconds(11250));
     CPPUNIT_ASSERT_MESSAGE("Test periodic timer - verify return value",
         returnValue);
     CPPUNIT_ASSERT_EQUAL_MESSAGE("Test periodic timer - verify that the "
                                  "timer is called periodically",
                                  5, gFunctionHandlerCount);
     delete pTimer;
 }
Beispiel #10
0
 void testStopTimerAfterOneShot()
 {
    OsCallback* pNotifier;
    OsTimer* pTimer;
    pNotifier = new OsCallback((void*)this, TVCallback);
    pTimer = new OsTimer(*pNotifier);
    gCallBackCount = 0;
    pTimer->oneshotAfter(oneSecond);
    OsTask::delay(500);
    pTimer->stop();
    OsTask::delay(1200);
    // We have waited for 1.7 sec after arming the timer, and the timer
    // was scheduled to fire after 1.0 sec.  But we disarmed the timer
    // after 0.5 sec, so gCallBackCount should be 0.
    CPPUNIT_ASSERT_MESSAGE("Verify that canceling the timer disarms it",
                           gCallBackCount == 0);
    delete pTimer;
    delete pNotifier;
 }
Beispiel #11
0
    void testOneShotAt()
    {
//       KNOWN_FATAL_BUG("Create tests for methods testOneShotAt and testPeriodicAt", "XPL-40");
       OsCallback* pNotifier;
       OsTimer* pTimer;
       UtlBoolean returnValue;
       pNotifier = new OsCallback((void*)this, TVCallback);
       pTimer = new OsTimer(*pNotifier);
       // Create an OsDateTime object for 2 seconds in the future
       // and call oneShotAt.
       // Get the current time in seconds and microseconds.
       struct timeval now;
       gettimeofday(&now, NULL);
       // Add 2 to the seconds and put it into a time_t so we can call gmtime.
       time_t now_t = now.tv_sec + 2;
       tm* gmtPlus2 = gmtime(&now_t);
       // Put the components of gmtime into an OsDateTime, and add the
       // microseconds from 'now'.
       OsDateTime odt(
          // gmtime returns (year - 1900), but OsDateTime requires 4-digit year.
          (unsigned short) gmtPlus2->tm_year + 1900,
          (unsigned char) gmtPlus2->tm_mon,
          (unsigned char) gmtPlus2->tm_mday,
          (unsigned char) gmtPlus2->tm_hour,
          (unsigned char) gmtPlus2->tm_min,
          (unsigned char) gmtPlus2->tm_sec,
          (unsigned int) now.tv_usec
          );
       gettimeofday(&startTV, NULL);
       gCallBackCount = 0;
       returnValue = pTimer->oneshotAt(odt);
       // Although the message is supposed to be immediate, give a little extra time
       OsTask::delay(2000 + OSTIMETOLERANCE);
       CPPUNIT_ASSERT_MESSAGE("Handle timer 1 - returnValue", returnValue);
       CPPUNIT_ASSERT_MESSAGE("Timer was fired",
                              gCallBackCount == 1);

       REPORT_SKEW(("      Timing inaccuracy = %6ld us;\n",
                    getTimeDeltaInUsecs() - MsecsToUsecs(2000)));

       delete pTimer;
       delete pNotifier;
    }
Beispiel #12
0
 void testPeriodicTimer()
 {
     OsCallback* pNotifier;
     OsTimer* pTimer;
     UtlBoolean returnValue;
     pNotifier = new OsCallback((void*)this, TVCallback);
     pTimer = new OsTimer(*pNotifier);
     gCallBackCount = 0;
     returnValue = pTimer->periodicEvery(twoSeconds, twoSeconds);
     // Give a delay of 10+ seconds . If all went well the call back method
     // must have been called once every 2 seconds and hence the callbackcount
     // must be up by 5.
     OsTask::delay(11250);
     CPPUNIT_ASSERT_MESSAGE("Test periodic timer - verify return value",
         returnValue);
     CPPUNIT_ASSERT_EQUAL_MESSAGE("Test periodic timer - verify that the "
                                  "timer is called periodically",
                                  5, gCallBackCount);
     delete pTimer;
     delete pNotifier;
 }
Beispiel #13
0
    void testStopPeriodicTimer()
    {
       OsCallback* pNotifier;
       OsTimer* pTimer;
       OsTimer* pTimer2;
       pNotifier = new OsCallback((void*)this, TVCallback);
       pTimer = new OsTimer(*pNotifier);
       gCallBackCount = 0;
       pTimer->periodicEvery(oneSecond, twoSeconds);
       // Test the case where the timer is stopped even before the first leg
       // is fired
       OsTask::delay(350);
       pTimer->stop();
       // Wait for another 5 seconds. Neither the first shot nor the repeat legs
       // should ever have been called.
       OsTask::delay(5000);
       CPPUNIT_ASSERT_MESSAGE("Verify that a periodictimer can be stopped even "
                              "before the first leg is called", gCallBackCount == 0);
       delete pTimer;

       pTimer2 = new OsTimer(*pNotifier);
       gCallBackCount = 0;
       gettimeofday(&startTV, NULL);
       pTimer2->periodicEvery(oneSecond, twoSeconds);
       OsTask::delay(1000 + OSTIMETOLERANCE);
       pTimer2->stop();
       // Wait for another 5 seconds. Only the first shot should have been called.
       OsTask::delay(5000);
       CPPUNIT_ASSERT_MESSAGE("Timer was fired",
                              gCallBackCount == 1);

       REPORT_SKEW(("      Timing inaccuracy = %ld us;\n",
                    getTimeDeltaInUsecs() - MsecsToUsecs(1000)));

       // Also verify that only the first leg was called.
       CPPUNIT_ASSERT_EQUAL_MESSAGE("Test stoping periodic timer - Verify that ONLY the first "
                                    "leg was fired", 1, gCallBackCount);
       delete pTimer2;
       delete pNotifier;
    }
Beispiel #14
0
void SipRefreshManager::setRefreshTimer(RefreshDialogState& state, 
                                        UtlBoolean isSuccessfulReschedule)
{
    // Create and set a new timer for the failed time out period
    int nextResendSeconds = 
        calculateResendTime(state.mExpirationPeriodSeconds,
                                  isSuccessfulReschedule);

    // If a signficant amount of time has passed since the prior
    // request was sent, decrease the error timeout a bit.
    // This is only a problem with the error case as in the
    // successful case we set the timer before sending the
    // request.
    if(!isSuccessfulReschedule)
    {
        long now = OsDateTime::getSecsSinceEpoch();
        if(state.mPendingStartTime > 0 &&
            now - state.mPendingStartTime > 5)
        {
            nextResendSeconds = nextResendSeconds - now + state.mPendingStartTime;
            if(nextResendSeconds < 30)
            {
                nextResendSeconds = 30;
            }
        }
    }

    OsSysLog::add(FAC_SIP, PRI_DEBUG,
                  "SipRefreshManager::setRefreshTimer setting resend timeout in %d seconds\n",
                  nextResendSeconds);

    OsMsgQ* incomingQ = getMessageQueue();
    OsTimer* resendTimer = new OsTimer(incomingQ,
        (int)&state);
    state.mpRefreshTimer = resendTimer;
    OsTime timerTime(nextResendSeconds, 0);
    resendTimer->oneshotAfter(timerTime);                

}
Beispiel #15
0
int main()
#endif
{
#ifdef TEST
   UtlMemCheck* pMemCheck = 0;
   pMemCheck = new UtlMemCheck();      // checkpoint for memory leak check
#endif //TEST

   cout << "Entering main()" << endl;

/* ============================ Testing Start ============================= */

   /* ============================ OsProcess ==================================== */
#if defined(_WIN32) || defined(__pingtel_on_posix__)  //vxworks wont use these classes

   osPrintf("Starting Process test...\n");


    //Text Process Class
    if (TestProcessClass() != OS_SUCCESS)
        osPrintf("TestProcessterator FAILED!\n");

    //Test Process Iterator
    if (TestProcessIterator() != OS_SUCCESS)
        osPrintf("TestProcessterator FAILED!\n");

    //Test Process Manager methods
    if (TestProcessMgr() != OS_SUCCESS)
        osPrintf("TestProcessterator FAILED!\n");


    osPrintf("Finished Process test.\n");
#endif

/* ============================ OsTime ==================================== */

   OsTime* pTime;

   pTime = new OsTime();
   delete pTime;

/* ============================ OsMutex =================================== */

   OsMutex* pMutex;

   pMutex = new OsMutex(0);
   delete pMutex;

/* ============================ OsBSem ==================================== */

   OsBSem* pBSem;

   pBSem = new OsBSem(OsBSem::Q_PRIORITY, OsBSem::FULL);
   assert(pBSem->acquire()    == OS_SUCCESS);
   assert(pBSem->tryAcquire() == OS_BUSY);
   assert(pBSem->release()    == OS_SUCCESS);
   delete pBSem;

/* ============================ OsCSem ==================================== */

   OsCSem* pCSem;

   // the initial count on the semaphore will be 2
   pCSem = new OsCSem(OsCSem::Q_PRIORITY, 2);
   assert(pCSem->acquire()    == OS_SUCCESS);  // take it once
   assert(pCSem->acquire()    == OS_SUCCESS);  // take it twice
   assert(pCSem->tryAcquire() == OS_BUSY);
   assert(pCSem->release()    == OS_SUCCESS);  // release once
   assert(pCSem->release()    == OS_SUCCESS);  // release twice
   delete pCSem;

/* ============================ OsLock ==================================== */

   // Create a binary semaphore for use with an OsLock object
   pBSem = new OsBSem(OsBSem::Q_PRIORITY, OsBSem::FULL);

   // Acquire semaphore at the start of the method, release it on exit
   guardedWithBSem(*pBSem);
   delete pBSem;

/* ============================ OsRWMutex ================================= */

   OsRWMutex* pRWMutex;

   pRWMutex = new OsRWMutex(OsRWMutex::Q_FIFO);
   assert(pRWMutex->acquireRead()     == OS_SUCCESS);
   assert(pRWMutex->tryAcquireWrite() == OS_BUSY);
   assert(pRWMutex->releaseRead()     == OS_SUCCESS);
   assert(pRWMutex->tryAcquireWrite() == OS_SUCCESS);
   assert(pRWMutex->tryAcquireRead()  == OS_BUSY);
   assert(pRWMutex->releaseWrite()    == OS_SUCCESS);
   delete pRWMutex;

/* ============================ OsReadLock and OsWriteLock ================ */

   // Create an OsRWMutex for use with OsReadLock and OsWriteLock objects
   pRWMutex = new OsRWMutex(OsRWMutex::Q_FIFO);

   // Acquire read lock at the start of the method, release it on exit
   guardedForReading(*pRWMutex);

   // Acquire write lock at the start of the method, release it on exit
   guardedForWriting(*pRWMutex);
   delete pRWMutex;

/* ============================ OsNameDb ================================== */

   OsNameDb* pNameDb;

   int storedInt;

   pNameDb = OsNameDb::getNameDb();
   assert(pNameDb->isEmpty());
   assert(pNameDb->numEntries() == 0);

   assert(pNameDb->insert("test1", 1) == OS_SUCCESS);
   assert(pNameDb->insert("test1", 2) == OS_NAME_IN_USE);
   assert(!pNameDb->isEmpty());
   assert(pNameDb->numEntries() == 1);

   assert(pNameDb->insert("test2", 2) == OS_SUCCESS);
   assert(pNameDb->numEntries() == 2);

   assert(pNameDb->lookup("test1", NULL)       == OS_SUCCESS);
   assert(pNameDb->lookup("test1", &storedInt) == OS_SUCCESS);
   assert(storedInt == 1);
   assert(pNameDb->lookup("test2", &storedInt) == OS_SUCCESS);
   assert(storedInt == 2);
   assert(pNameDb->lookup("test3", NULL)       == OS_NOT_FOUND);

   pNameDb->remove("test1");
   pNameDb->remove("test2");

   delete pNameDb;

/* ============================ OsMsgQ ==================================== */

   OsMsgQ* pMsgQ1;
   OsMsg* pMsg1;
   OsMsg* pMsg2;
   OsMsg* pRecvMsg;

   pMsgQ1 = new OsMsgQ(OsMsgQ::DEF_MAX_MSGS, OsMsgQ::DEF_MAX_MSG_LEN,
                       OsMsgQ::Q_PRIORITY, "MQ1");

   pMsg1  = new OsMsg(OsMsg::UNSPECIFIED, 0);
   pMsg2  = new OsMsg(OsMsg::UNSPECIFIED, 0);

   assert(pMsgQ1->isEmpty());
   assert(pMsgQ1->numMsgs() == 0);
   assert(pMsgQ1->getSendHook() == NULL);
   pMsgQ1->setSendHook(msgSendHook);
   assert(pMsgQ1->getSendHook() == msgSendHook);

   OsStatus stat = pMsgQ1->send(*pMsg1);

   assert(stat == OS_SUCCESS);
   assert(!pMsgQ1->isEmpty());
   assert(pMsgQ1->numMsgs() == 1);

   stat = pMsgQ1->send(*pMsg2);
   assert(stat == OS_SUCCESS);

   assert(pMsgQ1->numMsgs() == 2);

   stat = pMsgQ1->receive(pRecvMsg);
   assert(stat == OS_SUCCESS);

   delete pRecvMsg;
   assert(pMsgQ1->numMsgs() == 1);

   stat = pMsgQ1->receive(pRecvMsg);
   assert(stat == OS_SUCCESS);
   delete pRecvMsg;

   assert(pMsgQ1->numMsgs() == 0);

   delete pMsg1;
   delete pMsg2;

#if !defined(_VXWORKS)
   try
   {
      OsMsgQ* pMsgQ2 = new OsMsgQ(OsMsgQ::DEF_MAX_MSGS,
                                  OsMsgQ::DEF_MAX_MSG_LEN,
                                  OsMsgQ::Q_PRIORITY,
                                  "MQ1");
      delete pMsgQ2;
   }
   catch (const OsExcept* exc)
   {

      UtlString txt;
      cout << "Exception:" << endl;
      cout << "  Major Code: " << exc->getMajorCode() << endl;
      cout << "  Minor Code: " << exc->getMinorCode() << endl;
      txt = exc->getText();
      cout << "  Text:       " << txt.data()    << endl;
      txt = exc->getContext();
      cout << "  Context:    " << txt.data() << endl;

      delete exc;
   }
#endif

   delete pMsgQ1;

/* ============================ OsCallback ================================ */

   OsCallback* pCallback;

   pCallback = new OsCallback(12345, handleTimerEvent);
   pCallback->signal(67890);
   delete pCallback;

/* ============================ OsEvent =================================== */

   OsTime   eventTimeout(2,0);
   OsEvent* pEvent;

   cout << "Testing OsEvent, please wait..." << endl;
   pEvent = new OsEvent(12345);
   int epochTime = time(NULL);
   assert(pEvent->wait(eventTimeout) != OS_SUCCESS);
   pEvent->signal(67890);
   assert(pEvent->wait(eventTimeout) == OS_SUCCESS);
   pEvent->reset();
   assert(pEvent->wait(eventTimeout) != OS_SUCCESS);
   epochTime = time(NULL) - epochTime;

   // Make sure we waited (approximately) 2 seconds each time.
   assert(epochTime > 2 && epochTime < 6);

   delete pEvent;
   cout << "Done testing OsEvent." << endl;

/* ============================ OsConfigDb ================================ */

   OsConfigDb* pConfigDb;

   pConfigDb = new OsConfigDb();
   delete pConfigDb;

/* ============================ OsTimerTask =============================== */

   OsTimerTask* pTimerTask;
   pTimerTask = OsTimerTask::getTimerTask();
   OsTask::delay(500);    // wait 1/2 second
   osPrintf("Going to delete the timer task.\n");
   delete pTimerTask;

/* ============================ OsTimer =================================== */

   pTimerTask = OsTimerTask::getTimerTask();
   OsTask::delay(500);    // wait 1/2 second
   OsCallback* pNotifier;
   OsCallback* pNotifier2;
   OsTimer*    pTimer;
   OsTimer*    pTimer2;
   OsTime      tenMsec(0, 10000);// timer offset ten msec into the future
   OsTime      oneSecond(1,0);   // timer offset one second into the future
   OsTime      twoSeconds(2,0);  // timer offset two seconds into the future
   OsTime      tenSeconds(10,0); // timer offset ten seconds into the future
   OsTime      tenYears(10*365*24*60*60, 0);  // ten years into the future

   cout << "About to handle timer 1 (immediate)" << endl;
   pNotifier = new OsCallback(1, handleTimerEvent);
   pTimer = new OsTimer(*pNotifier);
   cout << "  Oneshot timer 1 armed at " <<
           OsDateTime::getSecsSinceEpoch() << " secs" << endl;
   pTimer->oneshotAfter(OsTime::NO_WAIT);
   delete pTimer;
   delete pNotifier;

   cout << "About to handle timer 2" << endl;
   pNotifier = new OsCallback(2, handleTimerEvent);
   pTimer = new OsTimer(*pNotifier);
   cout << "  Oneshot timer 2 armed at " <<
           OsDateTime::getSecsSinceEpoch() << " secs" << endl;
   pTimer->oneshotAfter(oneSecond);
   delete pTimer;            // delete the timer before it can expire
   delete pNotifier;

   cout << "About to handle timer 3" << endl;
   pNotifier = new OsCallback(3, handleTimerEvent);
   pTimer = new OsTimer(*pNotifier);
   cout << "  Oneshot timer 3 armed at " <<
           OsDateTime::getSecsSinceEpoch() << " secs" << endl;
   pTimer->oneshotAfter(oneSecond);
   pTimer->stop();           // stop the timer before it can expire
   delete pTimer;
   delete pNotifier;

   cout << "About to handle timer 4 (after 1 sec)" << endl;
   pNotifier = new OsCallback(4, handleTimerEvent);
   pTimer = new OsTimer(*pNotifier);
   cout << "  Oneshot timer armed at " <<
           OsDateTime::getSecsSinceEpoch() << endl;
   pTimer->oneshotAfter(oneSecond);
   OsTask::delay(1500);  // sleep for 1.5 seconds
   delete pTimer;
   delete pNotifier;

   cout << "About to handle timer 5" << endl;
   pNotifier = new OsCallback(5, handleTimerEvent);
   pTimer = new OsTimer(*pNotifier);
   cout << "  Periodic timer 5 armed at " <<
           OsDateTime::getSecsSinceEpoch() << " secs" << endl;
   pTimer->periodicEvery(oneSecond, oneSecond);
   delete pTimer;            // delete the timer before it can expire
   delete pNotifier;

   cout << "About to handle timer 6" << endl;
   pNotifier = new OsCallback(6, handleTimerEvent);
   pTimer = new OsTimer(*pNotifier);
   cout << "  Periodic timer 6 armed at " <<
           OsDateTime::getSecsSinceEpoch() << " secs" << endl;
   pTimer->periodicEvery(oneSecond, oneSecond);
   pTimer->stop();           // stop the timer before it can expire
   delete pTimer;
   delete pNotifier;

   cout << "About to handle timer 7 (immediate, then every second)" << endl;
   cout << "About to handle timer 8 (immediate, then every two seconds)" << endl;
   pNotifier  = new OsCallback(7, handleTimerEvent);
   pNotifier2 = new OsCallback(8, handleTimerEvent);
   pTimer  = new OsTimer(*pNotifier);
   pTimer2 = new OsTimer(*pNotifier2);
   cout << "  Periodic timer 7 armed at " <<
           OsDateTime::getSecsSinceEpoch() << " secs" << endl;
   pTimer->periodicEvery(OsTime::NO_WAIT, oneSecond);
   cout << "  Periodic timer 8 armed at " <<
           OsDateTime::getSecsSinceEpoch() << " secs" << endl;
   pTimer2->periodicEvery(OsTime::NO_WAIT, twoSeconds);
   OsTask::delay(4500);  // sleep for 4.5 seconds
   pTimer->stop();
   pTimer2->stop();
   delete pTimer;
   delete pTimer2;
   delete pNotifier;
   delete pNotifier2;

   cout << "About to handle timer 9 (after ten seconds)" << endl;
   pNotifier = new OsCallback(9, handleTimerEvent);
   pTimer = new OsTimer(*pNotifier);
   cout << "  Oneshot timer 9 armed at " <<
           OsDateTime::getSecsSinceEpoch() << " secs" << endl;
   pTimer->oneshotAfter(tenSeconds);
   OsTask::delay(12000);  // sleep for 12 seconds
   delete pTimer;
   delete pNotifier;

   cout << "About to handle timer 10 (after 6912 seconds)" << endl;
   OsTime secs6912(6912, 0);
   pNotifier = new OsCallback(10, handleTimerEvent);
   pTimer = new OsTimer(*pNotifier);
   cout << "  Oneshot timer 10 armed at " <<
           OsDateTime::getSecsSinceEpoch() << " secs" << endl;
   pTimer->oneshotAfter(secs6912);
   OsTask::delay(12000);  // sleep for 12 seconds
   delete pTimer;
   delete pNotifier;

   cout << "Verify that we can schedule timers in the distant future" << endl;
   pNotifier = new OsCallback(11, handleTimerEvent);
   pTimer  = new OsTimer(*pNotifier);
   cout << "  Oneshot timer armed 11 at " <<
           OsDateTime::getSecsSinceEpoch() << " secs" << endl;
   pTimer->oneshotAfter(tenYears);
   pTimer->stop();
   pTimer->periodicEvery(OsTime::NO_WAIT, tenYears);
   pTimer->stop();
   delete pTimer;
   delete pNotifier;

   cout << "Run 100x/second timer for 2 seconds..." << endl;
   int rapidTimerExpirations = 0;
   pNotifier = new OsCallback(&rapidTimerExpirations, handleRapidTimerEvent);
   pTimer = new OsTimer(*pNotifier);
   cout << "  Rapid-fire timer armed at " <<
           OsDateTime::getSecsSinceEpoch() << " secs" << endl;
   pTimer->periodicEvery(OsTime::NO_WAIT, tenMsec);
   OsTask::delay(2000);
   pTimer->stop();
   cout << "  Rapid-fire timer stopped at " <<
           OsDateTime::getSecsSinceEpoch() << " secs" << endl;
   cout << "Rapid-fire timer expired " << rapidTimerExpirations << " times" << endl;
   delete pTimer;
   delete pNotifier;

/* ============================ OsTimerTask cleanup ======================= */

   // The timer task may have been created indirectly (as a result of
   // creating OsTimer objects). If the timer task exists, delete it now.
   pTimerTask = OsTimerTask::getTimerTask();
   if (pTimerTask != NULL)
   {
      osPrintf("Going to delete the timer task.\n");
      delete pTimerTask;
   }

/* ============================ OsNameDb cleanup ========================== */

   // The name database may have been created indirectly (as a result of
   // creating objects with global names). If the name database exists,
   // delete it now.
   pNameDb = OsNameDb::getNameDb();
   if (pNameDb != NULL)
      delete pNameDb;

/* ============================ Testing Finish ============================ */

   cout << "Leaving main()" << endl;

#ifdef TEST
   assert(pMemCheck->delta() == 0);    // check for memory leak
   delete pMemCheck;
#endif //TEST

   return 0;
}
Beispiel #16
0
    void testOneShotAfter()
    {
       struct TestOneShotStruct
       {
          const char* testDescription;
          long seconds;
          long milliseconds;
          int tolerance;
       };

       OsCallback* pNotifier;
       string Message;
       int testCount;

       TestOneShotStruct testData[] = {
          { "Test one shot after when time is specified as 0", 0, 0, OSTIMETOLERANCE },
          { "Test one shot after when time is equal to one second", 1, 0,
            OSTIMETOLERANCE },
          /* The next case was added to check if the inaccuracy applies ONLY to decimal
             values or even to integers
          */
          { "Test one shot after when time is greater than one second", 2, 285,
            OSTIMETOLERANCE },
          { "Test one shot after when time is an integer > 1", 3, 0, OSTIMETOLERANCE },
          { "Test one shot after when time is greater than 0 but less than 1", 0, 252,
            OSTIMETOLERANCE },
       };

       testCount = sizeof(testData)/ sizeof(testData[0]);

       for (int i = 0; i < testCount; i++)
       {
          long expectedWaitUSecs;
          OsTimer* pTimer;
          UtlBoolean returnValue;

          OsTime timeToWait(testData[i].seconds,
                            testData[i].milliseconds*OsTime::USECS_PER_MSEC);

          pNotifier = new OsCallback((void*)this, TVCallback);
          pTimer = new OsTimer(*pNotifier);

          expectedWaitUSecs = SecsToUsecs(testData[i].seconds) +
             MsecsToUsecs(testData[i].milliseconds);

          // Give a small delay so we synchronize with the timer.
          OsTask::delay(20);
          gettimeofday(&startTV, NULL);
          gCallBackCount = 0;
          returnValue = pTimer->oneshotAfter(timeToWait);

          OsTask::delay(expectedWaitUSecs / OsTime::USECS_PER_MSEC +
                        testData[i].tolerance);

          // gCallBackCount is reinitialized to 0 each iteration, so
          // its value should be 1 now.

          UtlString failureMessage;

          failureMessage.remove(0);
          failureMessage.append("Timer did not fire for iteration ");
          failureMessage.appendNumber(i);
          failureMessage.append("\n");
          failureMessage.append(testData[i].testDescription);
          failureMessage.append("\n  seconds:      ");
          failureMessage.appendNumber(testData[i].seconds);
          failureMessage.append("\n  milliseconds: ");
          failureMessage.appendNumber(testData[i].milliseconds);
          failureMessage.append("\n  tolerance:    ");
          failureMessage.appendNumber(testData[i].tolerance);

          KNOWN_BUG("Fails on ecs-fc8. Timing issue.", "XECS-1975");
          CPPUNIT_ASSERT_MESSAGE(failureMessage.data(), gCallBackCount == 1);

          failureMessage.remove(0);
          failureMessage.append("oneshotAfter returned failure on iteration ");
          failureMessage.appendNumber(i);
          failureMessage.append("\n");
          failureMessage.append(testData[i].testDescription);
          failureMessage.append("\n  seconds:      ");
          failureMessage.appendNumber(testData[i].seconds);
          failureMessage.append("\n  milliseconds: ");
          failureMessage.appendNumber(testData[i].milliseconds);
          failureMessage.append("\n  tolerance:    ");
          failureMessage.appendNumber(testData[i].tolerance);
          CPPUNIT_ASSERT_MESSAGE(failureMessage.data(), returnValue);

          REPORT_SKEW(("      Timing inaccuracy for iter %3d = %8ld us; Time = %ld.%03ld;\n",
                       i,
                       getTimeDeltaInUsecs() - expectedWaitUSecs,
                       testData[i].seconds,
                       testData[i].milliseconds
                         ));
          delete pTimer;
          delete pNotifier;
       }
    }