-
Notifications
You must be signed in to change notification settings - Fork 46
Add waitForExit() method to Process interface #299
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
12 commits
Select commit
Hold shift + click to select a range
9d9f4c1
Add waitForExit() method to Process interface and tests
whoiskatrin 6437af6
fix error handling for unexpected stream terminations in Sandbox class
whoiskatrin 8468050
Merge branch 'main' into add-wait-for-exit
whoiskatrin 6a027b7
Simplify waitForExit to return only exit code
whoiskatrin fcfb045
Use streamProcessLogs instead of polling for waitForExit
whoiskatrin cfeac3a
Add changeset for waitForExit feature
whoiskatrin 68978f3
revert original changeset change
whoiskatrin d8d6181
revert changeset
whoiskatrin b0cd48d
fix changeset
whoiskatrin 55beb0f
fix changeset
whoiskatrin d83d76c
Update clean-pans-switch.md
whoiskatrin 4df089d
exit code fallback
whoiskatrin 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@cloudflare/sandbox': minor | ||
| --- | ||
|
|
||
| Add waitForExit() method to Process interface for waiting until a process terminates |
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 |
|---|---|---|
|
|
@@ -660,6 +660,146 @@ data: {"type":"exit","exitCode":127,"timestamp":"${new Date().toISOString()}"} | |
| }); | ||
| }); | ||
|
|
||
| describe('waitForExit() method', () => { | ||
|
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. Tests can be more concise. Lots of overlap with current setup. |
||
| it('should resolve when process exits successfully', async () => { | ||
| vi.spyOn(sandbox.client.processes, 'startProcess').mockResolvedValue({ | ||
| success: true, | ||
| processId: 'proc-build', | ||
| pid: 12345, | ||
| command: 'npm run build', | ||
| timestamp: new Date().toISOString() | ||
| } as any); | ||
|
|
||
| // Mock stream that emits exit event with code 0 | ||
| const sseData = `data: {"type":"exit","exitCode":0,"timestamp":"${new Date().toISOString()}"}\n\n`; | ||
| const mockStream = new ReadableStream({ | ||
| start(controller) { | ||
| controller.enqueue(new TextEncoder().encode(sseData)); | ||
| controller.close(); | ||
| } | ||
| }); | ||
|
|
||
| vi.spyOn(sandbox.client.processes, 'streamProcessLogs').mockResolvedValue( | ||
| mockStream | ||
| ); | ||
|
|
||
| const proc = await sandbox.startProcess('npm run build'); | ||
| const result = await proc.waitForExit(); | ||
|
|
||
| expect(result.exitCode).toBe(0); | ||
| }); | ||
|
|
||
| it('should return non-zero exit code when process fails', async () => { | ||
| vi.spyOn(sandbox.client.processes, 'startProcess').mockResolvedValue({ | ||
| success: true, | ||
| processId: 'proc-build', | ||
| pid: 12345, | ||
| command: 'npm run build', | ||
| timestamp: new Date().toISOString() | ||
| } as any); | ||
|
|
||
| // Mock stream that emits exit event with code 1 | ||
| const sseData = `data: {"type":"exit","exitCode":1,"timestamp":"${new Date().toISOString()}"}\n\n`; | ||
| const mockStream = new ReadableStream({ | ||
| start(controller) { | ||
| controller.enqueue(new TextEncoder().encode(sseData)); | ||
| controller.close(); | ||
| } | ||
| }); | ||
|
|
||
| vi.spyOn(sandbox.client.processes, 'streamProcessLogs').mockResolvedValue( | ||
| mockStream | ||
| ); | ||
|
|
||
| const proc = await sandbox.startProcess('npm run build'); | ||
| const result = await proc.waitForExit(); | ||
|
|
||
| expect(result.exitCode).toBe(1); | ||
| }); | ||
|
|
||
| it('should wait for exit event in stream', async () => { | ||
| vi.spyOn(sandbox.client.processes, 'startProcess').mockResolvedValue({ | ||
| success: true, | ||
| processId: 'proc-build', | ||
| pid: 12345, | ||
| command: 'npm run build', | ||
| timestamp: new Date().toISOString() | ||
| } as any); | ||
|
|
||
| // Mock stream that emits some log events before exit | ||
| const sseData = `data: {"type":"stdout","data":"Building...\\n","timestamp":"${new Date().toISOString()}"}\n\ndata: {"type":"stdout","data":"Done!\\n","timestamp":"${new Date().toISOString()}"}\n\ndata: {"type":"exit","exitCode":0,"timestamp":"${new Date().toISOString()}"}\n\n`; | ||
| const mockStream = new ReadableStream({ | ||
| start(controller) { | ||
| controller.enqueue(new TextEncoder().encode(sseData)); | ||
| controller.close(); | ||
| } | ||
| }); | ||
|
|
||
| vi.spyOn(sandbox.client.processes, 'streamProcessLogs').mockResolvedValue( | ||
| mockStream | ||
| ); | ||
|
|
||
| const proc = await sandbox.startProcess('npm run build'); | ||
| const result = await proc.waitForExit(); | ||
|
|
||
| expect(result.exitCode).toBe(0); | ||
| }); | ||
|
|
||
| it('should throw ProcessReadyTimeoutError when timeout exceeded', async () => { | ||
| vi.spyOn(sandbox.client.processes, 'startProcess').mockResolvedValue({ | ||
| success: true, | ||
| processId: 'proc-long', | ||
| pid: 12345, | ||
| command: 'sleep 1000', | ||
| timestamp: new Date().toISOString() | ||
| } as any); | ||
|
|
||
| // Mock stream that never emits exit event | ||
| const mockStream = new ReadableStream({ | ||
| start() { | ||
| // Never close - simulates long-running process | ||
| } | ||
| }); | ||
|
|
||
| vi.spyOn(sandbox.client.processes, 'streamProcessLogs').mockResolvedValue( | ||
| mockStream | ||
| ); | ||
|
|
||
| const proc = await sandbox.startProcess('sleep 1000'); | ||
|
|
||
| await expect(proc.waitForExit(100)).rejects.toThrow( | ||
| ProcessReadyTimeoutError | ||
| ); | ||
| }); | ||
|
|
||
| it('should throw error when stream ends without exit event', async () => { | ||
| vi.spyOn(sandbox.client.processes, 'startProcess').mockResolvedValue({ | ||
| success: true, | ||
| processId: 'proc-build', | ||
| pid: 12345, | ||
| command: 'npm run build', | ||
| timestamp: new Date().toISOString() | ||
| } as any); | ||
|
|
||
| // Mock stream that closes without exit event | ||
| const mockStream = new ReadableStream({ | ||
| start(controller) { | ||
| controller.close(); | ||
| } | ||
| }); | ||
|
|
||
| vi.spyOn(sandbox.client.processes, 'streamProcessLogs').mockResolvedValue( | ||
| mockStream | ||
| ); | ||
|
|
||
| const proc = await sandbox.startProcess('npm run build'); | ||
|
|
||
| await expect(proc.waitForExit()).rejects.toThrow( | ||
| 'stream ended unexpectedly' | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('conditionToString helper', () => { | ||
| it('should format string conditions as quoted strings', async () => { | ||
| vi.spyOn(sandbox.client.processes, 'startProcess').mockResolvedValue({ | ||
|
|
||
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.
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.
Use
ProcessNotFoundErrorinstead of genericErrorfor consistency with other process methods.