-
-
Notifications
You must be signed in to change notification settings - Fork 36
Add Redis integration tests #225
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
Open
mt-ks
wants to merge
5
commits into
main
Choose a base branch
from
add-integration-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a60662d
feat: Add Docker Compose configuration and Redis integration tests
mt-ks f6ae097
chore: Update CI workflow to include integration tests and adjust Nod…
mt-ks ae43273
chore: Update test scripts in package.json and CI workflow to include…
mt-ks 70e6107
refactor: Simplify Redis integration tests by removing unnecessary co…
mt-ks c0a3621
fix: handle NOSCRIPT errors in Redis Cluster with EVAL fallback
mt-ks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| version: '3' | ||
| services: | ||
| redis: | ||
| image: redis:7 | ||
| ports: | ||
| - '6385:6379' | ||
|
|
||
| redis-cluster: | ||
| image: grokzen/redis-cluster:7.0.0 | ||
| environment: | ||
| - IP=0.0.0.0 | ||
| ports: | ||
| - '7010-7015:7000-7005' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -46,8 +46,10 @@ | |||||||||||||||||||||||||||||||||
| "format:code": "xo --fix", | ||||||||||||||||||||||||||||||||||
| "format:rest": "prettier --write .", | ||||||||||||||||||||||||||||||||||
| "format": "run-s format:*", | ||||||||||||||||||||||||||||||||||
| "test:unit": "jest test/store-test.ts", | ||||||||||||||||||||||||||||||||||
| "test:integration": "jest test/redis-integration-test.ts", | ||||||||||||||||||||||||||||||||||
| "test:lib": "jest", | ||||||||||||||||||||||||||||||||||
| "test": "run-s lint test:*", | ||||||||||||||||||||||||||||||||||
| "test": "run-s lint test:lib", | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+49
to
+52
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this could be simplified by dropping
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or, alternatively, if we want
Suggested change
|
||||||||||||||||||||||||||||||||||
| "pre-commit": "lint-staged", | ||||||||||||||||||||||||||||||||||
| "prepare": "run-s compile && husky install config/husky" | ||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| import process from 'node:process' | ||
| import { describe, it, expect, beforeAll, afterAll, jest } from '@jest/globals' | ||
| import { Redis, Cluster } from 'ioredis' | ||
| import { type Options as RateLimitOptions } from 'express-rate-limit' | ||
| import { RedisStore } from '../source/index.js' | ||
| import type { | ||
| SendCommandClusterDetails, | ||
| RedisReply, | ||
| Options as RedisOptions, | ||
| } from '../source/types.js' | ||
|
|
||
| jest.setTimeout(30_000) | ||
|
|
||
| describe('Redis Integration Tests', () => { | ||
| describe('Single Redis Instance', () => { | ||
| let client: Redis | ||
| let store: RedisStore | ||
|
|
||
| beforeAll(async () => { | ||
| const host = process.env.REDIS_HOST ?? 'localhost' | ||
| const port = Number(process.env.REDIS_PORT ?? 6385) | ||
|
|
||
| client = new Redis({ | ||
| host, | ||
| port, | ||
| lazyConnect: true, | ||
| }) | ||
| await client.connect() | ||
| }) | ||
|
|
||
| afterAll(async () => { | ||
| await client?.quit() | ||
| }) | ||
|
|
||
| it('should work with sendCommand', async () => { | ||
| store = new RedisStore({ | ||
| async sendCommand(...args: string[]) { | ||
| const result = await client.call(args[0], ...args.slice(1)) | ||
| return result as RedisReply | ||
| }, | ||
| } as RedisOptions) | ||
| store.init({ windowMs: 1000 } as RateLimitOptions) | ||
|
|
||
| const key = 'test-single' | ||
| await store.resetKey(key) | ||
|
|
||
| const result1 = await store.increment(key) | ||
| expect(result1.totalHits).toBe(1) | ||
|
|
||
| const result2 = await store.increment(key) | ||
| expect(result2.totalHits).toBe(2) | ||
|
|
||
| await store.resetKey(key) | ||
| const result3 = await store.increment(key) | ||
| expect(result3.totalHits).toBe(1) | ||
| }) | ||
|
|
||
| it('should work with decrement', async () => { | ||
| const key = 'test-single-decr' | ||
| await store.resetKey(key) | ||
|
|
||
| await store.increment(key) // 1 | ||
| await store.increment(key) // 2 | ||
| await store.decrement(key) // 1 | ||
|
|
||
| const result = await store.increment(key) // 2 | ||
| expect(result.totalHits).toBe(2) | ||
| }) | ||
|
|
||
| it('should work with get', async () => { | ||
| const key = 'test-single-get' | ||
| await store.resetKey(key) | ||
|
|
||
| await store.increment(key) | ||
| const info = await store.get(key) | ||
| expect(info).toBeDefined() | ||
| expect(info?.totalHits).toBe(1) | ||
| expect(info?.resetTime).toBeInstanceOf(Date) | ||
| }) | ||
|
|
||
| it('should handle TTL correctly', async () => { | ||
| const key = 'test-single-ttl' | ||
| // Initialize with short window | ||
| const shortStore = new RedisStore({ | ||
| async sendCommand(...args: string[]) { | ||
| const result = await client.call(args[0], ...args.slice(1)) | ||
| return result as RedisReply | ||
| }, | ||
| } as RedisOptions) | ||
| shortStore.init({ windowMs: 1000 } as RateLimitOptions) | ||
|
|
||
| await shortStore.resetKey(key) | ||
| await shortStore.increment(key) | ||
|
|
||
| // Wait for expiration (1.1s) | ||
| await new Promise<void>((resolve) => { | ||
| setTimeout(resolve, 1100) | ||
| }) | ||
|
|
||
| const result = await shortStore.increment(key) | ||
| expect(result.totalHits).toBe(1) | ||
| }) | ||
| }) | ||
|
|
||
| describe('Redis Cluster', () => { | ||
| let client: Cluster | ||
| let store: RedisStore | ||
|
|
||
| beforeAll(async () => { | ||
| const host = process.env.REDIS_CLUSTER_HOST ?? 'localhost' | ||
| const port = Number(process.env.REDIS_CLUSTER_PORT ?? 7010) | ||
|
|
||
| client = new Cluster([{ host, port }], { | ||
| redisOptions: { connectTimeout: 2000 }, | ||
| clusterRetryStrategy: () => null, | ||
| lazyConnect: true, | ||
| }) | ||
| await client.connect() | ||
| }) | ||
|
|
||
| afterAll(async () => { | ||
| await client?.quit() | ||
| }) | ||
|
|
||
| it('should work with sendCommandCluster', async () => { | ||
| store = new RedisStore({ | ||
| sendCommandCluster: async (details: SendCommandClusterDetails) => | ||
| client.call(details.command[0], ...details.command.slice(1)), | ||
| } as RedisOptions) | ||
| store.init({ windowMs: 1000 } as RateLimitOptions) | ||
|
|
||
| const key = 'test-cluster' | ||
| await store.resetKey(key) | ||
|
|
||
| const result1 = await store.increment(key) | ||
| expect(result1.totalHits).toBe(1) | ||
|
|
||
| const result2 = await store.increment(key) | ||
| expect(result2.totalHits).toBe(2) | ||
|
|
||
| await store.resetKey(key) | ||
| const result3 = await store.increment(key) | ||
| expect(result3.totalHits).toBe(1) | ||
| }) | ||
|
|
||
| it('should work with decrement', async () => { | ||
| const key = 'test-cluster-decr' | ||
| await store.resetKey(key) | ||
|
|
||
| await store.increment(key) // 1 | ||
| await store.increment(key) // 2 | ||
| await store.decrement(key) // 1 | ||
|
|
||
| const result = await store.increment(key) // 2 | ||
| expect(result.totalHits).toBe(2) | ||
| }) | ||
|
|
||
| it('should work with get', async () => { | ||
| const key = 'test-cluster-get' | ||
| await store.resetKey(key) | ||
|
|
||
| await store.increment(key) | ||
| const info = await store.get(key) | ||
| expect(info).toBeDefined() | ||
| expect(info?.totalHits).toBe(1) | ||
| expect(info?.resetTime).toBeInstanceOf(Date) | ||
| }) | ||
|
|
||
| it('should handle TTL correctly', async () => { | ||
| const key = 'test-cluster-ttl' | ||
| // Initialize with short window | ||
| const shortStore = new RedisStore({ | ||
| sendCommandCluster: async (details: SendCommandClusterDetails) => | ||
| client.call(details.command[0], ...details.command.slice(1)), | ||
| } as RedisOptions) | ||
| shortStore.init({ windowMs: 1000 } as RateLimitOptions) | ||
|
|
||
| await shortStore.resetKey(key) | ||
| await shortStore.increment(key) | ||
|
|
||
| // Wait for expiration (1.1s) | ||
| await new Promise<void>((resolve) => { | ||
| setTimeout(resolve, 1100) | ||
| }) | ||
|
|
||
| const result = await shortStore.increment(key) | ||
| expect(result.totalHits).toBe(1) | ||
| }) | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the Compose file required if we are already mentioning
servicesin the CI file?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We actually don’t need a compose file, but it is required (or recommended) for quickly deploying a Redis cluster on local.