github.com/cloudwego/kitex@v0.9.0/pkg/rpcinfo/ctx_test.go (about) 1 /* 2 * Copyright 2021 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package rpcinfo_test 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/cloudwego/kitex/internal/test" 24 "github.com/cloudwego/kitex/pkg/rpcinfo" 25 "github.com/cloudwego/kitex/pkg/utils" 26 ) 27 28 func TestNewCtxWithRPCInfo(t *testing.T) { 29 ri := rpcinfo.NewRPCInfo(nil, nil, nil, nil, nil) 30 ctx0 := context.Background() 31 ctx1 := rpcinfo.NewCtxWithRPCInfo(ctx0, ri) 32 33 test.Assert(t, ctx0 != ctx1) 34 35 // nil safe 36 ctx2 := rpcinfo.NewCtxWithRPCInfo(ctx0, nil) 37 test.Assert(t, ctx0 == ctx2) 38 } 39 40 func TestGetCtxRPCInfo(t *testing.T) { 41 ri0 := rpcinfo.NewRPCInfo(nil, nil, nil, nil, nil) 42 ctx := context.Background() 43 44 test.Assert(t, rpcinfo.GetRPCInfo(ctx) == nil) 45 46 ctx = rpcinfo.NewCtxWithRPCInfo(ctx, ri0) 47 ri1 := rpcinfo.GetRPCInfo(ctx) 48 test.Assert(t, ri1 != nil) 49 test.Assert(t, ri0 == ri1) 50 } 51 52 func TestPutRPCInfo(t *testing.T) { 53 // pre-declared variables and initialization to ensure readability 54 method := "TestMethod" 55 svcName := "TestServiceName" 56 netAddr := utils.NewNetAddr("TestNetWork", "TestAddress") 57 tags := map[string]string{"MapTestKey": "MapTestValue"} 58 59 ri := rpcinfo.NewRPCInfo( 60 rpcinfo.NewEndpointInfo(svcName, method, netAddr, tags), 61 rpcinfo.NewEndpointInfo(svcName, method, netAddr, tags), 62 rpcinfo.NewInvocation(svcName, method), 63 rpcinfo.NewRPCConfig(), 64 rpcinfo.NewRPCStats(), 65 ) 66 67 // nil safe 68 rpcinfo.PutRPCInfo(nil) 69 rpcinfo.PutRPCInfo(ri) 70 71 test.Assert(t, ri.From() == nil) 72 test.Assert(t, ri.To() == nil) 73 test.Assert(t, ri.Stats() == nil) 74 test.Assert(t, ri.Invocation() == nil) 75 test.Assert(t, ri.Config() == nil) 76 test.Assert(t, ri.Stats() == nil) 77 }