From 8e36717769f37df28ef76c40b547040a39830836 Mon Sep 17 00:00:00 2001 From: Evgeny <113383200+KlassnayaAfrodita@users.noreply.github.com> Date: Wed, 9 Oct 2024 14:48:14 +0300 Subject: [PATCH] Update context_test.go github.com/goravel/goravel/issues/504 tests fo WithContext --- context_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) 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") + } +}