Skip to content

Commit e9ed703

Browse files
Simplify toxiproxy tests
Redis client has complex retry/reconnect logic which complicates the tests
1 parent e161950 commit e9ed703

File tree

2 files changed

+65
-101
lines changed

2 files changed

+65
-101
lines changed

packages/modules/toxiproxy/package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,5 @@
3131
"dependencies": {
3232
"testcontainers": "^11.0.1",
3333
"toxiproxy-node-client": "^4.0.0"
34-
},
35-
"devDependencies": {
36-
"@testcontainers/redis": "^11.0.0",
37-
"redis": "^5.5.6"
3834
}
3935
}

packages/modules/toxiproxy/src/toxiproxy-container.test.ts

Lines changed: 65 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,82 @@
1-
import { createClient } from "redis";
21
import { GenericContainer, Network } from "testcontainers";
32
import { getImage } from "../../../testcontainers/src/utils/test-helper";
43
import { ToxiProxyContainer, TPClient } from "./toxiproxy-container";
54

65
const IMAGE = getImage(__dirname);
76

87
describe("ToxiProxyContainer", { timeout: 240_000 }, () => {
9-
// Helper to connect to redis
10-
async function connectTo(url: string) {
11-
const client = createClient({
12-
url,
13-
});
14-
client.on("error", () => {}); // Ignore errors
15-
await client.connect();
16-
expect(client.isOpen).toBeTruthy();
17-
return client;
18-
}
19-
208
// create_proxy {
21-
it("Should create a proxy to an endpoint", async () => {
22-
const containerNetwork = await new Network().start();
23-
const redisContainer = await new GenericContainer("redis:7.2")
24-
.withNetwork(containerNetwork)
25-
.withNetworkAliases("redis")
9+
it("should create a proxy to an endpoint", async () => {
10+
const network = await new Network().start();
11+
const appContainer = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
12+
.withExposedPorts(8080)
13+
.withNetwork(network)
14+
.withNetworkAliases("app")
2615
.start();
2716

28-
const toxiproxyContainer = await new ToxiProxyContainer(IMAGE).withNetwork(containerNetwork).start();
17+
const toxiproxyContainer = await new ToxiProxyContainer(IMAGE).withNetwork(network).start();
2918

30-
// Create the proxy between Toxiproxy and Redis
31-
const redisProxy = await toxiproxyContainer.createProxy({
32-
name: "redis",
33-
upstream: "redis:6379",
19+
const appProxy = await toxiproxyContainer.createProxy({
20+
name: "app",
21+
upstream: "app:8080",
3422
});
3523

36-
const url = `redis://${redisProxy.host}:${redisProxy.port}`;
37-
const client = await connectTo(url);
38-
await client.set("key", "val");
39-
expect(await client.get("key")).toBe("val");
24+
const response = await fetch(`http://${appProxy.host}:${appProxy.port}/hello-world`);
25+
expect(response.status).toBe(200);
4026

41-
await client.disconnect();
4227
await toxiproxyContainer.stop();
43-
await redisContainer.stop();
28+
await appContainer.stop();
29+
await network.stop();
4430
});
4531
// }
4632

4733
// enabled_disabled {
48-
it("Should enable and disable a proxy", async () => {
49-
const containerNetwork = await new Network().start();
50-
const redisContainer = await new GenericContainer("redis:7.2")
51-
.withNetwork(containerNetwork)
52-
.withNetworkAliases("redis")
34+
it("should enable and disable a proxy", async () => {
35+
const network = await new Network().start();
36+
const appContainer = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
37+
.withExposedPorts(8080)
38+
.withNetwork(network)
39+
.withNetworkAliases("app")
5340
.start();
5441

55-
const toxiproxyContainer = await new ToxiProxyContainer(IMAGE).withNetwork(containerNetwork).start();
42+
const toxiproxyContainer = await new ToxiProxyContainer(IMAGE).withNetwork(network).start();
5643

57-
// Create the proxy between Toxiproxy and Redis
58-
const redisProxy = await toxiproxyContainer.createProxy({
59-
name: "redis",
60-
upstream: "redis:6379",
44+
const appProxy = await toxiproxyContainer.createProxy({
45+
name: "app",
46+
upstream: "app:8080",
6147
});
6248

63-
const url = `redis://${redisProxy.host}:${redisProxy.port}`;
64-
const client = await connectTo(url);
65-
66-
await client.set("key", "val");
67-
expect(await client.get("key")).toBe("val");
68-
69-
// Disable any new connections to the proxy
70-
await redisProxy.setEnabled(false);
49+
await appProxy.setEnabled(false);
50+
await expect(fetch(`http://${appProxy.host}:${appProxy.port}/hello-world`)).rejects.toThrow();
7151

72-
await expect(client.ping()).rejects.toThrow();
52+
await appProxy.setEnabled(true);
53+
const response = await fetch(`http://${appProxy.host}:${appProxy.port}/hello-world`);
54+
expect(response.status).toBe(200);
7355

74-
// Enable the proxy again
75-
await redisProxy.setEnabled(true);
76-
77-
expect(await client.ping()).toBe("PONG");
78-
79-
await client.disconnect();
8056
await toxiproxyContainer.stop();
81-
await redisContainer.stop();
57+
await appContainer.stop();
58+
await network.stop();
8259
});
8360
// }
8461

8562
// adding_toxic {
86-
it("Should add a toxic to a proxy and then remove", async () => {
87-
const containerNetwork = await new Network().start();
88-
const redisContainer = await new GenericContainer("redis:7.2")
89-
.withNetwork(containerNetwork)
90-
.withNetworkAliases("redis")
63+
it("should add a toxic to a proxy and then remove", async () => {
64+
const network = await new Network().start();
65+
const appContainer = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
66+
.withExposedPorts(8080)
67+
.withNetwork(network)
68+
.withNetworkAliases("app")
9169
.start();
9270

93-
const toxiproxyContainer = await new ToxiProxyContainer(IMAGE).withNetwork(containerNetwork).start();
71+
const toxiproxyContainer = await new ToxiProxyContainer(IMAGE).withNetwork(network).start();
9472

95-
// Create the proxy between Toxiproxy and Redis
96-
const redisProxy = await toxiproxyContainer.createProxy({
97-
name: "redis",
98-
upstream: "redis:6379",
73+
const appProxy = await toxiproxyContainer.createProxy({
74+
name: "app",
75+
upstream: "app:8080",
9976
});
10077

101-
const url = `redis://${redisProxy.host}:${redisProxy.port}`;
102-
const client = await connectTo(url);
103-
10478
// See https://github.com/ihsw/toxiproxy-node-client for details on the instance interface
105-
const toxic = await redisProxy.instance.addToxic<TPClient.Latency>({
79+
const toxic = await appProxy.instance.addToxic<TPClient.Latency>({
10680
attributes: {
10781
jitter: 50,
10882
latency: 1500,
@@ -114,55 +88,49 @@ describe("ToxiProxyContainer", { timeout: 240_000 }, () => {
11488
});
11589

11690
const before = Date.now();
117-
await client.ping();
91+
await fetch(`http://${appProxy.host}:${appProxy.port}/hello-world`);
11892
const after = Date.now();
11993
expect(after - before).toBeGreaterThan(1000);
12094

12195
await toxic.remove();
12296

123-
await client.disconnect();
12497
await toxiproxyContainer.stop();
125-
await redisContainer.stop();
98+
await appContainer.stop();
99+
await network.stop();
126100
});
127101
// }
128102

129-
it("Should create multiple proxies", async () => {
130-
const containerNetwork = await new Network().start();
131-
const redisContainer = await new GenericContainer("redis:7.2")
132-
.withNetwork(containerNetwork)
133-
.withNetworkAliases("redis")
103+
it("should create multiple proxies", async () => {
104+
const network = await new Network().start();
105+
const appContainer = await new GenericContainer("cristianrgreco/testcontainer:1.1.14")
106+
.withExposedPorts(8080)
107+
.withNetwork(network)
108+
.withNetworkAliases("app")
134109
.start();
135110

136-
const toxiproxyContainer = await new ToxiProxyContainer(IMAGE).withNetwork(containerNetwork).start();
111+
const toxiproxyContainer = await new ToxiProxyContainer(IMAGE).withNetwork(network).start();
137112

138-
// Create the proxy between Toxiproxy and Redis
139-
const redisProxy = await toxiproxyContainer.createProxy({
140-
name: "redis",
141-
upstream: "redis:6379",
113+
const appProxy = await toxiproxyContainer.createProxy({
114+
name: "app",
115+
upstream: "app:8080",
142116
});
143-
144-
// Create the proxy between Toxiproxy and Redis
145-
const redisProxy2 = await toxiproxyContainer.createProxy({
146-
name: "redis2",
147-
upstream: "redis:6379",
117+
const appProxy2 = await toxiproxyContainer.createProxy({
118+
name: "app2",
119+
upstream: "app:8080",
148120
});
149121

150-
const url = `redis://${redisProxy.host}:${redisProxy.port}`;
151-
const client = await connectTo(url);
152-
await client.set("key", "val");
153-
expect(await client.get("key")).toBe("val");
122+
const response = await fetch(`http://${appProxy.host}:${appProxy.port}/hello-world`);
123+
expect(response.status).toBe(200);
154124

155-
const url2 = `redis://${redisProxy2.host}:${redisProxy2.port}`;
156-
const client2 = await connectTo(url2);
157-
expect(await client2.get("key")).toBe("val");
125+
const response2 = await fetch(`http://${appProxy2.host}:${appProxy2.port}/hello-world`);
126+
expect(response2.status).toBe(200);
158127

159-
await client.disconnect();
160-
await client2.disconnect();
161128
await toxiproxyContainer.stop();
162-
await redisContainer.stop();
129+
await appContainer.stop();
130+
await network.stop();
163131
});
164132

165-
it("Throws an error when too many proxies are created", async () => {
133+
it("throws an error when too many proxies are created", async () => {
166134
const toxiproxyContainer = await new ToxiProxyContainer(IMAGE).start();
167135

168136
for (let i = 0; i < 32; i++) {

0 commit comments

Comments
 (0)