Repository navigation
fix(events): start every SSE response once its subscriptions are live #8700
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5bb98aa
fix(events): start every SSE response once its subscriptions are live
waleedlatif1 a6460c4
fix(events): hold events until the stream opens and bound the wait
waleedlatif1 5fd29f0
fix(events): keep a channel not ready while its subscribe fails or an…
waleedlatif1 a13b977
fix(events): write each stream's events in order and release a stream…
waleedlatif1 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,50 @@ | ||
| import { authMockFns, permissionsMock, permissionsMockFns } from '@sim/testing' | ||
| import { mcpPubsubMock, mcpPubsubMockFns } from '@sim/testing/mocks/mcp-pubsub.mock' | ||
| import { NextRequest } from 'next/server' | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { OPENED_COMMENT } from '@/lib/events/sse-endpoint' | ||
|
|
||
| vi.mock('@/lib/mcp/pubsub', () => mcpPubsubMock) | ||
| vi.mock('@/lib/mcp/connection-manager', () => ({ | ||
| mcpConnectionManager: { subscribe: () => () => {} }, | ||
| })) | ||
| vi.mock('@/lib/workspaces/permissions/utils', () => permissionsMock) | ||
|
|
||
| import { GET } from '@/app/api/mcp/events/route' | ||
|
|
||
| describe('MCP tool-change event stream', () => { | ||
| beforeEach(() => { | ||
| vi.useFakeTimers() | ||
| authMockFns.mockGetSession.mockResolvedValue({ user: { id: 'user-1' } }) | ||
| permissionsMockFns.mockGetUserEntityPermissions.mockResolvedValue('read') | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.useRealTimers() | ||
| }) | ||
|
|
||
| it('opens once tool-change events reach this process', async () => { | ||
| let live: () => void = () => {} | ||
| const ready = new Promise<void>((resolve) => { | ||
| live = resolve | ||
| }) | ||
| mcpPubsubMockFns.mockReady.mockReturnValue(ready) | ||
| const response = await GET(new NextRequest('http://localhost/api/mcp/events?workspaceId=ws-1')) | ||
| let first: string | undefined | ||
| if (!response.body) throw new Error('The event stream has no body') | ||
| void response.body | ||
| .getReader() | ||
| .read() | ||
| .then(({ value }) => { | ||
| first = new TextDecoder().decode(value) | ||
| }) | ||
|
|
||
| await vi.advanceTimersByTimeAsync(1_000) | ||
| expect(first).toBeUndefined() | ||
| expect(mcpPubsubMockFns.mockReady).toHaveBeenCalledTimes(2) | ||
|
|
||
| live() | ||
| await vi.advanceTimersByTimeAsync(0) | ||
| expect(first).toBe(OPENED_COMMENT) | ||
| }) | ||
| }) |
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
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
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,97 @@ | ||
| import { EventEmitter } from 'node:events' | ||
| import { redisConfigMockFns } from '@sim/testing/mocks/redis-config.mock' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { clients } = vi.hoisted(() => ({ | ||
| clients: [] as Array<EventEmitter & { subscribed: Array<(err: Error | null) => void> }>, | ||
| })) | ||
|
|
||
| vi.mock('ioredis', () => ({ | ||
| default: class extends EventEmitter { | ||
| subscribed: Array<(err: Error | null) => void> = [] | ||
|
|
||
| constructor() { | ||
| super() | ||
| clients.push(this) | ||
| } | ||
|
|
||
| subscribe(_channel: string, done: (err: Error | null) => void) { | ||
| this.subscribed.push(done) | ||
| } | ||
|
|
||
| publish = vi.fn(async () => 1) | ||
| unsubscribe = vi.fn(async () => undefined) | ||
| quit = vi.fn(async () => 'OK') | ||
| }, | ||
| })) | ||
|
|
||
| import { createPubSubChannel } from '@/lib/events/pubsub' | ||
|
|
||
| /** Whether the promise has settled by the time already-queued work runs. */ | ||
| async function settled(promise: Promise<void>): Promise<boolean> { | ||
| return Promise.race([promise.then(() => true), Promise.resolve().then(() => false)]) | ||
| } | ||
|
|
||
| /** The subscriber connection: the channel opens its publisher first. */ | ||
| function subscriber() { | ||
| const client = clients.at(-1) | ||
| if (!client) throw new Error('no Redis client') | ||
| return client | ||
| } | ||
|
|
||
| describe('createPubSubChannel over Redis', () => { | ||
| beforeEach(() => { | ||
| clients.length = 0 | ||
| redisConfigMockFns.mockGetConfiguredRedisUrl.mockReturnValue('redis://localhost:6379') | ||
| }) | ||
|
|
||
| it('is ready only once its connection has subscribed', async () => { | ||
| const channel = createPubSubChannel({ channel: 'test', label: 'Test' }) | ||
| expect(await settled(channel.ready())).toBe(false) | ||
|
|
||
| subscriber().emit('ready') | ||
| expect(await settled(channel.ready())).toBe(false) | ||
|
|
||
| subscriber().subscribed[0](null) | ||
| expect(await settled(channel.ready())).toBe(true) | ||
| }) | ||
|
|
||
| it('is not ready again until a dropped connection has resubscribed', async () => { | ||
| const channel = createPubSubChannel({ channel: 'test', label: 'Test' }) | ||
| subscriber().emit('ready') | ||
| subscriber().subscribed[0](null) | ||
|
|
||
| subscriber().emit('close') | ||
| expect(await settled(channel.ready())).toBe(false) | ||
|
|
||
| subscriber().emit('ready') | ||
| subscriber().subscribed[1](null) | ||
| expect(await settled(channel.ready())).toBe(true) | ||
| }) | ||
|
|
||
| it('is not ready while its subscribe fails, and retries on the next connection', async () => { | ||
| const channel = createPubSubChannel({ channel: 'test', label: 'Test' }) | ||
| subscriber().emit('ready') | ||
|
|
||
| subscriber().subscribed[0](new Error('NOPERM')) | ||
| expect(await settled(channel.ready())).toBe(false) | ||
|
|
||
| subscriber().emit('close') | ||
| subscriber().emit('ready') | ||
| subscriber().subscribed[1](null) | ||
| expect(await settled(channel.ready())).toBe(true) | ||
| }) | ||
|
|
||
| it('ignores a subscribe answered after its connection closed', async () => { | ||
| const channel = createPubSubChannel({ channel: 'test', label: 'Test' }) | ||
| subscriber().emit('ready') | ||
|
|
||
| subscriber().emit('close') | ||
| subscriber().subscribed[0](null) | ||
| expect(await settled(channel.ready())).toBe(false) | ||
|
|
||
| subscriber().emit('ready') | ||
| subscriber().subscribed[1](null) | ||
| expect(await settled(channel.ready())).toBe(true) | ||
| }) | ||
| }) |
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
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.
Uh oh!
There was an error while loading. Please reload this page.