public void ClusterSingleton_with_lease_should_retry_trying_to_get_lease_if_acquire_returns_fails()
        {
            var singletonProbe = CreateTestProbe();
            var settings       = NextSettings();

            Sys.ActorOf(
                ClusterSingletonManager.Props(Props.Create(() => new ImportantSingleton(singletonProbe.Ref)), PoisonPill.Instance, settings),
                settings.SingletonName);

            TestLease testLease = null;

            AwaitAssert(() =>
            {
                testLease = testLeaseExt.GetTestLease(LeaseNameFor(settings));
            }); // allow singleton manager to create the lease

            testLease.Probe.ExpectMsg(new TestLease.AcquireReq(leaseOwner));
            singletonProbe.ExpectNoMsg(shortDuration);
            TaskCompletionSource <bool> nextResponse = new TaskCompletionSource <bool>();

            testLease.SetNextAcquireResult(nextResponse.Task);
            testLease.InitialPromise.SetException(new TestException("no lease for you"));
            testLease.Probe.ExpectMsg(new TestLease.AcquireReq(leaseOwner));
            singletonProbe.ExpectNoMsg(shortDuration);
            nextResponse.SetResult(true);
            singletonProbe.ExpectMsg("preStart");
        }
        public void ClusterSingleton_with_lease_should_stop_singleton_if_the_lease_fails_periodic_check()
        {
            var lifecycleProbe = CreateTestProbe();
            var settings       = NextSettings();

            Sys.ActorOf(
                ClusterSingletonManager.Props(Props.Create(() => new ImportantSingleton(lifecycleProbe.Ref)), PoisonPill.Instance, settings),
                settings.SingletonName);

            TestLease testLease = null;

            AwaitAssert(() =>
            {
                testLease = testLeaseExt.GetTestLease(LeaseNameFor(settings));
            }); // allow singleton manager to create the lease

            testLease.Probe.ExpectMsg(new TestLease.AcquireReq(leaseOwner));
            testLease.InitialPromise.SetResult(true);
            lifecycleProbe.ExpectMsg("preStart");
            var callback = testLease.GetCurrentCallback();

            callback(null);
            lifecycleProbe.ExpectMsg("postStop");
            testLease.Probe.ExpectMsg(new TestLease.ReleaseReq(leaseOwner));

            // should try and reacquire lease
            testLease.Probe.ExpectMsg(new TestLease.AcquireReq(leaseOwner));
            lifecycleProbe.ExpectMsg("preStart");
        }
        public void ClusterSingleton_with_lease_should_do_not_start_if_lease_acquire_returns_false()
        {
            var probe    = CreateTestProbe();
            var settings = NextSettings();

            Sys.ActorOf(
                ClusterSingletonManager.Props(Props.Create(() => new ImportantSingleton(probe.Ref)), PoisonPill.Instance, settings),
                settings.SingletonName);

            TestLease testLease = null;

            AwaitAssert(() =>
            {
                testLease = testLeaseExt.GetTestLease(LeaseNameFor(settings));
            }); // allow singleton manager to create the lease

            probe.ExpectNoMsg(shortDuration);
            testLease.InitialPromise.SetResult(false);
            probe.ExpectNoMsg(shortDuration);
        }
        public void ClusterSingleton_with_lease_should_release_lease_when_leaving_oldest()
        {
            var singletonProbe = CreateTestProbe();
            var settings       = NextSettings();

            Sys.ActorOf(
                ClusterSingletonManager.Props(Props.Create(() => new ImportantSingleton(singletonProbe.Ref)), PoisonPill.Instance, settings),
                settings.SingletonName);

            TestLease testLease = null;

            AwaitAssert(() =>
            {
                testLease = testLeaseExt.GetTestLease(LeaseNameFor(settings));
            }); // allow singleton manager to create the lease

            singletonProbe.ExpectNoMsg(shortDuration);
            testLease.Probe.ExpectMsg(new TestLease.AcquireReq(leaseOwner));
            testLease.InitialPromise.SetResult(true);
            singletonProbe.ExpectMsg("preStart");
            cluster.Leave(cluster.SelfAddress);
            testLease.Probe.ExpectMsg(new TestLease.ReleaseReq(leaseOwner));
        }