diff --git a/context_test.go b/context_test.go index 9670eee..6d3ab59 100644 --- a/context_test.go +++ b/context_test.go @@ -30,3 +30,35 @@ func TestContext(t *testing.T) { assert.Equal(t, "one", ctx.Value(1)) assert.Equal(t, "two point two", ctx.Value(2.2)) } + +func TestWithContext(t *testing.T) { + httpCtx := Background() + + timeoutCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + + httpCtx.WithContext(timeoutCtx) + + ctx := httpCtx.Context() + assert.Equal(t, timeoutCtx, ctx) + + deadline, ok := ctx.Deadline() + assert.True(t, ok, "Deadline should be set") + assert.WithinDuration(t, time.Now().Add(2*time.Second), deadline, 50*time.Millisecond, "Deadline should be approximately 2 seconds from now") + + select { + case <-ctx.Done(): + assert.Fail(t, "context should not be done yet") + default: + + } + + time.Sleep(2 * time.Second) + + select { + case <-ctx.Done(): + assert.Equal(t, context.DeadlineExceeded, ctx.Err(), "context should be exceeded") + default: + assert.Fail(t, "context should be done after timeout") + } +}