github.com/pinpoint-apm/pinpoint-go-agent@v1.4.1-0.20240110120318-a50c2eb18c8c/span_event_test.go (about)

     1  package pinpoint
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func Test_newSpanEvent(t *testing.T) {
    12  	type args struct {
    13  		span          *span
    14  		operationName string
    15  	}
    16  	tests := []struct {
    17  		name string
    18  		args args
    19  	}{
    20  		{"1", args{defaultTestSpan(), "t1"}},
    21  	}
    22  	for _, tt := range tests {
    23  		t.Run(tt.name, func(t *testing.T) {
    24  			se := newSpanEvent(tt.args.span, tt.args.operationName)
    25  			assert.Equal(t, se.operationName, tt.args.operationName, "operationName")
    26  			assert.Equal(t, se.serviceType, int32(ServiceTypeGoFunction), "serviceType")
    27  			assert.NotNil(t, se.startTime, "startTime")
    28  		})
    29  	}
    30  }
    31  
    32  func Test_spanEvent_end(t *testing.T) {
    33  	type args struct {
    34  		span          *span
    35  		operationName string
    36  	}
    37  	tests := []struct {
    38  		name string
    39  		args args
    40  	}{
    41  		{"1", args{defaultTestSpan(), "t1"}},
    42  	}
    43  	for _, tt := range tests {
    44  		t.Run(tt.name, func(t *testing.T) {
    45  			se := newSpanEvent(tt.args.span, tt.args.operationName)
    46  			tt.args.span.appendSpanEvent(se)
    47  			assert.Equal(t, se.parentSpan.eventDepth, int32(2), "eventDepth")
    48  
    49  			time.Sleep(100 * time.Millisecond)
    50  			se.end()
    51  
    52  			assert.Equal(t, se.operationName, tt.args.operationName, "operationName")
    53  			assert.Equal(t, se.parentSpan.eventDepth, int32(1), "eventDepth")
    54  			assert.Greater(t, se.endElapsed, int64(99), "endElapsed")
    55  		})
    56  	}
    57  }
    58  
    59  func Test_spanEvent_generateNextSpanId(t *testing.T) {
    60  	type args struct {
    61  		span          *span
    62  		operationName string
    63  	}
    64  	tests := []struct {
    65  		name string
    66  		args args
    67  	}{
    68  		{"1", args{defaultTestSpan(), "t1"}},
    69  	}
    70  	for _, tt := range tests {
    71  		t.Run(tt.name, func(t *testing.T) {
    72  			se := newSpanEvent(tt.args.span, tt.args.operationName)
    73  			id := se.generateNextSpanId()
    74  			assert.Equal(t, se.operationName, tt.args.operationName, "operationName")
    75  			assert.Equal(t, se.nextSpanId, id, "nextSpanId")
    76  			assert.NotEqual(t, se.nextSpanId, int64(0), "nextSpanId")
    77  		})
    78  	}
    79  }
    80  
    81  func Test_spanEvent_SetError(t *testing.T) {
    82  	type args struct {
    83  		span          *span
    84  		operationName string
    85  	}
    86  	tests := []struct {
    87  		name string
    88  		args args
    89  	}{
    90  		{"1", args{defaultTestSpan(), "t1"}},
    91  	}
    92  	for _, tt := range tests {
    93  		t.Run(tt.name, func(t *testing.T) {
    94  			tt.args.span.agent = newTestAgent(defaultConfig())
    95  			se := newSpanEvent(tt.args.span, tt.args.operationName)
    96  			se.SetError(errors.New("TEST_ERROR"))
    97  			assert.Equal(t, int32(1), se.errorFuncId, "errorFuncId")
    98  			assert.Equal(t, "TEST_ERROR", se.errorString, "errorString")
    99  		})
   100  	}
   101  }
   102  
   103  func Test_spanEvent_SetSQL(t *testing.T) {
   104  	type args struct {
   105  		span          *span
   106  		operationName string
   107  	}
   108  	tests := []struct {
   109  		name string
   110  		args args
   111  	}{
   112  		{"1", args{defaultTestSpan(), "t1"}},
   113  	}
   114  	for _, tt := range tests {
   115  		t.Run(tt.name, func(t *testing.T) {
   116  			tt.args.span.agent = newTestAgent(defaultConfig())
   117  			se := newSpanEvent(tt.args.span, tt.args.operationName)
   118  			se.SetSQL("SELECT 1", "")
   119  			assert.Equal(t, len(se.annotations.list), int(1), "annotations.len")
   120  		})
   121  	}
   122  }