diff --git a/Foundation/Foundation_vs71.vcproj b/Foundation/Foundation_vs71.vcproj
index 074183f8e..8a579ec64 100644
--- a/Foundation/Foundation_vs71.vcproj
+++ b/Foundation/Foundation_vs71.vcproj
@@ -574,7 +574,6 @@
-
@@ -2132,6 +2131,12 @@
+
+
+
+
+
+
+
+
+ Name="Dynamic">
+ Name="Header Files">
+ RelativePath=".\include\Poco\DynamicAny.h">
+ RelativePath=".\include\Poco\DynamicAnyHolder.h">
+ RelativePath=".\include\Poco\DynamicStruct.h">
-
-
-
+ RelativePath=".\include\Poco\Dynamic\Pair.h">
+ RelativePath=".\include\Poco\Dynamic\Struct.h">
+ RelativePath=".\include\Poco\Dynamic\Var.h">
+
+
+ Name="Source Files">
+ RelativePath=".\src\Var.cpp">
+ RelativePath=".\src\VarHolder.cpp">
diff --git a/Foundation/Foundation_vs80.vcproj b/Foundation/Foundation_vs80.vcproj
index 414e179aa..77661b390 100644
--- a/Foundation/Foundation_vs80.vcproj
+++ b/Foundation/Foundation_vs80.vcproj
@@ -2761,6 +2761,14 @@
RelativePath=".\src\NotificationQueue.cpp"
>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
NotificationPtr;
+ typedef Notification::Ptr NotificationPtr;
typedef void (C::*Callback)(const NotificationPtr&);
NObserver(C& object, Callback method):
diff --git a/Foundation/include/Poco/Notification.h b/Foundation/include/Poco/Notification.h
index 798dff9ca..b33b906ac 100644
--- a/Foundation/include/Poco/Notification.h
+++ b/Foundation/include/Poco/Notification.h
@@ -1,7 +1,7 @@
//
// Notification.h
//
-// $Id: //poco/svn/Foundation/include/Poco/Notification.h#2 $
+// $Id: //poco/Main/Foundation/include/Poco/Notification.h#3 $
//
// Library: Foundation
// Package: Notifications
@@ -43,6 +43,7 @@
#include "Poco/Foundation.h"
#include "Poco/Mutex.h"
#include "Poco/RefCountedObject.h"
+#include "Poco/AutoPtr.h"
namespace Poco {
@@ -56,6 +57,8 @@ class Foundation_API Notification: public RefCountedObject
/// template class.
{
public:
+ typedef AutoPtr Ptr;
+
Notification();
/// Creates the notification.
diff --git a/Foundation/include/Poco/NotificationCenter.h b/Foundation/include/Poco/NotificationCenter.h
index 0bd259e48..208f6ffc8 100644
--- a/Foundation/include/Poco/NotificationCenter.h
+++ b/Foundation/include/Poco/NotificationCenter.h
@@ -1,7 +1,7 @@
//
// NotificationCenter.h
//
-// $Id: //poco/svn/Foundation/include/Poco/NotificationCenter.h#2 $
+// $Id: //poco/Main/Foundation/include/Poco/NotificationCenter.h#4 $
//
// Library: Foundation
// Package: Notifications
@@ -41,6 +41,7 @@
#include "Poco/Foundation.h"
+#include "Poco/Notification.h"
#include "Poco/Mutex.h"
#include
@@ -48,7 +49,6 @@
namespace Poco {
-class Notification;
class AbstractObserver;
@@ -116,7 +116,7 @@ public:
void removeObserver(const AbstractObserver& observer);
/// Unregisters an observer with the NotificationCenter.
- void postNotification(Notification* pNotification);
+ void postNotification(Notification::Ptr pNotification);
/// Posts a notification to the NotificationCenter.
/// The NotificationCenter then delivers the notification
/// to all interested observers.
diff --git a/Foundation/include/Poco/NotificationQueue.h b/Foundation/include/Poco/NotificationQueue.h
index 25ba0edf4..97d59d751 100644
--- a/Foundation/include/Poco/NotificationQueue.h
+++ b/Foundation/include/Poco/NotificationQueue.h
@@ -1,7 +1,7 @@
//
// NotificationQueue.h
//
-// $Id: //poco/svn/Foundation/include/Poco/NotificationQueue.h#2 $
+// $Id: //poco/Main/Foundation/include/Poco/NotificationQueue.h#3 $
//
// Library: Foundation
// Package: Notifications
@@ -41,6 +41,7 @@
#include "Poco/Foundation.h"
+#include "Poco/Notification.h"
#include "Poco/Mutex.h"
#include "Poco/Event.h"
#include
@@ -49,7 +50,6 @@
namespace Poco {
-class Notification;
class NotificationCenter;
@@ -77,7 +77,7 @@ public:
~NotificationQueue();
/// Destroys the NotificationQueue.
- void enqueueNotification(Notification* pNotification);
+ void enqueueNotification(Notification::Ptr pNotification);
/// Enqueues the given notification by adding it to
/// the end of the queue (FIFO).
/// The queue takes ownership of the notification, thus
@@ -85,7 +85,7 @@ public:
/// notificationQueue.enqueueNotification(new MyNotification);
/// does not result in a memory leak.
- void enqueueUrgentNotification(Notification* pNotification);
+ void enqueueUrgentNotification(Notification::Ptr pNotification);
/// Enqueues the given notification by adding it to
/// the front of the queue (LIFO). The event therefore gets processed
/// before all other events already in the queue.
@@ -99,6 +99,10 @@ public:
/// Returns 0 (null) if no notification is available.
/// The caller gains ownership of the notification and
/// is expected to release it when done with it.
+ ///
+ /// It is highly recommended that the result is immediately
+ /// assigned to a Notification::Ptr, to avoid potential
+ /// memory management issues.
Notification* waitDequeueNotification();
/// Dequeues the next pending notification.
@@ -108,6 +112,10 @@ public:
/// is expected to release it when done with it.
/// This method returns 0 (null) if wakeUpWaitingThreads()
/// has been called by another thread.
+ ///
+ /// It is highly recommended that the result is immediately
+ /// assigned to a Notification::Ptr, to avoid potential
+ /// memory management issues.
Notification* waitDequeueNotification(long milliseconds);
/// Dequeues the next pending notification.
@@ -116,6 +124,10 @@ public:
/// Returns 0 (null) if no notification is available.
/// The caller gains ownership of the notification and
/// is expected to release it when done with it.
+ ///
+ /// It is highly recommended that the result is immediately
+ /// assigned to a Notification::Ptr, to avoid potential
+ /// memory management issues.
void dispatch(NotificationCenter& notificationCenter);
/// Dispatches all queued notifications to the given
@@ -142,14 +154,14 @@ public:
/// NotificationQueue.
protected:
- Notification* dequeueOne();
+ Notification::Ptr dequeueOne();
private:
- typedef std::deque NfQueue;
+ typedef std::deque NfQueue;
struct WaitInfo
{
- Notification* pNf;
- Event nfAvailable;
+ Notification::Ptr pNf;
+ Event nfAvailable;
};
typedef std::deque WaitQueue;
diff --git a/Foundation/include/Poco/PriorityNotificationQueue.h b/Foundation/include/Poco/PriorityNotificationQueue.h
new file mode 100644
index 000000000..5b9710e84
--- /dev/null
+++ b/Foundation/include/Poco/PriorityNotificationQueue.h
@@ -0,0 +1,180 @@
+//
+// PriorityNotificationQueue.h
+//
+// $Id: //poco/Main/Foundation/include/Poco/PriorityNotificationQueue.h#1 $
+//
+// Library: Foundation
+// Package: Notifications
+// Module: PriorityNotificationQueue
+//
+// Definition of the PriorityNotificationQueue class.
+//
+// Copyright (c) 2009, Applied Informatics Software Engineering GmbH.
+// and Contributors.
+//
+// Permission is hereby granted, free of charge, to any person or organization
+// obtaining a copy of the software and accompanying documentation covered by
+// this license (the "Software") to use, reproduce, display, distribute,
+// execute, and transmit the Software, and to prepare derivative works of the
+// Software, and to permit third-parties to whom the Software is furnished to
+// do so, all subject to the following:
+//
+// The copyright notices in the Software and this entire statement, including
+// the above license grant, this restriction and the following disclaimer,
+// must be included in all copies of the Software, in whole or in part, and
+// all derivative works of the Software, unless such copies or derivative
+// works are solely in the form of machine-executable object code generated by
+// a source language processor.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
+// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
+// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
+// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+
+
+#ifndef Foundation_PriorityNotificationQueue_INCLUDED
+#define Foundation_PriorityNotificationQueue_INCLUDED
+
+
+#include "Poco/Foundation.h"
+#include "Poco/Notification.h"
+#include "Poco/Mutex.h"
+#include "Poco/Event.h"
+#include
+
+
+
+
+ Name="Dynamic">
+ Name="Header Files">
+ RelativePath=".\src\DynamicTestSuite.h">
+ RelativePath=".\src\VarTest.h">
+ Name="Source Files">
+ RelativePath=".\src\DynamicTestSuite.cpp">
+ RelativePath=".\src\VarTest.cpp">
diff --git a/Foundation/testsuite/TestSuite_vs80.vcproj b/Foundation/testsuite/TestSuite_vs80.vcproj
index e38035df4..fe07d705f 100644
--- a/Foundation/testsuite/TestSuite_vs80.vcproj
+++ b/Foundation/testsuite/TestSuite_vs80.vcproj
@@ -762,6 +762,14 @@
RelativePath=".\src\NotificationsTestSuite.cpp"
>
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
50);
- assert (_handled.count("thread2") > 50);
- assert (_handled.count("thread3") > 10);
+ assert (_handled.size() == NOTIFICATION_COUNT);
+ assert (_handled.count("thread1") > 0);
+ assert (_handled.count("thread2") > 0);
+ assert (_handled.count("thread3") > 0);
}
@@ -217,6 +223,7 @@ void NotificationQueueTest::tearDown()
void NotificationQueueTest::work()
{
+ Poco::Random rnd;
Thread::sleep(50);
Notification* pNf = _queue.waitDequeueNotification();
while (pNf)
@@ -225,7 +232,7 @@ void NotificationQueueTest::work()
_mutex.lock();
_handled.insert(Thread::current()->name());
_mutex.unlock();
- Thread::yield();
+ Thread::sleep(rnd.next(5));
pNf = _queue.waitDequeueNotification();
}
}
diff --git a/Foundation/testsuite/src/NotificationsTestSuite.cpp b/Foundation/testsuite/src/NotificationsTestSuite.cpp
index 779f9d6db..e45739a3d 100644
--- a/Foundation/testsuite/src/NotificationsTestSuite.cpp
+++ b/Foundation/testsuite/src/NotificationsTestSuite.cpp
@@ -1,7 +1,7 @@
//
// NotificationsTestSuite.cpp
//
-// $Id: //poco/svn/Foundation/testsuite/src/NotificationsTestSuite.cpp#2 $
+// $Id: //poco/Main/Foundation/testsuite/src/NotificationsTestSuite.cpp#9 $
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
@@ -33,6 +33,8 @@
#include "NotificationsTestSuite.h"
#include "NotificationCenterTest.h"
#include "NotificationQueueTest.h"
+#include "PriorityNotificationQueueTest.h"
+#include "TimedNotificationQueueTest.h"
CppUnit::Test* NotificationsTestSuite::suite()
@@ -41,6 +43,8 @@ CppUnit::Test* NotificationsTestSuite::suite()
pSuite->addTest(NotificationCenterTest::suite());
pSuite->addTest(NotificationQueueTest::suite());
+ pSuite->addTest(PriorityNotificationQueueTest::suite());
+ pSuite->addTest(TimedNotificationQueueTest::suite());
return pSuite;
}
diff --git a/Foundation/testsuite/src/PriorityNotificationQueueTest.cpp b/Foundation/testsuite/src/PriorityNotificationQueueTest.cpp
new file mode 100644
index 000000000..9d68987e0
--- /dev/null
+++ b/Foundation/testsuite/src/PriorityNotificationQueueTest.cpp
@@ -0,0 +1,234 @@
+//
+// PriorityNotificationQueueTest.cpp
+//
+// $Id: //poco/Main/Foundation/testsuite/src/PriorityNotificationQueueTest.cpp#1 $
+//
+// Copyright (c) 2009, Applied Informatics Software Engineering GmbH.
+// and Contributors.
+//
+// Permission is hereby granted, free of charge, to any person or organization
+// obtaining a copy of the software and accompanying documentation covered by
+// this license (the "Software") to use, reproduce, display, distribute,
+// execute, and transmit the Software, and to prepare derivative works of the
+// Software, and to permit third-parties to whom the Software is furnished to
+// do so, all subject to the following:
+//
+// The copyright notices in the Software and this entire statement, including
+// the above license grant, this restriction and the following disclaimer,
+// must be included in all copies of the Software, in whole or in part, and
+// all derivative works of the Software, unless such copies or derivative
+// works are solely in the form of machine-executable object code generated by
+// a source language processor.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
+// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
+// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
+// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+
+
+#include "PriorityNotificationQueueTest.h"
+#include "CppUnit/TestCaller.h"
+#include "CppUnit/TestSuite.h"
+#include "Poco/PriorityNotificationQueue.h"
+#include "Poco/Notification.h"
+#include "Poco/Thread.h"
+#include "Poco/Runnable.h"
+#include "Poco/RunnableAdapter.h"
+#include "Poco/Random.h"
+
+
+using Poco::PriorityNotificationQueue;
+using Poco::Notification;
+using Poco::Thread;
+using Poco::RunnableAdapter;
+
+
+namespace
+{
+ class QTestNotification: public Notification
+ {
+ public:
+ QTestNotification(const std::string& data): _data(data)
+ {
+ }
+ ~QTestNotification()
+ {
+ }
+ const std::string& data() const
+ {
+ return _data;
+ }
+
+ private:
+ std::string _data;
+ };
+}
+
+
+PriorityNotificationQueueTest::PriorityNotificationQueueTest(const std::string& name): CppUnit::TestCase(name)
+{
+}
+
+
+PriorityNotificationQueueTest::~PriorityNotificationQueueTest()
+{
+}
+
+
+void PriorityNotificationQueueTest::testQueueDequeue()
+{
+ PriorityNotificationQueue queue;
+ assert (queue.empty());
+ assert (queue.size() == 0);
+ Notification* pNf = queue.dequeueNotification();
+ assertNullPtr(pNf);
+ queue.enqueueNotification(new Notification, 1);
+ assert (!queue.empty());
+ assert (queue.size() == 1);
+ pNf = queue.dequeueNotification();
+ assertNotNullPtr(pNf);
+ assert (queue.empty());
+ assert (queue.size() == 0);
+ pNf->release();
+
+ queue.enqueueNotification(new QTestNotification("first"), 1);
+ queue.enqueueNotification(new QTestNotification("fourth"), 4);
+ queue.enqueueNotification(new QTestNotification("third"), 3);
+ queue.enqueueNotification(new QTestNotification("second"), 2);
+ assert (!queue.empty());
+ assert (queue.size() == 4);
+ QTestNotification* pTNf = dynamic_cast(queue.dequeueNotification());
+ assertNotNullPtr(pTNf);
+ assert (pTNf->data() == "first");
+ pTNf->release();
+ assert (!queue.empty());
+ assert (queue.size() == 3);
+ pTNf = dynamic_cast(queue.dequeueNotification());
+ assertNotNullPtr(pTNf);
+ assert (pTNf->data() == "second");
+ pTNf->release();
+ assert (!queue.empty());
+ assert (queue.size() == 2);
+ pTNf = dynamic_cast(queue.dequeueNotification());
+ assertNotNullPtr(pTNf);
+ assert (pTNf->data() == "third");
+ pTNf->release();
+ assert (!queue.empty());
+ assert (queue.size() == 1);
+ pTNf = dynamic_cast(queue.dequeueNotification());
+ assertNotNullPtr(pTNf);
+ assert (pTNf->data() == "fourth");
+ pTNf->release();
+ assert (queue.empty());
+ assert (queue.size() == 0);
+
+ pNf = queue.dequeueNotification();
+ assertNullPtr(pNf);
+}
+
+
+void PriorityNotificationQueueTest::testWaitDequeue()
+{
+ PriorityNotificationQueue queue;
+ queue.enqueueNotification(new QTestNotification("third"), 3);
+ queue.enqueueNotification(new QTestNotification("fourth"), 4);
+ assert (!queue.empty());
+ assert (queue.size() == 2);
+ QTestNotification* pTNf = dynamic_cast(queue.waitDequeueNotification(10));
+ assertNotNullPtr(pTNf);
+ assert (pTNf->data() == "third");
+ pTNf->release();
+ assert (!queue.empty());
+ assert (queue.size() == 1);
+ pTNf = dynamic_cast(queue.waitDequeueNotification(10));
+ assertNotNullPtr(pTNf);
+ assert (pTNf->data() == "fourth");
+ pTNf->release();
+ assert (queue.empty());
+ assert (queue.size() == 0);
+
+ Notification* pNf = queue.waitDequeueNotification(10);
+ assertNullPtr(pNf);
+}
+
+
+void PriorityNotificationQueueTest::testThreads()
+{
+ const int NOTIFICATION_COUNT = 5000;
+
+ Thread t1("thread1");
+ Thread t2("thread2");
+ Thread t3("thread3");
+
+ RunnableAdapter ra(*this, &PriorityNotificationQueueTest::work);
+ t1.start(ra);
+ t2.start(ra);
+ t3.start(ra);
+ for (int i = 0; i < NOTIFICATION_COUNT; ++i)
+ {
+ _queue.enqueueNotification(new Notification, 1);
+ }
+ while (!_queue.empty()) Thread::sleep(50);
+ Thread::sleep(20);
+ _queue.wakeUpAll();
+ t1.join();
+ t2.join();
+ t3.join();
+ assert (_handled.size() == NOTIFICATION_COUNT);
+ assert (_handled.count("thread1") > 0);
+ assert (_handled.count("thread2") > 0);
+ assert (_handled.count("thread3") > 0);
+}
+
+
+void PriorityNotificationQueueTest::testDefaultQueue()
+{
+ PriorityNotificationQueue& queue = PriorityNotificationQueue::defaultQueue();
+ assert (queue.empty());
+ assert (queue.size() == 0);
+}
+
+
+void PriorityNotificationQueueTest::setUp()
+{
+ _handled.clear();
+}
+
+
+void PriorityNotificationQueueTest::tearDown()
+{
+}
+
+
+void PriorityNotificationQueueTest::work()
+{
+ Poco::Random rnd;
+ Thread::sleep(50);
+ Notification* pNf = _queue.waitDequeueNotification();
+ while (pNf)
+ {
+ pNf->release();
+ _mutex.lock();
+ _handled.insert(Thread::current()->name());
+ _mutex.unlock();
+ Thread::sleep(rnd.next(5));
+ pNf = _queue.waitDequeueNotification();
+ }
+}
+
+
+CppUnit::Test* PriorityNotificationQueueTest::suite()
+{
+ CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("PriorityNotificationQueueTest");
+
+ CppUnit_addTest(pSuite, PriorityNotificationQueueTest, testQueueDequeue);
+ CppUnit_addTest(pSuite, PriorityNotificationQueueTest, testWaitDequeue);
+ CppUnit_addTest(pSuite, PriorityNotificationQueueTest, testThreads);
+ CppUnit_addTest(pSuite, PriorityNotificationQueueTest, testDefaultQueue);
+
+ return pSuite;
+}
diff --git a/Foundation/testsuite/src/PriorityNotificationQueueTest.h b/Foundation/testsuite/src/PriorityNotificationQueueTest.h
new file mode 100644
index 000000000..c2a13d75a
--- /dev/null
+++ b/Foundation/testsuite/src/PriorityNotificationQueueTest.h
@@ -0,0 +1,72 @@
+//
+// PriorityNotificationQueueTest.h
+//
+// $Id: //poco/Main/Foundation/testsuite/src/PriorityNotificationQueueTest.h#1 $
+//
+// Definition of the PriorityNotificationQueueTest class.
+//
+// Copyright (c) 2009, Applied Informatics Software Engineering GmbH.
+// and Contributors.
+//
+// Permission is hereby granted, free of charge, to any person or organization
+// obtaining a copy of the software and accompanying documentation covered by
+// this license (the "Software") to use, reproduce, display, distribute,
+// execute, and transmit the Software, and to prepare derivative works of the
+// Software, and to permit third-parties to whom the Software is furnished to
+// do so, all subject to the following:
+//
+// The copyright notices in the Software and this entire statement, including
+// the above license grant, this restriction and the following disclaimer,
+// must be included in all copies of the Software, in whole or in part, and
+// all derivative works of the Software, unless such copies or derivative
+// works are solely in the form of machine-executable object code generated by
+// a source language processor.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
+// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
+// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
+// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+
+
+#ifndef PriorityNotificationQueueTest_INCLUDED
+#define PriorityNotificationQueueTest_INCLUDED
+
+
+#include "Poco/Foundation.h"
+#include "CppUnit/TestCase.h"
+#include "Poco/PriorityNotificationQueue.h"
+#include "Poco/Mutex.h"
+#include
+
+
+class PriorityNotificationQueueTest: public CppUnit::TestCase
+{
+public:
+ PriorityNotificationQueueTest(const std::string& name);
+ ~PriorityNotificationQueueTest();
+
+ void testQueueDequeue();
+ void testWaitDequeue();
+ void testThreads();
+ void testDefaultQueue();
+
+ void setUp();
+ void tearDown();
+
+ static CppUnit::Test* suite();
+
+protected:
+ void work();
+
+private:
+ Poco::PriorityNotificationQueue _queue;
+ std::multiset _handled;
+ Poco::FastMutex _mutex;
+};
+
+
+#endif // PriorityNotificationQueueTest_INCLUDED
diff --git a/Foundation/testsuite/src/TimedNotificationQueueTest.cpp b/Foundation/testsuite/src/TimedNotificationQueueTest.cpp
new file mode 100644
index 000000000..73ca0721d
--- /dev/null
+++ b/Foundation/testsuite/src/TimedNotificationQueueTest.cpp
@@ -0,0 +1,291 @@
+//
+// TimedNotificationQueueTest.cpp
+//
+// $Id: //poco/Main/Foundation/testsuite/src/TimedNotificationQueueTest.cpp#1 $
+//
+// Copyright (c) 2009, Applied Informatics Software Engineering GmbH.
+// and Contributors.
+//
+// Permission is hereby granted, free of charge, to any person or organization
+// obtaining a copy of the software and accompanying documentation covered by
+// this license (the "Software") to use, reproduce, display, distribute,
+// execute, and transmit the Software, and to prepare derivative works of the
+// Software, and to permit third-parties to whom the Software is furnished to
+// do so, all subject to the following:
+//
+// The copyright notices in the Software and this entire statement, including
+// the above license grant, this restriction and the following disclaimer,
+// must be included in all copies of the Software, in whole or in part, and
+// all derivative works of the Software, unless such copies or derivative
+// works are solely in the form of machine-executable object code generated by
+// a source language processor.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
+// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
+// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
+// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+
+
+#include "TimedNotificationQueueTest.h"
+#include "CppUnit/TestCaller.h"
+#include "CppUnit/TestSuite.h"
+#include "Poco/TimedNotificationQueue.h"
+#include "Poco/Notification.h"
+#include "Poco/Timestamp.h"
+
+
+using Poco::TimedNotificationQueue;
+using Poco::Notification;
+using Poco::Timestamp;
+
+
+namespace
+{
+ class QTestNotification: public Notification
+ {
+ public:
+ QTestNotification(const std::string& data): _data(data)
+ {
+ }
+ ~QTestNotification()
+ {
+ }
+ const std::string& data() const
+ {
+ return _data;
+ }
+
+ private:
+ std::string _data;
+ };
+}
+
+
+TimedNotificationQueueTest::TimedNotificationQueueTest(const std::string& name): CppUnit::TestCase(name)
+{
+}
+
+
+TimedNotificationQueueTest::~TimedNotificationQueueTest()
+{
+}
+
+
+void TimedNotificationQueueTest::testDequeue()
+{
+ TimedNotificationQueue queue;
+ assert (queue.empty());
+ assert (queue.size() == 0);
+ Notification* pNf = queue.dequeueNotification();
+ assertNullPtr(pNf);
+ queue.enqueueNotification(new Notification, Timestamp());
+ assert (!queue.empty());
+ assert (queue.size() == 1);
+ pNf = queue.dequeueNotification();
+ assertNotNullPtr(pNf);
+ assert (queue.empty());
+ assert (queue.size() == 0);
+ pNf->release();
+
+ Poco::Timestamp ts1;
+ ts1 += 100000;
+ Poco::Timestamp ts2;
+ ts2 += 200000;
+ Poco::Timestamp ts3;
+ ts3 += 300000;
+ Poco::Timestamp ts4;
+ ts4 += 400000;
+
+ queue.enqueueNotification(new QTestNotification("first"), ts1);
+ queue.enqueueNotification(new QTestNotification("fourth"), ts4);
+ queue.enqueueNotification(new QTestNotification("third"), ts3);
+ queue.enqueueNotification(new QTestNotification("second"), ts2);
+ assert (!queue.empty());
+ assert (queue.size() == 4);
+ QTestNotification* pTNf = 0;
+ while (!pTNf)
+ {
+ pTNf = dynamic_cast(queue.dequeueNotification());
+ }
+ assertNotNullPtr(pTNf);
+ assert (pTNf->data() == "first");
+ pTNf->release();
+ assert (ts1.elapsed() >= 0);
+ assert (!queue.empty());
+ assert (queue.size() == 3);
+
+ pTNf = 0;
+ while (!pTNf)
+ {
+ pTNf = dynamic_cast(queue.dequeueNotification());
+ }
+ assertNotNullPtr(pTNf);
+ assert (pTNf->data() == "second");
+ pTNf->release();
+ assert (ts2.elapsed() >= 0);
+ assert (!queue.empty());
+ assert (queue.size() == 2);
+
+ pTNf = 0;
+ while (!pTNf)
+ {
+ pTNf = dynamic_cast(queue.dequeueNotification());
+ }
+ assertNotNullPtr(pTNf);
+ assert (pTNf->data() == "third");
+ pTNf->release();
+ assert (ts3.elapsed() >= 0);
+ assert (!queue.empty());
+ assert (queue.size() == 1);
+
+ pTNf = 0;
+ while (!pTNf)
+ {
+ pTNf = dynamic_cast(queue.dequeueNotification());
+ }
+ assertNotNullPtr(pTNf);
+ assert (pTNf->data() == "fourth");
+ pTNf->release();
+ assert (ts4.elapsed() >= 0);
+ assert (queue.empty());
+ assert (queue.size() == 0);
+
+ pNf = queue.dequeueNotification();
+ assertNullPtr(pNf);
+}
+
+
+void TimedNotificationQueueTest::testWaitDequeue()
+{
+ TimedNotificationQueue queue;
+
+ Poco::Timestamp ts1;
+ ts1 += 100000;
+ Poco::Timestamp ts2;
+ ts2 += 200000;
+ Poco::Timestamp ts3;
+ ts3 += 300000;
+ Poco::Timestamp ts4;
+ ts4 += 400000;
+
+ queue.enqueueNotification(new QTestNotification("first"), ts1);
+ queue.enqueueNotification(new QTestNotification("fourth"), ts4);
+ queue.enqueueNotification(new QTestNotification("third"), ts3);
+ queue.enqueueNotification(new QTestNotification("second"), ts2);
+ assert (!queue.empty());
+ assert (queue.size() == 4);
+ QTestNotification* pTNf = dynamic_cast(queue.waitDequeueNotification());
+ assertNotNullPtr(pTNf);
+ assert (pTNf->data() == "first");
+ pTNf->release();
+ assert (ts1.elapsed() >= 0);
+ assert (!queue.empty());
+ assert (queue.size() == 3);
+
+ pTNf = dynamic_cast(queue.waitDequeueNotification());
+ assertNotNullPtr(pTNf);
+ assert (pTNf->data() == "second");
+ pTNf->release();
+ assert (ts2.elapsed() >= 0);
+ assert (!queue.empty());
+ assert (queue.size() == 2);
+
+ pTNf = dynamic_cast(queue.waitDequeueNotification());
+ assertNotNullPtr(pTNf);
+ assert (pTNf->data() == "third");
+ pTNf->release();
+ assert (ts3.elapsed() >= 0);
+ assert (!queue.empty());
+ assert (queue.size() == 1);
+
+ pTNf = dynamic_cast(queue.waitDequeueNotification());
+ assertNotNullPtr(pTNf);
+ assert (pTNf->data() == "fourth");
+ pTNf->release();
+ assert (ts4.elapsed() >= 0);
+ assert (queue.empty());
+ assert (queue.size() == 0);
+}
+
+
+void TimedNotificationQueueTest::testWaitDequeueTimeout()
+{
+ TimedNotificationQueue queue;
+
+ Poco::Timestamp ts1;
+ ts1 += 200000;
+ Poco::Timestamp ts2;
+ ts2 += 400000;
+ Poco::Timestamp ts3;
+ ts3 += 600000;
+ Poco::Timestamp ts4;
+ ts4 += 800000;
+
+ queue.enqueueNotification(new QTestNotification("first"), ts1);
+ queue.enqueueNotification(new QTestNotification("fourth"), ts4);
+ queue.enqueueNotification(new QTestNotification("third"), ts3);
+ queue.enqueueNotification(new QTestNotification("second"), ts2);
+ assert (!queue.empty());
+ assert (queue.size() == 4);
+ QTestNotification* pTNf = dynamic_cast(queue.waitDequeueNotification(10));
+ assertNullPtr(pTNf);
+ pTNf = dynamic_cast(queue.waitDequeueNotification(20));
+ assertNullPtr(pTNf);
+ pTNf = dynamic_cast(queue.waitDequeueNotification(200));
+ assertNotNullPtr(pTNf);
+ assert (pTNf->data() == "first");
+ pTNf->release();
+ assert (ts1.elapsed() >= 0);
+ assert (!queue.empty());
+ assert (queue.size() == 3);
+
+ pTNf = dynamic_cast(queue.waitDequeueNotification(220));
+ assertNotNullPtr(pTNf);
+ assert (pTNf->data() == "second");
+ pTNf->release();
+ assert (ts2.elapsed() >= 0);
+ assert (!queue.empty());
+ assert (queue.size() == 2);
+
+ pTNf = dynamic_cast(queue.waitDequeueNotification(220));
+ assertNotNullPtr(pTNf);
+ assert (pTNf->data() == "third");
+ pTNf->release();
+ assert (ts3.elapsed() >= 0);
+ assert (!queue.empty());
+ assert (queue.size() == 1);
+
+ pTNf = dynamic_cast(queue.waitDequeueNotification(220));
+ assertNotNullPtr(pTNf);
+ assert (pTNf->data() == "fourth");
+ pTNf->release();
+ assert (ts1.elapsed() >= 0);
+ assert (queue.empty());
+ assert (queue.size() == 0);
+}
+
+
+void TimedNotificationQueueTest::setUp()
+{
+}
+
+
+void TimedNotificationQueueTest::tearDown()
+{
+}
+
+
+CppUnit::Test* TimedNotificationQueueTest::suite()
+{
+ CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("TimedNotificationQueueTest");
+
+ CppUnit_addTest(pSuite, TimedNotificationQueueTest, testDequeue);
+ CppUnit_addTest(pSuite, TimedNotificationQueueTest, testWaitDequeue);
+ CppUnit_addTest(pSuite, TimedNotificationQueueTest, testWaitDequeueTimeout);
+
+ return pSuite;
+}
diff --git a/Foundation/testsuite/src/TimedNotificationQueueTest.h b/Foundation/testsuite/src/TimedNotificationQueueTest.h
new file mode 100644
index 000000000..4f48d0de9
--- /dev/null
+++ b/Foundation/testsuite/src/TimedNotificationQueueTest.h
@@ -0,0 +1,68 @@
+//
+// TimedNotificationQueueTest.h
+//
+// $Id: //poco/Main/Foundation/testsuite/src/TimedNotificationQueueTest.h#1 $
+//
+// Definition of the TimedNotificationQueueTest class.
+//
+// Copyright (c) 2009, Applied Informatics Software Engineering GmbH.
+// and Contributors.
+//
+// Permission is hereby granted, free of charge, to any person or organization
+// obtaining a copy of the software and accompanying documentation covered by
+// this license (the "Software") to use, reproduce, display, distribute,
+// execute, and transmit the Software, and to prepare derivative works of the
+// Software, and to permit third-parties to whom the Software is furnished to
+// do so, all subject to the following:
+//
+// The copyright notices in the Software and this entire statement, including
+// the above license grant, this restriction and the following disclaimer,
+// must be included in all copies of the Software, in whole or in part, and
+// all derivative works of the Software, unless such copies or derivative
+// works are solely in the form of machine-executable object code generated by
+// a source language processor.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
+// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
+// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
+// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+
+
+#ifndef TimedNotificationQueueTest_INCLUDED
+#define TimedNotificationQueueTest_INCLUDED
+
+
+#include "Poco/Foundation.h"
+#include "CppUnit/TestCase.h"
+#include "Poco/TimedNotificationQueue.h"
+#include "Poco/Mutex.h"
+#include
+
+
+class TimedNotificationQueueTest: public CppUnit::TestCase
+{
+public:
+ TimedNotificationQueueTest(const std::string& name);
+ ~TimedNotificationQueueTest();
+
+ void testDequeue();
+ void testWaitDequeue();
+ void testWaitDequeueTimeout();
+
+ void setUp();
+ void tearDown();
+
+ static CppUnit::Test* suite();
+
+protected:
+ void work();
+
+private:
+};
+
+
+#endif // TimedNotificationQueueTest_INCLUDED