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 ws and starts an httptest.NewServer whose handler calls hub.Serve(w, r, id).
  • The hub test connects 50 clients using github.com/coder/websocket and waits for every client to register through hub.Connected before 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.Send succeeds.
  • The hub test uses unique helper names and does not redefine the existing helpers serveHub, dial, waitFor or redisFor.
  • The claim race test uses package assign_test and reuses riders(t), skipping when TEST_REDIS_ADDR is unset.
  • The two-rider claim test completes 100 rounds using unique order IDs. Each round has exactly one successful Claim call, and ClaimedBy identifies a contender from rider IDs 1 and 2.
  • The fifty-rider claim test makes 50 riders compete for one order. Exactly one Claim call succeeds, and ClaimedBy identifies a contender from rider IDs 1 through 50.
  • 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.WaitGroup to 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:

bash
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 make test W=5B7

Run the race detector three times:

bash
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.

Finished Exercise 5.13?

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

Live capacity planning

Up next · Module 5 · 1 items · about 35 min

Start module 5

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. The hub test connects 50 clients and waits for registration.
  2. Every connection receives 20 messages in order.
  3. The claim test verifies exactly one winner across 100 two-rider rounds.
  4. The fifty-rider race produces exactly one winner.
  5. Both tests pass under go test -race -count=3.
  6. The course tests and learner-written tests pass together with make test W=5B7.

Commands

terminal
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 make test W=5B7
terminal
cd backend && TEST_REDIS_ADDR=127.0.0.1:6379 go test -race -count=3 ./internal/ws/ ./internal/geo/...

Tests

  • TestW5B7_EveryConnectionReceivesEveryBroadcastInOrder in backend/internal/ws/hub_stress_test.go
  • TestW5B7_TwoRidersRaceForOneClaimAndExactlyOneWins in backend/internal/geo/assign/claim_test.go
  • TestW5B7_FiftyRidersRaceForOneClaimAndExactlyOneWins in backend/internal/geo/assign/claim_test.go