Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node: Update tests for blocking commands. #2127

Merged
merged 8 commits into from
Aug 22, 2024
4 changes: 2 additions & 2 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2356,8 +2356,8 @@ export enum FlushMode {
export type StreamReadOptions = {
/**
* If set, the read request will block for the set amount of milliseconds or
* until the server has the required number of entries. Equivalent to `BLOCK`
* in the Redis API.
* until the server has the required number of entries. A value of `0` will block indefinitely.
* Equivalent to `BLOCK` in the Redis API.
*/
block?: number;
/**
Expand Down
43 changes: 0 additions & 43 deletions node/tests/GlideClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { v4 as uuidv4 } from "uuid";
import {
Decoder,
GlideClient,
ListDirection,
ProtocolVersion,
RequestError,
Transaction,
Expand Down Expand Up @@ -132,48 +131,6 @@ describe("GlideClient", () => {
},
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
"check that blocking commands returns never timeout_%p",
async (protocol) => {
client = await GlideClient.createClient(
getClientConfigurationOption(
cluster.getAddresses(),
protocol,
300,
),
);

const promiseList = [
client.blmove(
"source",
"destination",
ListDirection.LEFT,
ListDirection.LEFT,
0.1,
),
client.blmpop(["key1", "key2"], ListDirection.LEFT, 0.1),
client.bzpopmax(["key1", "key2"], 0),
client.bzpopmin(["key1", "key2"], 0),
];

try {
for (const promise of promiseList) {
const timeoutPromise = new Promise((resolve) => {
setTimeout(resolve, 500);
});
await Promise.race([promise, timeoutPromise]);
}
} finally {
for (const promise of promiseList) {
await Promise.resolve([promise]);
}

client.close();
}
},
5000,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
"select dbsize flushdb test %p",
async (protocol) => {
Expand Down
69 changes: 69 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
ListDirection,
ProtocolVersion,
RequestError,
ReturnType,
ScoreFilter,
Script,
SignedEncoding,
Expand Down Expand Up @@ -8852,6 +8853,74 @@ export function runBaseTests<Context>(config: {
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
"check that blocking commands never time out %p",
async (protocol) => {
await runTest(async (client: BaseClient, cluster) => {
const key1 = "{blocking}-1-" + uuidv4();
const key2 = "{blocking}-1-" + uuidv4();
const keyz = [key1, key2];

// TODO: xreadgroup
const promiseList: [string, Promise<ReturnType>][] = [
["bzpopmax", client.bzpopmax(keyz, 0)],
["bzpopmin", client.bzpopmin(keyz, 0)],
["blpop", client.blpop(keyz, 0)],
["brpop", client.brpop(keyz, 0)],
[
"xread",
client.xread(
{ [key1]: "0-0", [key2]: "0-0" },
{ block: 0 },
),
],
["wait", client.wait(42, 0)],
];

if (!cluster.checkIfServerVersionLessThan("6.2.0")) {
promiseList.push([
"blmove",
client.blmove(
key1,
key2,
ListDirection.LEFT,
ListDirection.LEFT,
0,
),
]);
}

if (!cluster.checkIfServerVersionLessThan("7.0.0")) {
promiseList.push(
["blmpop", client.blmpop(keyz, ListDirection.LEFT, 0)],
["bzmpop", client.bzmpop(keyz, ScoreFilter.MAX, 0)],
);
}

try {
for (const [name, promise] of promiseList) {
const timeoutPromise = new Promise((resolve) => {
setTimeout(resolve, 500, "timeOutPromiseWins");
});
// client has default request timeout 250 ms, we run all commands with infinite blocking
// we expect that all commands will still await for the response even after 500 ms
expect(
await Promise.race([
promise.finally(() =>
fail(`${name} didn't block infintely`),
),
timeoutPromise,
]),
).toEqual("timeOutPromiseWins");
}
} finally {
client.close();
}
}, protocol);
},
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`getex test_%p`,
async (protocol) => {
Expand Down
Loading