github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/tracing/util_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package tracing_test
    15  
    16  import (
    17  	"context"
    18  	"testing"
    19  
    20  	basictracer "github.com/opentracing/basictracer-go"
    21  	"github.com/opentracing/opentracing-go"
    22  	. "github.com/whtcorpsinc/check"
    23  	"github.com/whtcorpsinc/milevadb/soliton/tracing"
    24  )
    25  
    26  var _ = Suite(&testTraceSuite{})
    27  
    28  func TestT(t *testing.T) {
    29  	TestingT(t)
    30  }
    31  
    32  type testTraceSuite struct {
    33  }
    34  
    35  func (s *testTraceSuite) TestSpanFromContext(c *C) {
    36  	ctx := context.TODO()
    37  	noopSp := tracing.SpanFromContext(ctx)
    38  	// test noop span
    39  	_, ok := noopSp.Tracer().(opentracing.NoopTracer)
    40  	c.Assert(ok, IsTrue)
    41  
    42  	// test milevadb tracing
    43  	defCauslectedSpan := make([]basictracer.RawSpan, 1)
    44  	sp := tracing.NewRecordedTrace("test", func(sp basictracer.RawSpan) {
    45  		defCauslectedSpan = append(defCauslectedSpan, sp)
    46  	})
    47  	sp.Finish()
    48  	opentracing.ContextWithSpan(ctx, sp)
    49  	child := tracing.SpanFromContext(ctx)
    50  	child.Finish()
    51  
    52  	// verify second span's operation is not nil, this way we can ensure
    53  	// callback logic works.
    54  	c.Assert(defCauslectedSpan[0].Operation, NotNil)
    55  }
    56  
    57  func (s *testTraceSuite) TestChildSpanFromContext(c *C) {
    58  	ctx := context.TODO()
    59  	noopSp, _ := tracing.ChildSpanFromContxt(ctx, "")
    60  	_, ok := noopSp.Tracer().(opentracing.NoopTracer)
    61  	c.Assert(ok, IsTrue)
    62  
    63  	// test milevadb tracing
    64  	defCauslectedSpan := make([]basictracer.RawSpan, 1)
    65  	sp := tracing.NewRecordedTrace("test", func(sp basictracer.RawSpan) {
    66  		defCauslectedSpan = append(defCauslectedSpan, sp)
    67  	})
    68  	sp.Finish()
    69  	opentracing.ContextWithSpan(ctx, sp)
    70  	child, _ := tracing.ChildSpanFromContxt(ctx, "test_child")
    71  	child.Finish()
    72  
    73  	// verify second span's operation is not nil, this way we can ensure
    74  	// callback logic works.
    75  	c.Assert(defCauslectedSpan[1].Operation, NotNil)
    76  
    77  }
    78  
    79  func (s *testTraceSuite) TestFollowFrom(c *C) {
    80  	var defCauslectedSpans []basictracer.RawSpan
    81  	// first start a root span
    82  	sp1 := tracing.NewRecordedTrace("test", func(sp basictracer.RawSpan) {
    83  		defCauslectedSpans = append(defCauslectedSpans, sp)
    84  	})
    85  	sp2 := sp1.Tracer().StartSpan("follow_from", opentracing.FollowsFrom(sp1.Context()))
    86  	sp1.Finish()
    87  	sp2.Finish()
    88  	c.Assert(defCauslectedSpans[1].Operation, Equals, "follow_from")
    89  	c.Assert(defCauslectedSpans[1].ParentSpanID, Not(Equals), uint64(0))
    90  	// only root span has 0 parent id
    91  	c.Assert(defCauslectedSpans[0].ParentSpanID, Equals, uint64(0))
    92  }
    93  
    94  func (s *testTraceSuite) TestCreateSapnBeforeSetupGlobalTracer(c *C) {
    95  	var defCauslectedSpans []basictracer.RawSpan
    96  	sp := opentracing.StartSpan("before")
    97  	sp.Finish()
    98  
    99  	// first start a root span
   100  	sp1 := tracing.NewRecordedTrace("test", func(sp basictracer.RawSpan) {
   101  		defCauslectedSpans = append(defCauslectedSpans, sp)
   102  	})
   103  	sp1.Finish()
   104  
   105  	// sp is a span started before we setup global tracer; hence such span will be
   106  	// droped.
   107  	c.Assert(len(defCauslectedSpans), Equals, 1)
   108  }
   109  
   110  func (s *testTraceSuite) TestTreeRelationship(c *C) {
   111  	var defCauslectedSpans []basictracer.RawSpan
   112  	ctx := context.TODO()
   113  	// first start a root span
   114  	sp1 := tracing.NewRecordedTrace("test", func(sp basictracer.RawSpan) {
   115  		defCauslectedSpans = append(defCauslectedSpans, sp)
   116  	})
   117  
   118  	// then causetstore such root span into context
   119  	ctx = opentracing.ContextWithSpan(ctx, sp1)
   120  
   121  	// create children span from context
   122  	sp2, ctx := tracing.ChildSpanFromContxt(ctx, "parent")
   123  	sp3, _ := tracing.ChildSpanFromContxt(ctx, "child")
   124  
   125  	// notify span that we are about to reach end of journey.
   126  	sp1.Finish()
   127  	sp2.Finish()
   128  	sp3.Finish()
   129  
   130  	// ensure length of defCauslectedSpans is greather than 0
   131  	c.Assert(len(defCauslectedSpans), Greater, 0)
   132  	if len(defCauslectedSpans) > 0 {
   133  		c.Assert(defCauslectedSpans[0].Operation, Equals, "test")
   134  		c.Assert(defCauslectedSpans[1].Operation, Equals, "parent")
   135  		c.Assert(defCauslectedSpans[2].Operation, Equals, "child")
   136  		// check tree relationship
   137  		// sp1 is parent of sp2. And sp2 is parent of sp3.
   138  		c.Assert(defCauslectedSpans[1].ParentSpanID, Equals, defCauslectedSpans[0].Context.SpanID)
   139  		c.Assert(defCauslectedSpans[2].ParentSpanID, Equals, defCauslectedSpans[1].Context.SpanID)
   140  	}
   141  }