dubbo.apache.org/dubbo-go/v3@v3.1.1/remoting/getty/listener_test.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package getty
    19  
    20  import (
    21  	"context"
    22  	"testing"
    23  )
    24  
    25  import (
    26  	"github.com/opentracing/opentracing-go"
    27  	"github.com/opentracing/opentracing-go/mocktracer"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  )
    31  
    32  import (
    33  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    34  	"dubbo.apache.org/dubbo-go/v3/protocol/invocation"
    35  )
    36  
    37  // test rebuild the ctx
    38  func TestRebuildCtx(t *testing.T) {
    39  	opentracing.SetGlobalTracer(mocktracer.New())
    40  	attach := make(map[string]interface{}, 10)
    41  	attach[constant.VersionKey] = "1.0"
    42  	attach[constant.GroupKey] = "MyGroup"
    43  	inv := invocation.NewRPCInvocation("MethodName", []interface{}{"OK", "Hello"}, attach)
    44  
    45  	// attachment doesn't contains any tracing key-value pair,
    46  	ctx := rebuildCtx(inv)
    47  	assert.NotNil(t, ctx)
    48  	assert.Nil(t, ctx.Value(constant.TracingRemoteSpanCtx))
    49  
    50  	span, ctx := opentracing.StartSpanFromContext(ctx, "Test-Client")
    51  	assert.NotNil(t, ctx)
    52  	err := injectTraceCtx(span, inv)
    53  	assert.NoError(t, err)
    54  
    55  	// rebuild the context success
    56  	inv = invocation.NewRPCInvocation("MethodName", []interface{}{"OK", "Hello"}, attach)
    57  	ctx = rebuildCtx(inv)
    58  	span.Finish()
    59  	assert.NotNil(t, ctx)
    60  	assert.NotNil(t, ctx.Value(constant.TracingRemoteSpanCtx))
    61  }
    62  
    63  // rebuildCtx rebuild the context by attachment.
    64  // Once we decided to transfer more context's key-value, we should change this.
    65  // now we only support rebuild the tracing context
    66  func rebuildCtx(inv *invocation.RPCInvocation) context.Context {
    67  	ctx := context.WithValue(context.Background(), constant.DubboCtxKey("attachment"), inv.Attachments())
    68  
    69  	// actually, if user do not use any opentracing framework, the err will not be nil.
    70  	spanCtx, err := opentracing.GlobalTracer().Extract(opentracing.TextMap,
    71  		opentracing.TextMapCarrier(filterContext(inv.Attachments())))
    72  	if err == nil {
    73  		ctx = context.WithValue(ctx, constant.TracingRemoteSpanCtx, spanCtx)
    74  	}
    75  	return ctx
    76  }