github.com/blend/go-sdk@v1.20220411.3/tracing/crontrace/tracer_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package crontrace
     9  
    10  import (
    11  	"context"
    12  	"fmt"
    13  	"testing"
    14  	"time"
    15  
    16  	opentracing "github.com/opentracing/opentracing-go"
    17  	"github.com/opentracing/opentracing-go/mocktracer"
    18  
    19  	"github.com/blend/go-sdk/assert"
    20  	"github.com/blend/go-sdk/tracing"
    21  )
    22  
    23  func TestStart(t *testing.T) {
    24  	assert := assert.New(t)
    25  	mockTracer := mocktracer.New()
    26  	cronTracer := Tracer(mockTracer)
    27  
    28  	ctx := context.Background()
    29  	ctx, _ = cronTracer.Start(ctx, "test_job")
    30  
    31  	span := opentracing.SpanFromContext(ctx)
    32  	mockSpan := span.(*mocktracer.MockSpan)
    33  	assert.Equal(tracing.OperationJob, mockSpan.OperationName)
    34  
    35  	assert.Len(mockSpan.Tags(), 3)
    36  	assert.Equal("test_job", mockSpan.Tags()[tracing.TagKeyResourceName])
    37  	assert.Equal(tracing.SpanTypeJob, mockSpan.Tags()[tracing.TagKeySpanType])
    38  	assert.True(mockSpan.FinishTime.IsZero())
    39  }
    40  
    41  func TestFinish(t *testing.T) {
    42  	assert := assert.New(t)
    43  	mockTracer := mocktracer.New()
    44  	cronTracer := Tracer(mockTracer)
    45  	testStartTime := time.Now()
    46  
    47  	ctx := context.Background()
    48  	ctx, tf := cronTracer.Start(ctx, "tracer-test")
    49  
    50  	tf.Finish(ctx, nil)
    51  	span := opentracing.SpanFromContext(ctx)
    52  	mockSpan := span.(*mocktracer.MockSpan)
    53  	assert.True(testStartTime.Before(mockSpan.FinishTime))
    54  	assert.Equal(nil, mockSpan.Tags()[tracing.TagKeyError])
    55  	assert.False(mockSpan.FinishTime.IsZero())
    56  }
    57  
    58  func TestFinishError(t *testing.T) {
    59  	assert := assert.New(t)
    60  	mockTracer := mocktracer.New()
    61  	cronTracer := Tracer(mockTracer)
    62  	testStartTime := time.Now()
    63  
    64  	ctx := context.Background()
    65  	// Start Span from Background Context
    66  	ctx, tf := cronTracer.Start(ctx, "tracer-test")
    67  
    68  	tf.Finish(ctx, fmt.Errorf("error"))
    69  	span := opentracing.SpanFromContext(ctx)
    70  	mockSpan := span.(*mocktracer.MockSpan)
    71  	assert.True(testStartTime.Before(mockSpan.FinishTime))
    72  	assert.Equal("error", mockSpan.Tags()[tracing.TagKeyError])
    73  	assert.False(mockSpan.FinishTime.IsZero())
    74  }
    75  
    76  func TestFinishNilSpan(t *testing.T) {
    77  	assert := assert.New(t)
    78  
    79  	ctx := context.Background()
    80  	traceFinisher{}.Finish(ctx, nil)
    81  	assert.Nil(opentracing.SpanFromContext(ctx))
    82  }