|
|
@ -7,9 +7,11 @@ |
|
|
package dtmsvr |
|
|
package dtmsvr |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
|
|
|
"context" |
|
|
"testing" |
|
|
"testing" |
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert" |
|
|
"github.com/stretchr/testify/assert" |
|
|
|
|
|
"google.golang.org/grpc/metadata" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
func TestUtils(t *testing.T) { |
|
|
func TestUtils(t *testing.T) { |
|
|
@ -29,3 +31,41 @@ func TestSetNextCron(t *testing.T) { |
|
|
tg.TimeoutToFail = 3 |
|
|
tg.TimeoutToFail = 3 |
|
|
assert.Equal(t, int64(3), tg.getNextCronInterval(cronReset)) |
|
|
assert.Equal(t, int64(3), tg.getNextCronInterval(cronReset)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestCopyContext(t *testing.T) { |
|
|
|
|
|
ctxWithValue := context.WithValue(context.Background(), "key", "value") |
|
|
|
|
|
newCtx := CopyContext(ctxWithValue) |
|
|
|
|
|
assert.Equal(t, ctxWithValue.Value("key"), newCtx.Value("key")) |
|
|
|
|
|
|
|
|
|
|
|
var ctx context.Context |
|
|
|
|
|
newCtx = CopyContext(ctx) |
|
|
|
|
|
assert.Nil(t, newCtx) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestCopyContextRecursive(t *testing.T) { |
|
|
|
|
|
ctxWithValue := context.WithValue(context.Background(), "key", "value") |
|
|
|
|
|
nestedCtx := context.WithValue(ctxWithValue, "nested_key", "nested_value") |
|
|
|
|
|
newCtx := CopyContext(nestedCtx) |
|
|
|
|
|
|
|
|
|
|
|
assert.Equal(t, nestedCtx.Value("nested_key"), newCtx.Value("nested_key")) |
|
|
|
|
|
assert.Equal(t, nestedCtx.Value("key"), newCtx.Value("key")) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestCopyContextWithMetadata(t *testing.T) { |
|
|
|
|
|
md := metadata.New(map[string]string{"key": "value"}) |
|
|
|
|
|
ctx := metadata.NewIncomingContext(context.Background(), md) |
|
|
|
|
|
newCtx := CopyContext(ctx) |
|
|
|
|
|
|
|
|
|
|
|
copiedMD, ok := metadata.FromIncomingContext(newCtx) |
|
|
|
|
|
assert.True(t, ok) |
|
|
|
|
|
assert.Equal(t, 1, len(copiedMD["key"])) |
|
|
|
|
|
assert.Equal(t, "value", copiedMD["key"][0]) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func BenchmarkCopyContext(b *testing.B) { |
|
|
|
|
|
ctx := context.WithValue(context.Background(), "key", "value") |
|
|
|
|
|
b.ResetTimer() |
|
|
|
|
|
for i := 0; i < b.N; i++ { |
|
|
|
|
|
CopyContext(ctx) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|