2021-08-20 15:44:08 +08:00
|
|
|
// 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"
|
2021-08-20 15:44:08 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_New(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
ctx := gctx.New()
|
|
|
|
t.AssertNE(ctx, nil)
|
2021-12-16 23:38:40 +08:00
|
|
|
t.AssertNE(gctx.CtxId(ctx), "")
|
2021-08-20 15:44:08 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_WithCtx(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
ctx := context.WithValue(context.TODO(), "TEST", 1)
|
|
|
|
ctx = gctx.WithCtx(ctx)
|
2021-12-16 23:38:40 +08:00
|
|
|
t.AssertNE(gctx.CtxId(ctx), "")
|
2021-08-20 15:44:08 +08:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|