MCPcopy
hub / github.com/redis/go-redis / TestReAuthOnlyWhenIdle

Function TestReAuthOnlyWhenIdle

internal/auth/streaming/pool_hook_state_test.go:14–63  ·  view source on GitHub ↗

TestReAuthOnlyWhenIdle verifies that re-authentication only happens when a connection is in IDLE state, not when it's IN_USE.

(t *testing.T)

Source from the content-addressed store, hash-verified

12// TestReAuthOnlyWhenIdle verifies that re-authentication only happens when
13// a connection is in IDLE state, not when it's IN_USE.
14func TestReAuthOnlyWhenIdle(t *testing.T) {
15 // Create a connection
16 cn := pool.NewConn(nil)
17
18 // Initialize to IDLE state
19 cn.GetStateMachine().Transition(pool.StateInitializing)
20 cn.GetStateMachine().Transition(pool.StateIdle)
21
22 // Simulate connection being acquired (IDLE → IN_USE)
23 if !cn.CompareAndSwapUsed(false, true) {
24 t.Fatal("Failed to acquire connection")
25 }
26
27 // Verify state is IN_USE
28 if state := cn.GetStateMachine().GetState(); state != pool.StateInUse {
29 t.Errorf("Expected state IN_USE, got %s", state)
30 }
31
32 // Try to transition to UNUSABLE (for reauth) - should fail
33 _, err := cn.GetStateMachine().TryTransition([]pool.ConnState{pool.StateIdle}, pool.StateUnusable)
34 if err == nil {
35 t.Error("Expected error when trying to transition IN_USE → UNUSABLE, but got none")
36 }
37
38 // Verify state is still IN_USE
39 if state := cn.GetStateMachine().GetState(); state != pool.StateInUse {
40 t.Errorf("Expected state to remain IN_USE, got %s", state)
41 }
42
43 // Release connection (IN_USE → IDLE)
44 if !cn.CompareAndSwapUsed(true, false) {
45 t.Fatal("Failed to release connection")
46 }
47
48 // Verify state is IDLE
49 if state := cn.GetStateMachine().GetState(); state != pool.StateIdle {
50 t.Errorf("Expected state IDLE, got %s", state)
51 }
52
53 // Now try to transition to UNUSABLE - should succeed
54 _, err = cn.GetStateMachine().TryTransition([]pool.ConnState{pool.StateIdle}, pool.StateUnusable)
55 if err != nil {
56 t.Errorf("Failed to transition IDLE → UNUSABLE: %v", err)
57 }
58
59 // Verify state is UNUSABLE
60 if state := cn.GetStateMachine().GetState(); state != pool.StateUnusable {
61 t.Errorf("Expected state UNUSABLE, got %s", state)
62 }
63}
64
65// TestReAuthWaitsForConnectionToBeIdle verifies that the re-auth worker
66// waits for a connection to become IDLE before performing re-authentication.

Callers

nothing calls this directly

Calls 7

GetStateMachineMethod · 0.95
CompareAndSwapUsedMethod · 0.95
NewConnFunction · 0.92
TransitionMethod · 0.80
TryTransitionMethod · 0.80
GetStateMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected