Ejemplo n.º 1
0
        private async Task ThenTheCommandIsReplicatedToAllStateMachines(FakeCommand command)
        {
            async Task <bool> CommandCalledOnAllStateMachines()
            {
                try
                {
                    var passed = 0;
                    foreach (var peer in _peers.Peers)
                    {
                        var fsmData = await File.ReadAllTextAsync(peer.HostAndPort.Replace("/", "").Replace(":", ""));

                        fsmData.ShouldNotBeNullOrEmpty();
                        var fakeCommand = JsonConvert.DeserializeObject <FakeCommand>(fsmData);
                        fakeCommand.Value.ShouldBe(command.Value);
                        passed++;
                    }

                    return(passed == 5);
                }
                catch (Exception e)
                {
                    return(false);
                }
            }

            Thread.Sleep(5000);
            var commandOnAllStateMachines = await WaitFor(20000).Until(async() => await CommandCalledOnAllStateMachines());

            commandOnAllStateMachines.ShouldBeTrue();
        }
Ejemplo n.º 2
0
        public async Task ShouldPersistTwoCommandsToFiveServers()
        {
            var peers = new List <FilePeer>
            {
                new FilePeer {
                    HostAndPort = "http://localhost:5005"
                },

                new FilePeer {
                    HostAndPort = "http://localhost:5006"
                },

                new FilePeer {
                    HostAndPort = "http://localhost:5007"
                },

                new FilePeer {
                    HostAndPort = "http://localhost:5008"
                },

                new FilePeer {
                    HostAndPort = "http://localhost:5009"
                }
            };

            var command = new FakeCommand("WHATS UP DOC?");

            GivenThePeersAre(peers);
            await GivenFiveServersAreRunning();
            await WhenISendACommandIntoTheCluster(command);
            await WhenISendACommandIntoTheCluster(command);

            Thread.Sleep(10000);
            await ThenTheCommandIsReplicatedToAllStateMachines(command, 2);
        }
Ejemplo n.º 3
0
        public async Task ShouldPersistCommandToFiveServers()
        {
            var command = new FakeCommand("WHATS UP DOC?");

            await GivenFiveServersAreRunning();
            await WhenISendACommandIntoTheCluster(command);
            await ThenTheCommandIsReplicatedToAllStateMachines(command);
        }
Ejemplo n.º 4
0
        public void ShouldPersistCommandToFiveServers()
        {
            var command = new FakeCommand("WHATS UP DOC?");

            GivenFiveServersAreRunning();
            GivenALeaderIsElected();
            WhenISendACommandIntoTheCluster(command);
            ThenTheCommandIsReplicatedToAllStateMachines(command);
        }
Ejemplo n.º 5
0
        private async Task WhenISendACommandIntoTheCluster(FakeCommand command)
        {
            async Task <bool> SendCommand()
            {
                try
                {
                    var p           = _peers.Peers.First();
                    var json        = JsonConvert.SerializeObject(command);
                    var httpContent = new StringContent(json);
                    using (var httpClient = new HttpClient())
                    {
                        var response = await httpClient.PostAsync($"{p.HostAndPort}/command", httpContent);

                        response.EnsureSuccessStatusCode();
                        var content = await response.Content.ReadAsStringAsync();

                        var error = JsonConvert.DeserializeObject <ErrorResponse <FakeCommand> >(content);
                        if (!string.IsNullOrEmpty(error.Error))
                        {
                            return(false);
                        }
                        var ok = JsonConvert.DeserializeObject <OkResponse <FakeCommand> >(content);
                        ok.Command.Value.ShouldBe(command.Value);
                        return(true);
                    }
                }
                catch (Exception e)
                {
                    return(false);
                }
            }

            var leaderElectedAndCommandReceived = await WaitFor(40000).Until(async() =>
            {
                var result = await SendCommand();
                Thread.Sleep(1000);
                return(result);
            });

            leaderElectedAndCommandReceived.ShouldBeTrue();
        }
Ejemplo n.º 6
0
        private void WhenISendACommandIntoTheCluster(FakeCommand command)
        {
            var p           = _peers.Peers.First();
            var json        = JsonConvert.SerializeObject(command);
            var httpContent = new StringContent(json);

            using (var httpClient = new HttpClient())
            {
                var response = httpClient.PostAsync($"{p.HostAndPort}/command", httpContent).GetAwaiter().GetResult();
                response.EnsureSuccessStatusCode();
                var content = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
                var result  = JsonConvert.DeserializeObject <OkResponse <FakeCommand> >(content);
                result.Command.Value.ShouldBe(command.Value);
            }

            //dirty sleep to make sure command replicated...
            var stopwatch = Stopwatch.StartNew();

            while (stopwatch.ElapsedMilliseconds < 10000)
            {
            }
        }
Ejemplo n.º 7
0
        private void ThenTheCommandIsReplicatedToAllStateMachines(FakeCommand command)
        {
            //dirty sleep to give a chance to replicate...
            var stopwatch = Stopwatch.StartNew();

            while (stopwatch.ElapsedMilliseconds < 2000)
            {
            }

            bool CommandCalledOnAllStateMachines()
            {
                try
                {
                    var passed = 0;
                    foreach (var peer in _peers.Peers)
                    {
                        string fsmData;
                        fsmData = File.ReadAllText(peer.HostAndPort.Replace("/", "").Replace(":", ""));
                        fsmData.ShouldNotBeNullOrEmpty();
                        var fakeCommand = JsonConvert.DeserializeObject <FakeCommand>(fsmData);
                        fakeCommand.Value.ShouldBe(command.Value);
                        passed++;
                    }

                    return(passed == 5);
                }
                catch (Exception e)
                {
                    return(false);
                }
            }

            var commandOnAllStateMachines = WaitFor(20000).Until(() => CommandCalledOnAllStateMachines());

            commandOnAllStateMachines.ShouldBeTrue();
        }
Ejemplo n.º 8
0
        private async Task ThenTheCommandIsReplicatedToAllStateMachines(FakeCommand fakeCommand, int expectedCommands)
        {
            async Task <bool> CommandCalledOnAllStateMachines()
            {
                try
                {
                    var passed = 0;
                    foreach (var peer in _peers.Peers)
                    {
                        var path = $"{peer.HostAndPort.Replace("/","").Replace(":","")}.db";
                        using (var connection = new SqliteConnection($"Data Source={path};"))
                        {
                            connection.Open();
                            var sql = @"select count(id) from logs";
                            using (var command = new SqliteCommand(sql, connection))
                            {
                                var count = Convert.ToInt32(command.ExecuteScalar());
                                if (count != expectedCommands)
                                {
                                    LogInformation($"{peer.HostAndPort} had {count} logs, expected {expectedCommands}");
                                    continue;
                                }
                            }
                        }

                        var fsmData = await File.ReadAllTextAsync(peer.HostAndPort.Replace("/", "").Replace(":", ""));

                        fsmData.ShouldNotBeNullOrEmpty();

                        var storedCommands = JsonConvert.DeserializeObject <List <ICommand> >(fsmData, new JsonSerializerSettings()
                        {
                            TypeNameHandling = TypeNameHandling.All
                        });

                        if (storedCommands.Count != expectedCommands)
                        {
                            LogInformation($"{peer.HostAndPort} had {fsmData.Length} length in fsm file and stored {storedCommands.Count} commands");
                            continue;
                        }
                        else
                        {
                            foreach (var command in storedCommands)
                            {
                                var fC = (FakeCommand)command;
                                fC.Value.ShouldBe(fakeCommand.Value);
                            }
                            passed++;
                        }
                    }

                    return(passed == 5);
                }
                catch (Exception e)
                {
                    LogException(e);
                    return(false);
                }
            }

            var commandOnAllStateMachines = await WaitFor(20000).Until(async() =>
            {
                var result = await CommandCalledOnAllStateMachines();
                Thread.Sleep(1000);
                return(result);
            });

            commandOnAllStateMachines.ShouldBeTrue();
        }