-
Notifications
You must be signed in to change notification settings - Fork 6
/
backend_test.go
95 lines (77 loc) · 2.49 KB
/
backend_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
84
85
86
87
88
89
90
91
92
93
94
95
package metrics
import (
"context"
"errors"
"testing"
"time"
"github.com/devopsfaith/krakend/config"
"github.com/devopsfaith/krakend/proxy"
"github.com/newrelic/go-agent"
)
func TestBackendFactory_okAppNil(t *testing.T) {
app = nil
cfg := &config.Backend{
URLPattern: "/my_endpoint",
Host: []string{
"localhost:8080",
},
Timeout: time.Second,
}
expectedError := errors.New("expected error")
bf := BackendFactory("segm", func(_ *config.Backend) proxy.Proxy {
return func(_ context.Context, _ *proxy.Request) (*proxy.Response, error) { return nil, expectedError }
})
if resp, err := bf(cfg)(context.Background(), nil); resp != nil || err != expectedError {
t.Errorf("unexpected response: resp = %v, error = %v", resp, err)
}
}
func TestBackendFactory_okNRApp(t *testing.T) {
cfg := &config.Backend{
URLPattern: "/my_endpoint",
Host: []string{
"localhost:8080",
},
Timeout: time.Second,
}
nrApp := newApp()
defer func() { app = nil }()
app = &Application{nrApp, Config{InstrumentationRate: 100}}
expectedError := errors.New("expected error")
bf := BackendFactory("segm", func(_ *config.Backend) proxy.Proxy {
return func(_ context.Context, _ *proxy.Request) (*proxy.Response, error) { return nil, expectedError }
})
totalCalls := 0
txn := newTx()
txn.startSegmentNow = func() newrelic.SegmentStartTime {
totalCalls++
return newrelic.SegmentStartTime{}
}
if resp, err := bf(cfg)(context.WithValue(context.Background(), nrCtxKey, txn), nil); resp != nil || err != expectedError {
t.Errorf("unexpected response: resp = %v, error = %v", resp, err)
}
if resp, err := bf(cfg)(context.Background(), nil); resp != nil || err != expectedError {
t.Errorf("unexpected response: resp = %v, error = %v", resp, err)
}
if totalCalls != 1 {
t.Errorf("unexpected number of calls to the txn end. have: %d, wanted: 1", totalCalls)
}
}
func TestNewBackend_okAppNil(t *testing.T) {
app = nil
expectedError := errors.New("expected error")
b := NewBackend("segm", func(_ context.Context, _ *proxy.Request) (*proxy.Response, error) {
return nil, expectedError
})
totalCalls := 0
txn := newTx()
txn.startSegmentNow = func() newrelic.SegmentStartTime {
totalCalls++
return newrelic.SegmentStartTime{}
}
if resp, err := b(context.Background(), nil); resp != nil || err != expectedError {
t.Errorf("unexpected response: resp = %v, error = %v", resp, err)
}
if totalCalls != 0 {
t.Errorf("unexpected number of calls to the txn end. have: %d, wanted: 0", totalCalls)
}
}