(t *testing.T)
| 12 | ) |
| 13 | |
| 14 | func TestCheckTimeout(t *testing.T) { |
| 15 | tests := []struct { |
| 16 | name string |
| 17 | setup func() context.Context |
| 18 | fail bool |
| 19 | }{ |
| 20 | { |
| 21 | name: "WithoutTimeout", |
| 22 | setup: context.Background, |
| 23 | fail: false, |
| 24 | }, |
| 25 | { |
| 26 | name: "ActiveTimerContext", |
| 27 | setup: func() context.Context { |
| 28 | req := httptest.NewRequest(http.MethodGet, "/test", nil) |
| 29 | newReq, _ := server.StartRequestTimer(req, 10*time.Second) |
| 30 | return newReq.Context() |
| 31 | }, |
| 32 | fail: false, |
| 33 | }, |
| 34 | { |
| 35 | name: "CancelledContext", |
| 36 | setup: func() context.Context { |
| 37 | req := httptest.NewRequest(http.MethodGet, "/test", nil) |
| 38 | newReq, cancel := server.StartRequestTimer(req, 10*time.Second) |
| 39 | cancel() // Cancel immediately |
| 40 | return newReq.Context() |
| 41 | }, |
| 42 | fail: true, |
| 43 | }, |
| 44 | { |
| 45 | name: "DeadlineExceeded", |
| 46 | setup: func() context.Context { |
| 47 | ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond) |
| 48 | defer cancel() |
| 49 | time.Sleep(time.Millisecond * 10) // Ensure timeout |
| 50 | return ctx |
| 51 | }, |
| 52 | fail: true, |
| 53 | }, |
| 54 | } |
| 55 | |
| 56 | for _, tt := range tests { |
| 57 | t.Run(tt.name, func(t *testing.T) { |
| 58 | ctx := tt.setup() |
| 59 | err := server.CheckTimeout(ctx) |
| 60 | |
| 61 | if tt.fail { |
| 62 | require.Error(t, err) |
| 63 | } else { |
| 64 | require.NoError(t, err) |
| 65 | } |
| 66 | }) |
| 67 | } |
| 68 | } |
nothing calls this directly
no test coverage detected