Files
gf/net/gtrace/gtrace_z_unit_test.go
John Guo dba903c13b add WithUUID for package gtrace (#2176)
* add WithUUID for package gtrace

* feat: improve import

Co-authored-by: houseme <housemecn@gmail.com>
2022-10-08 21:44:42 +08:00

56 lines
1.4 KiB
Go

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gtrace_test
import (
"context"
"testing"
"github.com/gogf/gf/v2/net/gtrace"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/text/gstr"
)
func TestWithTraceID(t *testing.T) {
var (
ctx = context.Background()
uuid = `a323f910-f690-11ec-963d-79c0b7fcf119`
)
gtest.C(t, func(t *gtest.T) {
newCtx, err := gtrace.WithTraceID(ctx, uuid)
t.AssertNE(err, nil)
t.Assert(newCtx, ctx)
})
gtest.C(t, func(t *gtest.T) {
var traceId = gstr.Replace(uuid, "-", "")
newCtx, err := gtrace.WithTraceID(ctx, traceId)
t.AssertNil(err)
t.AssertNE(newCtx, ctx)
t.Assert(gtrace.GetTraceID(ctx), "")
t.Assert(gtrace.GetTraceID(newCtx), traceId)
})
}
func TestWithUUID(t *testing.T) {
var (
ctx = context.Background()
uuid = `a323f910-f690-11ec-963d-79c0b7fcf119`
)
gtest.C(t, func(t *gtest.T) {
newCtx, err := gtrace.WithTraceID(ctx, uuid)
t.AssertNE(err, nil)
t.Assert(newCtx, ctx)
})
gtest.C(t, func(t *gtest.T) {
newCtx, err := gtrace.WithUUID(ctx, uuid)
t.AssertNil(err)
t.AssertNE(newCtx, ctx)
t.Assert(gtrace.GetTraceID(ctx), "")
t.Assert(gtrace.GetTraceID(newCtx), gstr.Replace(uuid, "-", ""))
})
}