github.com/matrixorigin/matrixone@v1.2.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 { 95 name: "normal", 96 args: args{in0: []SpanEndOption{}}, 97 }, 98 } 99 for _, tt := range tests { 100 t.Run(tt.name, func(t *testing.T) { 101 no := NoopSpan{} 102 no.End(tt.args.in0...) 103 }) 104 } 105 } 106 107 func Test_NoopSpan_ParentSpanContext(t *testing.T) { 108 tests := []struct { 109 name string 110 want SpanContext 111 }{ 112 { 113 name: "normal", 114 want: SpanContext{}, 115 }, 116 } 117 for _, tt := range tests { 118 t.Run(tt.name, func(t *testing.T) { 119 no := NoopSpan{} 120 assert.Equalf(t, tt.want, no.ParentSpanContext(), "ParentSpanContext()") 121 }) 122 } 123 } 124 125 func Test_NoopSpan_SetName(t *testing.T) { 126 type args struct { 127 in0 string 128 } 129 tests := []struct { 130 name string 131 args args 132 }{ 133 { 134 name: "normal", 135 args: args{in0: "name"}, 136 }, 137 } 138 for _, tt := range tests { 139 t.Run(tt.name, func(t *testing.T) { 140 no := NoopSpan{} 141 no.SetName(tt.args.in0) 142 }) 143 } 144 } 145 146 func Test_NoopSpan_SpanContext(t *testing.T) { 147 tests := []struct { 148 name string 149 want SpanContext 150 }{ 151 {name: "normal", want: SpanContext{}}, 152 } 153 for _, tt := range tests { 154 t.Run(tt.name, func(t *testing.T) { 155 no := NoopSpan{} 156 assert.Equalf(t, tt.want, no.SpanContext(), "SpanContext()") 157 }) 158 } 159 } 160 161 func Test_NoopSpan_TracerProvider(t *testing.T) { 162 tests := []struct { 163 name string 164 want TracerProvider 165 }{ 166 { 167 name: "normal", 168 want: noopTracerProvider{}, 169 }, 170 } 171 for _, tt := range tests { 172 t.Run(tt.name, func(t *testing.T) { 173 no := NoopSpan{} 174 assert.Equalf(t, tt.want, no.TracerProvider(), "TracerProvider()") 175 }) 176 } 177 } 178 179 func Test_NoopTracer_Start(t1 *testing.T) { 180 type args struct { 181 ctx context.Context 182 name string 183 in2 []SpanStartOption 184 endIn []SpanEndOption 185 } 186 tests := []struct { 187 name string 188 args args 189 want context.Context 190 want1 Span 191 }{ 192 { 193 name: "normal", 194 args: args{ 195 ctx: context.Background(), 196 name: "NoopTracer_Start", 197 in2: []SpanStartOption{WithNewRoot(true)}, 198 endIn: []SpanEndOption{}, 199 }, 200 want: ContextWithSpan(context.Background(), NoopSpan{}), 201 want1: NoopSpan{}, 202 }, 203 { 204 name: "empty", 205 args: args{ 206 ctx: context.Background(), 207 name: "NoopTracer_Start", 208 in2: []SpanStartOption{}, 209 endIn: []SpanEndOption{}, 210 }, 211 want: context.Background(), 212 want1: NoopSpan{}, 213 }, 214 { 215 name: "NonRecording", 216 args: args{ 217 ctx: ContextWithSpan(context.Background(), &NonRecordingSpan{}), 218 name: "NoopTracer_Start", 219 in2: []SpanStartOption{WithNewRoot(true)}, 220 endIn: []SpanEndOption{}, 221 }, 222 want: ContextWithSpan(ContextWithSpan(context.Background(), &NonRecordingSpan{}), NoopSpan{}), 223 want1: NoopSpan{}, 224 }, 225 } 226 for _, tt := range tests { 227 t1.Run(tt.name, func(t1 *testing.T) { 228 t := NoopTracer{} 229 got, got1 := t.Start(tt.args.ctx, tt.args.name, tt.args.in2...) 230 assert.Equalf(t1, tt.want, got, "Start(%v, %v, %v)", tt.args.ctx, tt.args.name, tt.args.in2) 231 assert.Equalf(t1, tt.want1, got1, "Start(%v, %v, %v)", tt.args.ctx, tt.args.name, tt.args.in2) 232 got1.End(tt.args.endIn...) 233 }) 234 } 235 }