From eebcf134c378a7ed5b953918c430bf33095cf44d Mon Sep 17 00:00:00 2001 From: Rounak Agarwal Date: Sun, 29 Nov 2026 08:27:50 +0000 Subject: [PATCH 1/2] test: make matchmaking tests deterministic TestMatchmakingService used Elo 1200 and 1250, which are inside each other's +-200 range, so the background matcher could pair and remove both users before the test checked the pool. - use 1200 and 2000 so the two users can never be matched - give each test its own user IDs and remove them with t.Cleanup, so leftovers from one test (e.g. Charlie) can't affect the next run - assert only on the test's own users instead of the global pool size - TestEloTolerance: wait for the match instead of a fixed 100ms sleep, and move Charlie to 700 (range 500-900); at 800 his range touched Alice's at 1000 and the two could match Fixes #760 Signed-off-by: Rounak Agarwal --- backend/services/matchmaking_test.go | 140 ++++++++++++--------------- 1 file changed, 62 insertions(+), 78 deletions(-) diff --git a/backend/services/matchmaking_test.go b/backend/services/matchmaking_test.go index 78445f74..1b791b6a 100644 --- a/backend/services/matchmaking_test.go +++ b/backend/services/matchmaking_test.go @@ -5,113 +5,97 @@ import ( "time" ) +// These tests share the matchmaking singleton with every other test in the +// package, so each one uses its own user IDs, removes them when it finishes, +// and only makes assertions about its own users. + func TestMatchmakingService(t *testing.T) { - // Get the singleton service ms := GetMatchmakingService() + alice, bob := "mm-svc-alice", "mm-svc-bob" + t.Cleanup(func() { + ms.RemoveFromPool(alice) + ms.RemoveFromPool(bob) + }) - // Test adding users to pool (but not starting matchmaking yet) - err := ms.AddToPool("user1", "Alice", 1200) - if err != nil { - t.Errorf("Failed to add user1 to pool: %v", err) + // 1200 covers 1000-1400 and 2000 covers 1800-2200. The ranges do not + // overlap, so the background matcher can never pair these two users and + // remove them while the test is checking the pool. + if err := ms.AddToPool(alice, "Alice", 1200); err != nil { + t.Fatalf("Failed to add Alice to pool: %v", err) } - - err = ms.AddToPool("user2", "Bob", 1250) - if err != nil { - t.Errorf("Failed to add user2 to pool: %v", err) + if err := ms.AddToPool(bob, "Bob", 2000); err != nil { + t.Fatalf("Failed to add Bob to pool: %v", err) } - // Test getting pool - should be empty since no one started matchmaking - pool := ms.GetPool() - if len(pool) != 0 { - t.Errorf("Expected 0 users in pool (no one started matchmaking), got %d", len(pool)) + if poolContains(ms, alice) || poolContains(ms, bob) { + t.Fatal("Users should not be in the active pool before they start matchmaking") } - // Test starting matchmaking for user1 - err = ms.StartMatchmaking("user1") - if err != nil { - t.Errorf("Failed to start matchmaking for user1: %v", err) + if err := ms.StartMatchmaking(alice); err != nil { + t.Fatalf("Failed to start matchmaking for Alice: %v", err) } - - // Test getting pool - should have 1 user now - pool = ms.GetPool() - if len(pool) != 1 { - t.Errorf("Expected 1 user in pool after starting matchmaking, got %d", len(pool)) + if !poolContains(ms, alice) { + t.Error("Expected Alice in pool after starting matchmaking") } - - // Test starting matchmaking for user2 - err = ms.StartMatchmaking("user2") - if err != nil { - t.Errorf("Failed to start matchmaking for user2: %v", err) + if poolContains(ms, bob) { + t.Error("Bob should not be in the active pool before he starts matchmaking") } - // Test getting pool - should have 2 users now - pool = ms.GetPool() - if len(pool) != 2 { - t.Errorf("Expected 2 users in pool after both started matchmaking, got %d", len(pool)) + if err := ms.StartMatchmaking(bob); err != nil { + t.Fatalf("Failed to start matchmaking for Bob: %v", err) + } + if !poolContains(ms, alice) || !poolContains(ms, bob) { + t.Error("Expected Alice and Bob in pool after both started matchmaking") } - // Test removing user from pool - ms.RemoveFromPool("user1") - pool = ms.GetPool() - if len(pool) != 1 { - t.Errorf("Expected 1 user in pool after removal, got %d", len(pool)) + ms.RemoveFromPool(alice) + if poolContains(ms, alice) { + t.Error("Expected Alice to be gone after RemoveFromPool") + } + if !poolContains(ms, bob) { + t.Error("Removing Alice should not remove Bob") } - // Test updating activity - ms.UpdateActivity("user2") - pool = ms.GetPool() - if len(pool) != 1 { - t.Errorf("Expected 1 user in pool after activity update, got %d", len(pool)) + ms.UpdateActivity(bob) + if !poolContains(ms, bob) { + t.Error("Expected Bob to stay in pool after an activity update") } } func TestEloTolerance(t *testing.T) { ms := GetMatchmakingService() + alice, bob, charlie := "mm-elo-alice", "mm-elo-bob", "mm-elo-charlie" + t.Cleanup(func() { + ms.RemoveFromPool(alice) + ms.RemoveFromPool(bob) + ms.RemoveFromPool(charlie) + }) - // Add users with different Elo ratings (but don't start matchmaking yet) - ms.AddToPool("user1", "Alice", 1200) // Range: 1000-1400 - ms.AddToPool("user2", "Bob", 1250) // Range: 1050-1450 - ms.AddToPool("user3", "Charlie", 800) // Range: 600-1000 (should not match) + ms.AddToPool(alice, "Alice", 1200) // Range: 1000-1400 + ms.AddToPool(bob, "Bob", 1250) // Range: 1050-1450 + ms.AddToPool(charlie, "Charlie", 700) // Range: 500-900, no overlap with Alice or Bob - // Check pool - should be empty since no one started matchmaking - pool := ms.GetPool() - if len(pool) != 0 { - t.Errorf("Expected 0 users in pool (no one started matchmaking), got %d", len(pool)) + if poolContains(ms, alice) || poolContains(ms, bob) || poolContains(ms, charlie) { + t.Fatal("Users should not be in the active pool before they start matchmaking") } - // Start matchmaking for user1 and user2 (compatible Elo ranges) - err := ms.StartMatchmaking("user1") - if err != nil { - t.Errorf("Failed to start matchmaking for user1: %v", err) + if err := ms.StartMatchmaking(alice); err != nil { + t.Fatalf("Failed to start matchmaking for Alice: %v", err) } - - err = ms.StartMatchmaking("user2") - if err != nil { - t.Errorf("Failed to start matchmaking for user2: %v", err) - } - - // Wait a bit for matching to happen - time.Sleep(100 * time.Millisecond) - - // Check pool - user1 and user2 should be matched and removed - pool = ms.GetPool() - if len(pool) != 0 { - t.Errorf("Expected 0 users remaining in pool (Alice and Bob should be matched), got %d", len(pool)) + if err := ms.StartMatchmaking(bob); err != nil { + t.Fatalf("Failed to start matchmaking for Bob: %v", err) } - // Start matchmaking for user3 (incompatible Elo range) - err = ms.StartMatchmaking("user3") - if err != nil { - t.Errorf("Failed to start matchmaking for user3: %v", err) - } + // Matching runs in a goroutine, so wait for it instead of sleeping a fixed time. + waitUntil(t, 2*time.Second, func() bool { + return !poolContains(ms, alice) && !poolContains(ms, bob) + }, "Alice and Bob have overlapping ranges and should be matched and removed") - // Check pool - user3 should remain since no compatible match - pool = ms.GetPool() - if len(pool) != 1 { - t.Errorf("Expected 1 user remaining in pool (Charlie), got %d", len(pool)) + if err := ms.StartMatchmaking(charlie); err != nil { + t.Fatalf("Failed to start matchmaking for Charlie: %v", err) } - - if pool[0].Username != "Charlie" { - t.Errorf("Expected Charlie to remain in pool, got %s", pool[0].Username) + time.Sleep(200 * time.Millisecond) + if !poolContains(ms, charlie) { + t.Error("Charlie has no compatible opponent and should stay in the pool") } } From 04bde34cd30e966a399d46a185a1e1a79652915e Mon Sep 17 00:00:00 2001 From: Rounak Agarwal Date: Sun, 29 Nov 2026 08:51:14 +0000 Subject: [PATCH 2/2] test: check Charlie with a direct matching pass instead of a sleep Call findMatch synchronously so the 'no compatible opponent' check doesn't depend on a fixed 200ms sleep, and check AddToPool errors in TestEloTolerance like the other tests do. Signed-off-by: Rounak Agarwal --- backend/services/matchmaking_test.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/backend/services/matchmaking_test.go b/backend/services/matchmaking_test.go index 1b791b6a..3dd6a279 100644 --- a/backend/services/matchmaking_test.go +++ b/backend/services/matchmaking_test.go @@ -71,9 +71,19 @@ func TestEloTolerance(t *testing.T) { ms.RemoveFromPool(charlie) }) - ms.AddToPool(alice, "Alice", 1200) // Range: 1000-1400 - ms.AddToPool(bob, "Bob", 1250) // Range: 1050-1450 - ms.AddToPool(charlie, "Charlie", 700) // Range: 500-900, no overlap with Alice or Bob + users := []struct { + id, name string + elo int + }{ + {alice, "Alice", 1200}, // Range: 1000-1400 + {bob, "Bob", 1250}, // Range: 1050-1450 + {charlie, "Charlie", 700}, // Range: 500-900, no overlap with Alice or Bob + } + for _, u := range users { + if err := ms.AddToPool(u.id, u.name, u.elo); err != nil { + t.Fatalf("Failed to add %s to pool: %v", u.name, err) + } + } if poolContains(ms, alice) || poolContains(ms, bob) || poolContains(ms, charlie) { t.Fatal("Users should not be in the active pool before they start matchmaking") @@ -94,7 +104,9 @@ func TestEloTolerance(t *testing.T) { if err := ms.StartMatchmaking(charlie); err != nil { t.Fatalf("Failed to start matchmaking for Charlie: %v", err) } - time.Sleep(200 * time.Millisecond) + // Run a matching pass directly instead of sleeping and hoping the + // background goroutine has finished. + ms.findMatch(charlie) if !poolContains(ms, charlie) { t.Error("Charlie has no compatible opponent and should stay in the pool") }