ICE 3.4.2
[php5-ice-freebsdport.git] / cpp / test / IceUtil / timer / Client.cpp
bloba46ec76551d735c0f91f49e27531a9128fbf2c14
1 // **********************************************************************
2 //
3 // Copyright (c) 2003-2011 ZeroC, Inc. All rights reserved.
4 //
5 // This copy of Ice is licensed to you under the terms described in the
6 // ICE_LICENSE file included in this distribution.
7 //
8 // **********************************************************************
10 #include <IceUtil/Timer.h>
11 #include <TestCommon.h>
13 #include <vector>
15 using namespace IceUtil;
16 using namespace std;
18 class TestTask : public IceUtil::TimerTask, IceUtil::Monitor<IceUtil::Mutex>
20 public:
22 TestTask() : _count(0)
26 TestTask(const IceUtil::Time& scheduledTime) : _scheduledTime(scheduledTime), _count(0)
30 virtual void
31 runTimerTask()
33 Lock sync(*this);
34 ++_count;
35 _run = IceUtil::Time::now(IceUtil::Time::Monotonic);
36 //cerr << "run: " << _scheduledTime.toMilliSeconds() << " " << _run.toMilliSeconds() << endl;
37 notifyAll();
40 virtual bool
41 operator<(const TestTask& r) const
43 return _scheduledTime < r._scheduledTime;
46 virtual bool
47 hasRun() const
49 Lock sync(*this);
50 return _run != IceUtil::Time();
53 int
54 getCount() const
56 Lock sync(*this);
57 return _count;
60 virtual IceUtil::Time
61 getRunTime() const
63 Lock sync(*this);
64 return _run;
67 IceUtil::Time
68 getScheduledTime() const
70 return _scheduledTime;
73 virtual void
74 waitForRun()
76 Lock sync(*this);
77 while(_run == IceUtil::Time())
79 if(!timedWait(IceUtil::Time::seconds(10)))
81 test(false); // Timeout.
86 void
87 clear()
89 _run = IceUtil::Time();
90 _count = 0;
93 private:
95 IceUtil::Time _run;
96 IceUtil::Time _scheduledTime;
97 int _count;
99 typedef IceUtil::Handle<TestTask> TestTaskPtr;
102 class DestroyTask : public IceUtil::TimerTask, IceUtil::Monitor<IceUtil::Mutex>
104 public:
106 DestroyTask(const IceUtil::TimerPtr& timer) : _timer(timer), _run(false)
110 virtual void
111 runTimerTask()
113 Lock sync(*this);
114 _timer->destroy();
115 _run = true;
116 notify();
119 virtual void
120 waitForRun()
122 Lock sync(*this);
123 while(!_run)
125 if(!timedWait(IceUtil::Time::seconds(10)))
127 test(false); // Timeout.
132 private:
134 IceUtil::TimerPtr _timer;
135 bool _run;
137 typedef IceUtil::Handle<DestroyTask> DestroyTaskPtr;
139 int main(int argc, char* argv[])
141 cout << "testing timer... " << flush;
143 IceUtil::TimerPtr timer = new IceUtil::Timer();
146 TestTaskPtr task = new TestTask();
147 timer->schedule(task, IceUtil::Time());
148 task->waitForRun();
149 task->clear();
152 // Verify that the same task cannot be scheduled more than once.
154 timer->schedule(task, IceUtil::Time::milliSeconds(100));
157 timer->schedule(task, IceUtil::Time());
159 catch(const IceUtil::IllegalArgumentException&)
161 // Expected.
163 task->waitForRun();
164 task->clear();
168 TestTaskPtr task = new TestTask();
169 test(!timer->cancel(task));
170 timer->schedule(task, IceUtil::Time::seconds(1));
171 test(!task->hasRun() && timer->cancel(task) && !task->hasRun());
172 test(!timer->cancel(task));
173 IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(1100));
174 test(!task->hasRun());
178 vector<TestTaskPtr> tasks;
179 IceUtil::Time start = IceUtil::Time::now(IceUtil::Time::Monotonic) + IceUtil::Time::milliSeconds(500);
180 for(int i = 0; i < 20; ++i)
182 tasks.push_back(new TestTask(IceUtil::Time::milliSeconds(500 + i * 5)));
185 random_shuffle(tasks.begin(), tasks.end());
186 vector<TestTaskPtr>::const_iterator p;
187 for(p = tasks.begin(); p != tasks.end(); ++p)
189 timer->schedule(*p, (*p)->getScheduledTime());
192 for(p = tasks.begin(); p != tasks.end(); ++p)
194 (*p)->waitForRun();
197 test(IceUtil::Time::now(IceUtil::Time::Monotonic) > start);
199 sort(tasks.begin(), tasks.end());
200 for(p = tasks.begin(); p + 1 != tasks.end(); ++p)
202 if((*p)->getRunTime() > (*(p + 1))->getRunTime())
204 test(false);
210 TestTaskPtr task = new TestTask();
211 timer->scheduleRepeated(task, IceUtil::Time::milliSeconds(20));
212 IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(500));
213 test(task->hasRun());
214 test(task->getCount() > 1);
215 test(task->getCount() < 26);
216 test(timer->cancel(task));
217 int count = task->getCount();
218 IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(100));
219 test(count == task->getCount() || count + 1 == task->getCount());
222 timer->destroy();
224 cout << "ok" << endl;
226 cout << "testing timer destroy... " << flush;
229 IceUtil::TimerPtr timer = new IceUtil::Timer();
230 DestroyTaskPtr destroyTask = new DestroyTask(timer);
231 timer->schedule(destroyTask, IceUtil::Time());
232 destroyTask->waitForRun();
235 timer->schedule(destroyTask, IceUtil::Time());
237 catch(const IceUtil::IllegalArgumentException&)
239 // Expected;
243 IceUtil::TimerPtr timer = new IceUtil::Timer();
244 TestTaskPtr testTask = new TestTask();
245 timer->schedule(testTask, IceUtil::Time());
246 timer->destroy();
249 timer->schedule(testTask, IceUtil::Time());
251 catch(const IceUtil::IllegalArgumentException&)
253 // Expected;
257 cout << "ok" << endl;
259 return EXIT_SUCCESS;