コード例 #1
0
        public void AddTwoToxicWithTheSameNameShouldThrowException()
        {
            var client = _connection.Client();
            var proxy  = new Proxy
            {
                Name     = "testingProxy",
                Enabled  = true,
                Listen   = "127.0.0.1:9090",
                Upstream = "google.com"
            };

            var newProxy = client.AddAsync(proxy).Result;

            var firstToxic = new SlicerToxic
            {
                Name   = "SlicerToxicTest",
                Stream = ToxicDirection.UpStream
            };

            firstToxic.Attributes.AverageSize   = 10;
            firstToxic.Attributes.Delay         = 5;
            firstToxic.Attributes.SizeVariation = 1;
            newProxy.AddAsync(firstToxic).Wait();

            var toxicWithSameName = new SlicerToxic
            {
                Name   = firstToxic.Name,
                Stream = ToxicDirection.UpStream
            };

            Assert.ThrowsAsync <ToxiProxiException>(
                () => newProxy.AddAsync(toxicWithSameName)).Wait();
        }
コード例 #2
0
        public void CreateANewSlicerToxicShouldWork()
        {
            var client = _connection.Client();
            var proxy  = new Proxy
            {
                Name     = "testingProxy",
                Enabled  = true,
                Listen   = "127.0.0.1:9090",
                Upstream = "google.com"
            };

            var newProxy = client.AddAsync(proxy).Result;

            var toxic = new SlicerToxic
            {
                Name   = "SlicerToxicTest",
                Stream = ToxicDirection.UpStream
            };

            toxic.Attributes.AverageSize   = 10;
            toxic.Attributes.Delay         = 5;
            toxic.Attributes.SizeVariation = 1;
            var newToxic = newProxy.AddAsync(toxic).Result;

            // Need to retrieve the proxy and check the toxic's values
            Assert.Equal(toxic.Name, newToxic.Name);
            Assert.Equal(toxic.Stream, newToxic.Stream);
            Assert.Equal(toxic.Attributes.AverageSize, newToxic.Attributes.AverageSize);
            Assert.Equal(toxic.Attributes.Delay, newToxic.Attributes.Delay);
            Assert.Equal(toxic.Attributes.SizeVariation, newToxic.Attributes.SizeVariation);
        }
コード例 #3
0
        public void GetAllToxicsFromAProxyShouldWork()
        {
            // Add two toxics to a proxy and check if they are present in the list
            // of the toxies for the given proxy
            var client = _connection.Client();
            var proxy  = new Proxy
            {
                Name     = "testingProxy",
                Enabled  = true,
                Listen   = "127.0.0.1:9090",
                Upstream = "google.com"
            };

            var newProxy = client.AddAsync(proxy).Result;

            var slicerToxic = new SlicerToxic
            {
                Name   = "SlicerToxicTest",
                Stream = ToxicDirection.UpStream
            };

            slicerToxic.Attributes.AverageSize   = 10;
            slicerToxic.Attributes.Delay         = 5;
            slicerToxic.Attributes.SizeVariation = 1;
            newProxy.AddAsync(slicerToxic).Wait();

            var slowCloseToxic = new SlowCloseToxic
            {
                Name     = "slowCloseToxic",
                Stream   = ToxicDirection.DownStream,
                Toxicity = 80
            };

            slowCloseToxic.Attributes.Delay = 50;
            newProxy.AddAsync(slowCloseToxic).Wait();

            // Retrieve the proxy and check the toxics
            var toxics = newProxy.GetAllToxicsAsync().Result;

            Assert.Equal(2, toxics.Count());

            var slicerToxicInTheProxy = toxics.OfType <SlicerToxic>().Single();

            Assert.Equal(slicerToxic.Name, slicerToxicInTheProxy.Name);
            Assert.Equal(slicerToxic.Stream, slicerToxicInTheProxy.Stream);
            Assert.Equal(slicerToxic.Attributes.AverageSize, slicerToxicInTheProxy.Attributes.AverageSize);
            Assert.Equal(slicerToxic.Attributes.Delay, slicerToxicInTheProxy.Attributes.Delay);
            Assert.Equal(slicerToxic.Attributes.SizeVariation, slicerToxicInTheProxy.Attributes.SizeVariation);

            var slowCloseToxicInTheProxy = toxics.OfType <SlowCloseToxic>().Single();

            Assert.Equal(slowCloseToxic.Name, slowCloseToxicInTheProxy.Name);
            Assert.Equal(slowCloseToxic.Stream, slowCloseToxicInTheProxy.Stream);
            Assert.Equal(slowCloseToxic.Attributes.Delay, slowCloseToxicInTheProxy.Attributes.Delay);
        }
コード例 #4
0
        public void UpdatingAToxicShouldWorks()
        {
            // Add a toxics to a proxy.
            // After update all the toxic's properties
            // Reload the toxic again and check that all the properties
            // are correctly updated
            var client = _connection.Client();
            var proxy  = new Proxy
            {
                Name     = "testingProxy",
                Enabled  = true,
                Listen   = "127.0.0.1:9090",
                Upstream = "google.com"
            };

            proxy = client.AddAsync(proxy).Result;

            var toxic = new SlicerToxic
            {
                Name   = "SlicerToxicTest",
                Stream = ToxicDirection.UpStream
            };

            toxic.Attributes.AverageSize   = 10;
            toxic.Attributes.Delay         = 5;
            toxic.Attributes.SizeVariation = 1;
            toxic = proxy.AddAsync(toxic).Result;

            // Reload the toxic and update the properties
            var toxicInProxy = (SlicerToxic)proxy.GetToxicByNameAsync(toxic.Name).Result;

            // Update the toxic's property
            toxicInProxy.Name   = "NewName";
            toxicInProxy.Stream = ToxicDirection.DownStream;
            toxicInProxy.Attributes.AverageSize   = 20;
            toxicInProxy.Attributes.Delay         = 10;
            toxicInProxy.Attributes.SizeVariation = 2;
            proxy.UpdateToxicAsync(toxic.Name, toxicInProxy).Wait();

            // Reload again (we must use the initial name because the toxic update
            // cannot update the toxic name
            var updatedToxic = proxy.GetToxicByNameAsync(toxic.Name).Result;

            // Assert
            // WARNING: By design it's not possible to update the name and the stream properties of the proxy.
            Assert.NotEqual(toxicInProxy.Name, updatedToxic.Name);
            Assert.NotEqual(toxicInProxy.Stream, updatedToxic.Stream);
            Assert.IsType <SlicerToxic>(toxicInProxy);
            var specificToxicInProxy = (SlicerToxic)toxicInProxy;

            Assert.Equal(toxicInProxy.Attributes.AverageSize, specificToxicInProxy.Attributes.AverageSize);
            Assert.Equal(toxicInProxy.Attributes.Delay, specificToxicInProxy.Attributes.Delay);
            Assert.Equal(toxicInProxy.Attributes.SizeVariation, specificToxicInProxy.Attributes.SizeVariation);
        }
コード例 #5
0
        public void DeleteAToxicShouldWork()
        {
            // Add two toxics to a proxy.
            // After delete the first one and check that
            // there is still the second toxic in the proxy
            var client = _connection.Client();
            var proxy  = new Proxy
            {
                Name     = "testingProxy",
                Enabled  = true,
                Listen   = "127.0.0.1:9090",
                Upstream = "google.com"
            };

            var newProxy = client.AddAsync(proxy).Result;

            var firstToxic = new SlicerToxic
            {
                Name   = "SlicerToxicTest",
                Stream = ToxicDirection.UpStream
            };

            firstToxic.Attributes.AverageSize   = 10;
            firstToxic.Attributes.Delay         = 5;
            firstToxic.Attributes.SizeVariation = 1;
            newProxy.AddAsync(firstToxic).Wait();

            var secondToxic = new SlowCloseToxic
            {
                Name     = "slowCloseToxic",
                Stream   = ToxicDirection.DownStream,
                Toxicity = 80
            };

            secondToxic.Attributes.Delay = 50;
            newProxy.AddAsync(secondToxic).Wait();

            // Delete the first toxic
            newProxy.RemoveToxicAsync(firstToxic.Name).Wait();

            // Retrieve the proxy and check that there is the
            // correct toxics
            var toxicsInProxy = newProxy.GetAllToxicsAsync().Result;

            Assert.True(1 == toxicsInProxy.Count());
            Assert.IsType <SlowCloseToxic>(toxicsInProxy.First());
            var singleToxicInProxy = (SlowCloseToxic)toxicsInProxy.First();

            Assert.Equal(secondToxic.Name, singleToxicInProxy.Name);
            Assert.Equal(secondToxic.Stream, singleToxicInProxy.Stream);
            Assert.Equal(secondToxic.Toxicity, singleToxicInProxy.Toxicity);
            Assert.Equal(secondToxic.Attributes.Delay, singleToxicInProxy.Attributes.Delay);
        }
コード例 #6
0
        public void GetAnExistingToxicFromAProxyShouldWork()
        {
            // Add a toxics to a proxy.
            // After reload the toxic again and check that all the properties
            // are correctly saved
            var client = _connection.Client();
            var proxy  = new Proxy
            {
                Name     = "testingProxy",
                Enabled  = true,
                Listen   = "127.0.0.1:9090",
                Upstream = "google.com"
            };

            proxy = client.AddAsync(proxy).Result;

            var toxic = new SlicerToxic
            {
                Name   = "SlicerToxicTest",
                Stream = ToxicDirection.UpStream
            };

            toxic.Attributes.AverageSize   = 10;
            toxic.Attributes.Delay         = 5;
            toxic.Attributes.SizeVariation = 1;
            toxic = proxy.AddAsync(toxic).Result;

            // Reload the toxic and update the properties
            var toxicInProxy = proxy.GetToxicByNameAsync(toxic.Name).Result;

            // Assert
            Assert.Equal(toxicInProxy.Name, toxic.Name);
            Assert.Equal(toxicInProxy.Stream, toxic.Stream);
            Assert.IsType <SlicerToxic>(toxicInProxy);
            var specificToxicInProxy = (SlicerToxic)toxicInProxy;

            Assert.Equal(specificToxicInProxy.Attributes.AverageSize, toxic.Attributes.AverageSize);
            Assert.Equal(specificToxicInProxy.Attributes.Delay, toxic.Attributes.Delay);
            Assert.Equal(specificToxicInProxy.Attributes.SizeVariation, toxic.Attributes.SizeVariation);
        }
コード例 #7
0
        public async Task MessagesShouldPassThruRedundantChannelWhenNotAllChildConnectionsAreSlowOrDown()
        {
            var toxiproxyServerPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "TransactTcp.Tests", "toxiproxy-server-windows-amd64.exe");

            foreach (var existentToxiserverProcess in Process.GetProcessesByName("toxiproxy-server-windows-amd64").ToList())
            {
                existentToxiserverProcess.Kill();
            }

            Directory.CreateDirectory(Path.GetDirectoryName(toxiproxyServerPath));

            await File.WriteAllBytesAsync(toxiproxyServerPath,
                                          Utils.LoadResourceAsByteArray("toxiproxy-server-windows-amd64.exe"));

            using var toxyproxyServerProcess = Process.Start(toxiproxyServerPath);

            try
            {
                //Setting up Toxiproxy proxies
                var connection = new Connection();
                var client     = connection.Client();

                var interface1Proxy = new Proxy()
                {
                    Name     = "interface1Proxy",
                    Enabled  = true,
                    Listen   = "127.0.0.1:12000",
                    Upstream = "127.0.0.1:12001"
                };

                await client.AddAsync(interface1Proxy);

                var interface2Proxy = new Proxy()
                {
                    Name     = "interface2Proxy",
                    Enabled  = true,
                    Listen   = "127.0.0.1:13000",
                    Upstream = "127.0.0.1:13001"
                };

                await client.AddAsync(interface2Proxy);

                using var serverConnection = TcpConnectionFactory.CreateRedundantServer(new[] { new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12001), new IPEndPoint(IPAddress.Parse("127.0.0.1"), 13001) });
                using var clientConnection = TcpConnectionFactory.CreateRedundantClient(new[] { new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12000), new IPEndPoint(IPAddress.Parse("127.0.0.1"), 13000) });

                using var serverConnectedEvent    = new AutoResetEvent(false);
                using var clientConnectedEvent    = new AutoResetEvent(false);
                using var errorsOnServerSideEvent = new AutoResetEvent(false);
                using var errorsOnClientSideEvent = new AutoResetEvent(false);

                int counterOfMessagesArrivedAtServer = 0;
                serverConnection.Start(
                    receivedAction: (c, data) =>
                {
                    if (BitConverter.ToInt32(data) != counterOfMessagesArrivedAtServer)
                    {
                        errorsOnServerSideEvent.Set();
                    }
                    counterOfMessagesArrivedAtServer++;
                },
                    connectionStateChangedAction: (c, fromState, toState) =>
                {
                    if (toState == ConnectionState.Connected)
                    {
                        serverConnectedEvent.Set();
                    }
                });

                int counterOfMessagesArrivedAtClient = 0;
                clientConnection.Start(
                    receivedAction: (c, data) =>
                {
                    if (BitConverter.ToInt32(data) != counterOfMessagesArrivedAtClient)
                    {
                        errorsOnClientSideEvent.Set();
                    }
                    counterOfMessagesArrivedAtClient++;
                },
                    connectionStateChangedAction: (c, fromState, toState) =>
                {
                    if (toState == ConnectionState.Connected)
                    {
                        clientConnectedEvent.Set();
                    }
                });

                WaitHandle.WaitAll(new[] { serverConnectedEvent, clientConnectedEvent }, 5000).ShouldBeTrue();

                var cancellationTokenSource = new CancellationTokenSource();
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                Task.Run(async() =>
                {
                    var counter = 0;
                    while (!cancellationTokenSource.IsCancellationRequested)
                    {
                        await clientConnection.SendDataAsync(BitConverter.GetBytes(counter));
                        await serverConnection.SendDataAsync(BitConverter.GetBytes(counter));
                        await Task.Delay(500, cancellationTokenSource.Token);
                        counter++;
                    }
                }, cancellationTokenSource.Token);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

                await Task.Delay(1000);

                interface1Proxy.Enabled = false;
                await client.UpdateAsync(interface1Proxy);

                WaitHandle.WaitAll(new[] { errorsOnServerSideEvent, errorsOnClientSideEvent }, 2000).ShouldBeFalse();

                interface1Proxy.Enabled = true;
                await client.UpdateAsync(interface1Proxy);

                interface2Proxy.Enabled = false;
                await client.UpdateAsync(interface2Proxy);

                WaitHandle.WaitAll(new[] { errorsOnServerSideEvent, errorsOnClientSideEvent }, 2000).ShouldBeFalse();

                interface2Proxy.Enabled = true;
                await client.UpdateAsync(interface2Proxy);

                var latencyProxy = new LatencyToxic()
                {
                    Name     = "latencyToxicInterface2",
                    Stream   = ToxicDirection.DownStream,
                    Toxicity = 1.0,
                };
                latencyProxy.Attributes.Jitter  = 100;
                latencyProxy.Attributes.Latency = 300;

                await interface1Proxy.AddAsync(latencyProxy);

                WaitHandle.WaitAll(new[] { errorsOnServerSideEvent, errorsOnClientSideEvent }, 2000).ShouldBeFalse();

                var slicerToxic = new SlicerToxic()
                {
                    Name     = "slicerToxicInterface1",
                    Stream   = ToxicDirection.UpStream,
                    Toxicity = 1.0,
                };
                slicerToxic.Attributes.AverageSize   = 10;
                slicerToxic.Attributes.Delay         = 5;
                slicerToxic.Attributes.SizeVariation = 1;

                await interface1Proxy.AddAsync(slicerToxic);

                WaitHandle.WaitAll(new[] { errorsOnServerSideEvent, errorsOnClientSideEvent }, 4000).ShouldBeFalse();

                interface2Proxy.Enabled = false;
                await client.UpdateAsync(interface2Proxy);

                WaitHandle.WaitAll(new[] { errorsOnServerSideEvent, errorsOnClientSideEvent }, 2000).ShouldBeFalse();

                cancellationTokenSource.Cancel();
            }
            finally
            {
                toxyproxyServerProcess.Kill();
            }
        }