Skip to content

Commit cbb0197

Browse files
committed
Avoid flushdb in Redis KV tests
Replace the Redis KV test cleanup logic with prefix-scoped key removal instead of flushing the whole database. This prevents the KV tests from deleting data that belongs to the Redis message queue tests when Node runs test files in parallel, which was causing flaky timeouts in CI.
1 parent 172af18 commit cbb0197

File tree

1 file changed

+44
-11
lines changed

1 file changed

+44
-11
lines changed

packages/redis/src/kv.test.ts

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,83 @@ import { test } from "node:test";
77
const redisUrl = process.env.REDIS_URL;
88
const skip = redisUrl == null;
99

10-
function getRedis(): { redis: Redis; keyPrefix: string; store: RedisKvStore } {
10+
async function cleanupPrefixedKeys(
11+
redis: Redis,
12+
keyPrefix: string,
13+
): Promise<void> {
14+
let cursor = "0";
15+
do {
16+
const [nextCursor, keys] = await redis.scan(
17+
cursor,
18+
"MATCH",
19+
`${keyPrefix}*`,
20+
"COUNT",
21+
"100",
22+
);
23+
cursor = nextCursor;
24+
if (keys.length > 0) {
25+
await redis.del(...keys);
26+
}
27+
} while (cursor !== "0");
28+
}
29+
30+
function getRedis(): {
31+
redis: Redis;
32+
keyPrefix: string;
33+
store: RedisKvStore;
34+
cleanup: () => Promise<void>;
35+
} {
1136
const redis = new Redis(redisUrl!);
1237
const keyPrefix = `fedify_test_${crypto.randomUUID()}::`;
1338
const store = new RedisKvStore(redis, { keyPrefix });
14-
return { redis, keyPrefix, store };
39+
return {
40+
redis,
41+
keyPrefix,
42+
store,
43+
cleanup: () => cleanupPrefixedKeys(redis, keyPrefix),
44+
};
1545
}
1646

1747
test("RedisKvStore.get()", { skip }, async () => {
1848
if (skip) return; // see https://github.com/oven-sh/bun/issues/19412
19-
const { redis, keyPrefix, store } = getRedis();
49+
const { redis, keyPrefix, store, cleanup } = getRedis();
2050
try {
2151
await redis.set(`${keyPrefix}foo::bar`, '"foobar"');
2252
assert.strictEqual(await store.get(["foo", "bar"]), "foobar");
2353
} finally {
54+
await cleanup();
2455
redis.disconnect();
2556
}
2657
});
2758

2859
test("RedisKvStore.set()", { skip }, async () => {
2960
if (skip) return; // see https://github.com/oven-sh/bun/issues/19412
30-
const { redis, keyPrefix, store } = getRedis();
61+
const { redis, keyPrefix, store, cleanup } = getRedis();
3162
try {
3263
await store.set(["foo", "baz"], "baz");
3364
assert.strictEqual(await redis.get(`${keyPrefix}foo::baz`), '"baz"');
3465
} finally {
66+
await cleanup();
3567
redis.disconnect();
3668
}
3769
});
3870

3971
test("RedisKvStore.delete()", { skip }, async () => {
4072
if (skip) return; // see https://github.com/oven-sh/bun/issues/19412
41-
const { redis, keyPrefix, store } = getRedis();
73+
const { redis, keyPrefix, store, cleanup } = getRedis();
4274
try {
4375
await redis.set(`${keyPrefix}foo::baz`, '"baz"');
4476
await store.delete(["foo", "baz"]);
4577
assert.equal(await redis.exists(`${keyPrefix}foo::baz`), 0);
4678
} finally {
79+
await cleanup();
4780
redis.disconnect();
4881
}
4982
});
5083

5184
test("RedisKvStore.list()", { skip }, async () => {
5285
if (skip) return; // see https://github.com/oven-sh/bun/issues/19412
53-
const { redis, store } = getRedis();
86+
const { redis, store, cleanup } = getRedis();
5487
try {
5588
await store.set(["prefix", "a"], "value-a");
5689
await store.set(["prefix", "b"], "value-b");
@@ -67,14 +100,14 @@ test("RedisKvStore.list()", { skip }, async () => {
67100
assert(entries.some((e) => e.key[1] === "b"));
68101
assert(entries.some((e) => e.key[1] === "nested"));
69102
} finally {
70-
await redis.flushdb();
103+
await cleanup();
71104
redis.disconnect();
72105
}
73106
});
74107

75108
test("RedisKvStore.list() - single element key", { skip }, async () => {
76109
if (skip) return; // see https://github.com/oven-sh/bun/issues/19412
77-
const { redis, store } = getRedis();
110+
const { redis, store, cleanup } = getRedis();
78111
try {
79112
await store.set(["a"], "value-a");
80113
await store.set(["b"], "value-b");
@@ -87,14 +120,14 @@ test("RedisKvStore.list() - single element key", { skip }, async () => {
87120
assert.strictEqual(entries.length, 1);
88121
assert.strictEqual(entries[0].value, "value-a");
89122
} finally {
90-
await redis.flushdb();
123+
await cleanup();
91124
redis.disconnect();
92125
}
93126
});
94127

95128
test("RedisKvStore.list() - empty prefix", { skip }, async () => {
96129
if (skip) return; // see https://github.com/oven-sh/bun/issues/19412
97-
const { redis, store } = getRedis();
130+
const { redis, store, cleanup } = getRedis();
98131
try {
99132
await store.set(["a"], "value-a");
100133
await store.set(["b", "c"], "value-bc");
@@ -107,7 +140,7 @@ test("RedisKvStore.list() - empty prefix", { skip }, async () => {
107140

108141
assert.strictEqual(entries.length, 3);
109142
} finally {
110-
await redis.flushdb();
143+
await cleanup();
111144
redis.disconnect();
112145
}
113146
});

0 commit comments

Comments
 (0)