@@ -7,50 +7,83 @@ import { test } from "node:test";
77const redisUrl = process . env . REDIS_URL ;
88const 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
1747test ( "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
2859test ( "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
3971test ( "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
5184test ( "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
75108test ( "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
95128test ( "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