reSIProcate/stack  9694
testTimer.cxx
Go to the documentation of this file.
00001 #include <iostream>
00002 #include "resip/stack/TransactionMessage.hxx"
00003 #include "resip/stack/TimerQueue.hxx"
00004 #include "resip/stack/TuSelector.hxx"
00005 #include "rutil/Fifo.hxx"
00006 #include "rutil/TimeLimitFifo.hxx"
00007 #ifdef WIN32
00008 #include <io.h>
00009 #else
00010 #include <unistd.h>
00011 #endif
00012 
00013 #ifdef WIN32
00014 #define usleep(x) Sleep(x/1000)
00015 #define sleep(x) Sleep(x*1000)
00016 #endif
00017 
00018 using namespace resip;
00019 using namespace std;
00020 
00021 bool
00022 isNear(int value, int reference, int epsilon=250)
00023 {
00024    int diff = ::abs(value-reference);
00025    return (diff < epsilon);
00026 }
00027 
00028 
00029 int
00030 main()
00031 {
00032 
00033    TimeLimitFifo<Message> f(0, 0);
00034    Fifo<TimerMessage> r;
00035    
00036    TransactionTimerQueue timer(r);
00037    TimeLimitTimerQueue timer2(f);
00038 
00039    cerr << "Before Fifo size: " << f.size() << endl;
00040    assert(f.size() == 0);
00041    cerr << "next timer = " << timer.msTillNextTimer() << endl;
00042    assert(timer.msTillNextTimer() == INT_MAX);
00043 
00044    // throw a few events in the queue
00045    timer.add(Timer::TimerA, "first", 1000);
00046    cerr << "next timer will fire in " << timer.msTillNextTimer() << "ms" << endl;
00047    assert(isNear(timer.msTillNextTimer(), 1000));
00048    timer.add(Timer::TimerA, "second", 2000);
00049    assert(isNear(timer.msTillNextTimer(), 1000));
00050    timer.add(Timer::TimerA, "third", 4000);
00051    assert(isNear(timer.msTillNextTimer(), 1000));
00052    timer.add(Timer::TimerA, "fourth", 8000);
00053    assert(isNear(timer.msTillNextTimer(), 1000));
00054    timer.add(Timer::TimerA, "fifth", 16000);
00055    assert(isNear(timer.msTillNextTimer(), 1000));
00056 
00057    cerr << timer;
00058    assert(r.size() == 0);
00059    assert(timer.size() == 5);
00060    
00061    timer.process();
00062 
00063    assert(r.size() == 0);
00064    assert(timer.size() == 5);
00065 
00066    cerr << timer;
00067    sleep(1);
00068    cerr << timer;
00069    timer.process();
00070    
00071    cerr << timer;
00072    assert(r.size() == 1);
00073    assert(timer.size() == 4);
00074 
00075 
00076    cerr << "next timer will fire in " << timer.msTillNextTimer() << "ms" << endl;
00077    timer.process();
00078    cerr << "next timer will fire in " << timer.msTillNextTimer() << "ms" << endl;
00079    cerr << "timer queue size=" << timer.size() << endl;
00080    cerr << "fired event queue size=" << r.size() << endl;
00081    assert(r.size() == 1);
00082    timer.process();   
00083    assert(r.size() == 1);
00084 
00085    sleep(1);
00086    timer.process();
00087    assert(r.size() == 2);
00088    timer.process();   
00089    assert(r.size() == 2);
00090 
00091    sleep(2);
00092    timer.process();
00093    assert(r.size() == 3);
00094    timer.process();   
00095    assert(r.size() == 3);
00096 
00097    sleep(4);
00098    timer.process();
00099    assert(r.size() == 4);
00100    timer.process();   
00101    assert(r.size() == 4);
00102 
00103 
00104    cerr << "next timer will fire in " << timer.msTillNextTimer() << "ms" << endl;
00105    assert(isNear(timer.msTillNextTimer(), 8000));
00106 
00107    sleep(8);
00108    timer.process();
00109    cerr << timer;
00110    
00111    assert(r.size() == 5);
00112    timer.process();   
00113    assert(r.size() == 5);
00114 
00115    sleep(1);
00116    timer.process();
00117    assert(r.size() == 5);
00118    timer.process();   
00119    assert(r.size() == 5);
00120 
00121    cerr << "All OK" << endl;
00122    return 0;
00123 }
00124 
00125 /* ====================================================================
00126  * The Vovida Software License, Version 1.0 
00127  * 
00128  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00129  * 
00130  * Redistribution and use in source and binary forms, with or without
00131  * modification, are permitted provided that the following conditions
00132  * are met:
00133  * 
00134  * 1. Redistributions of source code must retain the above copyright
00135  *    notice, this list of conditions and the following disclaimer.
00136  * 
00137  * 2. Redistributions in binary form must reproduce the above copyright
00138  *    notice, this list of conditions and the following disclaimer in
00139  *    the documentation and/or other materials provided with the
00140  *    distribution.
00141  * 
00142  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00143  *    and "Vovida Open Communication Application Library (VOCAL)" must
00144  *    not be used to endorse or promote products derived from this
00145  *    software without prior written permission. For written
00146  *    permission, please contact vocal@vovida.org.
00147  *
00148  * 4. Products derived from this software may not be called "VOCAL", nor
00149  *    may "VOCAL" appear in their name, without prior written
00150  *    permission of Vovida Networks, Inc.
00151  * 
00152  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00153  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00154  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00155  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00156  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00157  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00158  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00159  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00160  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00161  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00162  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00163  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00164  * DAMAGE.
00165  * 
00166  * ====================================================================
00167  * 
00168  * This software consists of voluntary contributions made by Vovida
00169  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00170  * Inc.  For more information on Vovida Networks, Inc., please see
00171  * <http://www.vovida.org/>.
00172  *
00173  */