protected VoteReceivedBaseTests()
        {
            // Arrange
            var mocker = new AutoMocker();

            ServerIdentifier = new ServerIdentifier();

            mocker.Use(ServerIdentifier);

            mocker.Setup<ISubject<RequestVoteMessage>>(p => p.Subscribe(It.IsAny<IObserver<RequestVoteMessage>>()))
                .Callback<IObserver<RequestVoteMessage>>((o) =>
                {
                    RequestVoteCallback = o;
                });
            RequestVoteCallback = mocker.Get<ISubject<RequestVoteMessage>>();

            Election = new Election();

            Message = new RequestVoteResultMessage();

            mocker.Setup<ISubject<RequestVoteResultMessage>>(p => p.OnNext(It.IsAny<RequestVoteResultMessage>()))
                .Callback<RequestVoteResultMessage>((o) =>
                {
                    Message = o;
                });

            VoteReceived = new Rafting.VoteReceived(mocker.Get<ISubject<RequestVoteResultMessage>>(),
                mocker.Get<ServerIdentifier>(),
                mocker.Get<ISubject<RequestVoteMessage>>(),
                Election,
                mocker.Get<ILoggerFactory>(),
                new Nodes(),
                new RaftOptions(),
                mocker.Get<ILogReplication>());
        }
Esempio n. 2
0
        public void ElectionResults(RequestVoteResultMessage result)
        {
            if (_serverIdentifier.Equals(result.From))
                return;

            if (isDispose) return;

            if (_options.UseLogging)            
                _logger.LogInformation($"Processing {nameof(ElectionResults)} \n\t\t Candidate {result.Candidate} \n\t\t Term {result.Term} \n\t\t VoteGranted {result.VoteGranted}");
            
            // We are not intrested in old terms
            if (result.Term < _election.CurrentTerm)
                return;

            // A candidate is already a term ahead so we follow that _election 
            if (result.Term > _election.CurrentTerm)
            {
                _election.CurrentTerm = result.Term;
                _election.VotedFor = result.Candidate;
                ChangeState = ServerStateType.Follower;
                return;
            }

            if (!_nodes.Contains(result.From))
            {
                _nodes.Add(result.From);
            }
            
            
            // We collect our votes for the _election to determining if we are the leader
            _votingSystem.AddVote(result.From, result.Candidate);
        }