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 @@ -2346,8 +2346,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
60 changes: 60 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 @@ -8584,6 +8585,65 @@ 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: Promise<ReturnType>[] = [
client.bzpopmax(keyz, 0),
client.bzpopmin(keyz, 0),
client.blpop(keyz, 0),
client.brpop(keyz, 0),
client.xread(
{ [key1]: "0-0", [key2]: "0-0" },
{ block: 0 },
),
client.wait(42, 0),
];

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

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

try {
for (const promise of promiseList) {
const timeoutPromise = new Promise((resolve) => {
setTimeout(resolve, 500);
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
});
await Promise.race([promise, timeoutPromise]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I understand the test, how do you know which one has resolved?
If I understand, what the test does is checking if at least one of the promises has resolved.
Then you resolve all the promises, if they are blocked and never timed out, we expect it to never resolve right?
So how the test doing it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

}
} finally {
for (const promise of promiseList) {
await Promise.resolve([promise]);
}

client.close();
}
}, protocol);
},
config.timeout,
);
}

export function runCommonTests<Context>(config: {
Expand Down
Loading