summaryrefslogtreecommitdiff
path: root/cpp/test
diff options
context:
space:
mode:
Diffstat (limited to 'cpp/test')
-rw-r--r--cpp/test/IceUtil/Makefile3
-rw-r--r--cpp/test/IceUtil/Makefile.mak3
-rw-r--r--cpp/test/IceUtil/timer/.depend1
-rw-r--r--cpp/test/IceUtil/timer/Client.cpp163
-rw-r--r--cpp/test/IceUtil/timer/Makefile29
-rw-r--r--cpp/test/IceUtil/timer/Makefile.mak33
-rwxr-xr-xcpp/test/IceUtil/timer/run.py39
7 files changed, 269 insertions, 2 deletions
diff --git a/cpp/test/IceUtil/Makefile b/cpp/test/IceUtil/Makefile
index 0f36cbf0aa5..0df25e078d1 100644
--- a/cpp/test/IceUtil/Makefile
+++ b/cpp/test/IceUtil/Makefile
@@ -16,7 +16,8 @@ SUBDIRS = condvar \
unicode \
inputUtil \
uuid \
- ctrlCHandler
+ ctrlCHandler \
+ timer
$(EVERYTHING)::
@for subdir in $(SUBDIRS); \
diff --git a/cpp/test/IceUtil/Makefile.mak b/cpp/test/IceUtil/Makefile.mak
index a144895cd03..d90fcbe69cf 100644
--- a/cpp/test/IceUtil/Makefile.mak
+++ b/cpp/test/IceUtil/Makefile.mak
@@ -16,7 +16,8 @@ SUBDIRS = condvar \
unicode \
inputUtil \
uuid \
- ctrlCHandler
+ ctrlCHandler \
+ timer
$(EVERYTHING)::
@for %i in ( $(SUBDIRS) ) do \
diff --git a/cpp/test/IceUtil/timer/.depend b/cpp/test/IceUtil/timer/.depend
new file mode 100644
index 00000000000..099366289c4
--- /dev/null
+++ b/cpp/test/IceUtil/timer/.depend
@@ -0,0 +1 @@
+Client$(OBJEXT): Client.cpp ../../../include/IceUtil/Timer.h ../../../include/IceUtil/Shared.h ../../../include/IceUtil/Config.h ../../../include/IceUtil/Mutex.h ../../../include/IceUtil/Lock.h ../../../include/IceUtil/ThreadException.h ../../../include/IceUtil/Exception.h ../../../include/IceUtil/Thread.h ../../../include/IceUtil/Handle.h ../../../include/IceUtil/Monitor.h ../../../include/IceUtil/Cond.h ../../../include/IceUtil/Time.h ../../include/TestCommon.h
diff --git a/cpp/test/IceUtil/timer/Client.cpp b/cpp/test/IceUtil/timer/Client.cpp
new file mode 100644
index 00000000000..013e33277e1
--- /dev/null
+++ b/cpp/test/IceUtil/timer/Client.cpp
@@ -0,0 +1,163 @@
+// **********************************************************************
+//
+// Copyright (c) 2003-2007 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()
+ {
+ }
+
+ TestTask(const IceUtil::Time& scheduledTime) : _scheduledTime(scheduledTime)
+ {
+ }
+
+ virtual void
+ run()
+ {
+ Lock sync(*this);
+ ++_count;
+ _run = IceUtil::Time::now();
+ //cerr << "run: " << _scheduledTime.toMicroSeconds() << " " << _run.toMicroSeconds() << 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.
+ }
+ }
+ }
+
+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::now());
+ task->waitForRun();
+ }
+
+ {
+ TestTaskPtr task = new TestTask();
+ test(!timer->cancel(task));
+ timer->schedule(task, IceUtil::Time::now() + 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::milliSeconds(100);
+ for(int i = 0; i < 100; ++i)
+ {
+ tasks.push_back(new TestTask(start + IceUtil::Time::milliSeconds(i)));
+ }
+
+ random_shuffle(tasks.begin(), tasks.end());
+
+ for(vector<TestTaskPtr>::const_iterator p = tasks.begin(); p != tasks.end(); ++p)
+ {
+ timer->schedule(*p, (*p)->getScheduledTime());
+ }
+
+ for(vector<TestTaskPtr>::const_iterator p = tasks.begin(); p != tasks.end(); ++p)
+ {
+ (*p)->waitForRun();
+ }
+
+ test(IceUtil::Time::now() - start > IceUtil::Time::milliSeconds(99));
+
+ sort(tasks.begin(), tasks.end());
+ for(vector<TestTaskPtr>::const_iterator 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() && task->getCount() > 15 && task->getCount() < 26);
+ test(timer->cancel(task));
+ int count = task->getCount();
+ IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(100));
+ test(count == task->getCount());
+ }
+
+ timer->destroy();
+ }
+ cout << "ok" << endl;
+ return EXIT_SUCCESS;
+}
diff --git a/cpp/test/IceUtil/timer/Makefile b/cpp/test/IceUtil/timer/Makefile
new file mode 100644
index 00000000000..e24d058530a
--- /dev/null
+++ b/cpp/test/IceUtil/timer/Makefile
@@ -0,0 +1,29 @@
+# **********************************************************************
+#
+# Copyright (c) 2003-2007 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.
+#
+# **********************************************************************
+
+top_srcdir = ../../..
+
+CLIENT = client
+
+TARGETS = $(CLIENT)
+
+OBJS = Client.o
+
+
+SRCS = $(OBJS:.o=.cpp)
+
+include $(top_srcdir)/config/Make.rules
+
+CPPFLAGS := -I. -I../../include $(CPPFLAGS)
+
+$(CLIENT): $(OBJS)
+ rm -f $@
+ $(CXX) $(LDFLAGS) -o $@ $(OBJS) $(BASELIBS)
+
+include .depend
diff --git a/cpp/test/IceUtil/timer/Makefile.mak b/cpp/test/IceUtil/timer/Makefile.mak
new file mode 100644
index 00000000000..eb223c31957
--- /dev/null
+++ b/cpp/test/IceUtil/timer/Makefile.mak
@@ -0,0 +1,33 @@
+# **********************************************************************
+#
+# Copyright (c) 2003-2007 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.
+#
+# **********************************************************************
+
+top_srcdir = ..\..\..
+
+CLIENT = client.exe
+
+TARGETS = $(CLIENT)
+
+OBJS = Client.obj
+
+SRCS = $(OBJS:.obj=.cpp)
+
+!include $(top_srcdir)/config/Make.rules.mak
+
+CPPFLAGS = -I. -I../../include $(CPPFLAGS) -DWIN32_LEAN_AND_MEAN
+
+!if "$(CPP_COMPILER)" != "BCC2006" && "$(OPTIMIZE)" != "yes"
+PDBFLAGS = /pdb:$(CLIENT:.exe=.pdb)
+!endif
+
+$(CLIENT): $(OBJS)
+ $(LINK) $(LD_EXEFLAGS) $(PDBFLAGS) $(SETARGV) $(OBJS) $(PREOUT)$@ $(PRELIBS)$(BASELIBS)
+ @if exist $@.manifest echo ^ ^ ^ Embedding manifest using $(MT) && \
+ $(MT) -nologo -manifest $@.manifest -outputresource:$@;#1 && del /q $@.manifest
+
+!include .depend
diff --git a/cpp/test/IceUtil/timer/run.py b/cpp/test/IceUtil/timer/run.py
new file mode 100755
index 00000000000..d7cb0424185
--- /dev/null
+++ b/cpp/test/IceUtil/timer/run.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+# **********************************************************************
+#
+# Copyright (c) 2003-2007 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.
+#
+# **********************************************************************
+
+import os, sys
+
+for toplevel in [".", "..", "../..", "../../..", "../../../.."]:
+ toplevel = os.path.normpath(toplevel)
+ if os.path.exists(os.path.join(toplevel, "config", "TestUtil.py")):
+ break
+else:
+ raise "can't find toplevel directory!"
+
+sys.path.append(os.path.join(toplevel, "config"))
+import TestUtil
+
+name = os.path.join("IceUtil", "timer")
+testdir = os.path.join(toplevel, "test", name)
+
+client = os.path.join(testdir, "client")
+
+print "starting client...",
+clientPipe = os.popen(client + " 2>&1")
+print "ok"
+
+TestUtil.printOutputFromPipe(clientPipe);
+
+clientStatus = TestUtil.closePipe(clientPipe)
+
+if clientStatus:
+ sys.exit(1)
+
+sys.exit(0)