Skip to content

Commit 9da4420

Browse files
committed
test: add connection handoff test
1 parent a4d99f5 commit 9da4420

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import diagnostics_channel from "node:diagnostics_channel";
2+
import { FaultInjectorClient } from "./fault-injector-client";
3+
import {
4+
getDatabaseConfig,
5+
getDatabaseConfigFromEnv,
6+
getEnvConfig,
7+
RedisConnectionConfig,
8+
} from "./test-scenario.util";
9+
import { createClient } from "../../..";
10+
import { DiagnosticsEvent } from "../../client/enterprise-maintenance-manager";
11+
import { before } from "mocha";
12+
import { spy } from "sinon";
13+
import assert from "node:assert";
14+
import { TestCommandRunner } from "./test-command-runner";
15+
import net from "node:net";
16+
17+
describe("Connection Handoff", () => {
18+
const diagnosticsLog: DiagnosticsEvent[] = [];
19+
20+
const onMessageHandler = (message: unknown) => {
21+
diagnosticsLog.push(message as DiagnosticsEvent);
22+
};
23+
24+
let clientConfig: RedisConnectionConfig;
25+
let client: ReturnType<typeof createClient<any, any, any, 3>>;
26+
let faultInjectorClient: FaultInjectorClient;
27+
let connectSpy = spy(net, "createConnection");
28+
29+
before(() => {
30+
const envConfig = getEnvConfig();
31+
const redisConfig = getDatabaseConfigFromEnv(
32+
envConfig.redisEndpointsConfigPath,
33+
);
34+
35+
faultInjectorClient = new FaultInjectorClient(envConfig.faultInjectorUrl);
36+
clientConfig = getDatabaseConfig(redisConfig);
37+
});
38+
39+
beforeEach(async () => {
40+
diagnosticsLog.length = 0;
41+
diagnostics_channel.subscribe("redis.maintenance", onMessageHandler);
42+
43+
connectSpy.resetHistory();
44+
45+
client = createClient({
46+
socket: {
47+
host: clientConfig.host,
48+
port: clientConfig.port,
49+
...(clientConfig.tls === true ? { tls: true } : {}),
50+
},
51+
password: clientConfig.password,
52+
username: clientConfig.username,
53+
RESP: 3,
54+
maintPushNotifications: "auto",
55+
maintMovingEndpointType: "external-ip",
56+
maintRelaxedCommandTimeout: 10000,
57+
maintRelaxedSocketTimeout: 10000,
58+
});
59+
60+
client.on("error", (err: Error) => {
61+
throw new Error(`Client error: ${err.message}`);
62+
});
63+
64+
await client.connect();
65+
await client.flushAll();
66+
});
67+
68+
afterEach(() => {
69+
diagnostics_channel.unsubscribe("redis.maintenance", onMessageHandler);
70+
client.destroy();
71+
});
72+
73+
describe("New Connection Establishment", () => {
74+
it("should establish new connection", async () => {
75+
assert.equal(connectSpy.callCount, 1);
76+
77+
const { action_id: lowTimeoutBindAndMigrateActionId } =
78+
await faultInjectorClient.migrateAndBindAction({
79+
bdbId: clientConfig.bdbId,
80+
clusterIndex: 0,
81+
});
82+
83+
const lowTimeoutWaitPromise = faultInjectorClient.waitForAction(
84+
lowTimeoutBindAndMigrateActionId,
85+
);
86+
87+
await lowTimeoutWaitPromise;
88+
assert.equal(connectSpy.callCount, 2);
89+
});
90+
});
91+
92+
describe("TLS Connection Handoff", () => {
93+
it("TODO receiveMessagesWithTLSEnabledTest", async () => {
94+
//
95+
});
96+
it("TODO connectionHandoffWithStaticInternalNameTest", async () => {
97+
//
98+
});
99+
it("TODO connectionHandoffWithStaticExternalNameTest", async () => {
100+
//
101+
});
102+
});
103+
104+
describe("Traffic Resumption", () => {
105+
it("Traffic resumed after handoff", async () => {
106+
const { action_id } = await faultInjectorClient.migrateAndBindAction({
107+
bdbId: clientConfig.bdbId,
108+
clusterIndex: 0,
109+
});
110+
111+
const workloadPromise = faultInjectorClient.waitForAction(action_id);
112+
113+
const commandPromises =
114+
await TestCommandRunner.fireCommandsUntilStopSignal(
115+
client,
116+
workloadPromise,
117+
);
118+
119+
const rejected = (
120+
await Promise.all(commandPromises.commandPromises)
121+
).filter((result) => result.status === "rejected");
122+
123+
assert.ok(rejected.length === 0);
124+
});
125+
});
126+
});

0 commit comments

Comments
 (0)