1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
///////////////////////////////////////////////////////////////////////////////
// Name: tests/thread/queue.cpp
// Purpose: Unit test for wxMessageQueue
// Author: Evgeniy Tarassov
// Created: 31/10/2007
// Copyright: (c) 2007 Evgeniy Tarassov
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "testprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/dynarray.h"
#include "wx/thread.h"
#endif // WX_PRECOMP
#include "wx/msgqueue.h"
// ----------------------------------------------------------------------------
// test class
// ----------------------------------------------------------------------------
class QueueTestCase : public CppUnit::TestCase
{
public:
QueueTestCase() { }
enum WaitTestType
{
WaitWithTimeout = 0,
WaitInfinitlyLong
};
private:
typedef wxMessageQueue<int> Queue;
// This class represents a thread that waits (following WaitTestType type)
// for exactly maxMsgCount messages from its message queue and if another
// MyThread is specified, then every message received is posted
// to that next thread.
class MyThread : public wxThread
{
public:
MyThread(WaitTestType type, MyThread *next, int maxMsgCount)
: wxThread(wxTHREAD_JOINABLE),
m_type(type), m_nextThread(next), m_maxMsgCount(maxMsgCount)
{}
// thread execution starts here
virtual void *Entry();
// Thread message queue
Queue& GetQueue()
{
return m_queue;
}
private:
WaitTestType m_type;
MyThread* m_nextThread;
int m_maxMsgCount;
Queue m_queue;
};
WX_DEFINE_ARRAY_PTR(MyThread *, ArrayThread);
CPPUNIT_TEST_SUITE( QueueTestCase );
CPPUNIT_TEST( TestReceive );
CPPUNIT_TEST( TestReceiveTimeout );
CPPUNIT_TEST_SUITE_END();
void TestReceive();
void TestReceiveTimeout();
DECLARE_NO_COPY_CLASS(QueueTestCase)
};
// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( QueueTestCase );
// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( QueueTestCase, "QueueTestCase" );
// this function creates the given number of threads and posts msgCount
// messages to the last created thread which, in turn, posts all the messages
// it receives to the previously created thread which does the same and so on
// in cascade -- at the end, each thread will have received all msgCount
// messages directly or indirectly
void QueueTestCase::TestReceive()
{
const int msgCount = 100;
const int threadCount = 10;
ArrayThread threads;
int i;
for ( i = 0; i < threadCount; ++i )
{
MyThread *previousThread = i == 0 ? NULL : threads[i-1];
MyThread *thread =
new MyThread(WaitInfinitlyLong, previousThread, msgCount);
CPPUNIT_ASSERT_EQUAL ( thread->Create(), wxTHREAD_NO_ERROR );
threads.Add(thread);
}
for ( i = 0; i < threadCount; ++i )
{
threads[i]->Run();
}
MyThread* lastThread = threads[threadCount - 1];
for ( i = 0; i < msgCount; ++i )
{
lastThread->GetQueue().Post(i);
}
for ( i = 0; i < threadCount; ++i )
{
// each thread should return the number of messages received.
// if it returns a negative, then it detected some problem.
wxThread::ExitCode code = threads[i]->Wait();
CPPUNIT_ASSERT_EQUAL( code, (wxThread::ExitCode)wxMSGQUEUE_NO_ERROR );
}
}
// this function creates two threads, each one waiting (with a timeout) for
// exactly two messages.
// Exactly to messages are posted into first thread queue, but
// only one message is posted to the second thread queue.
// Therefore first thread should return with wxMSGQUEUE_NO_ERROR, but the second
// should return wxMSGQUEUUE_TIMEOUT.
void QueueTestCase::TestReceiveTimeout()
{
MyThread* thread1 = new MyThread(WaitWithTimeout, NULL, 2);
MyThread* thread2 = new MyThread(WaitWithTimeout, NULL, 2);
CPPUNIT_ASSERT_EQUAL ( thread1->Create(), wxTHREAD_NO_ERROR );
CPPUNIT_ASSERT_EQUAL ( thread2->Create(), wxTHREAD_NO_ERROR );
thread1->Run();
thread2->Run();
// Post two messages to the first thread
CPPUNIT_ASSERT_EQUAL( thread1->GetQueue().Post(0), wxMSGQUEUE_NO_ERROR );
CPPUNIT_ASSERT_EQUAL( thread1->GetQueue().Post(1), wxMSGQUEUE_NO_ERROR );
// ...but only one message to the second
CPPUNIT_ASSERT_EQUAL( thread2->GetQueue().Post(0), wxMSGQUEUE_NO_ERROR );
wxThread::ExitCode code1 = thread1->Wait();
wxThread::ExitCode code2 = thread2->Wait();
CPPUNIT_ASSERT_EQUAL( code1, (wxThread::ExitCode)wxMSGQUEUE_NO_ERROR );
CPPUNIT_ASSERT_EQUAL( code2, (wxThread::ExitCode)wxMSGQUEUE_TIMEOUT );
}
// every thread tries to read exactly m_maxMsgCount messages from its queue
// following the waiting strategy specified in m_type. If it succeeds then it
// returns 0. Otherwise it returns the error code - one of wxMessageQueueError.
void *QueueTestCase::MyThread::Entry()
{
int messagesReceived = 0;
while ( messagesReceived < m_maxMsgCount )
{
wxMessageQueueError result;
int msg = -1; // just to suppress "possibly uninitialized" warnings
if ( m_type == WaitWithTimeout )
result = m_queue.ReceiveTimeout(1000, msg);
else
result = m_queue.Receive(msg);
if ( result == wxMSGQUEUE_MISC_ERROR )
return (wxThread::ExitCode)wxMSGQUEUE_MISC_ERROR;
if ( result == wxMSGQUEUE_NO_ERROR )
{
if ( m_nextThread != NULL )
{
wxMessageQueueError res = m_nextThread->GetQueue().Post(msg);
if ( res == wxMSGQUEUE_MISC_ERROR )
return (wxThread::ExitCode)wxMSGQUEUE_MISC_ERROR;
CPPUNIT_ASSERT_EQUAL( wxMSGQUEUE_NO_ERROR, res );
}
++messagesReceived;
continue;
}
CPPUNIT_ASSERT_EQUAL ( result, wxMSGQUEUE_TIMEOUT );
break;
}
if ( messagesReceived != m_maxMsgCount )
{
CPPUNIT_ASSERT_EQUAL( m_type, WaitWithTimeout );
return (wxThread::ExitCode)wxMSGQUEUE_TIMEOUT;
}
return (wxThread::ExitCode)wxMSGQUEUE_NO_ERROR;
}
|