TEST(ContextGroupTests, DispatcherInterruptSetsFlag) { Dispatcher dispatcher; Context<> context(dispatcher, [&] { try { Timer(dispatcher).sleep(std::chrono::milliseconds(10)); } catch (InterruptedException&) { } }); dispatcher.interrupt(); dispatcher.yield(); ASSERT_TRUE(dispatcher.interrupted()); ASSERT_FALSE(dispatcher.interrupted()); }
TEST(ContextTests, interruptIsInterrupting) { Dispatcher dispatcher; Context<> context(dispatcher, [&] { if (dispatcher.interrupted()) { throw InterruptedException(); } }); context.interrupt(); ASSERT_THROW(context.get(), InterruptedException); }
TEST(ContextTests, destructorInterrupts) { Dispatcher dispatcher; bool interrupted = false; { Context<> context(dispatcher, [&] { if (dispatcher.interrupted()) { interrupted = true; } }); } ASSERT_TRUE(interrupted); }
TEST(ContextGroupTests, ContextGroupInterruptIsInterrupting) { Dispatcher dispatcher; bool interrupted = false; ContextGroup cg1(dispatcher); cg1.spawn([&] { interrupted = dispatcher.interrupted(); }); cg1.interrupt(); cg1.wait(); ASSERT_TRUE(interrupted); }
TEST(ContextGroupTests, ContextGroupDestructorIsInterrupt_Waitable) { Dispatcher dispatcher; bool interrupted = false; bool contextFinished = false; { ContextGroup cg1(dispatcher); cg1.spawn([&] { interrupted = dispatcher.interrupted(); Timer(dispatcher).sleep(std::chrono::milliseconds(100)); contextFinished = true; }); } ASSERT_TRUE(interrupted); ASSERT_TRUE(contextFinished); }
TEST(ContextTests, getChecksInterruption) { Dispatcher dispatcher; Event event(dispatcher); Context<int> context1(dispatcher, [&] { event.wait(); if (dispatcher.interrupted()) { return 11; } return 10; }); Context<int> context2(dispatcher, [&] { event.set(); return context1.get(); }); context2.interrupt(); ASSERT_EQ(11, context2.get()); }