void NotificationQueueTest::testQueueDequeueUrgent() { NotificationQueue queue; queue.enqueueNotification(new QTestNotification("first")); queue.enqueueNotification(new QTestNotification("second")); queue.enqueueUrgentNotification(new QTestNotification("third")); assert (!queue.empty()); assert (queue.size() == 3); QTestNotification* pTNf = dynamic_cast<QTestNotification*>(queue.dequeueNotification()); assertNotNullPtr(pTNf); assert (pTNf->data() == "third"); pTNf->release(); assert (!queue.empty()); assert (queue.size() == 2); pTNf = dynamic_cast<QTestNotification*>(queue.dequeueNotification()); assert (pTNf->data() == "first"); pTNf->release(); assert (!queue.empty()); assert (queue.size() == 1); pTNf = dynamic_cast<QTestNotification*>(queue.dequeueNotification()); assertNotNullPtr(pTNf); assert (pTNf->data() == "second"); pTNf->release(); assert (queue.empty()); assert (queue.size() == 0); Notification* pNf = queue.dequeueNotification(); assertNullPtr(pNf); }
int main(int argc, char** argv) { NotificationQueue queue; // create some worker threads Worker worker1("Worker 1", queue); Worker worker2("Worker 2", queue); Worker worker3("Worker 3", queue); // start worker threads ThreadPool::defaultPool().start(worker1); ThreadPool::defaultPool().start(worker2); ThreadPool::defaultPool().start(worker3); // distribute some work for (int i = 0; i < 50; ++i) { queue.enqueueNotification(new WorkNotification(i)); } // wait until queue is empty and all threads are // waiting for new work. while (!queue.empty()) Thread::sleep(200); Thread::sleep(500); // stop all worker threads queue.wakeUpAll(); ThreadPool::defaultPool().joinAll(); return 0; }
void NotificationQueueTest::testWaitDequeue() { NotificationQueue queue; queue.enqueueNotification(new QTestNotification("third")); queue.enqueueNotification(new QTestNotification("fourth")); assert (!queue.empty()); assert (queue.size() == 2); QTestNotification* pTNf = dynamic_cast<QTestNotification*>(queue.waitDequeueNotification(10)); assertNotNullPtr(pTNf); assert (pTNf->data() == "third"); pTNf->release(); assert (!queue.empty()); assert (queue.size() == 1); pTNf = dynamic_cast<QTestNotification*>(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); }