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

fix xread timeout #334

Merged
merged 2 commits into from
Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 24 additions & 39 deletions stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ export type XReadReplyRaw = XReadStreamRaw[];
* which will represent the the epoch Millis with
* seqNo of zero. (Especially useful is to pass 0.)
*/
export type XIdInput =
| XId
| [number, number]
| number;
export type XIdInput = XId | [number, number] | number;
/**
* ID input type for XADD, which is allowed to include the
* "*" operator. */
Expand Down Expand Up @@ -212,9 +209,7 @@ export interface XClaimOpts {
justXId?: boolean;
}

export function parseXMessage(
raw: XReadIdData,
): XMessage {
export function parseXMessage(raw: XReadIdData): XMessage {
const fieldValues: Record<string, string> = {};
let f: string | undefined = undefined;

Expand Down Expand Up @@ -248,11 +243,9 @@ export function convertMap(raw: ConditionalArray): Map<string, Raw> {
return fieldValues;
}

export function parseXReadReply(
raw: XReadReplyRaw,
): XReadReply {
export function parseXReadReply(raw: XReadReplyRaw): XReadReply {
const out: XReadStream[] = [];
for (const [key, idData] of raw) {
for (const [key, idData] of raw ?? []) {
const messages = [];
for (const rawMsg of idData) {
messages.push(parseXMessage(rawMsg));
Expand Down Expand Up @@ -286,18 +279,18 @@ export function parseXPendingCounts(raw: ConditionalArray): XPendingCount[] {
const infos: XPendingCount[] = [];
for (const r of raw) {
if (
isCondArray(r) && isString(r[0]) &&
isString(r[1]) && isNumber(r[2]) &&
isCondArray(r) &&
isString(r[0]) &&
isString(r[1]) &&
isNumber(r[2]) &&
isNumber(r[3])
) {
infos.push(
{
xid: parseXId(r[0]),
owner: r[1],
lastDeliveredMs: r[2],
timesDelivered: r[3],
},
);
infos.push({
xid: parseXId(r[0]),
owner: r[1],
lastDeliveredMs: r[2],
timesDelivered: r[3],
});
}
}

Expand All @@ -314,30 +307,22 @@ export function parseXGroupDetail(rawGroups: ConditionalArray): XGroupDetail[] {
// array of arrays
const consDeets = data.get("consumers") as ConditionalArray[];

out.push(
{
name: rawstr(data.get("name") ?? null),
lastDeliveredId: parseXId(
rawstr(data.get("last-delivered-id") ?? null),
),
pelCount: rawnum(data.get("pel-count") ?? null),
pending: parseXPendingCounts(
data.get("pending") as ConditionalArray,
),
consumers: parseXConsumerDetail(
consDeets,
),
},
);
out.push({
name: rawstr(data.get("name") ?? null),
lastDeliveredId: parseXId(
rawstr(data.get("last-delivered-id") ?? null),
),
pelCount: rawnum(data.get("pel-count") ?? null),
pending: parseXPendingCounts(data.get("pending") as ConditionalArray),
consumers: parseXConsumerDetail(consDeets),
});
}
}

return out;
}

export function parseXConsumerDetail(
nestedRaws: Raw[][],
): XConsumerDetail[] {
export function parseXConsumerDetail(nestedRaws: Raw[][]): XConsumerDetail[] {
const out: XConsumerDetail[] = [];

for (const raws of nestedRaws) {
Expand Down
113 changes: 55 additions & 58 deletions tests/commands/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import {
import { newClient } from "../test_util.ts";
import type { TestServer } from "../test_util.ts";

export function streamTests(
getServer: () => TestServer,
): void {
export function streamTests(getServer: () => TestServer): void {
let client!: Redis;
beforeAll(async () => {
const server = getServer();
Expand All @@ -28,7 +26,7 @@ export function streamTests(

const rnum = () => Math.floor(Math.random() * 1000);
const randomStream = () =>
`test-deno-${(new Date().getTime())}-${rnum()}${rnum()}${rnum()}`;
`test-deno-${new Date().getTime()}-${rnum()}${rnum()}${rnum()}`;

const cleanupStream = async (client: Redis, ...keys: string[]) => {
await Promise.all(keys.map((key) => client.xtrim(key, { elements: 0 })));
Expand Down Expand Up @@ -184,6 +182,14 @@ export function streamTests(
await cleanupStream(client, key, key2);
});

it("xread manage empty stream on timeout", async () => {
const key = randomStream();
const [stream] = await client.xread([{ key, xid: 0 }], {
block: 1,
});
assertEquals(stream, undefined);
});

it("xgrouphelp", async () => {
const helpText = await client.xgroupHelp();
assert(helpText.length > 4);
Expand Down Expand Up @@ -413,50 +419,47 @@ export function streamTests(
});
});

it(
"broadcast pattern, all groups read their own version of the stream",
async () => {
const key = randomStream();
const group0 = "tg0";
const group1 = "tg1";
const group2 = "tg2";
const groups = [group0, group1, group2];

for (const g of groups) {
const created = await client.xgroupCreate(key, g, "$", true);
assertEquals(created, "OK");
}
it("broadcast pattern, all groups read their own version of the stream", async () => {
const key = randomStream();
const group0 = "tg0";
const group1 = "tg1";
const group2 = "tg2";
const groups = [group0, group1, group2];

const addedIds = [];
for (const g of groups) {
const created = await client.xgroupCreate(key, g, "$", true);
assertEquals(created, "OK");
}

let msgCount = 0;
for (const group of groups) {
const payload = `data-${msgCount}`;
const a = await client.xadd(key, "*", { target: payload });
assert(a);
addedIds.push(a);
msgCount++;
const addedIds = [];

const consumer = "someconsumer";
const xid = ">";
const data = await client.xreadgroup([{ key, xid }], {
group,
consumer,
});
let msgCount = 0;
for (const group of groups) {
const payload = `data-${msgCount}`;
const a = await client.xadd(key, "*", { target: payload });
assert(a);
addedIds.push(a);
msgCount++;

// each group should see ALL the messages
// that have been emitted
const toCheck = data[0].messages;
assertEquals(toCheck.length, msgCount);
}
const consumer = "someconsumer";
const xid = ">";
const data = await client.xreadgroup([{ key, xid }], {
group,
consumer,
});

for (const g of groups) {
assertEquals(await client.xgroupDestroy(key, g), 1);
}
// each group should see ALL the messages
// that have been emitted
const toCheck = data[0].messages;
assertEquals(toCheck.length, msgCount);
}

await cleanupStream(client, key);
},
);
for (const g of groups) {
assertEquals(await client.xgroupDestroy(key, g), 1);
}

await cleanupStream(client, key);
});

it("xrange and xrevrange", async () => {
const key = randomStream();
Expand Down Expand Up @@ -528,14 +531,8 @@ export function streamTests(
);
assert(firstClaimed.kind === "messages");
assertEquals(firstClaimed.messages.length, 2);
assertEquals(
firstClaimed.messages[0].fieldValues,
{ field: "foo" },
);
assertEquals(
firstClaimed.messages[1].fieldValues,
{ field: "bar" },
);
assertEquals(firstClaimed.messages[0].fieldValues, { field: "foo" });
assertEquals(firstClaimed.messages[1].fieldValues, { field: "bar" });

// ACK these messages so we can try XPENDING/XCLAIM
// on a new batch
Expand Down Expand Up @@ -658,14 +655,14 @@ export function streamTests(
);
assert(thirdClaimed.kind === "messages");
assertEquals(thirdClaimed.messages.length, 2);
assertEquals(
thirdClaimed.messages[0].fieldValues,
{ field: "woof", farm: "chicken" },
);
assertEquals(
thirdClaimed.messages[1].fieldValues,
{ field: "bop", farm: "duck" },
);
assertEquals(thirdClaimed.messages[0].fieldValues, {
field: "woof",
farm: "chicken",
});
assertEquals(thirdClaimed.messages[1].fieldValues, {
field: "bop",
farm: "duck",
});
});
});

Expand Down