Repository navigation
fix(messages): a configured Postgres is the only message write path (TASK-503) #2989
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
6 commits
Select commit
Hold shift + click to select a range
05a7516
fix(messages): a configured Postgres is the only message write path (…
lilyshen0722 804b286
fix(messages): map an agent-post store outage to 503; give e2e suites…
lilyshen0722 2ba6ae4
test(clawdbot-e2e): clear PG messages between tests (TASK-503)
lilyshen0722 c3c5c50
fix(messages): the HTTP send follows PG_HOST like the socket and agen…
lilyshen0722 973009b
test(messages): the create-route test opts into PG_HOST (TASK-503)
lilyshen0722 b08aee5
fix(chat): a refused socket send reaches the sender without breaking …
lilyshen0722 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
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
87 changes: 87 additions & 0 deletions
87
backend/__tests__/unit/services/socketMessageWrite.test.js
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,87 @@ | ||
| /** | ||
| * TASK-503: with PostgreSQL configured, the socket send writes PG only. A PG | ||
| * that is still booting or a failed insert is a retryable error, never a | ||
| * MongoDB row (those rows carried no pgFallback flag and were never moved). | ||
| */ | ||
|
|
||
| jest.mock('../../../models/pg/Message', () => ({ create: jest.fn(), findById: jest.fn() })); | ||
| jest.mock('../../../models/pg/Pod', () => ({ findById: jest.fn() })); | ||
| jest.mock('../../../services/pgPodSyncService', () => ({ syncPodFromMongo: jest.fn() })); | ||
| jest.mock('../../../models/Message', () => jest.fn()); | ||
|
|
||
| const PGMessage = require('../../../models/pg/Message'); | ||
| const PGPod = require('../../../models/pg/Pod'); | ||
| const { syncPodFromMongo } = require('../../../services/pgPodSyncService'); | ||
| const MongoMessage = require('../../../models/Message'); | ||
| const { writeSocketMessage, MESSAGE_STORE_UNAVAILABLE } = require('../../../services/socketMessageWrite'); | ||
|
|
||
| // The refusal names the send so the client can drop its optimistic copy. | ||
| const REFUSED = { | ||
| ok: false, | ||
| error: { | ||
| ...MESSAGE_STORE_UNAVAILABLE, podId: 'p1', content: 'hi', messageType: 'text', | ||
| }, | ||
| }; | ||
|
|
||
| const input = (over = {}) => ({ | ||
| pgReady: true, podId: 'p1', userId: 'u1', content: 'hi', messageType: 'text', replyToMessageId: null, ...over, | ||
| }); | ||
|
|
||
| describe('writeSocketMessage', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
| jest.spyOn(console, 'warn').mockImplementation(() => {}); | ||
| PGPod.findById.mockResolvedValue({ id: 'p1' }); | ||
| }); | ||
| afterEach(() => jest.restoreAllMocks()); | ||
|
|
||
| it('refuses while PG is still booting, without touching either store', async () => { | ||
| const result = await writeSocketMessage(input({ pgReady: false })); | ||
|
|
||
| expect(result).toEqual(REFUSED); | ||
| expect(PGMessage.create).not.toHaveBeenCalled(); | ||
| expect(MongoMessage).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('returns a retryable error when the PG insert throws, writing nothing to MongoDB', async () => { | ||
| PGMessage.create.mockRejectedValue(new Error('connection terminated')); | ||
|
|
||
| const result = await writeSocketMessage(input()); | ||
|
|
||
| expect(result).toEqual(REFUSED); | ||
| expect(MongoMessage).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('returns the joined row on success, passing the reply edge through', async () => { | ||
| PGMessage.create.mockResolvedValue({ id: 7, content: 'hi' }); | ||
| PGMessage.findById.mockResolvedValue({ id: 7, content: 'hi', username: 'sam' }); | ||
|
|
||
| const result = await writeSocketMessage(input({ replyToMessageId: '5' })); | ||
|
|
||
| expect(PGMessage.create).toHaveBeenCalledWith('p1', 'u1', 'hi', 'text', '5'); | ||
| expect(result).toEqual({ ok: true, message: { id: 7, content: 'hi', username: 'sam' } }); | ||
| }); | ||
|
|
||
| it('keeps an already-stored message when only the joined re-read fails', async () => { | ||
| PGMessage.create.mockResolvedValue({ id: 8, content: 'hi' }); | ||
| PGMessage.findById.mockRejectedValue(new Error('read timeout')); | ||
|
|
||
| const result = await writeSocketMessage(input()); | ||
|
|
||
| expect(result).toEqual({ ok: true, message: { id: 8, content: 'hi' } }); | ||
| expect(PGMessage.create).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('heals a MongoDB-only pod into PG before the insert', async () => { | ||
| PGPod.findById.mockResolvedValue(null); | ||
| PGMessage.create.mockResolvedValue({ id: 9 }); | ||
| PGMessage.findById.mockResolvedValue({ id: 9 }); | ||
|
|
||
| await writeSocketMessage(input()); | ||
|
|
||
| expect(syncPodFromMongo).toHaveBeenCalledWith('p1', 'u1'); | ||
| expect(syncPodFromMongo.mock.invocationCallOrder[0]) | ||
| .toBeLessThan(PGMessage.create.mock.invocationCallOrder[0]); | ||
| }); | ||
| }); |
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.