github.com/blend/go-sdk@v1.20220411.3/logger/event_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 logger
     9  
    10  import (
    11  	"context"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/blend/go-sdk/assert"
    16  )
    17  
    18  type timestampProvider time.Time
    19  
    20  func (tsp timestampProvider) GetFlag() string { return "timestamp_provider" }
    21  
    22  func (tsp timestampProvider) GetTimestamp() time.Time {
    23  	return time.Time(tsp)
    24  }
    25  
    26  var (
    27  	_ Event             = (*timestampProvider)(nil)
    28  	_ TimestampProvider = (*timestampProvider)(nil)
    29  )
    30  
    31  func TestGetEventTimestamp(t *testing.T) {
    32  	assert := assert.New(t)
    33  
    34  	ts1 := time.Date(2019, 8, 21, 12, 11, 10, 9, time.UTC)
    35  	ts2 := time.Date(2019, 8, 20, 12, 11, 10, 9, time.UTC)
    36  	ts3 := time.Date(2019, 8, 21, 12, 11, 10, 9, time.UTC)
    37  
    38  	tsp := timestampProvider(ts1)
    39  
    40  	tsc := WithTimestamp(context.Background(), ts2)
    41  	ttsc := WithTriggerTimestamp(context.Background(), ts3)
    42  
    43  	comboctx := WithTimestamp(WithTriggerTimestamp(context.Background(), ts3), ts2)
    44  	comboctx2 := WithTriggerTimestamp(WithTimestamp(context.Background(), ts2), ts3)
    45  
    46  	// test the timestamp provider takes precedence
    47  	assert.Equal(ts1, GetEventTimestamp(context.Background(), tsp))
    48  	assert.Equal(ts1, GetEventTimestamp(tsc, tsp))
    49  	assert.Equal(ts1, GetEventTimestamp(ttsc, tsp))
    50  	assert.Equal(ts1, GetEventTimestamp(comboctx, tsp))
    51  	assert.Equal(ts1, GetEventTimestamp(comboctx2, tsp))
    52  
    53  	me := NewMessageEvent(Info, "this is just a test")
    54  
    55  	assert.False(GetEventTimestamp(context.Background(), me).IsZero())
    56  	assert.Equal(ts2, GetEventTimestamp(tsc, me))
    57  	assert.Equal(ts3, GetEventTimestamp(ttsc, me))
    58  	assert.Equal(ts2, GetEventTimestamp(comboctx, me))
    59  	assert.Equal(ts2, GetEventTimestamp(comboctx2, me))
    60  }