github.com/matrixorigin/matrixone@v0.7.0/pkg/util/trace/noop_trace_test.go (about)

     1  // Copyright The OpenTelemetry Authors
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Portions of this file are additionally subject to the following
    16  // copyright.
    17  //
    18  // Copyright (C) 2022 Matrix Origin.
    19  //
    20  // Modified the behavior and the interface of the step.
    21  
    22  package trace
    23  
    24  import (
    25  	"context"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  func Test_nonRecordingSpan_ParentSpanContext(t *testing.T) {
    33  	type fields struct {
    34  		NoopSpan NoopSpan
    35  		sc       SpanContext
    36  	}
    37  	tests := []struct {
    38  		name   string
    39  		fields fields
    40  		want   SpanContext
    41  	}{
    42  		{
    43  			name:   "normal",
    44  			fields: fields{NoopSpan: NoopSpan{}, sc: SpanContext{}},
    45  			want:   SpanContext{},
    46  		},
    47  	}
    48  	for _, tt := range tests {
    49  		t.Run(tt.name, func(t *testing.T) {
    50  			s := &NonRecordingSpan{
    51  				NoopSpan: tt.fields.NoopSpan,
    52  				sc:       tt.fields.sc,
    53  			}
    54  			require.Equal(t, tt.want, s.ParentSpanContext())
    55  		})
    56  	}
    57  }
    58  
    59  func Test_nonRecordingSpan_SpanContext(t *testing.T) {
    60  	type fields struct {
    61  		NoopSpan NoopSpan
    62  		sc       SpanContext
    63  	}
    64  	tests := []struct {
    65  		name   string
    66  		fields fields
    67  		want   SpanContext
    68  	}{
    69  		{
    70  			name:   "normal",
    71  			fields: fields{NoopSpan: NoopSpan{}, sc: SpanContext{}},
    72  			want:   SpanContext{},
    73  		},
    74  	}
    75  	for _, tt := range tests {
    76  		t.Run(tt.name, func(t *testing.T) {
    77  			s := &NonRecordingSpan{
    78  				NoopSpan: tt.fields.NoopSpan,
    79  				sc:       tt.fields.sc,
    80  			}
    81  			assert.Equalf(t, tt.want, s.SpanContext(), "SpanContext()")
    82  		})
    83  	}
    84  }
    85  
    86  func Test_NoopSpan_End(t *testing.T) {
    87  	type args struct {
    88  		in0 []SpanEndOption
    89  	}
    90  	tests := []struct {
    91  		name string
    92  		args args
    93  	}{
    94  		// TODO: Add test cases.
    95  	}
    96  	for _, tt := range tests {
    97  		t.Run(tt.name, func(t *testing.T) {
    98  			no := NoopSpan{}
    99  			no.End(tt.args.in0...)
   100  		})
   101  	}
   102  }
   103  
   104  func Test_NoopSpan_ParentSpanContext(t *testing.T) {
   105  	tests := []struct {
   106  		name string
   107  		want SpanContext
   108  	}{
   109  		{
   110  			name: "normal",
   111  			want: SpanContext{},
   112  		},
   113  	}
   114  	for _, tt := range tests {
   115  		t.Run(tt.name, func(t *testing.T) {
   116  			no := NoopSpan{}
   117  			assert.Equalf(t, tt.want, no.ParentSpanContext(), "ParentSpanContext()")
   118  		})
   119  	}
   120  }
   121  
   122  func Test_NoopSpan_SetName(t *testing.T) {
   123  	type args struct {
   124  		in0 string
   125  	}
   126  	tests := []struct {
   127  		name string
   128  		args args
   129  	}{
   130  		{
   131  			name: "normal",
   132  			args: args{in0: "name"},
   133  		},
   134  	}
   135  	for _, tt := range tests {
   136  		t.Run(tt.name, func(t *testing.T) {
   137  			no := NoopSpan{}
   138  			no.SetName(tt.args.in0)
   139  		})
   140  	}
   141  }
   142  
   143  func Test_NoopSpan_SpanContext(t *testing.T) {
   144  	tests := []struct {
   145  		name string
   146  		want SpanContext
   147  	}{
   148  		{name: "normal", want: SpanContext{}},
   149  	}
   150  	for _, tt := range tests {
   151  		t.Run(tt.name, func(t *testing.T) {
   152  			no := NoopSpan{}
   153  			assert.Equalf(t, tt.want, no.SpanContext(), "SpanContext()")
   154  		})
   155  	}
   156  }
   157  
   158  func Test_NoopSpan_TracerProvider(t *testing.T) {
   159  	tests := []struct {
   160  		name string
   161  		want TracerProvider
   162  	}{
   163  		{
   164  			name: "normal",
   165  			want: noopTracerProvider{},
   166  		},
   167  	}
   168  	for _, tt := range tests {
   169  		t.Run(tt.name, func(t *testing.T) {
   170  			no := NoopSpan{}
   171  			assert.Equalf(t, tt.want, no.TracerProvider(), "TracerProvider()")
   172  		})
   173  	}
   174  }
   175  
   176  func Test_NoopTracer_Start(t1 *testing.T) {
   177  	type args struct {
   178  		ctx   context.Context
   179  		name  string
   180  		in2   []SpanOption
   181  		endIn []SpanEndOption
   182  	}
   183  	tests := []struct {
   184  		name  string
   185  		args  args
   186  		want  context.Context
   187  		want1 Span
   188  	}{
   189  		{
   190  			name: "normal",
   191  			args: args{
   192  				ctx:   context.Background(),
   193  				name:  "NoopTracer_Start",
   194  				in2:   []SpanOption{WithNewRoot(true)},
   195  				endIn: []SpanEndOption{},
   196  			},
   197  			want:  ContextWithSpan(context.Background(), NoopSpan{}),
   198  			want1: NoopSpan{},
   199  		},
   200  		{
   201  			name: "NonRecording",
   202  			args: args{
   203  				ctx:   ContextWithSpan(context.Background(), &NonRecordingSpan{}),
   204  				name:  "NoopTracer_Start",
   205  				in2:   []SpanOption{WithNewRoot(true)},
   206  				endIn: []SpanEndOption{},
   207  			},
   208  			want:  ContextWithSpan(ContextWithSpan(context.Background(), &NonRecordingSpan{}), NoopSpan{}),
   209  			want1: NoopSpan{},
   210  		},
   211  	}
   212  	for _, tt := range tests {
   213  		t1.Run(tt.name, func(t1 *testing.T) {
   214  			t := NoopTracer{}
   215  			got, got1 := t.Start(tt.args.ctx, tt.args.name, tt.args.in2...)
   216  			assert.Equalf(t1, tt.want, got, "Start(%v, %v, %v)", tt.args.ctx, tt.args.name, tt.args.in2)
   217  			assert.Equalf(t1, tt.want1, got1, "Start(%v, %v, %v)", tt.args.ctx, tt.args.name, tt.args.in2)
   218  			got1.End(tt.args.endIn...)
   219  		})
   220  	}
   221  }