示例#1
0
TEST_F(AutoRestarterTest, RestartsOnException) {
    AutoCurrentContext()->Initiate();

    // Use a bolt facility to attach one of our text fixtures:
    AutoCurrentContext()->BoltTo<ThrowsAnExceptionFirstTime, RestartingSigil>();

    // Create our bolt and the restarter
    AutoRequired<CreationDetectionBolt> bolt;

    AutoRestarterConfig cfg;
    cfg.restartOnException = true;
    cfg.restartOnShutdown = false;
    cfg.startWhenCreated = true;
    AutoConstruct<AutoRestarter<RestartingSigil>> restarter(cfg);

    // Verify the bolt got called:
    ASSERT_TRUE(bolt->called) << "Bolt was not called even though a context restarter was present in the current context";

    // Verify subcontext properties:
    auto subCtxt = restarter->GetContext();
    ASSERT_TRUE(subCtxt != nullptr) << "Restarter did not correctly create a subcontext";
    ASSERT_TRUE(subCtxt->Is<RestartingSigil>()) << "Generated subcontext was not marked with the right sigil";
    ASSERT_TRUE(subCtxt->IsInitiated()) << "Generated subcontext should have been prospectively started, but was not";

    // Terminate the subcontext directly:
    subCtxt->SignalShutdown();

    // Verify that this causes the restarter to release its hold on the subcontext:
    auto newSubCtxt = restarter->GetContext();
    ASSERT_NE(subCtxt, newSubCtxt) << "Restarter did not release a terminated subcontext";
    ASSERT_FALSE(newSubCtxt) << "Restarter should not have attempted to create any new contexts on teardown";
}
示例#2
0
TEST_F(CoreContextTest, InitiateMultipleChildren) {
  AutoCurrentContext testCtxt;
  testCtxt->Initiate();
  // Initiate all children
  {
    auto outerCtxt = testCtxt->Create<void>();
    auto child1 = outerCtxt->Create<void>();
    auto child2 = outerCtxt->Create<void>();
    auto child3 = outerCtxt->Create<void>();

    child1->Initiate();
    child2->Initiate();
    child3->Initiate();

    outerCtxt->Initiate();

    ASSERT_TRUE(child1->IsRunning());
    ASSERT_TRUE(child2->IsRunning());
    ASSERT_TRUE(child3->IsRunning());

    outerCtxt->SignalShutdown(true);
  }

  // Don't initiate middle child
  {
    auto outerCtxt = testCtxt->Create<void>();
    auto child1 = outerCtxt->Create<void>();
    auto child2 = outerCtxt->Create<void>();
    auto child3 = outerCtxt->Create<void>();

    child1->Initiate();
    child3->Initiate();

    outerCtxt->Initiate();

    ASSERT_TRUE(child1->IsRunning());
    ASSERT_FALSE(child2->IsInitiated());
    ASSERT_TRUE(child3->IsRunning());

    outerCtxt->SignalShutdown(true);
  }

  // Don't initiate middle child and initiate parent first
  {
    auto outerCtxt = testCtxt->Create<void>();
    auto child1 = outerCtxt->Create<void>();
    auto child2 = outerCtxt->Create<void>();
    auto child3 = outerCtxt->Create<void>();

    outerCtxt->Initiate();

    child1->Initiate();
    child3->Initiate();

    ASSERT_TRUE(child1->IsRunning());
    ASSERT_FALSE(child2->IsInitiated());
    ASSERT_TRUE(child3->IsRunning());

    outerCtxt->SignalShutdown(true);
  }
}