From 05e37ee20fe3b23726e63ebb881afecd722a8395 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Mon, 5 Apr 2021 22:59:24 +0800 Subject: [PATCH] refactor - remove ShrinkDeadline, it's the same as context.WithTimeout (#599) --- core/contextx/deadline.go | 12 ------------ core/contextx/deadline_test.go | 31 ------------------------------- 2 files changed, 43 deletions(-) delete mode 100644 core/contextx/deadline.go delete mode 100644 core/contextx/deadline_test.go diff --git a/core/contextx/deadline.go b/core/contextx/deadline.go deleted file mode 100644 index 0a3c3fae..00000000 --- a/core/contextx/deadline.go +++ /dev/null @@ -1,12 +0,0 @@ -package contextx - -import ( - "context" - "time" -) - -// ShrinkDeadline returns a new Context with proper deadline base on the given ctx and timeout. -// And returns a cancel function as well. -func ShrinkDeadline(ctx context.Context, timeout time.Duration) (context.Context, func()) { - return context.WithTimeout(ctx, timeout) -} diff --git a/core/contextx/deadline_test.go b/core/contextx/deadline_test.go deleted file mode 100644 index d33adfba..00000000 --- a/core/contextx/deadline_test.go +++ /dev/null @@ -1,31 +0,0 @@ -package contextx - -import ( - "context" - "testing" - "time" - - "github.com/stretchr/testify/assert" -) - -func TestShrinkDeadlineLess(t *testing.T) { - deadline := time.Now().Add(time.Second) - ctx, cancel := context.WithDeadline(context.Background(), deadline) - defer cancel() - ctx, cancel = ShrinkDeadline(ctx, time.Minute) - defer cancel() - dl, ok := ctx.Deadline() - assert.True(t, ok) - assert.Equal(t, deadline, dl) -} - -func TestShrinkDeadlineMore(t *testing.T) { - deadline := time.Now().Add(time.Minute) - ctx, cancel := context.WithDeadline(context.Background(), deadline) - defer cancel() - ctx, cancel = ShrinkDeadline(ctx, time.Second) - defer cancel() - dl, ok := ctx.Deadline() - assert.True(t, ok) - assert.True(t, dl.Before(deadline)) -}