gf/os/gctx/gctx_z_unit_test.go

42 lines
978 B
Go
Raw Normal View History

// 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 gctx_test
import (
"context"
"testing"
2021-11-13 23:34:16 +08:00
"github.com/gogf/gf/v2/os/gctx"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/test/gtest"
)
func Test_New(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
ctx := gctx.New()
t.AssertNE(ctx, nil)
t.AssertNE(gctx.CtxId(ctx), "")
})
}
func Test_WithCtx(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
ctx := context.WithValue(context.TODO(), "TEST", 1)
ctx = gctx.WithCtx(ctx)
t.AssertNE(gctx.CtxId(ctx), "")
t.Assert(ctx.Value("TEST"), 1)
})
}
2022-07-12 19:27:42 +08:00
func Test_SetInitCtx(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
ctx := context.WithValue(context.TODO(), "TEST", 1)
gctx.SetInitCtx(ctx)
t.AssertNE(gctx.GetInitCtx(), "")
t.Assert(gctx.GetInitCtx().Value("TEST"), 1)
})
}