amazon: add a Ready() signal for ProcessEvents' LISTEN registration

Tests (and any other caller) previously had no way to know when the
Postgres LISTEN behind the reactive event-processing loop had actually
registered, forcing a guessed sleep before relying on it. Mocks now
exposes Ready() <-chan struct{}, closed once listenForNotifications
successfully issues LISTEN. Purely additive - ProcessEvents' signature
is unchanged.

Updates the integration tests to wait on Ready() instead of a flat
sleep, which also cut TestProcessEvents_ReactsToNotification's runtime
from ~0.25s to ~0.06s with no flakes across repeated runs.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XEDaCB7C2NEBgyvqEtZuxY
This commit is contained in:
2026-08-19 23:20:15 -06:00
co-authored by Claude Sonnet 5
parent 869f98344d
commit de9848679f
3 changed files with 50 additions and 11 deletions
+10 -9
View File
@@ -184,14 +184,11 @@ func TestProcessEvents_ReactsToNotification(t *testing.T) {
errCh <- m.ProcessEvents(ctx)
}()
// ProcessEvents registers its Postgres LISTEN synchronously before
// entering its main loop, but that registration still races against
// this goroutine actually getting scheduled - the production code
// exposes no "listening now" signal a caller (or a test) can wait
// on. A short sleep is the only lever available from outside; see
// the accompanying report for why that's a real testability/design
// gap, not just a test-flakiness workaround.
time.Sleep(200 * time.Millisecond)
select {
case <-m.Ready():
case <-time.After(5 * time.Second):
t.Fatal("ProcessEvents() did not become ready (LISTEN registered) within 5s")
}
insertRawAmazonEvent(t, pool, shopID, "evt-1")
@@ -234,7 +231,11 @@ func TestProcessEvents_ShutsDownOnContextCancel(t *testing.T) {
errCh <- m.ProcessEvents(ctx)
}()
time.Sleep(50 * time.Millisecond) // let it reach its first select
select {
case <-m.Ready():
case <-time.After(5 * time.Second):
t.Fatal("ProcessEvents() did not become ready (LISTEN registered) within 5s")
}
cancel()
select {