Changeset 12756


Ignore:
Timestamp:
Mar 10, 2014, 6:09:22 PM (10 years ago)
Author:
hock@…
Message:

TEST_F(SystemQueueTimingTest, TimingSingleCall)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/SystemQueue-tests.cc

    r12755 r12756  
    33
    44#include <unistd.h>  // usleep
     5#include <boost/date_time.hpp> // local_time()
    56
    67#include <ostream>
     
    910using namespace ariba::utility;
    1011using namespace std;
     12
     13using boost::posix_time::microsec_clock;
     14
    1115
    1216/**
     
    264268/**
    265269 *  This subclass has some special member functions suitable for timing tests
     270 *
     271 *  NOTE: Timing tests are not always reproducable.. sorry. :-/
    266272 */
    267273class SystemQueueTimingTest :
     
    277283 */
    278284#define DELAY_TIME 20  // ms
    279 #define DELAY_MARGIN 2000  // microseconds (1/1000 ms)
     285#define DELAY_MARGIN 2000  // microseconds (1/1000 ms)   // TODO maybe this is too much..
    280286
    281287
     
    299305        sysq.cancel();
    300306    }
     307   
     308   
     309    /**
     310     * @param placed_in_queue The time (as ptime) when this event is put into the delay queue
     311     * @param intended_sleep_time The time (in microseconds) the event is supposed to sleep in the queue
     312     * @param margin The acceptable margin (in microseconds)
     313     */
     314    void TimeCheckingCall(ptime placed_in_queue, uint64_t intended_sleep_time, uint64_t margin)
     315    {
     316        ptime called_at = microsec_clock::local_time();
     317       
     318        // calculate actual sleep time and difference to intended sleep time
     319        boost::posix_time::time_duration actual_sleep_time = called_at - placed_in_queue;
     320        uint64_t diff = actual_sleep_time.total_microseconds() - intended_sleep_time;
     321       
     322        // info
     323        cout << "/// Timing difference: " << diff << " microseconds" << endl;
     324       
     325        EXPECT_LT( abs(diff), margin );
     326       
     327        checkmark = true;
     328    }
    301329
    302330    SystemQueue& sysq;
     
    315343
    316344    // noting to do until the delay is up..
    317     usleep(DELAY_TIME + DELAY_MARGIN*10);  // XXX margin too high (TODO lower when SysQ is reimplemented)
     345    usleep(DELAY_TIME*1000 + DELAY_MARGIN*10);  // XXX margin too high (TODO lower when SysQ is reimplemented)
    318346   
    319347    // wait for the event to happen
     
    379407    // XXX the usual bug..
    380408    sysq.scheduleCall(
    381         boost::bind(&SystemQueueTest::Check, this),
     409        boost::bind(&SystemQueueTimingTest::Check, this),
    382410                    DELAY_TIME*4
    383411    );
    384412
    385413    // noting to do until the delay is up..
    386     usleep(DELAY_TIME*4 + DELAY_MARGIN*100);  // XXX margin too high
     414    usleep(DELAY_TIME * 4000 + DELAY_MARGIN*100);  // XXX margin too high
    387415   
    388416    // wait for the event to happen
     
    392420    EXPECT_EQ( 4, last_ordered_call);
    393421}
     422
     423
     424/**
     425 *  Schedules a delayed call and test whether the sleep time is acurate
     426 */
     427TEST_F(SystemQueueTimingTest, TimingSingleCall)
     428{
     429    ptime now = microsec_clock::local_time();
     430   
     431    sysq.scheduleCall(
     432        boost::bind(&SystemQueueTimingTest::TimeCheckingCall, this,
     433                    now, DELAY_TIME*1000, DELAY_MARGIN),
     434        DELAY_TIME
     435    );
     436
     437    // main thread is going to sleep..
     438    usleep(DELAY_TIME * 1000 + DELAY_MARGIN * 2);
     439    wait_for_checkmark(MAX_WAIT);
     440   
     441    // make sure the measurement function was called at all
     442    EXPECT_TRUE(checkmark) << "Measurement function was NOT RUN AT ALL!";
     443}
     444
Note: See TracChangeset for help on using the changeset viewer.