Commit bc96556
test: fix flaky tests caused by worker thread pollution (#88)
## Summary
Fixes flaky test failures across `client_spec.rb` and `headers_spec.rb`
caused by SSE worker thread pollution between tests.
**Root cause:** `SSE::Client` spawns a fire-and-forget worker thread
(`Thread.new { run_stream }`) that is not joined on `close`. After
`client.close` sets `@stopped`, the worker thread may still complete one
more reconnection attempt before checking the flag. Since all tests
reuse port 50000, this phantom request hits the *next* test's WEBrick
server — arriving without query params or with stale headers, causing
`nil` assertions.
**Fix:** In both `with_client` helpers, join the named `'LD/SSEClient'`
thread after close:
```ruby
Thread.list.select { |t| t.name == 'LD/SSEClient' }.each { |t| t.join(1) }
```
Also uses EOF-based reconnection (stream close) instead of HTTP 500
responses for the multi-reconnection test, matching the pattern of all
stable tests.
Verified: 5/5 CI passes, 20/20 local runs on Ruby 3.2, 20/20 on Ruby
3.3.
Link to Devin session:
https://app.devin.ai/sessions/e40ffc3296dd45648f86ac10c42b6da5
Requested by: @kinyoklion
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Low Risk**
> Spec-only changes; no production library behavior is modified.
>
> **Overview**
> Stabilizes end-to-end specs in **`client_spec.rb`** and
**`headers_spec.rb`** by ensuring each example fully tears down the
**`SSE::Client`** worker before the shared stub server port is reused.
>
> Both **`with_client`** helpers now **`join(0.01s)** any thread named
**`LD/SSEClient`** after **`client.close`**, so a lingering reconnect
cannot hit the next example’s WEBrick handler with wrong query strings
or headers.
>
> Two dynamic **query_params** reconnection examples no longer force
reconnect via **HTTP 500**; they close the event stream (minimal SSE
comment payloads) instead, aligning with other stable reconnect tests.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
27e0006. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
---------
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>1 parent e889cde commit bc96556
2 files changed
Lines changed: 6 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
| 36 | + | |
35 | 37 | | |
36 | 38 | | |
37 | 39 | | |
| |||
1124 | 1126 | | |
1125 | 1127 | | |
1126 | 1128 | | |
1127 | | - | |
| 1129 | + | |
1128 | 1130 | | |
1129 | 1131 | | |
1130 | 1132 | | |
| |||
1334 | 1336 | | |
1335 | 1337 | | |
1336 | 1338 | | |
1337 | | - | |
1338 | | - | |
1339 | | - | |
| 1339 | + | |
1340 | 1340 | | |
1341 | 1341 | | |
1342 | 1342 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
| 22 | + | |
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
| |||
0 commit comments