summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/tests/TimerTest.cpp
blob: 682699dbd3017ad30e7f1fd893ed59127f7303c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
#include "qpid/broker/Timer.h"
#include "qpid/sys/Monitor.h"
#include "qpid_test_plugin.h"
#include <math.h>
#include <iostream>
#include <memory>
#include <boost/format.hpp>

using namespace qpid::broker;
using namespace qpid::sys;
using boost::dynamic_pointer_cast;

class TimerTest : public CppUnit::TestCase  
{
    CPPUNIT_TEST_SUITE(TimerTest);
    CPPUNIT_TEST(testGeneral);
    CPPUNIT_TEST_SUITE_END();

    class Counter
    {
        Mutex lock;
        uint counter;
    public:
        Counter() : counter(0) {}
        uint next()
        {
            Mutex::ScopedLock l(lock);
            return ++counter;
        }
    };
    
    class TestTask : public TimerTask
    {
        const AbsTime start;
        const Duration expected;
        AbsTime end;
        bool fired;
        uint position;
        Monitor monitor;
        Counter& counter;

    public:
        TestTask(Duration timeout, Counter& _counter) 
            : TimerTask(timeout), start(now()), expected(timeout), end(start), fired(false), counter(_counter) {}

        void fire()
        {
            Monitor::ScopedLock l(monitor);
            fired = true;
            position = counter.next();
            end = now();
            monitor.notify();
        }

        void check(uint expected_position, uint64_t tolerance = 500 * TIME_MSEC)
        {
            Monitor::ScopedLock l(monitor);
            CPPUNIT_ASSERT(fired);
            CPPUNIT_ASSERT_EQUAL(expected_position, position);
            Duration actual(start, end);
            uint64_t difference = abs(expected - actual);
            std::string msg(boost::lexical_cast<std::string>(boost::format("tolerance = %1%, difference = %2%") % tolerance % difference));
            CPPUNIT_ASSERT_MESSAGE(msg, difference < tolerance);
        }

        void wait(Duration d)
        {
            Monitor::ScopedLock l(monitor);
            monitor.wait(AbsTime(now(), d));
        }
    };

    class DummyRunner : public Runnable
    {
    public:
        void run() {}
    };

public:

    void testGeneral()
    {
        Counter counter;
        Timer timer;
        TestTask::shared_ptr task1(new TestTask(Duration(3 * TIME_SEC), counter));
        TestTask::shared_ptr task2(new TestTask(Duration(1 * TIME_SEC), counter));
        TestTask::shared_ptr task3(new TestTask(Duration(4 * TIME_SEC), counter));
        TestTask::shared_ptr task4(new TestTask(Duration(2 * TIME_SEC), counter));
        
        timer.add(task1);
        timer.add(task2);
        timer.add(task3);
        timer.add(task4);
        
        dynamic_pointer_cast<TestTask>(task3)->wait(Duration(6 * TIME_SEC));
        
        dynamic_pointer_cast<TestTask>(task1)->check(3);
        dynamic_pointer_cast<TestTask>(task2)->check(1);
        dynamic_pointer_cast<TestTask>(task3)->check(4);
        dynamic_pointer_cast<TestTask>(task4)->check(2);
    }
};

// Make this test suite a plugin.
CPPUNIT_PLUGIN_IMPLEMENT();
CPPUNIT_TEST_SUITE_REGISTRATION(TimerTest);