-
-
Notifications
You must be signed in to change notification settings - Fork 187
feat: 增加 Redis Pub/Sub 缓存失效模块与单测 #494
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
Closed
+507
−0
Closed
Changes from all commits
Commits
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,369 @@ | ||
| import { EventEmitter } from "node:events"; | ||
| import { beforeEach, describe, expect, test, vi } from "vitest"; | ||
|
|
||
| class MockRedis extends EventEmitter { | ||
| publish = vi.fn(); | ||
| subscribe = vi.fn(); | ||
| unsubscribe = vi.fn(); | ||
| quit = vi.fn(); | ||
| duplicate = vi.fn(); | ||
| } | ||
|
|
||
| vi.mock("@/lib/logger", () => ({ | ||
| logger: { | ||
| debug: vi.fn(), | ||
| info: vi.fn(), | ||
| warn: vi.fn(), | ||
| error: vi.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock("@/lib/redis/client", () => ({ | ||
| getRedisClient: vi.fn(), | ||
| })); | ||
|
|
||
| async function mockRedisClient(value: MockRedis | null) { | ||
| const { getRedisClient } = await import("@/lib/redis/client"); | ||
| (getRedisClient as unknown as ReturnType<typeof vi.fn>).mockReturnValue(value); | ||
| } | ||
|
|
||
| describe("Redis Pub/Sub 缓存失效通知", () => { | ||
| beforeEach(() => { | ||
| vi.resetModules(); | ||
| }); | ||
|
|
||
| test("publishCacheInvalidation: should publish message to channel", async () => { | ||
| const base = new MockRedis(); | ||
| base.publish.mockResolvedValue(1); | ||
|
|
||
| await mockRedisClient(base); | ||
|
|
||
| const { publishCacheInvalidation } = await import("@/lib/redis/pubsub"); | ||
| await publishCacheInvalidation("test-channel"); | ||
|
|
||
| expect(base.publish).toHaveBeenCalledTimes(1); | ||
| const [channel, message] = base.publish.mock.calls[0] as [unknown, unknown]; | ||
| expect(channel).toBe("test-channel"); | ||
| expect(typeof message).toBe("string"); | ||
| expect((message as string).length).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| test("publishCacheInvalidation: should handle Redis not available gracefully", async () => { | ||
| await mockRedisClient(null); | ||
|
|
||
| const { publishCacheInvalidation } = await import("@/lib/redis/pubsub"); | ||
| await expect(publishCacheInvalidation("test-channel")).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| test("publishCacheInvalidation: should swallow publish errors and log warn", async () => { | ||
| const base = new MockRedis(); | ||
| const publishError = new Error("network error"); | ||
| base.publish.mockRejectedValue(publishError); | ||
|
|
||
| await mockRedisClient(base); | ||
|
|
||
| const { logger } = await import("@/lib/logger"); | ||
| const { publishCacheInvalidation } = await import("@/lib/redis/pubsub"); | ||
|
|
||
| await expect(publishCacheInvalidation("test-channel")).resolves.toBeUndefined(); | ||
|
|
||
| expect(base.publish).toHaveBeenCalledTimes(1); | ||
| expect(logger.warn).toHaveBeenCalledTimes(1); | ||
| expect(logger.warn).toHaveBeenCalledWith( | ||
| "[RedisPubSub] Failed to publish cache invalidation", | ||
| expect.objectContaining({ | ||
| channel: "test-channel", | ||
| error: publishError, | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| test("subscribeCacheInvalidation: should register callback and receive messages", async () => { | ||
| const base = new MockRedis(); | ||
| const subscriber = new MockRedis(); | ||
| base.duplicate.mockReturnValue(subscriber); | ||
| subscriber.subscribe.mockResolvedValue(1); | ||
| subscriber.unsubscribe.mockResolvedValue(1); | ||
|
|
||
| await mockRedisClient(base); | ||
|
|
||
| const { subscribeCacheInvalidation } = await import("@/lib/redis/pubsub"); | ||
| const onInvalidate = vi.fn(); | ||
|
|
||
| const cleanup = await subscribeCacheInvalidation("test-channel", onInvalidate); | ||
| expect(typeof cleanup).toBe("function"); | ||
|
|
||
| expect(base.duplicate).toHaveBeenCalledTimes(1); | ||
| expect(subscriber.subscribe).toHaveBeenCalledWith("test-channel"); | ||
|
|
||
| subscriber.emit("message", "test-channel", Date.now().toString()); | ||
| expect(onInvalidate).toHaveBeenCalledTimes(1); | ||
|
|
||
| cleanup(); | ||
| expect(subscriber.unsubscribe).toHaveBeenCalledTimes(1); | ||
| expect(subscriber.unsubscribe).toHaveBeenCalledWith("test-channel"); | ||
| subscriber.emit("message", "test-channel", Date.now().toString()); | ||
| expect(onInvalidate).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test("subscribeCacheInvalidation: should handle Redis not configured gracefully", async () => { | ||
| await mockRedisClient(null); | ||
|
|
||
| const { subscribeCacheInvalidation } = await import("@/lib/redis/pubsub"); | ||
| const cleanup = await subscribeCacheInvalidation("test-channel", vi.fn()); | ||
|
|
||
| expect(typeof cleanup).toBe("function"); | ||
| expect(() => cleanup()).not.toThrow(); | ||
| }); | ||
|
|
||
| test("subscribeCacheInvalidation: should subscribe once per channel and unsubscribe on last cleanup", async () => { | ||
| const base = new MockRedis(); | ||
| const subscriber = new MockRedis(); | ||
| base.duplicate.mockReturnValue(subscriber); | ||
| subscriber.subscribe.mockResolvedValue(1); | ||
| subscriber.unsubscribe.mockResolvedValue(1); | ||
|
|
||
| await mockRedisClient(base); | ||
|
|
||
| const { subscribeCacheInvalidation } = await import("@/lib/redis/pubsub"); | ||
| const cb1 = vi.fn(); | ||
| const cb2 = vi.fn(); | ||
|
|
||
| const cleanup1 = await subscribeCacheInvalidation("test-channel", cb1); | ||
| const cleanup2 = await subscribeCacheInvalidation("test-channel", cb2); | ||
|
|
||
| expect(base.duplicate).toHaveBeenCalledTimes(1); | ||
| expect(subscriber.subscribe).toHaveBeenCalledTimes(1); | ||
| expect(subscriber.subscribe).toHaveBeenCalledWith("test-channel"); | ||
|
|
||
| subscriber.emit("message", "test-channel", "msg"); | ||
| expect(cb1).toHaveBeenCalledTimes(1); | ||
| expect(cb2).toHaveBeenCalledTimes(1); | ||
|
|
||
| cleanup1(); | ||
| expect(subscriber.unsubscribe).not.toHaveBeenCalled(); | ||
|
|
||
| subscriber.emit("message", "test-channel", "msg"); | ||
| expect(cb1).toHaveBeenCalledTimes(1); | ||
| expect(cb2).toHaveBeenCalledTimes(2); | ||
|
|
||
| cleanup2(); | ||
| expect(subscriber.unsubscribe).toHaveBeenCalledTimes(1); | ||
| expect(subscriber.unsubscribe).toHaveBeenCalledWith("test-channel"); | ||
|
|
||
| subscriber.emit("message", "test-channel", "msg"); | ||
| expect(cb1).toHaveBeenCalledTimes(1); | ||
| expect(cb2).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| test("subscribeCacheInvalidation: should catch callback errors and continue", async () => { | ||
| const base = new MockRedis(); | ||
| const subscriber = new MockRedis(); | ||
| base.duplicate.mockReturnValue(subscriber); | ||
| subscriber.subscribe.mockResolvedValue(1); | ||
| subscriber.unsubscribe.mockResolvedValue(1); | ||
|
|
||
| await mockRedisClient(base); | ||
|
|
||
| const { logger } = await import("@/lib/logger"); | ||
| const { subscribeCacheInvalidation } = await import("@/lib/redis/pubsub"); | ||
|
|
||
| const badCb = vi.fn(() => { | ||
| throw new Error("callback error"); | ||
| }); | ||
| const goodCb = vi.fn(); | ||
|
|
||
| const cleanupBad = await subscribeCacheInvalidation("test-channel", badCb); | ||
| const cleanupGood = await subscribeCacheInvalidation("test-channel", goodCb); | ||
|
|
||
| subscriber.emit("message", "test-channel", "msg"); | ||
|
|
||
| expect(badCb).toHaveBeenCalledTimes(1); | ||
| expect(goodCb).toHaveBeenCalledTimes(1); | ||
| expect(logger.error).toHaveBeenCalledTimes(1); | ||
| expect(logger.error).toHaveBeenCalledWith( | ||
| "[RedisPubSub] Callback error", | ||
| expect.objectContaining({ | ||
| channel: "test-channel", | ||
| error: expect.any(Error), | ||
| }) | ||
| ); | ||
|
|
||
| cleanupBad(); | ||
| cleanupGood(); | ||
| }); | ||
|
|
||
| test("subscribeCacheInvalidation: should log warn on subscriber connection error event", async () => { | ||
| const base = new MockRedis(); | ||
| const subscriber = new MockRedis(); | ||
| base.duplicate.mockReturnValue(subscriber); | ||
| subscriber.subscribe.mockResolvedValue(1); | ||
|
|
||
| await mockRedisClient(base); | ||
|
|
||
| const { logger } = await import("@/lib/logger"); | ||
| const { subscribeCacheInvalidation } = await import("@/lib/redis/pubsub"); | ||
|
|
||
| await subscribeCacheInvalidation("test-channel", vi.fn()); | ||
|
|
||
| const error = new Error("subscriber network error"); | ||
| subscriber.emit("error", error); | ||
|
|
||
| expect(logger.warn).toHaveBeenCalledTimes(1); | ||
| expect(logger.warn).toHaveBeenCalledWith( | ||
| "[RedisPubSub] Subscriber connection error", | ||
| expect.objectContaining({ error }) | ||
| ); | ||
| }); | ||
|
|
||
| test("subscribeCacheInvalidation: should handle duplicate errors gracefully", async () => { | ||
| const base = new MockRedis(); | ||
| const duplicateError = new Error("duplicate error"); | ||
| base.duplicate.mockImplementation(() => { | ||
| throw duplicateError; | ||
| }); | ||
|
|
||
| await mockRedisClient(base); | ||
|
|
||
| const { logger } = await import("@/lib/logger"); | ||
| const { subscribeCacheInvalidation } = await import("@/lib/redis/pubsub"); | ||
|
|
||
| const cleanup = await subscribeCacheInvalidation("test-channel", vi.fn()); | ||
| expect(typeof cleanup).toBe("function"); | ||
| expect(() => cleanup()).not.toThrow(); | ||
|
|
||
| expect(logger.warn).toHaveBeenCalledTimes(1); | ||
| expect(logger.warn).toHaveBeenCalledWith( | ||
| "[RedisPubSub] Failed to subscribe cache invalidation", | ||
| expect.objectContaining({ | ||
| channel: "test-channel", | ||
| error: duplicateError, | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| test("subscribeCacheInvalidation: should not leak state when subscribe fails (allow retry)", async () => { | ||
| const base = new MockRedis(); | ||
| const subscriber = new MockRedis(); | ||
| base.duplicate.mockReturnValue(subscriber); | ||
|
|
||
| const subscribeError = new Error("subscribe error"); | ||
| subscriber.subscribe.mockRejectedValueOnce(subscribeError).mockResolvedValueOnce(1); | ||
| subscriber.unsubscribe.mockResolvedValue(1); | ||
|
|
||
| await mockRedisClient(base); | ||
|
|
||
| const { logger } = await import("@/lib/logger"); | ||
| const { subscribeCacheInvalidation } = await import("@/lib/redis/pubsub"); | ||
|
|
||
| const cb1 = vi.fn(); | ||
| const cleanup1 = await subscribeCacheInvalidation("test-channel", cb1); | ||
| expect(typeof cleanup1).toBe("function"); | ||
|
|
||
| expect(base.duplicate).toHaveBeenCalledTimes(1); | ||
| expect(subscriber.subscribe).toHaveBeenCalledTimes(1); | ||
| expect(logger.warn).toHaveBeenCalledTimes(1); | ||
| expect(logger.warn).toHaveBeenCalledWith( | ||
| "[RedisPubSub] Failed to subscribe cache invalidation", | ||
| expect.objectContaining({ | ||
| channel: "test-channel", | ||
| error: subscribeError, | ||
| }) | ||
| ); | ||
|
|
||
| const cb2 = vi.fn(); | ||
| const cleanup2 = await subscribeCacheInvalidation("test-channel", cb2); | ||
|
|
||
| expect(base.duplicate).toHaveBeenCalledTimes(1); | ||
| expect(subscriber.subscribe).toHaveBeenCalledTimes(2); | ||
|
|
||
| subscriber.emit("message", "test-channel", "msg"); | ||
| expect(cb1).toHaveBeenCalledTimes(0); | ||
| expect(cb2).toHaveBeenCalledTimes(1); | ||
|
|
||
| cleanup2(); | ||
| expect(subscriber.unsubscribe).toHaveBeenCalledWith("test-channel"); | ||
| }); | ||
|
|
||
| test("subscribeCacheInvalidation: should swallow unsubscribe errors and log warn", async () => { | ||
| const base = new MockRedis(); | ||
| const subscriber = new MockRedis(); | ||
| base.duplicate.mockReturnValue(subscriber); | ||
| subscriber.subscribe.mockResolvedValue(1); | ||
|
|
||
| const unsubscribeError = new Error("unsubscribe error"); | ||
| subscriber.unsubscribe.mockRejectedValueOnce(unsubscribeError); | ||
|
|
||
| await mockRedisClient(base); | ||
|
|
||
| const { logger } = await import("@/lib/logger"); | ||
| const { subscribeCacheInvalidation } = await import("@/lib/redis/pubsub"); | ||
|
|
||
| const cleanup = await subscribeCacheInvalidation("test-channel", vi.fn()); | ||
| expect(() => cleanup()).not.toThrow(); | ||
|
|
||
| // 等待 microtask queue 清空,确保 Promise.resolve().then().catch() 链完成 | ||
| await new Promise((resolve) => setTimeout(resolve, 10)); | ||
|
|
||
| expect(logger.warn).toHaveBeenCalledWith( | ||
| "[RedisPubSub] Failed to unsubscribe cache invalidation", | ||
| expect.objectContaining({ | ||
| channel: "test-channel", | ||
| error: unsubscribeError, | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| test("closeSubscriber: should quit subscriber and clear subscriptions", async () => { | ||
| const base = new MockRedis(); | ||
| const subscriber1 = new MockRedis(); | ||
| const subscriber2 = new MockRedis(); | ||
| base.duplicate.mockReturnValueOnce(subscriber1).mockReturnValueOnce(subscriber2); | ||
| subscriber1.subscribe.mockResolvedValue(1); | ||
| subscriber2.subscribe.mockResolvedValue(1); | ||
|
|
||
| await mockRedisClient(base); | ||
|
|
||
| const { closeSubscriber, subscribeCacheInvalidation } = await import("@/lib/redis/pubsub"); | ||
|
|
||
| const cb1 = vi.fn(); | ||
| await subscribeCacheInvalidation("test-channel", cb1); | ||
| expect(base.duplicate).toHaveBeenCalledTimes(1); | ||
|
|
||
| await closeSubscriber(); | ||
| expect(subscriber1.quit).toHaveBeenCalledTimes(1); | ||
|
|
||
| subscriber1.emit("message", "test-channel", "msg"); | ||
| expect(cb1).toHaveBeenCalledTimes(0); | ||
|
|
||
| const cb2 = vi.fn(); | ||
| await subscribeCacheInvalidation("test-channel", cb2); | ||
| expect(base.duplicate).toHaveBeenCalledTimes(2); | ||
|
|
||
| subscriber2.emit("message", "test-channel", "msg"); | ||
| expect(cb2).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test("closeSubscriber: should swallow quit errors and log warn", async () => { | ||
| const base = new MockRedis(); | ||
| const subscriber = new MockRedis(); | ||
| base.duplicate.mockReturnValue(subscriber); | ||
| subscriber.subscribe.mockResolvedValue(1); | ||
|
|
||
| const quitError = new Error("quit error"); | ||
| subscriber.quit.mockRejectedValue(quitError); | ||
|
|
||
| await mockRedisClient(base); | ||
|
|
||
| const { logger } = await import("@/lib/logger"); | ||
| const { closeSubscriber, subscribeCacheInvalidation } = await import("@/lib/redis/pubsub"); | ||
|
|
||
| await subscribeCacheInvalidation("test-channel", vi.fn()); | ||
| await expect(closeSubscriber()).resolves.toBeUndefined(); | ||
|
|
||
| expect(logger.warn).toHaveBeenCalledWith( | ||
| "[RedisPubSub] Failed to close subscriber", | ||
| expect.objectContaining({ error: quitError }) | ||
| ); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
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.
在测试中使用
setTimeout加一个固定的延迟(如 10ms)来等待异步操作完成,这种做法比较脆弱,可能会导致测试不稳定(flaky)。测试的执行时间可能因环境而异。为了使测试更健壮,建议使用
setImmediate来等待事件循环的下一个 tick。这可以确保所有已排队的 microtask 都已执行完毕,而无需依赖任意的等待时间。