Hub broadcast and claim race tests
You build concurrency tests for the WebSocket hub and Redis rider claims, proving that broadcasts arrive in order and exactly one rider wins each order claim.
Ticket
Write two concurrency tests for the Week 5 backend. The first tests the WebSocket hub with 50 clients in backend/internal/ws/hub_broadcast_test.go. The second makes competing riders attempt the same Redis claim in backend/internal/geo/assign/claim_race_test.go.
The course command make test W=5B7 runs its own race tests, TestW5B7_EveryConnectionReceivesEveryBroadcastInOrder, TestW5B7_TwoRidersRaceForOneClaimAndExactlyOneWins and TestW5B7_FiftyRidersRaceForOneClaimAndExactlyOneWins, against your hub and claim code. Your own tests in the two files above must pass alongside them.
The hub must deliver every message to the right connection without dropping or reordering it. The claim must have exactly one winner, even when many riders compete together. Week 6 measures these components under load, so these tests establish their concurrency guarantees first.
Acceptance criteria
- The hub broadcast test uses
package wsand starts anhttptest.NewServerwhose handler callshub.Serve(w, r, id). - The hub test connects 50 clients using
github.com/coder/websocketand waits for every client to register throughhub.Connectedbefore broadcasting. - Under the course stress test, 50 connections each receive 20 messages in order. Messages are sent using 50 goroutines, and every call to
hub.Sendsucceeds. - The hub test uses unique helper names and does not redefine the existing helpers
serveHub,dial,waitFororredisFor. - The claim race test uses
package assign_testand reusesriders(t), skipping whenTEST_REDIS_ADDRis unset. - The two-rider claim test completes 100 rounds using unique order IDs. Each round has exactly one successful
Claimcall, andClaimedByidentifies a contender from rider IDs1and2. - The fifty-rider claim test makes 50 riders compete for one order. Exactly one
Claimcall succeeds, andClaimedByidentifies a contender from rider IDs1through50. - Both tests coordinate concurrent operations without sleeps. They wait for goroutines to finish and clean up WebSocket connections and HTTP test servers.
- Both tests pass under
go test -race -count=3.
Constraints
- Use exactly 50 WebSocket connections and 20 messages per connection in the hub stress test.
- Send the hub messages from 50 goroutines.
- Run 100 rounds of two-rider competition.
- Run one round with 50 competing riders.
- Generate unique order IDs using
time.Now().UnixNano(). - Use a closed start channel as the claim race barrier.
- Use
sync.WaitGroupto wait for competing goroutines. - Use condition-based registration checks rather than sleeps.
- Both tests must pass under
go test -race -count=3.
Design direction
Keep the hub test in the ws package so it can exercise the existing Hub directly. Use an httptest.Server to expose the hub and real WebSocket clients to exercise its connections. Wait until clients register before sending messages. Give each client a distinct user ID and verify its expected messages in order.
Keep the claim test in the external assign_test package. Reuse riders(t) so the test exercises actual Redis claim behavior rather than a mock. Give every competing rider its own goroutine. Release them together through a closed start channel, then collect their results after the WaitGroup finishes.
Protect shared test state with a Mutex where multiple goroutines access it. A sync.WaitGroup is useful for completion, while a channel provides a start barrier. Any layout that meets the contract passes.
Think about the trade-off between testing one broadcast per client and testing repeated ordered delivery. The stress test should catch failures that a single message might miss. Also distinguish a race in the test's shared state from a race in the actual Exclusive claim with SET NX PX operation.
How to check your work
Run the exercise tests:
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 make test W=5B7Run the race detector three times:
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 go test -race -count=3 ./internal/ws/ ./internal/geo/...Read the first failing assertion to identify whether a WebSocket message was dropped or reordered, a claim produced the wrong number of winners, or ClaimedBy returned an unexpected rider.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Live rider tracking
5 items
- 5.9Watching foreground locationnot completed
- 5.10Reconnecting WebSocket hooksnot completed
- 5.11Order location streamnot completed
- 5.12Rider mode and customer tracking screensnot completed
- 5.13Hub broadcast and claim race testsnot completed
Up next · Module 5 · 1 items · about 35 min
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- The hub test connects 50 clients and waits for registration.
- Every connection receives 20 messages in order.
- The claim test verifies exactly one winner across 100 two-rider rounds.
- The fifty-rider race produces exactly one winner.
- Both tests pass under
go test -race -count=3. - The course tests and learner-written tests pass together with
make test W=5B7.
Commands
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 make test W=5B7cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 go test -race -count=3 ./internal/ws/ ./internal/geo/...Tests
TestW5B7_EveryConnectionReceivesEveryBroadcastInOrderinbackend/internal/ws/hub_stress_test.goTestW5B7_TwoRidersRaceForOneClaimAndExactlyOneWinsinbackend/internal/geo/assign/claim_test.goTestW5B7_FiftyRidersRaceForOneClaimAndExactlyOneWinsinbackend/internal/geo/assign/claim_test.go