Esempio n. 1
0
        public async Task NeverVotesTwice()
        {
            var leaderId = Guid.NewGuid();

            _server = new DefaultRaftServer(_sister, _sister, _sister, _maqina.Object, _manijer.Object, _settings);
            _server.LastHeartBeat     = new AlwaysRecentTimestamp();
            _server.LastHeartBeatSent = new AlwaysRecentTimestamp();
            _server.Start();

            Thread.Sleep(400);
            var result = await _server.RequestVoteAsync(new RequestVoteRequest()
            {
                CandidateId  = Guid.NewGuid(),
                CurrentTerm  = 2,
                LastLogIndex = 2,
                LastLogTerm  = 1
            });

            Thread.Sleep(400);
            var result2 = await _server.RequestVoteAsync(new RequestVoteRequest()
            {
                CandidateId  = Guid.NewGuid(),
                CurrentTerm  = 2,
                LastLogIndex = 2,
                LastLogTerm  = 1
            });

            Assert.False(result2.VoteGranted);
        }
Esempio n. 2
0
        public async Task NeverVotesYesToLowerTerm()
        {
            var leaderId = Guid.NewGuid();

            _server = new DefaultRaftServer(_sister, _sister, _sister, _maqina.Object, _manijer.Object, _settings);
            _server.Start();

            // to set the term to 1
            var result = await _server.RequestVoteAsync(new RequestVoteRequest()
            {
                CandidateId  = Guid.NewGuid(),
                CurrentTerm  = 1,
                LastLogIndex = 2,
                LastLogTerm  = 1
            });

            Thread.Sleep(400); // now it becomes candidate and returns no

            // now its term already moved to 2 due to timeout
            result = await _server.RequestVoteAsync(new RequestVoteRequest()
            {
                CandidateId  = Guid.NewGuid(),
                CurrentTerm  = 1,
                LastLogIndex = 2,
                LastLogTerm  = 1
            });

            Assert.False(result.VoteGranted);
            Assert.Equal(2, result.CurrentTerm);
        }