Skip to content

Commit 21b7dc2

Browse files
committed
Fix event subscription timing in ConnectionSupervisorTests to prevent dropped events
1 parent 0153279 commit 21b7dc2

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

tests/RustPlusBot.Features.Connections.Tests/ConnectionSupervisorTests.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,16 @@ public async Task StatusEvents_CarryWasConnected_OnlyAfterAConnectedDrop()
250250

251251
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
252252
var events = new List<ConnectionStatusChangedEvent>();
253+
// Register the subscription synchronously on this thread BEFORE any connection work runs.
254+
// InMemoryEventBus registers the channel eagerly when SubscribeAsync is invoked, so buffering
255+
// starts here. Deferring the call into the Task.Run below would race the supervisor's first
256+
// publishes: on a slow runner the initial Connecting/Connected events are dropped, the drop
257+
// event (WasConnected=true) becomes the collector's first observation, and the "before first
258+
// Connected" assertion fails.
259+
var stream = h.Bus.SubscribeAsync<ConnectionStatusChangedEvent>(cts.Token);
253260
_ = Task.Run(async () =>
254261
{
255-
await foreach (var e in h.Bus.SubscribeAsync<ConnectionStatusChangedEvent>(cts.Token))
262+
await foreach (var e in stream)
256263
{
257264
lock (events)
258265
{

0 commit comments

Comments
 (0)