@@ -284,7 +284,7 @@ func TestAccountCRUD(t *testing.T) {
284284 t.Errorf("expected 1 available, got %d", len(available))
285285 }
286286
287- // Claim (no arguments - returns first available)
287+ // Claim (no arguments - returns first available and atomically marks as in_use)
288288 claimed, err := s.ClaimAccount()
289289 if err != nil {
290290 t.Fatalf("ClaimAccount() error: %v", err)
@@ -292,25 +292,20 @@ func TestAccountCRUD(t *testing.T) {
292292 if claimed.Username != "crab_01" {
293293 t.Errorf("expected crab_01, got %s", claimed.Username)
294294 }
295- if claimed.InUse {
296- t.Error("ClaimAccount() should NOT mark account as in_use")
295+ if !claimed.InUse {
296+ t.Error("ClaimAccount() should atomically mark account as in_use")
297297 }
298298
299- // Verify NOT marked as in_use (only MarkAccountInUse should do this)
299+ // Verify marked as in_use (ClaimAccount does this atomically now)
300300 fetched, _ := s.GetAccount("crab_01")
301- if fetched.InUse {
302- t.Error("account should NOT be in use after ClaimAccount()")
301+ if !fetched.InUse {
302+ t.Error("account should be in use after ClaimAccount()")
303303 }
304304
305- // List available (should still have the account)
305+ // List available (should now be empty since account is claimed)
306306 available, _ = s.ListAvailableAccounts()
307- if len(available) != 1 {
308- t.Errorf("expected 1 available, got %d", len(available))
309- }
310-
311- // Now mark it in use (simulating login handler behavior)
312- if err := s.MarkAccountInUse("crab_01"); err != nil {
313- t.Fatalf("MarkAccountInUse() error: %v", err)
307+ if len(available) != 0 {
308+ t.Errorf("expected 0 available after claim, got %d", len(available))
314309 }
315310
316311 // Verify marked as in_use
@@ -384,12 +379,12 @@ func TestClaimAccount_ReturnsAvailableAccount(t *testing.T) {
384379 if claimed.Password != "pass123" {
385380 t.Errorf("expected password=pass123, got %s", claimed.Password)
386381 }
387- if claimed.InUse {
388- t.Error("ClaimAccount() should return account with InUse=false")
382+ if !claimed.InUse {
383+ t.Error("ClaimAccount() should return account with InUse=true (atomically claimed)")
389384 }
390385 }
391386
392-func TestClaimAccount_DoesNotModifyInUseFlag(t *testing.T) {
387+func TestClaimAccount_AtomicallyMarksInUse(t *testing.T) {
393388 s, cleanup := setupStoreTest(t)
394389 defer cleanup()
395390
@@ -405,37 +400,41 @@ func TestClaimAccount_DoesNotModifyInUseFlag(t *testing.T) {
405400 t.Fatalf("ClaimAccount() error: %v", err)
406401 }
407402
408- // Verify in_use flag remains 0 in database
403+ // Verify in_use flag is now 1 in database (atomically set)
409404 var inUse int
410405 err = s.db.QueryRow("SELECT in_use FROM accounts WHERE username = ?", "crab_01").Scan(&inUse)
411406 if err != nil {
412407 t.Fatalf("Query in_use error: %v", err)
413408 }
414- if inUse != 0 {
415- t.Errorf("expected in_use=0 after ClaimAccount(), got %d", inUse)
409+ if inUse != 1 {
410+ t.Errorf("expected in_use=1 after ClaimAccount(), got %d", inUse)
416411 }
417412
418- // Verify GetAccount also shows InUse=false
413+ // Verify GetAccount also shows InUse=true
419414 fetched, err := s.GetAccount("crab_01")
420415 if err != nil {
421416 t.Fatalf("GetAccount() error: %v", err)
422417 }
423- if fetched.InUse {
424- t.Error("account should remain not in use after ClaimAccount()")
418+ if !fetched.InUse {
419+ t.Error("account should be marked in use after ClaimAccount()")
425420 }
426421 }
427422
428-func TestClaimAccount_MultipleCallsSameAccount(t *testing.T) {
423+func TestClaimAccount_PreventsRaceCondition(t *testing.T) {
429424 s, cleanup := setupStoreTest(t)
430425 defer cleanup()
431426
432- // Create and release an account
427+ // Create two accounts
433428 s.CreateAccount("crab_01", "pass123")
429+ s.CreateAccount("crab_02", "pass456")
434430 if err := s.ReleaseAccount("crab_01"); err != nil {
435- t.Fatalf("ReleaseAccount() error: %v", err)
431+ t.Fatalf("ReleaseAccount(crab_01) error: %v", err)
432+ }
433+ if err := s.ReleaseAccount("crab_02"); err != nil {
434+ t.Fatalf("ReleaseAccount(crab_02) error: %v", err)
436435 }
437436
438- // First claim
437+ // First claim should get crab_01 and lock it
439438 claimed1, err := s.ClaimAccount()
440439 if err != nil {
441440 t.Fatalf("First ClaimAccount() error: %v", err)
@@ -444,31 +443,19 @@ func TestClaimAccount_MultipleCallsSameAccount(t *testing.T) {
444443 t.Errorf("First claim: expected crab_01, got %s", claimed1.Username)
445444 }
446445
447- // Second claim should return the same account (no locking)
446+ // Second claim should get crab_02 (crab_01 is now locked)
448447 claimed2, err := s.ClaimAccount()
449448 if err != nil {
450449 t.Fatalf("Second ClaimAccount() error: %v", err)
451450 }
452- if claimed2.Username != "crab_01" {
453- t.Errorf("Second claim: expected crab_01, got %s", claimed2.Username)
454- }
455-
456- // Third claim should also work
457- claimed3, err := s.ClaimAccount()
458- if err != nil {
459- t.Fatalf("Third ClaimAccount() error: %v", err)
460- }
461- if claimed3.Username != "crab_01" {
462- t.Errorf("Third claim: expected crab_01, got %s", claimed3.Username)
451+ if claimed2.Username != "crab_02" {
452+ t.Errorf("Second claim: expected crab_02, got %s", claimed2.Username)
463453 }
464454
465- // Verify account is still available
466- available, err := s.ListAvailableAccounts()
467- if err != nil {
468- t.Fatalf("ListAvailableAccounts() error: %v", err)
469- }
470- if len(available) != 1 {
471- t.Errorf("expected 1 available account after multiple claims, got %d", len(available))
455+ // Third claim should fail (no more available accounts)
456+ _, err = s.ClaimAccount()
457+ if err == nil {
458+ t.Error("Third ClaimAccount() should fail when no accounts available")
472459 }
473460 }