TestLBClientRemoveClientsCallbackPanic ensures a panic in the user-supplied RemoveClients callback does not leak the write lock and deadlock later calls.
(t *testing.T)
| 69 | // TestLBClientRemoveClientsCallbackPanic ensures a panic in the user-supplied |
| 70 | // RemoveClients callback does not leak the write lock and deadlock later calls. |
| 71 | func TestLBClientRemoveClientsCallbackPanic(t *testing.T) { |
| 72 | t.Parallel() |
| 73 | |
| 74 | lbc := &LBClient{Clients: []BalancingClient{ |
| 75 | &mockBalancingClient{}, |
| 76 | &mockBalancingClient{}, |
| 77 | &mockBalancingClient{}, |
| 78 | }} |
| 79 | |
| 80 | // Trigger the lazy init so cc.cs is populated and the callback runs. |
| 81 | var req Request |
| 82 | var resp Response |
| 83 | if err := lbc.DoDeadline(&req, &resp, time.Now().Add(time.Second)); err != nil { |
| 84 | t.Fatalf("unexpected error: %v", err) |
| 85 | } |
| 86 | |
| 87 | // Panic part way through so the old code would have already niled an |
| 88 | // earlier cc.cs slot, leaving a hole that crashes later get() calls. |
| 89 | calls := 0 |
| 90 | func() { |
| 91 | defer func() { |
| 92 | if recover() == nil { |
| 93 | t.Fatal("expected panic from RemoveClients callback") |
| 94 | } |
| 95 | }() |
| 96 | lbc.RemoveClients(func(BalancingClient) bool { |
| 97 | calls++ |
| 98 | if calls == 2 { |
| 99 | panic("boom") |
| 100 | } |
| 101 | return false |
| 102 | }) |
| 103 | }() |
| 104 | |
| 105 | // If the lock leaked, AddClient would block forever. |
| 106 | done := make(chan struct{}) |
| 107 | go func() { |
| 108 | lbc.AddClient(&mockBalancingClient{}) |
| 109 | close(done) |
| 110 | }() |
| 111 | select { |
| 112 | case <-done: |
| 113 | case <-time.After(time.Second): |
| 114 | t.Fatal("AddClient deadlocked after RemoveClients callback panicked") |
| 115 | } |
| 116 | |
| 117 | // cc.cs must not be left with nil holes: a request still has to work |
| 118 | // instead of panicking on a nil client. |
| 119 | if err := lbc.DoDeadline(&req, &resp, time.Now().Add(time.Second)); err != nil { |
| 120 | t.Fatalf("unexpected error after panicking RemoveClients: %v", err) |
| 121 | } |
| 122 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…