Add InterruptibleThread unit test

This commit is contained in:
Vinnie Falco
2013-09-19 14:35:01 -07:00
parent cc1fd7bc6a
commit 97e961a048
3 changed files with 81 additions and 0 deletions

View File

@@ -63,6 +63,9 @@ void InterruptibleThread::start (EntryPoint* const entryPoint)
void InterruptibleThread::join ()
{
m_thread.signalThreadShouldExit();
m_thread.notify();
interrupt();
m_thread.stopThread (-1);
}
@@ -200,3 +203,73 @@ bool CurrentInterruptibleThread::interruptionPoint ()
return interrupted;
}
//------------------------------------------------------------------------------
class InterruptibleThreadTests : public UnitTest
{
public:
enum
{
callsPerThread = 100000
};
struct TestThread : InterruptibleThread::EntryPoint
{
explicit TestThread (int id)
: m_thread ("#" + String::fromNumber (id))
{
m_thread.start (this);
}
void threadRun ()
{
while (! m_thread.peekThread().threadShouldExit())
{
String s;
while (!m_thread.interruptionPoint ())
{
s = s + String::fromNumber (m_random.nextInt ());
if (s.length () > 100)
s = String::empty;
}
}
}
Random m_random;
InterruptibleThread m_thread;
};
void testThreads (std::size_t nThreads)
{
beginTestCase (String::fromNumber (nThreads) + " threads");
OwnedArray <TestThread> threads;
threads.ensureStorageAllocated (nThreads);
for (std::size_t i = 0; i < nThreads; ++i)
threads.add (new TestThread (i + 1));
for (std::size_t i = 0; i < callsPerThread * nThreads; ++i)
{
int const n (random().nextInt (threads.size()));
threads[n]->m_thread.interrupt();
}
pass ();
}
void runTest ()
{
testThreads (8);
testThreads (64);
}
InterruptibleThreadTests () : UnitTest ("InterruptibleThread", "beast")
{
}
};
static InterruptibleThreadTests interruptibleThreadTests;

View File

@@ -125,6 +125,12 @@ public:
*/
static InterruptibleThread* getCurrentThread ();
// private
Thread& peekThread ()
{
return m_thread;
}
private:
class ThreadHelper : public Thread
{

View File

@@ -259,10 +259,12 @@ public:
}
#endif
#if 1
for (int i = 0; i < threads.size(); ++i)
threads[i]->stop (false);
for (int i = 0; i < threads.size(); ++i)
threads[i]->stop (true);
#endif
pass ();
}