mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-20 11:05:54 +00:00
Add InterruptibleThread unit test
This commit is contained in:
@@ -63,6 +63,9 @@ void InterruptibleThread::start (EntryPoint* const entryPoint)
|
|||||||
|
|
||||||
void InterruptibleThread::join ()
|
void InterruptibleThread::join ()
|
||||||
{
|
{
|
||||||
|
m_thread.signalThreadShouldExit();
|
||||||
|
m_thread.notify();
|
||||||
|
interrupt();
|
||||||
m_thread.stopThread (-1);
|
m_thread.stopThread (-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,3 +203,73 @@ bool CurrentInterruptibleThread::interruptionPoint ()
|
|||||||
|
|
||||||
return interrupted;
|
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;
|
||||||
|
|||||||
@@ -125,6 +125,12 @@ public:
|
|||||||
*/
|
*/
|
||||||
static InterruptibleThread* getCurrentThread ();
|
static InterruptibleThread* getCurrentThread ();
|
||||||
|
|
||||||
|
// private
|
||||||
|
Thread& peekThread ()
|
||||||
|
{
|
||||||
|
return m_thread;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
class ThreadHelper : public Thread
|
class ThreadHelper : public Thread
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -259,10 +259,12 @@ public:
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if 1
|
||||||
for (int i = 0; i < threads.size(); ++i)
|
for (int i = 0; i < threads.size(); ++i)
|
||||||
threads[i]->stop (false);
|
threads[i]->stop (false);
|
||||||
for (int i = 0; i < threads.size(); ++i)
|
for (int i = 0; i < threads.size(); ++i)
|
||||||
threads[i]->stop (true);
|
threads[i]->stop (true);
|
||||||
|
#endif
|
||||||
|
|
||||||
pass ();
|
pass ();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user