public void Init() { var mocker = new AutoMocker(); var machine = new Machine<State, Trigger>(State.First, mocker.Get<ILoggerFactory>(), mocker.Get<IObservableProvider>()); machine.On(State.First). OnEntry(() => { Assert.Equal(State.First, machine.State); System.Console.WriteLine($"first {machine.State}"); }) .Permit(Trigger.ChangeToSecond, State.Second) .Permit(Trigger.ChangeToFirst, State.First); machine.On(State.Second). OnEntry(() => { Assert.Equal(State.Second, machine.State); System.Console.WriteLine($"second {machine.State}"); }) .Permit(Trigger.ChangeToSecond, State.Second) .Permit(Trigger.ChangeToThird, State.Third); machine.On(State.Third). OnEntry(() => { Assert.Equal(State.Third, machine.State); }); machine.OnNext(Trigger.ChangeToFirst); Assert.Equal(State.First, machine.State); machine.OnNext(Trigger.ChangeToThird); Assert.Equal(State.First, machine.State); machine.OnNext(Trigger.ChangeToSecond); Assert.Equal(State.Second, machine.State); machine.OnNext(Trigger.ChangeToThird); Assert.Equal(State.Third, machine.State); machine.OnNext(Trigger.ChangeToFirst); }
public Node(IVoteReceived voteReceived, IAppendEntry appendEntry, IHartbeatTimer hartbeat, ILoggerFactory loggerFactory, IObservableProvider observableProvider, ILogReplication logReplication, ServerIdentifier serverIdentifier) { _logReplication = logReplication; _serverIdentifier = serverIdentifier; _hartbeatTimer = hartbeat; _stateMachine = new Machine<ServerStateType, ServerStateType>(ServerStateType.Candidate, loggerFactory, observableProvider); _stateMachine.On(ServerStateType.Follower) .OnEntry(() => { hartbeat.Subscribe(_stateMachine); }) .OnEntrySubscribe<Follower>() .Permit(ServerStateType.Candidate, ServerStateType.Candidate); _stateMachine.On(ServerStateType.Candidate) .OnEntrySubscribe<Candidate>() .OnEntry(hartbeat.Reset) .Permit(ServerStateType.Follower, ServerStateType.Follower) .Permit(ServerStateType.Leader, ServerStateType.Leader) .Permit(ServerStateType.Candidate, ServerStateType.Candidate); _stateMachine.On(ServerStateType.Leader) .OnEntrySubscribe<Leader>() .Permit(ServerStateType.Follower, ServerStateType.Follower); _voteReceivedeDispose = voteReceived.Subscribe(_stateMachine); _appendEntryDispose = appendEntry.Subscribe(_stateMachine); _stateMachine.OnNext(ServerStateType.Follower); }