generated from commandbox-modules/commandbox-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Add streaming test results support via SSE #21
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
Open
elpete
wants to merge
7
commits into
development
Choose a base branch
from
feature/streaming-test-results
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
18a5016
Add streaming test results support via SSE
elpete de1857c
Fix real-time running spec indicator display
elpete ca6c31b
Formatting
elpete 667c23e
Fix bundleEnd color to include skipped count
elpete 779148f
Add terminal flush to specEnd for consistent real-time rendering
elpete 0ab7aef
Fix resource cleanup and handle stream ending without trailing blank …
elpete 4e78303
Set exit code 1 on streaming connection failures
elpete 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,176 @@ | ||
| /** | ||
| * Service for consuming Server-Sent Events (SSE) streams from TestBox | ||
| * Parses SSE format and invokes callbacks for each event type | ||
| */ | ||
| component singleton { | ||
|
|
||
| property name="shell" inject="shell"; | ||
|
|
||
| /** | ||
| * Consume an SSE stream from a URL | ||
| * | ||
| * @url The URL to stream from (should have streaming=true) | ||
| * @eventHandlers A struct of callbacks keyed by event type (e.g., bundleStart, specEnd, testRunEnd) | ||
| * @onError Callback for connection errors | ||
| * | ||
| * @return The final testRunEnd event data containing full results, or empty struct on error | ||
| */ | ||
| public struct function consumeStream( | ||
| required string url, | ||
| required struct eventHandlers, | ||
| any onError | ||
| ){ | ||
| var finalResults = {}; | ||
| var reader = javacast( "null", "" ); | ||
| var inputStream = javacast( "null", "" ); | ||
| var connection = javacast( "null", "" ); | ||
|
|
||
| try { | ||
| // Create URL connection | ||
| var netURL = createObject( "java", "java.net.URL" ).init( arguments.url ); | ||
| connection = netURL.openConnection(); | ||
|
|
||
| connection.setRequestProperty( "Accept", "text/event-stream" ); | ||
| connection.setRequestProperty( | ||
| "User-Agent", | ||
| "Mozilla/5.0 (Compatible MSIE 9.0;Windows NT 6.1;WOW64; Trident/5.0)" | ||
| ); | ||
| connection.setConnectTimeout( 30000 ); | ||
| connection.setReadTimeout( 0 ); // No read timeout for streaming | ||
|
|
||
| connection.connect(); | ||
|
|
||
| // Check response code | ||
| if ( connection.responseCode < 200 || connection.responseCode > 299 ) { | ||
| throw( | ||
| message = "HTTP Error: #connection.responseCode# #connection.responseMessage#", | ||
| detail = arguments.url | ||
| ); | ||
| } | ||
|
|
||
| // Read the stream line by line | ||
| inputStream = connection.getInputStream(); | ||
| reader = createObject( "java", "java.io.BufferedReader" ).init( | ||
| createObject( "java", "java.io.InputStreamReader" ).init( inputStream, "UTF-8" ) | ||
| ); | ||
|
|
||
| var currentEvent = ""; | ||
| var currentData = ""; | ||
|
|
||
| while ( true ) { | ||
| // Check for user interrupt | ||
| shell.checkInterrupted(); | ||
|
|
||
| var line = reader.readLine(); | ||
|
|
||
| // End of stream | ||
| if ( isNull( line ) ) { | ||
elpete marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Process any buffered event if stream ends without trailing blank line | ||
| if ( len( currentEvent ) && len( currentData ) ) { | ||
| processEvent( | ||
| eventType = currentEvent, | ||
| eventData = currentData, | ||
| eventHandlers = arguments.eventHandlers, | ||
| finalResults = finalResults | ||
| ); | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| // Parse SSE format | ||
| if ( line.startsWith( "event:" ) ) { | ||
| currentEvent = trim( line.mid( 7, len( line ) ) ); | ||
| } else if ( line.startsWith( "data:" ) ) { | ||
| currentData = trim( line.mid( 6, len( line ) ) ); | ||
| } else if ( line == "" && len( currentEvent ) && len( currentData ) ) { | ||
| // Empty line signals end of event - process it | ||
| processEvent( | ||
| eventType = currentEvent, | ||
| eventData = currentData, | ||
| eventHandlers = arguments.eventHandlers, | ||
| finalResults = finalResults | ||
| ); | ||
|
|
||
| // Reset for next event | ||
| currentEvent = ""; | ||
| currentData = ""; | ||
| } | ||
| } | ||
| } catch ( any e ) { | ||
| if ( !isNull( arguments.onError ) && isClosure( arguments.onError ) ) { | ||
| arguments.onError( e ); | ||
| } else { | ||
| rethrow; | ||
| } | ||
| } finally { | ||
| // Clean up resources | ||
| try { | ||
| if ( !isNull( reader ) ) { | ||
| reader.close(); | ||
| } | ||
| } catch ( any ignore ) { | ||
| } | ||
| try { | ||
| if ( !isNull( inputStream ) ) { | ||
| inputStream.close(); | ||
| } | ||
| } catch ( any ignore ) { | ||
| } | ||
| try { | ||
| if ( !isNull( connection ) ) { | ||
| connection.disconnect(); | ||
| } | ||
| } catch ( any ignore ) { | ||
| } | ||
| } | ||
|
|
||
| return finalResults; | ||
| } | ||
|
|
||
| /** | ||
| * Process a single SSE event | ||
| */ | ||
| private function processEvent( | ||
| required string eventType, | ||
| required string eventData, | ||
| required struct eventHandlers, | ||
| required struct finalResults | ||
| ){ | ||
| // Parse JSON data | ||
| var data = {}; | ||
| if ( isJSON( arguments.eventData ) ) { | ||
| data = deserializeJSON( arguments.eventData ); | ||
| } | ||
|
|
||
| // If this is the final event, capture the full results | ||
| if ( arguments.eventType == "testRunEnd" && structKeyExists( data, "results" ) ) { | ||
| structAppend( | ||
| arguments.finalResults, | ||
| data.results, | ||
| true | ||
| ); | ||
| } | ||
|
|
||
| // Call the appropriate handler if one exists | ||
| if ( | ||
| structKeyExists( | ||
| arguments.eventHandlers, | ||
| arguments.eventType | ||
| ) | ||
| ) { | ||
| var handler = arguments.eventHandlers[ arguments.eventType ]; | ||
| if ( isClosure( handler ) ) { | ||
| handler( data ); | ||
| } | ||
| } | ||
|
|
||
| // Also call a generic "onEvent" handler if present | ||
| if ( structKeyExists( arguments.eventHandlers, "onEvent" ) ) { | ||
| var handler = arguments.eventHandlers[ "onEvent" ]; | ||
| if ( isClosure( handler ) ) { | ||
| handler( arguments.eventType, data ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
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.