Esempio n. 1
0
        public async Task ShouldTimeoutAfterXSecondsIfCannotReplicateCommandAndRollbackIndexes()
        {
            _peers = new List <IPeer>();
            for (var i = 0; i < 3; i++)
            {
                _peers.Add(new FakePeer(false, false, false));
            }

            _peers.Add(new FakePeer(true, true, true));

            _currentState = new CurrentState(_id, 1, default(string), 0, 0, default(string));
            _settings     = new InMemorySettingsBuilder().WithCommandTimeout(1).Build();
            var leader   = new Leader(_currentState, _fsm, (s) => _peers, _log, _node, _settings, _rules, _loggerFactory.Object);
            var command  = new FakeCommand();
            var response = await leader.Accept(command);

            var error = (ErrorResponse <FakeCommand>)response;

            error.Error.ShouldBe("Unable to replicate command to peers due to timeout.");
            bool TestPeerStates(List <PeerState> peerState)
            {
                var passed = 0;

                peerState.ForEach(pS =>
                {
                    if (pS.MatchIndex.IndexOfHighestKnownReplicatedLog == 0)
                    {
                        passed++;
                    }

                    if (pS.NextIndex.NextLogIndexToSendToPeer == 1)
                    {
                        passed++;
                    }
                });

                return(passed == peerState.Count * 2);
            }

            var result = WaitFor(1000).Until(() => TestPeerStates(leader.PeerStates));

            _log.Count().Result.ShouldBe(0);
            result.ShouldBeTrue();
        }
Esempio n. 2
0
        public async Task ShouldReplicateCommand()
        {
            _peers = new List <IPeer>();
            for (var i = 0; i < 4; i++)
            {
                _peers.Add(new FakePeer(true, true, true));
            }
            _currentState = new CurrentState(_id, 1, default(string), 0, 0, default(string));
            var leader   = new Leader(_currentState, _fsm, (s) => _peers, _log, _node, _settings, _rules, _loggerFactory.Object);
            var command  = new FakeCommand();
            var response = await leader.Accept(command);

            response.ShouldBeOfType <OkResponse <FakeCommand> >();
            bool TestPeerStates(List <PeerState> peerState)
            {
                var passed = 0;

                peerState.ForEach(pS =>
                {
                    if (pS.MatchIndex.IndexOfHighestKnownReplicatedLog == 1)
                    {
                        passed++;
                    }

                    if (pS.NextIndex.NextLogIndexToSendToPeer == 2)
                    {
                        passed++;
                    }
                });

                return(passed == peerState.Count * 2);
            }

            var result = WaitFor(1000).Until(() => TestPeerStates(leader.PeerStates));

            result.ShouldBeTrue();
        }
Esempio n. 3
0
 private void ServerReceives(FakeCommand fakeCommand)
 {
     _server.Receive(fakeCommand).Wait();
 }
Esempio n. 4
0
 private void WhenTheServerReceivesACommand(FakeCommand fakeCommand)
 {
     _fakeCommand = fakeCommand;
     _server.Receive(fakeCommand).Wait();
 }