Exemplo n.º 1
0
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);
}
Exemplo n.º 2
0
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;
}
Exemplo n.º 3
0
void NotificationQueueTest::testQueueRemove()
{
	NotificationQueue queue;
	Notification::Ptr frontNotification = new QTestNotification("front");
	Notification::Ptr middleNotification = new QTestNotification("middle");
	Notification::Ptr backNotification = new QTestNotification("back");
	queue.enqueueNotification(frontNotification);
	queue.enqueueNotification(new QTestNotification("dummy"));
	queue.enqueueNotification(middleNotification);
	queue.enqueueNotification(new QTestNotification("dummy"));
	queue.enqueueNotification(backNotification);
	assert (queue.size() == 5);
	assert (queue.remove(frontNotification));
	assert (queue.size() == 4);
	assert (queue.remove(middleNotification));
	assert (queue.size() == 3);
	assert (queue.remove(backNotification));
	assert (queue.size() == 2);
	assert (!queue.remove(backNotification));
	assert (queue.size() == 2);
}
Exemplo n.º 4
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);
}