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

Function TestConnStateMachine_TryTransition

internal/pool/conn_state_test.go:34–93  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

32}
33
34func TestConnStateMachine_TryTransition(t *testing.T) {
35 tests := []struct {
36 name string
37 initialState ConnState
38 validStates []ConnState
39 targetState ConnState
40 expectError bool
41 }{
42 {
43 name: "valid transition from CREATED to INITIALIZING",
44 initialState: StateCreated,
45 validStates: []ConnState{StateCreated},
46 targetState: StateInitializing,
47 expectError: false,
48 },
49 {
50 name: "invalid transition from CREATED to IDLE",
51 initialState: StateCreated,
52 validStates: []ConnState{StateInitializing},
53 targetState: StateIdle,
54 expectError: true,
55 },
56 {
57 name: "transition to same state",
58 initialState: StateIdle,
59 validStates: []ConnState{StateIdle},
60 targetState: StateIdle,
61 expectError: false,
62 },
63 {
64 name: "multiple valid from states",
65 initialState: StateIdle,
66 validStates: []ConnState{StateInitializing, StateIdle, StateUnusable},
67 targetState: StateUnusable,
68 expectError: false,
69 },
70 }
71
72 for _, tt := range tests {
73 t.Run(tt.name, func(t *testing.T) {
74 sm := NewConnStateMachine()
75 sm.Transition(tt.initialState)
76
77 _, err := sm.TryTransition(tt.validStates, tt.targetState)
78
79 if tt.expectError && err == nil {
80 t.Error("expected error but got none")
81 }
82 if !tt.expectError && err != nil {
83 t.Errorf("unexpected error: %v", err)
84 }
85
86 if !tt.expectError {
87 if state := sm.GetState(); state != tt.targetState {
88 t.Errorf("expected state %s, got %s", tt.targetState, state)
89 }
90 }
91 })

Callers

nothing calls this directly

Calls 6

TransitionMethod · 0.95
TryTransitionMethod · 0.95
GetStateMethod · 0.95
NewConnStateMachineFunction · 0.85
RunMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected