Skip to content

Commit

Permalink
test: add trace_test.go (#497)
Browse files Browse the repository at this point in the history
* test: add trace_test.go

* refactor: rename test title
  • Loading branch information
wafuwafu13 committed Apr 23, 2023
1 parent 81d63bd commit dc78417
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions lambda/handlertrace/trace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package handlertrace

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestTrace(t *testing.T) {
ctx := context.Background()

requestCall := 0
responseCall := 0

existedContext := context.WithValue(ctx, handlerTraceKey{}, HandlerTrace{
RequestEvent: func(ctx context.Context, event interface{}) {
requestCall += 1
},
ResponseEvent: func(ctx context.Context, event interface{}) {
responseCall += 1
},
})

trace := HandlerTrace{
RequestEvent: func(ctx context.Context, event interface{}) {
requestCall += 1
},
ResponseEvent: func(ctx context.Context, event interface{}) {
responseCall += 1
},
}

ctxWithTrace := NewContext(existedContext, trace)
traceFromCtx := FromContext(ctxWithTrace)

traceFromCtx.RequestEvent(ctxWithTrace, nil)
assert.Equal(t, requestCall, 2)

traceFromCtx.ResponseEvent(ctxWithTrace, nil)
fmt.Println(responseCall)
assert.Equal(t, responseCall, 2)
}

0 comments on commit dc78417

Please sign in to comment.