diff --git a/backend/services/matchmaking_test.go b/backend/services/matchmaking_test.go index 78445f74..3dd6a279 100644 --- a/backend/services/matchmaking_test.go +++ b/backend/services/matchmaking_test.go @@ -5,113 +5,109 @@ 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) - - // 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)) + 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 } - - // 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) + 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) + } } - err = ms.StartMatchmaking("user2") - if err != nil { - t.Errorf("Failed to start matchmaking for user2: %v", 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") } - // 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(alice); err != nil { + t.Fatalf("Failed to start matchmaking for Alice: %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) + if err := ms.StartMatchmaking(bob); err != nil { + t.Fatalf("Failed to start matchmaking for Bob: %v", err) } - // 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)) - } + // 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") - if pool[0].Username != "Charlie" { - t.Errorf("Expected Charlie to remain in pool, got %s", pool[0].Username) + if err := ms.StartMatchmaking(charlie); err != nil { + t.Fatalf("Failed to start matchmaking for Charlie: %v", err) + } + // 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") } }