MCPcopy
hub / github.com/cloudflare/cloudflared / TestWaitForBackoffFallback

Function TestWaitForBackoffFallback

supervisor/tunnel_test.go:34–125  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

32}
33
34func TestWaitForBackoffFallback(t *testing.T) {
35 maxRetries := uint(3)
36 backoff := retry.NewBackoff(maxRetries, 40*time.Millisecond, false)
37 backoff.Clock.After = immediateTimeAfter
38 log := zerolog.Nop()
39 resolveTTL := 10 * time.Second
40 mockFetcher := dynamicMockFetcher{
41 protocolPercents: edgediscovery.ProtocolPercents{edgediscovery.ProtocolPercent{Protocol: "quic", Percentage: 100}},
42 }
43 protocolSelector, err := connection.NewProtocolSelector(
44 "auto",
45 "",
46 false,
47 mockFetcher.fetch(),
48 resolveTTL,
49 &log,
50 )
51 require.NoError(t, err)
52
53 initProtocol := protocolSelector.Current()
54 assert.Equal(t, connection.QUIC, initProtocol)
55
56 protoFallback := &protocolFallback{
57 backoff,
58 initProtocol,
59 false,
60 }
61
62 // Retry #0 and #1. At retry #2, we switch protocol, so the fallback loop has one more retry than this
63 for i := 0; i < int(maxRetries-1); i++ {
64 protoFallback.BackoffTimer() // simulate retry
65 ok := selectNextProtocol(&log, protoFallback, protocolSelector, nil)
66 assert.True(t, ok)
67 assert.Equal(t, initProtocol, protoFallback.protocol)
68 }
69
70 // Retry fallback protocol
71 protoFallback.BackoffTimer() // simulate retry
72 ok := selectNextProtocol(&log, protoFallback, protocolSelector, nil)
73 assert.True(t, ok)
74 fallback, ok := protocolSelector.Fallback()
75 assert.True(t, ok)
76 assert.Equal(t, fallback, protoFallback.protocol)
77 assert.Equal(t, connection.HTTP2, protoFallback.protocol)
78
79 currentGlobalProtocol := protocolSelector.Current()
80 assert.Equal(t, initProtocol, currentGlobalProtocol)
81
82 // Simulate max retries again (retries reset after protocol switch)
83 for i := 0; i < int(maxRetries); i++ {
84 protoFallback.BackoffTimer()
85 }
86 // No protocol to fallback, return error
87 ok = selectNextProtocol(&log, protoFallback, protocolSelector, nil)
88 assert.False(t, ok)
89
90 protoFallback.reset()
91 protoFallback.BackoffTimer() // simulate retry

Callers

nothing calls this directly

Calls 9

fetchMethod · 0.95
CurrentMethod · 0.95
FallbackMethod · 0.95
resetMethod · 0.95
NewBackoffFunction · 0.92
NewProtocolSelectorFunction · 0.92
selectNextProtocolFunction · 0.85
BackoffTimerMethod · 0.80
EqualMethod · 0.65

Tested by

no test coverage detected