| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/sha256" |
| 6 | "errors" |
| 7 | "io" |
| 8 | "sync" |
| 9 | "sync/atomic" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "golang.org/x/net/http2" |
| 14 | ) |
| 15 | |
| 16 | func freshProtocolHints() *protocolHints { |
| 17 | return &protocolHints{entries: make(map[[32]byte]*protocolHint)} |
| 18 | } |
| 19 | |
| 20 | func TestProtocolHintsBoundedConcurrentAndExpiring(t *testing.T) { |
| 21 | hints := freshProtocolHints() |
| 22 | key := sha256.Sum256([]byte("path")) |
| 23 | now := time.Now() |
| 24 | hints.failedHTTP2(key, "PROTOCOL_ERROR", hints.begin(), now) |
| 25 | var probes atomic.Int32 |
| 26 | var wg sync.WaitGroup |
| 27 | for range 32 { |
| 28 | wg.Go(func() { |
| 29 | if hints.selectHTTP1(key, now) != nil { |
| 30 | probes.Add(1) |
| 31 | } |
| 32 | }) |
| 33 | } |
| 34 | wg.Wait() |
| 35 | if probes.Load() != 1 { |
| 36 | t.Fatalf("concurrent probes = %d", probes.Load()) |
| 37 | } |
| 38 | old := hints.entries[key] |
| 39 | hints.settle(key, old, "active") |
| 40 | if hints.selectHTTP1(key, now) != old { |
| 41 | t.Fatal("successful path not reused") |
| 42 | } |
| 43 | hints.settle(key, old, "pending") // late cancellation must not undo success |
| 44 | if old.mode != "active" { |
| 45 | t.Fatal("late cancellation reset active hint") |
| 46 | } |
| 47 | hints.settle(key, old, "disabled") |
| 48 | hints.settle(key, old, "active") // late success must not undo failure |
| 49 | hints.failedHTTP2(key, "HTTP_1_1_REQUIRED", hints.begin(), now.Add(time.Minute)) |
| 50 | if hints.selectHTTP1(key, now.Add(time.Minute)) != nil || old.reason != "PROTOCOL_ERROR" { |
| 51 | t.Fatal("failed compatibility was rearmed or overwritten") |
| 52 | } |
| 53 | next := now.Add(protocolCompatibilityTTL) |
| 54 | if hints.selectHTTP1(key, next) != nil { |
| 55 | t.Fatal("expired hint remained selected") |
| 56 | } |
| 57 | hints.failedHTTP2(key, "PROTOCOL_ERROR", hints.begin(), next) |
| 58 | hints.settle(key, old, "disabled") |
| 59 | if hints.selectHTTP1(key, next) == nil { |
| 60 | t.Fatal("old completion disabled new generation") |
| 61 | } |
| 62 | for i := range protocolCompatibilityLimit + 10 { |
| 63 | k := sha256.Sum256([]byte{byte(i), byte(i >> 8)}) |
| 64 | hints.failedHTTP2(k, "INTERNAL_ERROR", hints.begin(), next) |
| 65 | } |
| 66 | if len(hints.entries) != protocolCompatibilityLimit { |
| 67 | t.Fatalf("unbounded hint count: %d", len(hints.entries)) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestInternalErrorNeedsALaterRequest(t *testing.T) { |
| 72 | hints := freshProtocolHints() |
| 73 | key := sha256.Sum256([]byte("path")) |
| 74 | now := time.Now() |
| 75 | first, sibling := hints.begin(), hints.begin() |
| 76 | hints.failedHTTP2(key, "INTERNAL_ERROR", first, now) |
| 77 | // A sibling that started before the observation shares its cause, e.g. one |
| 78 | // connection-level reset seen by several concurrent streams. |
| 79 | hints.failedHTTP2(key, "INTERNAL_ERROR", sibling, now.Add(time.Second)) |
| 80 | if hints.selectHTTP1(key, now.Add(time.Second)) != nil { |
| 81 | t.Fatal("one INTERNAL_ERROR event selected HTTP/1.1") |
| 82 | } |
| 83 | later := now.Add(2 * time.Second) |
| 84 | hints.failedHTTP2(key, "INTERNAL_ERROR", hints.begin(), later) |
| 85 | h := hints.selectHTTP1(key, later) |
| 86 | if h == nil || h.reason != "INTERNAL_ERROR" || !h.until.Equal(later.Add(protocolCompatibilityTTL)) { |
| 87 | t.Fatalf("repeated INTERNAL_ERROR not confirmed: %+v", h) |
| 88 | } |
| 89 | other := sha256.Sum256([]byte("other")) |
| 90 | hints.failedHTTP2(other, "INTERNAL_ERROR", hints.begin(), now) |
| 91 | hints.failedHTTP2(other, "PROTOCOL_ERROR", hints.begin(), now) |
| 92 | if h := hints.selectHTTP1(other, now); h == nil || h.reason != "PROTOCOL_ERROR" { |
| 93 | t.Fatalf("explicit protocol evidence waited for a repeat: %+v", h) |
| 94 | } |
| 95 | expired := now.Add(protocolCompatibilityTTL) |
| 96 | third := sha256.Sum256([]byte("third")) |
| 97 | hints.failedHTTP2(third, "INTERNAL_ERROR", hints.begin(), now) |
| 98 | hints.failedHTTP2(third, "INTERNAL_ERROR", hints.begin(), expired) |
| 99 | if hints.selectHTTP1(third, expired) != nil { |
| 100 | t.Fatal("expired observation was confirmed") |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | func TestProtocolFailureOnlyUsesTypedEvidence(t *testing.T) { |
| 105 | for _, tc := range []struct { |
| 106 | name string |
| 107 | err error |
| 108 | want bool |
| 109 | }{ |
| 110 | {"internal-once", http2.StreamError{Code: http2.ErrCodeInternal}, false}, |
| 111 | {"protocol", http2.ConnectionError(http2.ErrCodeProtocol), true}, |
| 112 | {"requires-http1", http2.StreamError{Code: http2.ErrCodeHTTP11Required}, true}, |
| 113 | {"wording", errors.New("HTTP/2 INTERNAL_ERROR secret-token"), false}, |
| 114 | {"eof", io.ErrUnexpectedEOF, false}, |
| 115 | {"api", &APIError{Status: 401, Body: "INTERNAL_ERROR"}, false}, |
| 116 | {"cancel", context.Canceled, false}, |
| 117 | {"refused", http2.StreamError{Code: http2.ErrCodeRefusedStream}, false}, |
| 118 | } { |
| 119 | t.Run(tc.name, func(t *testing.T) { |
| 120 | hints := freshProtocolHints() |
| 121 | tr := &protocolTransport{hints: hints} |
| 122 | tr.failed(t.Context(), [32]byte{}, nil, hints.begin(), tc.err) |
| 123 | if got := hints.selectHTTP1([32]byte{}, time.Now()) != nil; got != tc.want { |
| 124 | t.Fatalf("selected=%t want=%t", got, tc.want) |
| 125 | } |
| 126 | }) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func TestOnlyCompatibilityTriggersAreTerminal(t *testing.T) { |
| 131 | for _, tc := range []struct { |
| 132 | name string |
| 133 | err error |
| 134 | terminal bool |
| 135 | }{ |
| 136 | {"internal", errors.Join(http2.StreamError{Code: http2.ErrCodeInternal}, io.ErrUnexpectedEOF), true}, |
| 137 | {"protocol", http2.ConnectionError(http2.ErrCodeProtocol), true}, |
| 138 | {"requires-http1", http2.StreamError{Code: http2.ErrCodeHTTP11Required}, true}, |
| 139 | // The peer guarantees it never processed a refused stream. |
| 140 | {"refused", errors.Join(http2.StreamError{Code: http2.ErrCodeRefusedStream}, io.ErrUnexpectedEOF), false}, |
| 141 | {"eof", io.ErrUnexpectedEOF, false}, |
| 142 | {"send-timeout", &RequestSendTimeoutError{}, false}, |
| 143 | } { |
| 144 | t.Run(tc.name, func(t *testing.T) { |
| 145 | recovery := ClassifyRecovery(tc.err) |
| 146 | if protocolFailureIsTerminal(tc.err) != tc.terminal || (recovery.Phase == "protocol") != tc.terminal { |
| 147 | t.Fatalf("terminal=%t phase=%q", protocolFailureIsTerminal(tc.err), recovery.Phase) |
| 148 | } |
| 149 | if tc.terminal && (IsConnReset(tc.err) || recovery.Retryable) { |
| 150 | t.Fatal("terminal protocol failure became retryable") |
| 151 | } |
| 152 | }) |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | func TestInternalErrorConfirmationIgnoresClockResolution(t *testing.T) { |
| 157 | hints := freshProtocolHints() |
| 158 | key := sha256.Sum256([]byte("path")) |
| 159 | tick := time.Now() // every request below lands in the same clock tick |
| 160 | first, concurrent := hints.begin(), hints.begin() |
| 161 | hints.failedHTTP2(key, "INTERNAL_ERROR", first, tick) |
| 162 | hints.failedHTTP2(key, "INTERNAL_ERROR", concurrent, tick) |
| 163 | if hints.selectHTTP1(key, tick) != nil { |
| 164 | t.Fatal("a request already running at the observation confirmed it") |
| 165 | } |
| 166 | hints.failedHTTP2(key, "INTERNAL_ERROR", hints.begin(), tick) |
| 167 | if hints.selectHTTP1(key, tick) == nil { |
| 168 | t.Fatal("a later request in the same clock tick did not confirm the route") |
| 169 | } |
| 170 | } |
| 171 |