github.com/matrixorigin/matrixone@v1.2.0/pkg/util/trace/impl/motrace/batch_span_processor_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/matrixorigin/matrixone/pkg/util/batchpipe"
    32  	"github.com/stretchr/testify/assert"
    33  )
    34  
    35  var _ batchpipe.HasName = &namedNoopSpan{}
    36  
    37  type namedNoopSpan struct{ trace.NoopSpan }
    38  
    39  func (n namedNoopSpan) GetName() string { return "NamedNopSpan" }
    40  
    41  func TestNewBatchSpanProcessor(t *testing.T) {
    42  	type args struct {
    43  		exporter BatchProcessor
    44  	}
    45  	tests := []struct {
    46  		name string
    47  		args args
    48  		want trace.SpanProcessor
    49  	}{
    50  		{
    51  			name: "normal",
    52  			args: args{exporter: &NoopBatchProcessor{}},
    53  			want: &batchSpanProcessor{
    54  				e:      &NoopBatchProcessor{},
    55  				stopCh: make(chan struct{}),
    56  			},
    57  		},
    58  	}
    59  	for _, tt := range tests {
    60  		t.Run(tt.name, func(t *testing.T) {
    61  			if got := NewBatchSpanProcessor(tt.args.exporter); reflect.DeepEqual(got, tt.want) {
    62  				assert.Equalf(t, tt.want, got, "NewBatchSpanProcessor(%v)", tt.args.exporter)
    63  			}
    64  		})
    65  	}
    66  }
    67  
    68  func Test_batchSpanProcessor_OnEnd(t *testing.T) {
    69  	type fields struct {
    70  		e BatchProcessor
    71  	}
    72  	type args struct {
    73  		c context.Context
    74  		s trace.Span
    75  	}
    76  	tests := []struct {
    77  		name   string
    78  		fields fields
    79  		args   args
    80  	}{
    81  		{
    82  			name:   "normal",
    83  			fields: fields{NoopBatchProcessor{}},
    84  			args:   args{context.Background(), namedNoopSpan{}},
    85  		},
    86  	}
    87  	for _, tt := range tests {
    88  		t.Run(tt.name, func(t *testing.T) {
    89  			bsp := NewBatchSpanProcessor(tt.fields.e)
    90  			bsp.OnStart(tt.args.c, tt.args.s)
    91  			bsp.OnEnd(tt.args.s)
    92  		})
    93  	}
    94  }
    95  
    96  func Test_batchSpanProcessor_Shutdown(t *testing.T) {
    97  	type fields struct {
    98  		e      BatchProcessor
    99  		stopCh chan struct{}
   100  	}
   101  	type args struct {
   102  		ctx context.Context
   103  	}
   104  	tests := []struct {
   105  		name    string
   106  		fields  fields
   107  		args    args
   108  		wantErr assert.ErrorAssertionFunc
   109  	}{
   110  		{
   111  			name: "normal",
   112  			fields: fields{
   113  				e:      NoopBatchProcessor{},
   114  				stopCh: make(chan struct{}),
   115  			},
   116  			args: args{context.Background()},
   117  			wantErr: func(t assert.TestingT, err error, msgs ...interface{}) bool {
   118  				if err != nil {
   119  					t.Errorf(msgs[0].(string), msgs[1:])
   120  					return false
   121  				}
   122  				return true
   123  			},
   124  		},
   125  	}
   126  	for _, tt := range tests {
   127  		t.Run(tt.name, func(t *testing.T) {
   128  			bsp := &batchSpanProcessor{
   129  				e:      tt.fields.e,
   130  				stopCh: tt.fields.stopCh,
   131  			}
   132  			tt.wantErr(t, bsp.Shutdown(tt.args.ctx), fmt.Sprintf("Shutdown(%v)", tt.args.ctx))
   133  		})
   134  	}
   135  }