-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_test.go
83 lines (65 loc) · 1.89 KB
/
helper_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package OpenTracing
import (
"context"
"github.com/opentracing/opentracing-go"
"github.com/stretchr/testify/require"
"github.com/uber/jaeger-client-go"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func TestTracer(t *testing.T) {
ctx := context.Background()
tracer, err := NewTracer(ctx, "test", "")
require.NoError(t, err)
require.NotNil(t, tracer)
tracer, err = NewTracer(ctx, "test", "wtf")
require.Error(t, err)
}
func TestInjectToReq(t *testing.T) {
tracer, err := NewTracer(context.Background(), "test", "")
require.NoError(t, err)
require.NotNil(t, tracer)
handler := func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(r.Header.Get("Uber-Trace-Id")))
}
t.Run("empty context", func(t *testing.T) {
ctx := context.Background()
req := httptest.NewRequest("GET", "http://test.com", nil)
err := InjectToReq(ctx, req)
require.Error(t, err)
})
t.Run("create span", func(t *testing.T) {
ctx := context.Background()
span, ctx := opentracing.StartSpanFromContext(ctx, "test")
require.NotNil(t, span)
req := httptest.NewRequest("GET", "http://test.com", nil)
err := InjectToReq(ctx, req)
require.NoError(t, err)
span.Finish()
})
t.Run("request with span", func(t *testing.T) {
ctx := context.Background()
span, ctx := opentracing.StartSpanFromContext(ctx, "test")
require.NotNil(t, span)
req := httptest.NewRequest("GET", "http://test.com", nil)
err := InjectToReq(ctx, req)
require.NoError(t, err)
w := httptest.NewRecorder()
handler(w, req)
resp := w.Result()
body, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
sc, ok := span.Context().(jaeger.SpanContext)
require.True(t, ok)
require.NotNil(t, sc)
require.Equal(t, sc.String(), string(body))
span.Finish()
})
}
func TestError(t *testing.T) {
ctx := context.Background()
span, ctx := opentracing.StartSpanFromContext(ctx, "test")
Error(span)
}