TestKeyConcurrent exercises the documented concurrent-safety contract by hammering a single key with concurrent Mark calls and asserting the resulting state honors the pool's invariants.
(t *testing.T)
| 530 | // contract by hammering a single key with concurrent Mark calls |
| 531 | // and asserting the resulting state honors the pool's invariants. |
| 532 | func TestKeyConcurrent(t *testing.T) { |
| 533 | t.Parallel() |
| 534 | |
| 535 | tests := []struct { |
| 536 | name string |
| 537 | // run is called concurrently from numGoroutines, each |
| 538 | // with its own index. |
| 539 | run func(idx int, key *keypool.Key) |
| 540 | // verify asserts the final state. May advance the clock. |
| 541 | verify func(t *testing.T, key *keypool.Key, clk *quartz.Mock) |
| 542 | }{ |
| 543 | { |
| 544 | // Half of the goroutines mark the key as temporary |
| 545 | // with 60s, the other half with 10s. The longer |
| 546 | // cooldown must win regardless of ordering. |
| 547 | name: "longer_cooldown_wins", |
| 548 | run: func(idx int, key *keypool.Key) { |
| 549 | if idx%2 == 0 { |
| 550 | key.MarkTemporary(60 * time.Second) |
| 551 | } else { |
| 552 | key.MarkTemporary(10 * time.Second) |
| 553 | } |
| 554 | }, |
| 555 | verify: func(t *testing.T, key *keypool.Key, clk *quartz.Mock) { |
| 556 | // At 50s the 60s cooldown is still active. |
| 557 | clk.Advance(50 * time.Second) |
| 558 | assert.Equal(t, keypool.KeyStateTemporary, key.State()) |
| 559 | // At 65s the 60s cooldown has expired. |
| 560 | clk.Advance(15 * time.Second) |
| 561 | assert.Equal(t, keypool.KeyStateValid, key.State()) |
| 562 | }, |
| 563 | }, |
| 564 | { |
| 565 | // Half of the goroutines mark the key as permanent, |
| 566 | // the other half mark it as temporary. Permanent is |
| 567 | // terminal: any permanent call wins. |
| 568 | name: "permanent_wins_over_temporary", |
| 569 | run: func(idx int, key *keypool.Key) { |
| 570 | if idx%2 == 0 { |
| 571 | key.MarkPermanent() |
| 572 | } else { |
| 573 | key.MarkTemporary(60 * time.Second) |
| 574 | } |
| 575 | }, |
| 576 | verify: func(t *testing.T, key *keypool.Key, _ *quartz.Mock) { |
| 577 | assert.Equal(t, keypool.KeyStatePermanent, key.State()) |
| 578 | }, |
| 579 | }, |
| 580 | } |
| 581 | |
| 582 | for _, tc := range tests { |
| 583 | t.Run(tc.name, func(t *testing.T) { |
| 584 | t.Parallel() |
| 585 | |
| 586 | clk := quartz.NewMock(t) |
| 587 | pool, err := keypool.New([]string{"key-0"}, clk) |
| 588 | require.NoError(t, err) |
| 589 | key, keyPoolErr := pool.Walker().Next() |
nothing calls this directly
no test coverage detected