(t *testing.T)
| 212 | } |
| 213 | |
| 214 | func TestAnnotateContext_SupportsTimeouts(t *testing.T) { |
| 215 | ctx := context.Background() |
| 216 | expectedRPCName := "/example.Example/Example" |
| 217 | request, err := http.NewRequestWithContext(ctx, "GET", "http://example.com", nil) |
| 218 | if err != nil { |
| 219 | t.Fatalf(`http.NewRequestWithContext(ctx, "GET", "http://example.com", nil) failed with %v; want success`, err) |
| 220 | } |
| 221 | annotated, err := runtime.AnnotateContext(ctx, runtime.NewServeMux(), request, expectedRPCName) |
| 222 | if err != nil { |
| 223 | t.Errorf("runtime.AnnotateContext(ctx, %#v) failed with %v; want success", request, err) |
| 224 | return |
| 225 | } |
| 226 | if _, ok := annotated.Deadline(); ok { |
| 227 | // no deadline by default |
| 228 | t.Errorf("annotated.Deadline() = _, true; want _, false") |
| 229 | } |
| 230 | |
| 231 | const acceptableError = 50 * time.Millisecond |
| 232 | runtime.DefaultContextTimeout = 10 * time.Second |
| 233 | annotated, err = runtime.AnnotateContext(ctx, runtime.NewServeMux(), request, expectedRPCName) |
| 234 | if err != nil { |
| 235 | t.Errorf("runtime.AnnotateContext(ctx, %#v) failed with %v; want success", request, err) |
| 236 | return |
| 237 | } |
| 238 | deadline, ok := annotated.Deadline() |
| 239 | if !ok { |
| 240 | t.Errorf("annotated.Deadline() = _, false; want _, true") |
| 241 | } |
| 242 | if got, want := time.Until(deadline), runtime.DefaultContextTimeout; got-want > acceptableError || got-want < -acceptableError { |
| 243 | t.Errorf("time.Until(deadline) = %v; want %v; with error %v", got, want, acceptableError) |
| 244 | } |
| 245 | |
| 246 | for _, spec := range []struct { |
| 247 | timeout string |
| 248 | want time.Duration |
| 249 | }{ |
| 250 | { |
| 251 | timeout: "17H", |
| 252 | want: 17 * time.Hour, |
| 253 | }, |
| 254 | { |
| 255 | timeout: "19M", |
| 256 | want: 19 * time.Minute, |
| 257 | }, |
| 258 | { |
| 259 | timeout: "23S", |
| 260 | want: 23 * time.Second, |
| 261 | }, |
| 262 | { |
| 263 | timeout: "1009m", |
| 264 | want: 1009 * time.Millisecond, |
| 265 | }, |
| 266 | { |
| 267 | timeout: "1000003u", |
| 268 | want: 1000003 * time.Microsecond, |
| 269 | }, |
| 270 | { |
| 271 | timeout: "100000007n", |
nothing calls this directly
no test coverage detected