Repository navigation
Internal cleanup #32130
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
Internal cleanup #32130
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
4e7b0d8
fix(connectors): a connector backlog cannot fill orchestration's rece…
Jinwoo-H 2a62927
fix(connectors): settle only a previous process's receipts, not a re-…
Jinwoo-H 25be15b
Merge remote-tracking branch 'origin/main' into jinwoo/connector-rece…
Jinwoo-H 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
111 changes: 111 additions & 0 deletions
111
src/main/external-conversations/external-conversation-delivery-receipts.test.ts
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,111 @@ | ||
| import { mkdtempSync, rmSync } from 'node:fs' | ||
| import { tmpdir } from 'node:os' | ||
| import { join } from 'node:path' | ||
| import { afterEach, describe, expect, it } from 'vitest' | ||
| import { OrchestrationDb } from '../runtime/orchestration/db/orchestration-db' | ||
| import { MUTATION_RECEIPT_MAX_ROWS } from '../runtime/orchestration/mutation-receipt-capacity' | ||
| import { | ||
| CONNECTOR_PENDING_RECEIPT_LIMIT, | ||
| DeliveryReceipts | ||
| } from './external-conversation-delivery-receipts' | ||
| import type { ConversationKey, Delivery } from './external-conversation-types' | ||
|
|
||
| const KEY: ConversationKey = { source: 'dev', externalId: 'thread' } | ||
| const ORDINARY = { | ||
| callerFingerprint: 'ordinary-user', | ||
| requestId: 'send', | ||
| method: 'orchestration.send', | ||
| payloadHash: 'hash' | ||
| } | ||
| const never = (): Promise<Delivery> => new Promise(() => {}) | ||
|
|
||
| const directories: string[] = [] | ||
| function fileDb(): { path: string; db: OrchestrationDb } { | ||
| const directory = mkdtempSync(join(tmpdir(), 'connector-receipts-')) | ||
| directories.push(directory) | ||
| const path = join(directory, 'orchestration.db') | ||
| return { path, db: new OrchestrationDb(path) } | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| for (const directory of directories.splice(0)) { | ||
| rmSync(directory, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
|
|
||
| describe('DeliveryReceipts ledger capacity', () => { | ||
| it('cannot fill orchestration’s ledger with a backlog that outlives the process', async () => { | ||
| const { path, db } = fileDb() | ||
| const receipts = new DeliveryReceipts(() => db) | ||
| const answers: Promise<Delivery>[] = [] | ||
| for (let i = 0; i < MUTATION_RECEIPT_MAX_ROWS; i++) { | ||
| answers.push(receipts.run(KEY, String(i), 'connector.send', ['hello'], never)) | ||
| } | ||
| await expect(answers[CONNECTOR_PENDING_RECEIPT_LIMIT]).resolves.toEqual({ | ||
| outcome: 'refused', | ||
| reason: 'delivery_backlog_full' | ||
| }) | ||
| expect(db.countPendingMutationReceipts(['connector:dev'])).toBe(CONNECTOR_PENDING_RECEIPT_LIMIT) | ||
| db.close() | ||
|
|
||
| // Process loss, then a restart that never starts connectors. | ||
| const reopened = new OrchestrationDb(path) | ||
| expect(reopened.beginMutationReceipt(ORDINARY).disposition).toBe('started') | ||
| reopened.close() | ||
| }) | ||
|
|
||
| it('answers a recorded delivery even when the backlog is full', async () => { | ||
| const { db } = fileDb() | ||
| const receipts = new DeliveryReceipts(() => db) | ||
| for (let i = 0; i < CONNECTOR_PENDING_RECEIPT_LIMIT; i++) { | ||
| void receipts.run(KEY, String(i), 'connector.send', ['hello'], never) | ||
| } | ||
| const joined = receipts.run(KEY, '0', 'connector.send', ['hello'], never) | ||
| const raced = await Promise.race([joined, Promise.resolve('waiting')]) | ||
| expect(raced).toBe('waiting') | ||
| db.close() | ||
| }) | ||
|
|
||
| it('settles a previous process’s pending deliveries as unknown, without replaying them', async () => { | ||
| const { path, db } = fileDb() | ||
| const before = new DeliveryReceipts(() => db) | ||
| for (let i = 0; i < CONNECTOR_PENDING_RECEIPT_LIMIT; i++) { | ||
| void before.run(KEY, String(i), 'connector.send', ['hello'], never) | ||
| } | ||
| db.close() | ||
|
|
||
| const reopened = new OrchestrationDb(path) | ||
| const after = new DeliveryReceipts(() => reopened) | ||
| let executed = 0 | ||
| const execute = async (): Promise<Delivery> => { | ||
| executed++ | ||
| return { outcome: 'accepted' } | ||
| } | ||
| await expect(after.run(KEY, '0', 'connector.send', ['hello'], execute)).resolves.toEqual({ | ||
| outcome: 'unknown', | ||
| reason: 'replayed_after_restart' | ||
| }) | ||
| expect( | ||
| reopened.getMutationReceipt('connector:dev', JSON.stringify(['thread', '0']))?.state | ||
| ).toBe('completed') | ||
| // The settled rows no longer hold the connector's budget. | ||
| await expect(after.run(KEY, 'new', 'connector.send', ['hello'], execute)).resolves.toEqual({ | ||
| outcome: 'accepted' | ||
| }) | ||
| expect(executed).toBe(1) | ||
| expect(reopened.countPendingMutationReceipts(['connector:dev'])).toBe(0) | ||
| reopened.close() | ||
| }) | ||
|
|
||
| it('leaves other callers’ pending receipts alone', async () => { | ||
| const { db } = fileDb() | ||
| db.beginMutationReceipt(ORDINARY) | ||
| await new DeliveryReceipts(() => db).run(KEY, 'd1', 'connector.send', ['hello'], async () => ({ | ||
| outcome: 'accepted' | ||
| })) | ||
| expect(db.getMutationReceipt(ORDINARY.callerFingerprint, ORDINARY.requestId)?.state).toBe( | ||
| 'pending' | ||
| ) | ||
| db.close() | ||
| }) | ||
| }) |
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
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.