trpc.group/trpc-go/trpc-go@v1.0.3/rpcz/noopspan_test.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package rpcz
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/stretchr/testify/require"
    20  )
    21  
    22  func Test_noopSpan(t *testing.T) {
    23  	s := noopSpan{}
    24  	t.Run("AddEvent", func(t *testing.T) {
    25  		s.AddEvent("happy birthday, tencent 24th anniversary")
    26  		require.Equal(t, noopSpan{}, s)
    27  	})
    28  	t.Run("ID", func(t *testing.T) {
    29  		require.Equal(t, nilSpanID, s.ID())
    30  		require.Equal(t, noopSpan{}, s)
    31  	})
    32  	t.Run("Name", func(t *testing.T) {
    33  		require.Empty(t, s.Name())
    34  	})
    35  	t.Run("SetAttribute", func(t *testing.T) {
    36  		s.SetAttribute("Friday", "go home early :)")
    37  		require.Equal(t, noopSpan{}, s)
    38  	})
    39  	t.Run("NewChild", func(t *testing.T) {
    40  		s, _ := s.NewChild("child")
    41  		require.Equal(t, noopSpan{}, s)
    42  	})
    43  	t.Run("NoopSpan Comparison", func(t *testing.T) {
    44  		s1 := noopSpan{}
    45  		s2 := noopSpan{}
    46  		require.True(t, s1 == s2)
    47  		require.Same(t, &noopSpan{}, &s1)
    48  	})
    49  	t.Run("Attribute", func(t *testing.T) {
    50  		s := noopSpan{}
    51  		const attributeName = "attribute"
    52  		s.SetAttribute(attributeName, "value")
    53  		attribute, ok := s.Attribute(attributeName)
    54  		require.False(t, ok)
    55  		require.Nil(t, attribute)
    56  	})
    57  	t.Run("StartTime", func(t *testing.T) {
    58  		s := noopSpan{}
    59  		require.Zero(t, s.StartTime())
    60  	})
    61  	t.Run("EndTime", func(t *testing.T) {
    62  		s := noopSpan{}
    63  		require.Zero(t, s.EndTime())
    64  	})
    65  	t.Run("Events", func(t *testing.T) {
    66  		s := noopSpan{}
    67  		s.AddEvent("event")
    68  		time, ok := s.Event("event")
    69  		require.False(t, ok)
    70  		require.Zero(t, time)
    71  	})
    72  	t.Run("Child", func(t *testing.T) {
    73  		s := noopSpan{}
    74  		_, ender := s.NewChild("child")
    75  		ender.End()
    76  		_, ok := s.Child("child")
    77  		require.False(t, ok)
    78  	})
    79  }