MCPcopy
hub / github.com/grpc/grpc-go / TestBackoffWhenNoServerPrefaceReceived

Method TestBackoffWhenNoServerPrefaceReceived

clientconn_test.go:204–256  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

202}
203
204func (s) TestBackoffWhenNoServerPrefaceReceived(t *testing.T) {
205 lis, err := net.Listen("tcp", "localhost:0")
206 if err != nil {
207 t.Fatalf("Unexpected error from net.Listen(%q, %q): %v", "tcp", "localhost:0", err)
208 }
209 defer lis.Close()
210 done := make(chan struct{})
211 go func() { // Launch the server.
212 defer close(done)
213 conn, err := lis.Accept() // Accept the connection only to close it immediately.
214 if err != nil {
215 t.Errorf("Error while accepting. Err: %v", err)
216 return
217 }
218 prevAt := time.Now()
219 conn.Close()
220 var prevDuration time.Duration
221 // Make sure the retry attempts are backed off properly.
222 for i := 0; i < 3; i++ {
223 conn, err := lis.Accept()
224 if err != nil {
225 t.Errorf("Error while accepting. Err: %v", err)
226 return
227 }
228 meow := time.Now()
229 conn.Close()
230 dr := meow.Sub(prevAt)
231 if dr <= prevDuration {
232 t.Errorf("Client backoff did not increase with retries. Previous duration: %v, current duration: %v", prevDuration, dr)
233 return
234 }
235 prevDuration = dr
236 prevAt = meow
237 }
238 }()
239 bc := backoff.Config{
240 BaseDelay: 200 * time.Millisecond,
241 Multiplier: 2.0,
242 Jitter: 0,
243 MaxDelay: 120 * time.Second,
244 }
245 cp := ConnectParams{
246 Backoff: bc,
247 MinConnectTimeout: 1 * time.Second,
248 }
249 cc, err := NewClient(lis.Addr().String(), WithTransportCredentials(insecure.NewCredentials()), WithConnectParams(cp))
250 if err != nil {
251 t.Fatalf("grpc.NewClient(%q) = %v", lis.Addr().String(), err)
252 }
253 defer cc.Close()
254 go stayConnected(cc)
255 <-done
256}
257
258// When creating a transport configured with n addresses, only calculate the
259// backoff once per "round" of attempts instead of once per address (n times

Callers

nothing calls this directly

Calls 12

NewCredentialsFunction · 0.92
WithTransportCredentialsFunction · 0.85
WithConnectParamsFunction · 0.85
stayConnectedFunction · 0.85
NowMethod · 0.80
NewClientFunction · 0.70
FatalfMethod · 0.65
CloseMethod · 0.65
ErrorfMethod · 0.65
StringMethod · 0.65
AcceptMethod · 0.45
AddrMethod · 0.45

Tested by

no test coverage detected