Skip to content

Commit 1eb406d

Browse files
chore: wip
1 parent a9c2a6a commit 1eb406d

File tree

3 files changed

+57
-14
lines changed

3 files changed

+57
-14
lines changed

pkgx.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ dependencies:
2020
rust-lang.org: ^1.74.1
2121
sqlite.org: ^3.42.0
2222
stedolan.github.io/jq: ^1.7 # can be removed when publishing works without npm
23+
redis.io: ^7.2.5

storage/framework/core/cache/tests/Redis.test.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,66 @@
1-
import { beforeEach, describe, expect, it } from 'bun:test'
1+
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'bun:test'
22
import { redis } from '../src/drivers/redis'
3+
import { startRedisServer, stopRedisServer } from './scripts/redis-server'
4+
5+
beforeAll(async () => {
6+
await startRedisServer()
7+
})
38

49
beforeEach(async () => {
510
await redis.clear()
611
})
712

13+
afterAll(() => {
14+
stopRedisServer()
15+
})
16+
817
describe('@stacksjs/cache - Redis', () => {
918
it('should set and get a redis cache value', async () => {
1019
await redis.set('key1', 'value1')
1120
expect(await redis.get('key1')).toBe('value1')
1221
})
13-
1422
it('should set a redis cache value with no TTL and get it', async () => {
1523
await redis.setForever('key2', 'value2')
1624
expect(await redis.get('key2')).toBe('value2')
1725
})
18-
1926
it('should get or set a redis cache value if not set', async () => {
2027
expect(await redis.get('key3')).toBeUndefined()
21-
2228
await redis.getOrSet('key3', 'value3')
2329
expect(await redis.get('key3')).toBe('value3')
2430
})
25-
2631
it('should delete a redis cache value', async () => {
2732
await redis.set('key4', 'value4')
2833
await redis.del('key4')
2934
expect(await redis.get('key4')).toBeUndefined()
3035
})
31-
3236
it('should delete multiple redis cache values', async () => {
3337
await redis.set('key5', 'value5')
3438
await redis.set('key6', 'value6')
3539
await redis.deleteMany(['key5', 'key6'])
36-
3740
expect(await redis.get('key5')).toBeUndefined()
3841
expect(await redis.get('key6')).toBeUndefined()
3942
})
40-
4143
it('should check if a key exists in redis cache', async () => {
4244
await redis.set('key7', 'value7')
4345
expect(await redis.has('key7')).toBe(true)
4446
})
45-
4647
it('should return false if a key is missing in redis cache', async () => {
4748
expect(await redis.missing('nonExistentKey')).toBe(true)
4849
await redis.set('key8', 'value8')
4950
expect(await redis.missing('key8')).toBe(false)
5051
})
51-
5252
it('should clear all redis cache values', async () => {
5353
await redis.set('key9', 'value9')
5454
await redis.set('key10', 'value10')
5555
await redis.clear()
56-
5756
expect(await redis.get('key9')).toBeUndefined()
5857
expect(await redis.get('key10')).toBeUndefined()
5958
})
60-
6159
it('should remove a specific redis cache value', async () => {
6260
await redis.set('key11', 'value11')
6361
await redis.remove('key11')
64-
6562
expect(await redis.get('key11')).toBeUndefined()
6663
})
67-
6864
it('should disconnect from redis', async () => {
6965
await redis.set('key12', 'value12')
7066
await redis.disconnect()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { spawn } from 'node:child_process'
2+
3+
// Define the redisProcess variable to hold the Redis process
4+
let redisProcess: any | null = null
5+
6+
// Function to start the Redis server using Node's spawn
7+
export async function startRedisServer(): Promise<void> {
8+
return new Promise((resolve, reject) => {
9+
redisProcess = spawn('redis-server', {
10+
stdio: 'pipe',
11+
})
12+
13+
redisProcess.stdout.on('data', (data) => {
14+
console.log(`Redis server output: ${data}`)
15+
// Optionally resolve if you detect the server is ready
16+
if (data.toString().includes('Ready to accept connections')) {
17+
resolve()
18+
}
19+
})
20+
21+
redisProcess.stderr.on('data', (data) => {
22+
console.error(`Redis server error: ${data}`)
23+
reject(new Error(`Redis server error: ${data}`))
24+
})
25+
26+
redisProcess.on('exit', (code) => {
27+
console.log(`Redis server process exited with code ${code}`)
28+
if (code !== 0) {
29+
reject(new Error(`Redis server exited with code ${code}`))
30+
}
31+
})
32+
33+
console.log('Starting Redis server...')
34+
})
35+
}
36+
37+
// Function to stop Redis by killing the process
38+
export function stopRedisServer(): void {
39+
if (redisProcess) {
40+
redisProcess.kill() // Kills the Redis process
41+
console.log('Redis server process killed.')
42+
redisProcess = null // Reset the process variable
43+
} else {
44+
console.log('Redis server is not running.')
45+
}
46+
}

0 commit comments

Comments
 (0)