File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ package handlertrace
2+
3+ import (
4+ "context"
5+ "fmt"
6+ "testing"
7+
8+ "github.com/stretchr/testify/assert"
9+ )
10+
11+ func TestTrace (t * testing.T ) {
12+ ctx := context .Background ()
13+
14+ requestCall := 0
15+ responseCall := 0
16+
17+ existedContext := context .WithValue (ctx , handlerTraceKey {}, HandlerTrace {
18+ RequestEvent : func (ctx context.Context , event interface {}) {
19+ requestCall += 1
20+ },
21+ ResponseEvent : func (ctx context.Context , event interface {}) {
22+ responseCall += 1
23+ },
24+ })
25+
26+ trace := HandlerTrace {
27+ RequestEvent : func (ctx context.Context , event interface {}) {
28+ requestCall += 1
29+ },
30+ ResponseEvent : func (ctx context.Context , event interface {}) {
31+ responseCall += 1
32+ },
33+ }
34+
35+ ctxWithTrace := NewContext (existedContext , trace )
36+ traceFromCtx := FromContext (ctxWithTrace )
37+
38+ traceFromCtx .RequestEvent (ctxWithTrace , nil )
39+ assert .Equal (t , requestCall , 2 )
40+
41+ traceFromCtx .ResponseEvent (ctxWithTrace , nil )
42+ fmt .Println (responseCall )
43+ assert .Equal (t , responseCall , 2 )
44+ }
You can’t perform that action at this time.
0 commit comments