Пример #1
0
        public void BecomeCandidate(CurrentState state)
        {
            State?.Stop();
            _logger.LogInformation($"{state.Id} became candidate");
            var candidate = new Candidate(state, _fsm, _getPeers(state), _log, _random, this, _settings, _rules, _loggerFactory);

            State = candidate;
            candidate.BeginElection();
        }
Пример #2
0
        public void ShouldVoteForSelfWhenElectionStarts()
        {
            _peers = new List <IPeer>();
            for (var i = 0; i < 4; i++)
            {
                _peers.Add(new FakePeer(true));
            }
            var candidate = new Candidate(_currentState, _fsm, _peers, _log, _random, _node, _settings, _rules);

            candidate.BeginElection();
            candidate.CurrentState.VotedFor.ShouldBe(_id);
        }
Пример #3
0
        public void ShouldIncrementCurrentTermWhenElectionStarts()
        {
            _peers = new List <IPeer>();
            for (var i = 0; i < 4; i++)
            {
                _peers.Add(new FakePeer(true));
            }
            var candidate = new Candidate(_currentState, _fsm, _peers, _log, _random, _node, _settings, _rules, _loggerFactory.Object);

            candidate.BeginElection();
            candidate.CurrentState.CurrentTerm.ShouldBe(1);
        }
Пример #4
0
        public void ShouldBecomeLeaderIfReceivesMajorityOfVotes()
        {
            _peers = new List <IPeer>();
            for (var i = 0; i < 4; i++)
            {
                _peers.Add(new FakePeer(true));
            }
            var candidate = new Candidate(_currentState, _fsm, _peers, _log, _random, _node, _settings, _rules);

            candidate.BeginElection();
            var node = (NothingNode)_node;

            node.BecomeLeaderCount.ShouldBe(1);
        }
Пример #5
0
        public void ShouldBecomeFollowerIfDoesntReceiveAnyVotes()
        {
            _peers = new List <IPeer>();
            for (var i = 0; i < 4; i++)
            {
                _peers.Add(new FakePeer(false));
            }
            var candidate = new Candidate(_currentState, _fsm, _peers, _log, _random, _node, _settings, _rules, _loggerFactory.Object);

            candidate.BeginElection();
            var node = (NothingNode)_node;

            node.BecomeFollowerCount.ShouldBe(1);
        }
Пример #6
0
        public void ShouldStartNewElectionIfTimesout()
        {
            _peers = new List <IPeer>
            {
                new FakePeer(false),
                new FakePeer(false),
                new FakePeer(true),
                new FakePeer(true)
            };
            var candidate = new Candidate(_currentState, _fsm, _peers, _log, _random, _node, _settings, _rules);

            candidate.BeginElection();
            candidate.ShouldBeOfType <Candidate>();
            candidate.CurrentState.CurrentTerm.ShouldBe(1);
        }
Пример #7
0
        public void ShouldBecomeFollowerIfRequestVoteResponseTermGreaterThanCurrentTerm()
        {
            _peers = new List <IPeer>
            {
                new FakePeer(10),
                new FakePeer(true),
                new FakePeer(true),
                new FakePeer(true)
            };
            var candidate = new Candidate(_currentState, _fsm, _peers, _log, _random, _node, _settings, _rules);

            candidate.BeginElection();
            var node = (NothingNode)_node;

            node.BecomeFollowerCount.ShouldBe(1);
        }
Пример #8
0
        public void ShouldRequestVotesFromAllPeersWhenElectionStarts()
        {
            _peers = new List <IPeer>();
            for (var i = 0; i < 4; i++)
            {
                _peers.Add(new FakePeer(true));
            }
            var candidate = new Candidate(_currentState, _fsm, _peers, _log, _random, _node, _settings, _rules);

            candidate.BeginElection();
            _peers.ForEach(x =>
            {
                var peer = (FakePeer)x;
                peer.RequestVoteResponses.Count.ShouldBe(1);
            });
        }
Пример #9
0
        public void ShouldBecomeFollowerIfDoesntReceiveMajorityOfVotes()
        {
            _peers = new List <IPeer>
            {
                new FakePeer(false),
                new FakePeer(false),
                new FakePeer(false),
                new FakePeer(true)
            };
            var candidate = new Candidate(_currentState, _fsm, _peers, _log, _random, _node, _settings, _rules);

            candidate.BeginElection();
            var node = (NothingNode)_node;

            node.BecomeFollowerCount.ShouldBe(1);
        }
Пример #10
0
        public void ShouldBecomeFollowerIfAppendEntriesReceivedFromNewLeaderAndTermGreaterThanCurrentTerm()
        {
            _peers = new List <IPeer>
            {
                new FakePeer(false),
                new FakePeer(false),
                new FakePeer(true),
                new FakePeer(true)
            };
            var candidate = new Candidate(_currentState, _fsm, _peers, _log, _random, _node, _settings, _rules);

            candidate.BeginElection();
            var appendEntriesResponse = candidate.Handle(new AppendEntriesBuilder().WithTerm(2).Build());

            appendEntriesResponse.Success.ShouldBeTrue();
            var node = (NothingNode)_node;

            node.BecomeFollowerCount.ShouldBe(1);
        }
Пример #11
0
        public async Task ShouldNotBecomeFollowerIfAppendEntriesReceivedFromNewLeaderAndTermLessThanCurrentTerm()
        {
            _peers = new List <IPeer>
            {
                new FakePeer(false),
                new FakePeer(false),
                new FakePeer(true),
                new FakePeer(true)
            };
            var candidate = new Candidate(_currentState, _fsm, _peers, _log, _random, _node, _settings, _rules, _loggerFactory.Object);

            candidate.BeginElection();
            var appendEntriesResponse = await candidate.Handle(new AppendEntriesBuilder().WithTerm(0).Build());

            appendEntriesResponse.Success.ShouldBeFalse();
            var node = (NothingNode)_node;

            node.BecomeFollowerCount.ShouldBe(0);
        }
Пример #12
0
        public void ShouldResetTimeoutWhenElectionStarts()
        {
            var candidate = new Candidate(_currentState, _fsm, _peers, _log, _random, _node, _settings, _rules);

            candidate.BeginElection();
        }