github.com/matrixorigin/matrixone@v0.7.0/pkg/util/trace/impl/motrace/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 motrace 23 24 import ( 25 "context" 26 "fmt" 27 "github.com/matrixorigin/matrixone/pkg/util/trace" 28 "reflect" 29 "testing" 30 31 "github.com/stretchr/testify/assert" 32 "github.com/stretchr/testify/require" 33 ) 34 35 func Test_initExport(t *testing.T) { 36 type args struct { 37 enableTracer bool 38 config *tracerProviderConfig 39 needRecover bool 40 shutdownCtx context.Context 41 } 42 cancledCtx, cancle := context.WithCancel(context.Background()) 43 cancle() 44 ch := make(chan string, 10) 45 tests := []struct { 46 name string 47 args args 48 empty bool 49 }{ 50 { 51 name: "disable", 52 args: args{ 53 enableTracer: false, 54 config: &tracerProviderConfig{enable: false}, 55 shutdownCtx: nil, 56 }, 57 empty: true, 58 }, 59 { 60 name: "enable_InternalExecutor", 61 args: args{ 62 enableTracer: true, 63 config: &tracerProviderConfig{ 64 enable: true, batchProcessMode: InternalExecutor, sqlExecutor: newDummyExecutorFactory(ch), 65 batchProcessor: NoopBatchProcessor{}, 66 }, 67 needRecover: true, 68 shutdownCtx: context.Background(), 69 }, 70 empty: false, 71 }, 72 { 73 name: "enable_FileService", 74 args: args{ 75 enableTracer: true, 76 config: &tracerProviderConfig{ 77 enable: true, batchProcessMode: FileService, sqlExecutor: newDummyExecutorFactory(ch), 78 batchProcessor: NoopBatchProcessor{}, 79 }, 80 shutdownCtx: context.Background(), 81 }, 82 empty: false, 83 }, 84 { 85 name: "enable_FileService_with_canceled_ctx", 86 args: args{ 87 enableTracer: true, 88 config: &tracerProviderConfig{ 89 enable: true, batchProcessMode: FileService, sqlExecutor: newDummyExecutorFactory(ch), 90 batchProcessor: NoopBatchProcessor{}, 91 }, 92 shutdownCtx: cancledCtx, 93 }, 94 empty: false, 95 }, 96 } 97 for _, tt := range tests { 98 t.Run(tt.name, func(t *testing.T) { 99 if tt.args.needRecover { 100 defer func() { 101 if err := recover(); err != nil { 102 t.Logf("pass with panic.") 103 return 104 } 105 panic("not catch panic") 106 }() 107 } 108 initExporter(context.TODO(), tt.args.config) 109 require.Equal(t, "motrace.NoopBatchProcessor", fmt.Sprintf("%v", reflect.ValueOf(GetGlobalBatchProcessor()).Type())) 110 require.Equal(t, Shutdown(tt.args.shutdownCtx), nil) 111 }) 112 } 113 } 114 115 func TestDefaultContext(t *testing.T) { 116 var spanId trace.SpanID 117 spanId.SetByUUID(GetNodeResource().NodeUuid) 118 tests := []struct { 119 name string 120 want context.Context 121 }{ 122 { 123 name: "normal", 124 want: trace.ContextWithSpanContext(context.Background(), trace.SpanContextWithIDs(trace.NilTraceID, spanId)), 125 }, 126 } 127 for _, tt := range tests { 128 t.Run(tt.name, func(t *testing.T) { 129 assert.Equalf(t, tt.want, DefaultContext(), "DefaultContext()") 130 }) 131 } 132 } 133 134 func TestDefaultSpanContext(t *testing.T) { 135 var spanId trace.SpanID 136 spanId.SetByUUID(GetNodeResource().NodeUuid) 137 sc := trace.SpanContextWithIDs(trace.NilTraceID, spanId) 138 tests := []struct { 139 name string 140 want *trace.SpanContext 141 }{ 142 { 143 name: "normal", 144 want: &sc, 145 }, 146 } 147 for _, tt := range tests { 148 t.Run(tt.name, func(t *testing.T) { 149 assert.Equalf(t, tt.want, DefaultSpanContext(), "DefaultSpanContext()") 150 }) 151 } 152 } 153 154 func TestGetNodeResource(t *testing.T) { 155 tests := []struct { 156 name string 157 want *trace.MONodeResource 158 }{ 159 { 160 name: "normal", 161 want: &trace.MONodeResource{NodeUuid: "node_uuid", NodeType: trace.NodeTypeStandalone}, 162 }, 163 } 164 for _, tt := range tests { 165 t.Run(tt.name, func(t *testing.T) { 166 assert.Equalf(t, tt.want, GetNodeResource(), "GetNodeResource()") 167 }) 168 } 169 } 170 171 func TestGetGlobalBatchProcessor(t *testing.T) { 172 tests := []struct { 173 name string 174 want BatchProcessor 175 }{ 176 { 177 name: "normal", 178 want: NoopBatchProcessor{}, 179 }, 180 } 181 for _, tt := range tests { 182 t.Run(tt.name, func(t *testing.T) { 183 if got := GetGlobalBatchProcessor(); !reflect.DeepEqual(got, tt.want) { 184 t.Errorf("GetGlobalBatchProcessor() = %v, want %v", got, tt.want) 185 } 186 }) 187 } 188 }