Supported container runtimes
ToxiProxy
登录后可跨设备保存划线和私人笔记登录
Toxiproxy
Install
1npm install @testcontainers/toxiproxy --save-devExamples
These examples use the following libraries:
-
1npm install toxiproxy-node-client
Choose an image from the container registry and substitute IMAGE.
Create a proxy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16await using network = await new Network().start();
await using _ = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withExposedPorts(8080)
.withNetwork(network)
.withNetworkAliases("app")
.start();
await using toxiproxyContainer = await new ToxiProxyContainer(IMAGE).withNetwork(network).start();
const appProxy = await toxiproxyContainer.createProxy({
name: "app",
upstream: "app:8080",
});
const response = await fetch(`http://${appProxy.host}:${appProxy.port}/hello-world`);
expect(response.status).toBe(200);Add a toxic
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
31await using network = await new Network().start();
await using _ = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withExposedPorts(8080)
.withNetwork(network)
.withNetworkAliases("app")
.start();
await using toxiproxyContainer = await new ToxiProxyContainer(IMAGE).withNetwork(network).start();
const appProxy = await toxiproxyContainer.createProxy({
name: "app",
upstream: "app:8080",
});
const toxic = await appProxy.instance.addToxic<TPClient.Latency>({
attributes: {
jitter: 50,
latency: 1500,
},
name: "upstream-latency",
stream: "upstream",
toxicity: 1, // 1 is 100%
type: "latency",
});
const before = Date.now();
await fetch(`http://${appProxy.host}:${appProxy.port}/hello-world`);
const after = Date.now();
expect(after - before).toBeGreaterThan(1000);
await toxic.remove();Enable/disable the proxy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20await using network = await new Network().start();
await using _ = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
.withExposedPorts(8080)
.withNetwork(network)
.withNetworkAliases("app")
.start();
await using toxiproxyContainer = await new ToxiProxyContainer(IMAGE).withNetwork(network).start();
const appProxy = await toxiproxyContainer.createProxy({
name: "app",
upstream: "app:8080",
});
await appProxy.setEnabled(false);
await expect(fetch(`http://${appProxy.host}:${appProxy.port}/hello-world`)).rejects.toThrow();
await appProxy.setEnabled(true);
const response = await fetch(`http://${appProxy.host}:${appProxy.port}/hello-world`);
expect(response.status).toBe(200);评论
登录后参与评论
正在加载评论…
KnowForge