예제 #1
0
        public void LeadsFollowersAndTheirHeartbeatLikeALeader()
        {
            var list = new ConcurrentBag <FriendlyPeer>();

            _manijer.Setup(x => x.GetPeers()).Returns(new[] { "1", "3", "5", "7" }.Select(s => new Peer(s, Guid.NewGuid())));
            _manijer.Setup(x => x.GetProxy(It.IsAny <string>())).Returns(
                () => {
                var s = new FriendlyPeer();
                list.Add(s);
                return(s);
            }
                );

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

            Thread.Sleep(3000);

            Assert.Equal(Role.Leader, _server.Role);
            var timesFollowersServed = list.SelectMany(x => x.AllThemAppendEntriesRequests.Where(y => y.Entries.Length == 0)).Count();

            Assert.True(timesFollowersServed > 10);
            _output.WriteLine($"{nameof(timesFollowersServed)}: {timesFollowersServed}");
        }
예제 #2
0
        public async Task LeadsFollowersAndTheirLogsLikeALeader()
        {
            var list = new ConcurrentBag <FriendlyPeer>();

            _manijer.Setup(x => x.GetPeers()).Returns(new[] { "1", "3", "5", "7" }.Select(s => new Peer(s, Guid.NewGuid())));
            _manijer.Setup(x => x.GetProxy(It.IsAny <string>())).Returns(
                () => {
                var s = new FriendlyPeer();
                list.Add(s);
                return(s);
            }
                );

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

            Thread.Sleep(2000); // must be a leader by now
            Assert.Equal(Role.Leader, _server.Role);
            await _server.ApplyCommandAsync(new StateMachineCommandRequest()
            {
                Command = new byte[128]
            });

            await _server.ApplyCommandAsync(new StateMachineCommandRequest()
            {
                Command = new byte[128]
            });

            await _server.ApplyCommandAsync(new StateMachineCommandRequest()
            {
                Command = new byte[128]
            });

            Thread.Sleep(2000);

            var timesFollowersServed = list.SelectMany(x => {
                var copy = x.AllThemAppendEntriesRequests.ToArray();
                return(copy.Where(y => y.Entries.Length > 0));
            }).Count();

            _output.WriteLine($"timesFollowersServed: {timesFollowersServed}");
            Assert.True(timesFollowersServed > 1);
            Assert.Equal(2, _sister.LastIndex);
        }