Exemplo n.º 1
0
        public void CandidateShouldApplyLogsToFsm()
        {
            var currentState  = new CurrentState(Guid.NewGuid(), 0, default(Guid), 0, 0, default(Guid));
            var fsm           = new Rafty.FiniteStateMachine.InMemoryStateMachine();
            var candidate     = new Candidate(currentState, fsm, _peers, _log, _random, _node, _settings, _rules);
            var log           = new LogEntry(new FakeCommand("test"), typeof(string), 1);
            var appendEntries = new AppendEntriesBuilder()
                                .WithTerm(1)
                                .WithPreviousLogTerm(1)
                                .WithEntry(log)
                                .WithPreviousLogIndex(1)
                                .WithLeaderCommitIndex(1)
                                .Build();

            //assume node has added the log..
            _log.Apply(log);
            var appendEntriesResponse = candidate.Handle(appendEntries);

            candidate.CurrentState.CurrentTerm.ShouldBe(1);
            candidate.CurrentState.LastApplied.ShouldBe(1);
            fsm.ExposedForTesting.ShouldBe(1);
            var node = (NothingNode)_node;

            node.BecomeFollowerCount.ShouldBe(1);
        }
        public async Task LeaderShouldApplyLogsToFsm()
        {
            var currentState  = new CurrentState(Guid.NewGuid().ToString(), 0, default(string), 0, 0, default(string));
            var fsm           = new Rafty.FiniteStateMachine.InMemoryStateMachine();
            var leader        = new Leader(currentState, fsm, (s) => _peers, _log, _node, _settings, _rules);
            var log           = new LogEntry(new FakeCommand("test"), typeof(string), 1);
            var appendEntries = new AppendEntriesBuilder()
                                .WithTerm(1)
                                .WithPreviousLogTerm(1)
                                .WithEntry(log)
                                .WithPreviousLogIndex(1)
                                .WithLeaderCommitIndex(1)
                                .Build();
            //assume node has added the log..
            await _log.Apply(log);

            var appendEntriesResponse = leader.Handle(appendEntries);

            leader.CurrentState.CurrentTerm.ShouldBe(1);
            leader.CurrentState.LastApplied.ShouldBe(1);
            fsm.HandledLogEntries.ShouldBe(1);
        }