|
| 1 | +import assert from "node:assert"; |
| 2 | +import { setTimeout } from "node:timers/promises"; |
| 3 | +import { FaultInjectorClient } from "./fault-injector-client"; |
| 4 | +import { |
| 5 | + getDatabaseConfig, |
| 6 | + getDatabaseConfigFromEnv, |
| 7 | + getEnvConfig, |
| 8 | + RedisConnectionConfig, |
| 9 | +} from "./test-scenario.util"; |
| 10 | +import { createClient } from "../../../dist"; |
| 11 | +import { before } from "mocha"; |
| 12 | +import { TestCommandRunner } from "./test-command-runner"; |
| 13 | + |
| 14 | +describe("Timeout Handling During Notifications", () => { |
| 15 | + let clientConfig: RedisConnectionConfig; |
| 16 | + let client: ReturnType<typeof createClient<any, any, any, 3>>; |
| 17 | + let faultInjectorClient: FaultInjectorClient; |
| 18 | + let commandRunner: TestCommandRunner; |
| 19 | + |
| 20 | + before(() => { |
| 21 | + const envConfig = getEnvConfig(); |
| 22 | + const redisConfig = getDatabaseConfigFromEnv( |
| 23 | + envConfig.redisEndpointsConfigPath |
| 24 | + ); |
| 25 | + |
| 26 | + faultInjectorClient = new FaultInjectorClient(envConfig.faultInjectorUrl); |
| 27 | + clientConfig = getDatabaseConfig(redisConfig); |
| 28 | + }); |
| 29 | + |
| 30 | + beforeEach(async () => { |
| 31 | + client = createClient({ |
| 32 | + socket: { |
| 33 | + host: clientConfig.host, |
| 34 | + port: clientConfig.port, |
| 35 | + ...(clientConfig.tls === true ? { tls: true } : {}), |
| 36 | + }, |
| 37 | + password: clientConfig.password, |
| 38 | + username: clientConfig.username, |
| 39 | + RESP: 3, |
| 40 | + maintPushNotifications: "auto", |
| 41 | + maintMovingEndpointType: "auto", |
| 42 | + }); |
| 43 | + |
| 44 | + client.on("error", (err: Error) => { |
| 45 | + throw new Error(`Client error: ${err.message}`); |
| 46 | + }); |
| 47 | + |
| 48 | + commandRunner = new TestCommandRunner(client); |
| 49 | + |
| 50 | + await client.connect(); |
| 51 | + }); |
| 52 | + |
| 53 | + afterEach(() => { |
| 54 | + client.destroy(); |
| 55 | + }); |
| 56 | + |
| 57 | + it("should relax command timeout on MOVING, MIGRATING, and MIGRATED", async () => { |
| 58 | + // PART 1 |
| 59 | + // Set very low timeout to trigger errors |
| 60 | + client.options!.maintRelaxedCommandTimeout = 50; |
| 61 | + |
| 62 | + const { action_id: lowTimeoutBindAndMigrateActionId } = |
| 63 | + await faultInjectorClient.migrateAndBindAction({ |
| 64 | + bdbId: clientConfig.bdbId, |
| 65 | + clusterIndex: 0, |
| 66 | + }); |
| 67 | + |
| 68 | + const lowTimeoutWaitPromise = faultInjectorClient.waitForAction( |
| 69 | + lowTimeoutBindAndMigrateActionId |
| 70 | + ); |
| 71 | + |
| 72 | + const lowTimeoutCommandPromises = |
| 73 | + await commandRunner.fireCommandsUntilStopSignal(lowTimeoutWaitPromise); |
| 74 | + |
| 75 | + const lowTimeoutRejectedCommands = ( |
| 76 | + await Promise.all(lowTimeoutCommandPromises.commandPromises) |
| 77 | + ).filter((result) => result.status === "rejected"); |
| 78 | + |
| 79 | + assert.ok(lowTimeoutRejectedCommands.length > 0); |
| 80 | + assert.strictEqual( |
| 81 | + lowTimeoutRejectedCommands.filter((rejected) => { |
| 82 | + return ( |
| 83 | + // TODO instanceof doesn't work for some reason |
| 84 | + rejected.error.constructor.name === |
| 85 | + "CommandTimeoutDuringMaintananceError" |
| 86 | + ); |
| 87 | + }).length, |
| 88 | + lowTimeoutRejectedCommands.length |
| 89 | + ); |
| 90 | + |
| 91 | + // PART 2 |
| 92 | + // Set high timeout to avoid errors |
| 93 | + client.options!.maintRelaxedCommandTimeout = 10000; |
| 94 | + |
| 95 | + const { action_id: highTimeoutBindAndMigrateActionId } = |
| 96 | + await faultInjectorClient.migrateAndBindAction({ |
| 97 | + bdbId: clientConfig.bdbId, |
| 98 | + clusterIndex: 0, |
| 99 | + }); |
| 100 | + |
| 101 | + const highTimeoutWaitPromise = faultInjectorClient.waitForAction( |
| 102 | + highTimeoutBindAndMigrateActionId |
| 103 | + ); |
| 104 | + |
| 105 | + const highTimeoutCommandPromises = |
| 106 | + await commandRunner.fireCommandsUntilStopSignal(highTimeoutWaitPromise); |
| 107 | + |
| 108 | + const highTimeoutRejectedCommands = ( |
| 109 | + await Promise.all(highTimeoutCommandPromises.commandPromises) |
| 110 | + ).filter((result) => result.status === "rejected"); |
| 111 | + |
| 112 | + assert.strictEqual(highTimeoutRejectedCommands.length, 0); |
| 113 | + }); |
| 114 | + |
| 115 | + // TODO this is WIP |
| 116 | + it.skip("should unrelax command timeout after MAINTENANCE", async () => { |
| 117 | + client.options!.maintRelaxedCommandTimeout = 10000; |
| 118 | + client.options!.commandOptions = { |
| 119 | + ...client.options!.commandOptions, |
| 120 | + timeout: 1, // Set very low timeout to trigger errors |
| 121 | + }; |
| 122 | + |
| 123 | + const { action_id: bindAndMigrateActionId } = |
| 124 | + await faultInjectorClient.migrateAndBindAction({ |
| 125 | + bdbId: clientConfig.bdbId, |
| 126 | + clusterIndex: 0, |
| 127 | + }); |
| 128 | + |
| 129 | + const lowTimeoutWaitPromise = faultInjectorClient.waitForAction( |
| 130 | + bindAndMigrateActionId |
| 131 | + ); |
| 132 | + |
| 133 | + const relaxedTimeoutCommandPromises = |
| 134 | + await commandRunner.fireCommandsUntilStopSignal(lowTimeoutWaitPromise); |
| 135 | + |
| 136 | + const relaxedTimeoutRejectedCommands = ( |
| 137 | + await Promise.all(relaxedTimeoutCommandPromises.commandPromises) |
| 138 | + ).filter((result) => result.status === "rejected"); |
| 139 | + console.log( |
| 140 | + "relaxedTimeoutRejectedCommands", |
| 141 | + relaxedTimeoutRejectedCommands |
| 142 | + ); |
| 143 | + |
| 144 | + assert.ok(relaxedTimeoutRejectedCommands.length === 0); |
| 145 | + |
| 146 | + const unrelaxedCommandPromises = |
| 147 | + await commandRunner.fireCommandsUntilStopSignal(setTimeout(1 * 1000)); |
| 148 | + |
| 149 | + const unrelaxedRejectedCommands = ( |
| 150 | + await Promise.all(unrelaxedCommandPromises.commandPromises) |
| 151 | + ).filter((result) => result.status === "rejected"); |
| 152 | + |
| 153 | + assert.ok(unrelaxedRejectedCommands.length > 0); |
| 154 | + }); |
| 155 | +}); |
0 commit comments