summaryrefslogtreecommitdiff
path: root/internal/httpctx/httpctx_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/httpctx/httpctx_test.go')
-rw-r--r--internal/httpctx/httpctx_test.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/internal/httpctx/httpctx_test.go b/internal/httpctx/httpctx_test.go
new file mode 100644
index 0000000..6cd6521
--- /dev/null
+++ b/internal/httpctx/httpctx_test.go
@@ -0,0 +1,55 @@
+package httpctx
+
+import (
+ "context"
+ "testing"
+ "time"
+)
+
+func TestWithTimeoutUnlessSet_AlreadyHasDeadline(t *testing.T) {
+ t.Parallel()
+
+ parent, cancel := context.WithTimeout(context.Background(), time.Hour)
+ defer cancel()
+
+ ctx, childCancel := WithTimeoutUnlessSet(parent, time.Nanosecond)
+ defer childCancel()
+
+ if ctx != parent {
+ t.Fatal("expected same context when parent already has deadline")
+ }
+
+ deadline, ok := ctx.Deadline()
+ if !ok {
+ t.Fatal("expected deadline on context")
+ }
+ if time.Until(deadline) < 30*time.Minute {
+ t.Fatalf("expected parent ~1h deadline preserved, got %v", time.Until(deadline))
+ }
+}
+
+func TestWithTimeoutUnlessSet_NoDeadline(t *testing.T) {
+ t.Parallel()
+
+ ctx, cancel := WithTimeoutUnlessSet(context.Background(), 50*time.Millisecond)
+ defer cancel()
+
+ deadline, ok := ctx.Deadline()
+ if !ok {
+ t.Fatal("expected deadline")
+ }
+ if time.Until(deadline) > time.Second {
+ t.Fatalf("deadline too far: %v", deadline)
+ }
+}
+
+func TestWithTimeoutUnlessSet_NilUsesBackground(t *testing.T) {
+ t.Parallel()
+
+ ctx, cancel := WithTimeoutUnlessSet(nil, 50*time.Millisecond)
+ defer cancel()
+
+ if err := ctx.Err(); err != nil {
+ t.Fatalf("context should not be done: %v", err)
+ }
+}