Esempio n. 1
0
        public void RecomputeChangesStatesAsExpected()
        {
            var clusterState = new ClusterStateMachine();
            var cfg          = new ClusterStateRecomputeConfiguration()
            {
                // Force recompute to run
                RecomputeFrequency = TimeSpan.Zero,
            };

            var nowUtc = _clock.UtcNow;

            // We want to test all possible state machine transitions. To do so, we generate a very specific instance
            // of cluster state meant to transition the way we expect instead of actually simulating each possible
            // branch.

            var       n1 = MachineLocation.Create("node2", 0);
            MachineId n1Id;

            (clusterState, n1Id) = clusterState.ForceRegisterMachineWithState(n1, nowUtc, MachineState.DeadUnavailable);

            var       n2 = MachineLocation.Create("node3", 0);
            MachineId n2Id;

            (clusterState, n2Id) = clusterState.ForceRegisterMachineWithState(n2, nowUtc, MachineState.DeadExpired);

            var       n3 = MachineLocation.Create("node4", 0);
            MachineId n3Id;

            (clusterState, n3Id) = clusterState.ForceRegisterMachineWithState(n3, nowUtc - cfg.ActiveToDeadExpiredInterval, MachineState.Open);

            var       n4 = MachineLocation.Create("node5", 0);
            MachineId n4Id;

            (clusterState, n4Id) = clusterState.ForceRegisterMachineWithState(n4, nowUtc - cfg.ActiveToClosedInterval, MachineState.Open);

            var       n5 = MachineLocation.Create("node6", 0);
            MachineId n5Id;

            (clusterState, n5Id) = clusterState.ForceRegisterMachineWithState(n5, nowUtc, MachineState.Open);

            var       n6 = MachineLocation.Create("node7", 0);
            MachineId n6Id;

            (clusterState, n6Id) = clusterState.ForceRegisterMachineWithState(n6, nowUtc - cfg.ClosedToDeadExpiredInterval, MachineState.Closed);

            var       n7 = MachineLocation.Create("node8", 0);
            MachineId n7Id;

            (clusterState, n7Id) = clusterState.ForceRegisterMachineWithState(n7, nowUtc, MachineState.Closed);

            clusterState = clusterState.Recompute(cfg, nowUtc);

            clusterState.GetStatus(n1Id).ThrowIfFailure().State.Should().Be(MachineState.DeadUnavailable);
            clusterState.GetStatus(n2Id).ThrowIfFailure().State.Should().Be(MachineState.DeadExpired);
            clusterState.GetStatus(n3Id).ThrowIfFailure().State.Should().Be(MachineState.DeadExpired);
            clusterState.GetStatus(n4Id).ThrowIfFailure().State.Should().Be(MachineState.Closed);
            clusterState.GetStatus(n5Id).ThrowIfFailure().State.Should().Be(MachineState.Open);
            clusterState.GetStatus(n6Id).ThrowIfFailure().State.Should().Be(MachineState.DeadExpired);
            clusterState.GetStatus(n7Id).ThrowIfFailure().State.Should().Be(MachineState.Closed);
        }
Esempio n. 2
0
        public void RecomputeDoesntRunIfNotNeeded()
        {
            var now     = _clock.UtcNow;
            var current = new ClusterStateMachine()
            {
                LastStateMachineRecomputeTimeUtc = now,
            };

            var cfg = new ClusterStateRecomputeConfiguration()
            {
                RecomputeFrequency = TimeSpan.FromMilliseconds(1),
            };

            var next = current.Recompute(cfg, now);

            next.Should().BeEquivalentTo(current);
        }