Packets: Extend collection parser documentation
[senf.git] / Scheduler / Scheduler.test.cc
index 102d1a3..b45b1a3 100644 (file)
@@ -2,9 +2,9 @@
 // $Id$
 //
 // Copyright (C) 2006
-// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
-// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
-//     Stefan Bund <stefan.bund@fokus.fraunhofer.de>
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
+//     Stefan Bund <g0dil@berlios.de>
 //
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
@@ -39,7 +39,7 @@
 
 #include "Scheduler.hh"
 
-#include <boost/test/auto_unit_test.hpp>
+#include "../Utils/auto_unit_test.hh"
 #include <boost/test/test_tools.hpp>
 
 #define prefix_
@@ -141,7 +141,7 @@ namespace {
     int size;
     int event;
 
-    void callback(int fd, Scheduler::EventId ev)
+    void callback(int fd, int ev)
     {
         event = ev;
         switch (event & Scheduler::EV_ALL) {
@@ -160,8 +160,10 @@ namespace {
         Scheduler::instance().terminate();
     }
 
+    bool timeoutCalled = false;
     void timeout()
     {
+        timeoutCalled = true;
         Scheduler::instance().terminate();
     }
 
@@ -177,7 +179,7 @@ namespace {
         return handle.fd_;
     }
 
-    void handleCallback(HandleWrapper const & handle, Scheduler::EventId event)
+    void handleCallback(HandleWrapper const & handle, int event)
     {
         if (handle.tag_ != "TheTag")
             return;
@@ -186,12 +188,34 @@ namespace {
 
     bool is_close(ClockService::clock_type a, ClockService::clock_type b)
     {
-        return (a<b ? b-a : a-b) < 10100000UL; // a little bit over 10ms
+        return (a<b ? b-a : a-b) < ClockService::milliseconds(100);
+    }
+    
+    ClockService::clock_type sigtime (0);
+
+    void sigusr(siginfo_t const &)
+    {
+        sigtime = ClockService::now();
+        Scheduler::instance().terminate();
+    }
+
+    void delay(unsigned long milliseconds)
+    {
+        struct timespec ts;
+        ts.tv_sec = milliseconds / 1000;
+        ts.tv_nsec = (milliseconds % 1000) * 1000000;
+        while (nanosleep(&ts,&ts) < 0 && errno == EINTR) ;
+    }
+
+    void blockingHandler()
+    {
+        delay(2200);
+        Scheduler::instance().terminate();
     }
 
 }
 
-BOOST_AUTO_UNIT_TEST(scheduler)
+BOOST_AUTO_UNIT_TEST(testScheduler)
 {
     int pid = start_server();
     BOOST_REQUIRE (pid);
@@ -215,7 +239,8 @@ BOOST_AUTO_UNIT_TEST(scheduler)
 
     BOOST_CHECK_NO_THROW( Scheduler::instance() );
 
-    BOOST_CHECK_NO_THROW( Scheduler::instance().add(sock,&callback,Scheduler::EV_READ) );
+    BOOST_CHECK_NO_THROW( Scheduler::instance().add(sock,boost::bind(&callback, sock, _1),
+                                                    Scheduler::EV_READ) );
     event = Scheduler::EV_NONE;
     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
     BOOST_CHECK_EQUAL( event, Scheduler::EV_READ );
@@ -223,18 +248,31 @@ BOOST_AUTO_UNIT_TEST(scheduler)
     buffer[size]=0;
     BOOST_CHECK_EQUAL( buffer, "READ" );
 
+    event = Scheduler::EV_NONE;
     BOOST_CHECK_NO_THROW( Scheduler::instance().timeout(
-                              ClockService::now()+100000000UL,&timeout) );
+                              ClockService::now()+ClockService::milliseconds(200),&timeout) );
     BOOST_CHECK_NO_THROW( Scheduler::instance().timeout(
-                              ClockService::now()+200000000UL,&timeout) );
+                              ClockService::now()+ClockService::milliseconds(400),&timeout) );
     ClockService::clock_type t (ClockService::now());
     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
-    BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (t+100000000UL) );
+    BOOST_CHECK_PREDICATE( is_close, (ClockService::now()-t) (ClockService::milliseconds(200)) );
+    BOOST_CHECK( timeoutCalled );
+    BOOST_CHECK_EQUAL( event, Scheduler::EV_NONE );
+    BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (Scheduler::instance().eventTime()) );
+    timeoutCalled = false;
+    BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
+    BOOST_CHECK_PREDICATE( is_close, (ClockService::now()-t) (ClockService::milliseconds(400)) );
+    BOOST_CHECK( timeoutCalled );
+    BOOST_CHECK_EQUAL( event, Scheduler::EV_NONE );
+
+    BOOST_CHECK_NO_THROW( Scheduler::instance().timeout(ClockService::now(), &blockingHandler) );
     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
-    BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (t+200000000UL) );
+    BOOST_CHECK_EQUAL( Scheduler::instance().hangCount(), 1u );
 
     HandleWrapper handle(sock,"TheTag");
-    BOOST_CHECK_NO_THROW( Scheduler::instance().add(handle,&handleCallback,Scheduler::EV_WRITE) );
+    BOOST_CHECK_NO_THROW( Scheduler::instance().add(handle,
+                                                    boost::bind(&handleCallback,handle,_1),
+                                                    Scheduler::EV_WRITE) );
     strcpy(buffer,"WRITE");
     size=5;
     event = Scheduler::EV_NONE;
@@ -249,6 +287,19 @@ BOOST_AUTO_UNIT_TEST(scheduler)
     BOOST_REQUIRE_EQUAL( size, 2 );
     buffer[size]=0;
     BOOST_CHECK_EQUAL( buffer, "OK" );
+    BOOST_CHECK_NO_THROW( Scheduler::instance().remove(handle) );
+
+    unsigned tid (Scheduler::instance().timeout(
+                      ClockService::now()+ClockService::milliseconds(400),&timeout));
+    BOOST_CHECK_NO_THROW( Scheduler::instance().registerSignal(SIGUSR1, &sigusr) );
+    t = ClockService::now();
+    ::kill(::getpid(), SIGUSR1);
+    delay(100);
+    BOOST_CHECK_NO_THROW( Scheduler::instance().process() ); 
+    BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (t+ClockService::milliseconds(200)) );
+    BOOST_CHECK_PREDICATE( is_close, (sigtime) (t+ClockService::milliseconds(200)) );
+    Scheduler::instance().cancelTimeout(tid);
+    BOOST_CHECK_NO_THROW( Scheduler::instance().unregisterSignal(SIGUSR1) );
 
     ///////////////////////////////////////////////////////////////////////////