From edf7a75045f83189112bf8d666e7116a7d756be7 Mon Sep 17 00:00:00 2001 From: Makonike Date: Mon, 4 Sep 2023 14:23:12 +0800 Subject: [PATCH] fix --- dtmsvr/utils.go | 29 +---------------------------- dtmsvr/utils_test.go | 23 ++++++++++++++++++----- 2 files changed, 19 insertions(+), 33 deletions(-) diff --git a/dtmsvr/utils.go b/dtmsvr/utils.go index ac60f3c..16f28f2 100644 --- a/dtmsvr/utils.go +++ b/dtmsvr/utils.go @@ -18,7 +18,6 @@ import ( "github.com/dtm-labs/dtm/dtmsvr/storage" "github.com/dtm-labs/dtm/dtmsvr/storage/registry" "github.com/lithammer/shortuuid/v3" - "google.golang.org/grpc/metadata" ) type branchStatus struct { @@ -69,27 +68,7 @@ type cancelCtx struct { } type timerCtx struct { - cancelCtx *cancelCtx -} - -func (*timerCtx) Deadline() (deadline time.Time, ok bool) { - return -} - -func (*timerCtx) Done() <-chan struct{} { - return nil -} - -func (*timerCtx) Err() error { - return nil -} - -func (*timerCtx) Value(key any) any { - return nil -} - -func (e *timerCtx) String() string { - return "" + cancelCtx } // CopyContext copy context with value and grpc metadata @@ -104,12 +83,6 @@ func CopyContext(ctx context.Context) context.Context { for k, v := range kv { newCtx = context.WithValue(newCtx, k, v) } - if md, ok := metadata.FromIncomingContext(ctx); ok { - newCtx = metadata.NewIncomingContext(newCtx, md) - } - if md, ok := metadata.FromOutgoingContext(ctx); ok { - newCtx = metadata.NewOutgoingContext(newCtx, md) - } return newCtx } diff --git a/dtmsvr/utils_test.go b/dtmsvr/utils_test.go index cd3007f..3349ad0 100644 --- a/dtmsvr/utils_test.go +++ b/dtmsvr/utils_test.go @@ -9,6 +9,7 @@ package dtmsvr import ( "context" "testing" + "time" "github.com/stretchr/testify/assert" "google.golang.org/grpc/metadata" @@ -32,8 +33,6 @@ func TestSetNextCron(t *testing.T) { assert.Equal(t, int64(3), tg.getNextCronInterval(cronReset)) } -type testContextType string - func TestCopyContext(t *testing.T) { var key testContextType = "key" var value testContextType = "value" @@ -46,17 +45,31 @@ func TestCopyContext(t *testing.T) { assert.Nil(t, newCtx) } +type testContextType string + func TestCopyContextRecursive(t *testing.T) { var key testContextType = "key" + var key2 testContextType = "key2" + var key3 testContextType = "key3" var value testContextType = "value" + var value2 testContextType = "value2" + var value3 testContextType = "value3" var nestedKey testContextType = "nested_key" var nestedValue testContextType = "nested_value" ctxWithValue := context.WithValue(context.Background(), key, value) nestedCtx := context.WithValue(ctxWithValue, nestedKey, nestedValue) - newCtx := CopyContext(nestedCtx) + cancelCtxx, cancel := context.WithCancel(nestedCtx) + defer cancel() + timerCtxx, cancel2 := context.WithTimeout(cancelCtxx, time.Duration(10)*time.Second) + defer cancel2() + timer2 := context.WithValue(timerCtxx, key2, value2) + timer3 := context.WithValue(timer2, key3, value3) + newCtx := CopyContext(timer3) - assert.Equal(t, nestedCtx.Value(nestedKey), newCtx.Value(nestedKey)) - assert.Equal(t, nestedCtx.Value(key), newCtx.Value(key)) + assert.Equal(t, timer3.Value(nestedKey), newCtx.Value(nestedKey)) + assert.Equal(t, timer3.Value(key), newCtx.Value(key)) + assert.Equal(t, timer3.Value(key2), newCtx.Value(key2)) + assert.Equal(t, timer3.Value(key3), newCtx.Value(key3)) } func TestCopyContextWithMetadata(t *testing.T) {