github.com/gogf/gf/v2@v2.7.4/os/gctx/gctx_z_unit_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gctx_test
     8  
     9  import (
    10  	"context"
    11  	"testing"
    12  
    13  	"github.com/gogf/gf/v2/os/gctx"
    14  	"github.com/gogf/gf/v2/test/gtest"
    15  )
    16  
    17  func Test_New(t *testing.T) {
    18  	gtest.C(t, func(t *gtest.T) {
    19  		ctx := gctx.New()
    20  		t.AssertNE(ctx, nil)
    21  		t.AssertNE(gctx.CtxId(ctx), "")
    22  	})
    23  }
    24  
    25  func Test_WithCtx(t *testing.T) {
    26  	gtest.C(t, func(t *gtest.T) {
    27  		ctx := context.WithValue(context.TODO(), "TEST", 1)
    28  		ctx = gctx.WithCtx(ctx)
    29  		t.AssertNE(gctx.CtxId(ctx), "")
    30  		t.Assert(ctx.Value("TEST"), 1)
    31  	})
    32  }
    33  
    34  func Test_SetInitCtx(t *testing.T) {
    35  	gtest.C(t, func(t *gtest.T) {
    36  		ctx := context.WithValue(context.TODO(), "TEST", 1)
    37  		gctx.SetInitCtx(ctx)
    38  		t.AssertNE(gctx.GetInitCtx(), "")
    39  		t.Assert(gctx.GetInitCtx().Value("TEST"), 1)
    40  	})
    41  }