Toxiproxy
Toxiproxy is a proxy to simulate network and system conditions for chaos and resiliency testing. It can simulate latency, timeouts, bandwidth limits, and connection issues between services.
Add the following dependency to your project file:
| NuGet | |
|---|---|
1 | |
You can start a Toxiproxy container instance from any .NET application. This example demonstrates how to test network conditions by proxying traffic through Toxiproxy to a Redis container. The test creates both containers in the same network and configures Toxiproxy to redirect traffic from the test host to the Redis container.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | |
This example uses xUnit.net's IAsyncLifetime interface to manage the lifecycle of the containers. Both the Redis and Toxiproxy containers are started in the InitializeAsync method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the containers are removed in the DisposeAsync method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
How it works
To test network conditions with Toxiproxy, you need to configure the communication between your test host, Toxiproxy, and the service you want to test:
-
Place both containers in the same network so the Toxiproxy container and the service container (Redis in this example) can communicate with each other.
-
Configure the Toxiproxy proxy to redirect traffic from the test host to the service container. The proxy's
Listenaddress specifies where Toxiproxy listens for incoming connections, and theUpstreamaddress specifies the target service.1 2 3 4 5 6
var proxy = new Proxy(); proxy.Name = "redis"; proxy.Enabled = true; proxy.Listen = "0.0.0.0:" + redisProxyPort; proxy.Upstream = RedisNetworkAlias + ":6379"; var redisProxy = client.Add(proxy); -
Connect through Toxiproxy using its hostname and one of its proxied ports. The Toxiproxy module initializes
32ports starting from8666that can be used to configure proxies and redirect traffic. In the example, the Redis connection uses the Toxiproxy container's hostname and port instead of connecting directly to Redis.1var connectionString = new UriBuilder("redis", _toxiproxyContainer.Hostname, _toxiproxyContainer.GetMappedPublicPort(redisProxyPort)).Uri.Authority; -
Apply toxics to the proxy to simulate network conditions. For example, a latency toxic to the downstream traffic.
1 2 3 4 5 6 7
var latencyToxic = new LatencyToxic(); latencyToxic.Name = "latency_downstream"; latencyToxic.Stream = ToxicDirection.DownStream; latencyToxic.Toxicity = 1; latencyToxic.Attributes.Latency = 1100; latencyToxic.Attributes.Jitter = 100; redisProxy.Add(latencyToxic);
Tip
Toxiproxy allows you to configure the Toxicity property, which determines the probability (0.0 to 1.0) that a toxic will be applied to the connection. A value of 1.0 means the toxic is applied 100% of the time, while 0.5 would apply it to approximately 50% of requests.
The test example uses the following NuGet dependencies:
1 2 3 4 5 6 | |
To execute the tests, use the command dotnet test from a terminal.
Tip
For the complete source code of this example and additional information, please refer to our test projects.