@@ -192,6 +192,63 @@ class TrySleepRunnable : public Runnable
192192};
193193
194194
195+ class InterruptionRunnable : public Runnable
196+ {
197+ public:
198+ virtual void run () override
199+ {
200+ _sleep = !Thread::trySleep (300000 );
201+ _interrupted = Thread::current ()->isInterrupted ();
202+
203+ try
204+ {
205+ Thread::current ()->checkInterrupted ();
206+ }
207+ catch (const Poco::ThreadInterruptedException&)
208+ {
209+ _exception = true ;
210+ }
211+
212+ // interrupt state should be cleared
213+ if (!Thread::current ()->isInterrupted ())
214+ {
215+ _interruptCleared = true ;
216+ }
217+
218+ // interrupt state should be cleared
219+ try
220+ {
221+ Thread::current ()->checkInterrupted ();
222+ _exceptionCleared = true ;
223+ }
224+ catch (const Poco::ThreadInterruptedException&)
225+ {
226+ _exceptionCleared = false ;
227+ }
228+ }
229+
230+ bool isTestOK () const
231+ {
232+ if (_sleep &&
233+ _interrupted &&
234+ _exception &&
235+ _interruptCleared &&
236+ _exceptionCleared)
237+ {
238+ return true ;
239+ }
240+ return false ;
241+ }
242+
243+ private:
244+ bool _sleep = false ;
245+ bool _interrupted = false ;
246+ bool _exception = false ;
247+ bool _interruptCleared = false ;
248+ bool _exceptionCleared = false ;
249+ };
250+
251+
195252ThreadTest::ThreadTest (const std::string& name): CppUnit::TestCase(name)
196253{
197254}
@@ -553,6 +610,41 @@ void ThreadTest::testAffinity()
553610}
554611
555612
613+ void ThreadTest::testInterrupt ()
614+ {
615+ Thread thread;
616+
617+ for (int i = 0 ; i < 2 ; i++)
618+ {
619+ InterruptionRunnable r;
620+
621+ thread.start (r);
622+ Thread::sleep (200 );
623+ assertTrue (thread.isRunning ());
624+ assertTrue (!thread.tryJoin (100 ));
625+
626+ // interrupt
627+ thread.interrupt ();
628+ thread.join ();
629+
630+ // clear the interrupt state to re-use the thread
631+ thread.clearInterrupt ();
632+ assertTrue (!thread.isInterrupted ());
633+
634+ try
635+ {
636+ thread.checkInterrupted ();
637+ }
638+ catch (const std::exception&)
639+ {
640+ assertTrue (false );
641+ }
642+
643+ assertTrue (r.isTestOK ());
644+ }
645+ }
646+
647+
556648void ThreadTest::setUp ()
557649{
558650}
@@ -583,6 +675,7 @@ CppUnit::Test* ThreadTest::suite()
583675 CppUnit_addTest (pSuite, ThreadTest, testThreadStackSize);
584676 CppUnit_addTest (pSuite, ThreadTest, testSleep);
585677 CppUnit_addTest (pSuite, ThreadTest, testAffinity);
678+ CppUnit_addTest (pSuite, ThreadTest, testInterrupt);
586679
587680 return pSuite;
588681}
0 commit comments