public void AddToxic_NonNullFields() { // Create a proxy and add the proxy to the client var client = _connection.Client(); client.Add(ProxyOne); // Retrieve the proxy var proxy = client.FindProxy("one"); var latencyToxic = new LatencyToxic(); latencyToxic.Attributes.Latency = 1000; latencyToxic.Stream = ToxicDirection.UpStream; latencyToxic.Name = "testName"; latencyToxic.Toxicity = 0.5; proxy.Add(latencyToxic); proxy.Update(); var toxics = proxy.GetAllToxics(); Assert.Equal(1, toxics.Count()); var toxic = toxics.First(); Assert.Equal(0.5, toxic.Toxicity); Assert.Equal(ToxicDirection.UpStream, toxic.Stream); //default pattern is <type>_<stream> Assert.Equal("testName", toxic.Name); }
public void CreateANewLatencyToxicShouldWork() { 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 LatencyToxic { Name = "LatencyToxicTest", Stream = ToxicDirection.UpStream }; toxic.Attributes.Jitter = 10; toxic.Attributes.Latency = 5; 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.Jitter, newToxic.Attributes.Jitter); Assert.Equal(toxic.Attributes.Latency, newToxic.Attributes.Latency); }
public void AddToxic_NullFields() { // Create a proxy and add the proxy to the client var client = _connection.Client(); client.AddAsync(ProxyOne).Wait(); // Retrieve the proxy var proxy = client.FindProxyAsync("one").Result; var latencyToxic = new LatencyToxic(); latencyToxic.Attributes.Latency = 1000; proxy.AddAsync(latencyToxic).Wait(); proxy.UpdateAsync().Wait(); var toxics = proxy.GetAllToxicsAsync().Result; Assert.True(toxics.Count() == 1); var toxic = toxics.First(); Assert.Equal(1, toxic.Toxicity); Assert.Equal(ToxicDirection.DownStream, toxic.Stream); //default pattern is <type>_<stream> Assert.Equal("latency_downstream", toxic.Name); }
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(); } }
public async Task Get_Echo_Latency_Test_Toxiproxy_Endpoint() { // var to specify 5 seconds of latency to introduce into the API call var latency = 5000; // create a new ToxiproxyClient instance var toxiproxyClient = new ToxiproxyClient(_httpClient, _toxiproxyServerUri); // add the toxiproxy proxy we will use for testing var proxy = await toxiproxyClient.AddProxyAsync(new Proxy() { Name = "GetEchoLatencyTestToxiproxyEndpoint", Listen = "127.0.0.1:8888", Upstream = "postman-echo.com:80", Enabled = true }); // create a new latency toxic instance var newToxic = new LatencyToxic() { Name = "GetEchoLatencyTestToxic", Type = LatencyToxic.ToxicType, Stream = "upstream", // TODO: need to move to enum Toxicity = 1 }; newToxic.Attributes.Latency = latency; newToxic.Attributes.Jitter = 0; // add the toxic to the toxiproxy proxy var toxic = await toxiproxyClient.AddToxicAsync <LatencyToxic>(newToxic, proxy.Name); PostmanEcho result = null; var stopWatch = new Stopwatch(); try { // set the uri to test as the toxiproxy proxy upstream endpoint _chaosApiClient.ApiUri = new Uri($"http://{proxy.Listen}/get?foo1=bar1&foo2=bar2"); // create a new instance of Module to test _module = new Module(_chaosApiClient, new Mock <ILogger <Module> >().Object); // test the GetDataAsync method and track the execution time stopWatch.Start(); result = await _module.GetDataAsync(); stopWatch.Stop(); } catch (Exception exception) { // TODO: add logging throw; } finally { // clean up the proxy we created for testing await toxiproxyClient.DeleteProxyAsync(proxy.Name); } // result should not be null Assert.IsNotNull(result); // API call should have taken longer to execute than the latency injected into toxiproxy Assert.IsTrue(stopWatch.ElapsedMilliseconds > latency); }