summaryrefslogtreecommitdiff
path: root/cpp/test/IceUtil/timer/Client.cpp
blob: 1af264b6120c20dc720b2f4ba7229b3eb6c97168 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// **********************************************************************
//
// Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <IceUtil/Timer.h>
#include <TestCommon.h>

#include <vector>

using namespace IceUtil;
using namespace std;

class TestTask : public IceUtil::TimerTask, IceUtil::Monitor<IceUtil::Mutex>
{
public:

    TestTask() : _count(0)
    {
    }

    TestTask(const IceUtil::Time& scheduledTime) : _scheduledTime(scheduledTime), _count(0)
    {
    }
    
    virtual void 
    runTimerTask()
    {
        Lock sync(*this);
        ++_count;
        _run = IceUtil::Time::now(IceUtil::Time::Monotonic);
        //cerr << "run: " << _scheduledTime.toMilliSeconds() << " " << _run.toMilliSeconds() << endl;
        notifyAll();
    }

    virtual bool
    operator<(const TestTask& r) const
    {
        return _scheduledTime < r._scheduledTime;
    }

    virtual bool
    hasRun() const
    {
        Lock sync(*this);
        return _run != IceUtil::Time();
    }

    int 
    getCount() const
    {
        Lock sync(*this);
        return _count;
    }
    
    virtual IceUtil::Time
    getRunTime() const
    {
        Lock sync(*this);
        return _run;
    }

    IceUtil::Time
    getScheduledTime() const
    {
        return _scheduledTime;
    }

    virtual void
    waitForRun()
    {
        Lock sync(*this);
        while(_run == IceUtil::Time())
        {
            if(!timedWait(IceUtil::Time::seconds(10)))
            {
                test(false); // Timeout.
            }
        }
    }

    void 
    clear()
    {
        _run = IceUtil::Time();
        _count = 0;
    }

private:

    IceUtil::Time _run;
    IceUtil::Time _scheduledTime;
    int _count;
};
typedef IceUtil::Handle<TestTask> TestTaskPtr;

int main(int argc, char* argv[])
{
    cout << "testing timer... " << flush;
    {
        IceUtil::TimerPtr timer = new IceUtil::Timer();

        {
            TestTaskPtr task = new TestTask();
            timer->schedule(task, IceUtil::Time());
            task->waitForRun();
            task->clear();
	    while(true)
	    {
		try
		{
                    timer->schedule(task, IceUtil::Time::milliSeconds(-10));
		    timer->schedule(task, IceUtil::Time());
		}
		catch(const IceUtil::IllegalArgumentException&)
		{
		    break;
		}
                task->waitForRun();
                task->clear();
	    }
        }

        {
            TestTaskPtr task = new TestTask();
            test(!timer->cancel(task));
            timer->schedule(task, IceUtil::Time::seconds(1));
            test(!task->hasRun() && timer->cancel(task) && !task->hasRun());
            test(!timer->cancel(task));
            IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(1100));
            test(!task->hasRun());
        }

        {
            vector<TestTaskPtr> tasks;
            IceUtil::Time start = IceUtil::Time::now(IceUtil::Time::Monotonic) + IceUtil::Time::milliSeconds(500);
            for(int i = 0; i < 20; ++i)
            {
                tasks.push_back(new TestTask(IceUtil::Time::milliSeconds(500 + i * 5)));
            }

            random_shuffle(tasks.begin(), tasks.end());
	    vector<TestTaskPtr>::const_iterator p;
            for(p = tasks.begin(); p != tasks.end(); ++p)
            {
                timer->schedule(*p, (*p)->getScheduledTime());
            }

            for(p = tasks.begin(); p != tasks.end(); ++p)
            {
                (*p)->waitForRun();
            }            

            test(IceUtil::Time::now(IceUtil::Time::Monotonic) > start);

            sort(tasks.begin(), tasks.end());
            for(p = tasks.begin(); p + 1 != tasks.end(); ++p)
            {
                if((*p)->getRunTime() > (*(p + 1))->getRunTime())
                {
                    test(false);
                }
            }
        }

        {
            TestTaskPtr task = new TestTask();
            timer->scheduleRepeated(task, IceUtil::Time::milliSeconds(20));
            IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(500));
            test(task->hasRun());
	    test(task->getCount() > 1);
	    test(task->getCount() < 26);
            test(timer->cancel(task));
            int count = task->getCount();
            IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(100));
            test(count == task->getCount() || count + 1 == task->getCount());
        }

        timer->destroy();
    }
    cout << "ok" << endl;
    return EXIT_SUCCESS;
}