qa_timebug.cpp

00001 
00002 /***************************************************************************
00003  *  qa_timebug.cpp - QA app to find a potential bug related to the Time class
00004  *
00005  *  Created: Tue Dec 18 10:38:30 2007
00006  *  Copyright  2007  Tim Niemueller 
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 /// @cond QA
00025 
00026 #include <core/threading/thread.h>
00027 #include <utils/time/clock.h>
00028 #include <utils/time/time.h>
00029 #include <utils/time/wait.h>
00030 #include <utils/system/signal.h>
00031 
00032 #include <unistd.h>
00033 
00034 #include <iostream>
00035 
00036 using namespace std;
00037 using namespace fawkes;
00038 
00039 
00040 class QaTestWait
00041 {
00042 public:
00043   QaTestWait()
00044   {
00045     __clock = Clock::instance();
00046     __until = new Time();
00047   }
00048 
00049 
00050   void mark_start()
00051   {
00052     __clock->get_time(__until);
00053     *__until += (long int)30000;
00054   }
00055 
00056   void wait()
00057   {
00058     Time now;
00059     printf("Now at %p\n", &now);
00060     __clock->get_time(&now);
00061     usleep(0);
00062     long int remaining_usec = (*__until - now).in_usec();
00063     while ( remaining_usec > 0 ) {
00064       usleep(remaining_usec);
00065       __clock->get_time(&now);
00066       remaining_usec = (*__until - now).in_usec();
00067       //remaining_usec = 0;
00068     }
00069   }
00070 
00071   Clock *__clock;
00072   Time  *__until;
00073 };
00074 
00075 class QaSignalHandler : public SignalHandler
00076 {
00077 public:
00078   QaSignalHandler(Thread *thread)
00079   {
00080     this->thread = thread;
00081   }
00082 
00083   virtual void handle_signal(int signum)
00084   {
00085     thread->cancel();
00086   }
00087 
00088   Thread *thread;
00089 };
00090 
00091 class QaTestThread : public Thread
00092 {
00093 public:
00094   QaTestThread() : Thread("QaTestThread")
00095   {
00096     timewait = new TimeWait(Clock::instance(), 30000);
00097     testwait = new QaTestWait();
00098   }
00099 
00100   virtual void loop()
00101   {
00102     printf("Loop running\n");
00103     timewait->mark_start();
00104     timewait->wait();
00105     //testwait->mark_start();
00106     //testwait->wait();
00107   }
00108 
00109   QaTestWait *testwait;
00110   TimeWait *timewait;
00111 };
00112 
00113 int main(int argc, char** argv)
00114 {
00115   QaTestThread t;
00116   t.start();
00117 
00118   QaSignalHandler h(&t);
00119   SignalManager::register_handler(SIGINT, &h);
00120 
00121   t.join();
00122 
00123   return 0;
00124 }
00125 
00126 /// @endcond

Generated on Tue Feb 22 13:32:28 2011 for Fawkes API by  doxygen 1.4.7