-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathshadow_test.go
172 lines (153 loc) · 4.61 KB
/
shadow_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// SPDX-License-Identifier: Apache-2.0
package proxy
import (
"bytes"
"context"
"errors"
"sync/atomic"
"testing"
"time"
"github.com/luraproject/lura/v2/config"
"github.com/luraproject/lura/v2/logging"
)
var (
extraCfg = config.ExtraConfig{
Namespace: map[string]interface{}{
"shadow": true,
"shadow_timeout": "10s",
},
}
badExtra = config.ExtraConfig{
Namespace: map[string]interface{}{
"shadow": "string",
},
}
)
func newAssertionProxy(counter *uint64) Proxy {
return func(ctx context.Context, request *Request) (*Response, error) {
atomic.AddUint64(counter, 1)
return nil, nil
}
}
func TestIsShadowBackend(t *testing.T) {
cfg := &config.Backend{ExtraConfig: extraCfg}
badCfg := &config.Backend{ExtraConfig: badExtra}
d, ok := isShadowBackend(cfg)
if !ok {
t.Error("The shadow backend should be true")
}
if d != 10*time.Second {
t.Errorf("Invalid duration %s", d)
}
if _, ok := isShadowBackend(&config.Backend{}); ok {
t.Error("The shadow backend should be false")
}
if _, ok := isShadowBackend(badCfg); ok {
t.Error("The shadow backend should be false")
}
}
func TestShadowMiddleware(t *testing.T) {
var counter uint64
assertProxy := newAssertionProxy(&counter)
p := ShadowMiddleware(assertProxy, assertProxy)
p(context.Background(), &Request{})
time.Sleep(100 * time.Millisecond)
if atomic.LoadUint64(&counter) != 2 {
t.Errorf("The shadow proxy should have been called 2 times, not %d", counter)
}
}
func TestShadowFactory_noBackends(t *testing.T) {
buff := bytes.NewBuffer(make([]byte, 1024))
logger, err := logging.NewLogger("ERROR", buff, "pref")
if err != nil {
t.Error("building the logger:", err.Error())
return
}
factory := DefaultFactory(logger)
sFactory := NewShadowFactory(factory)
if _, err := sFactory.New(&config.EndpointConfig{}); err != ErrNoBackends {
t.Errorf("Expecting ErrNoBackends. Got: %v\n", err)
}
}
func TestNewShadowFactory(t *testing.T) {
buff := bytes.NewBuffer(make([]byte, 1024))
logger, err := logging.NewLogger("ERROR", buff, "pref")
if err != nil {
t.Error("building the logger:", err.Error())
return
}
var counter uint64
assertProxy := newAssertionProxy(&counter)
factory := NewDefaultFactory(func(_ *config.Backend) Proxy { return assertProxy }, logger)
f := NewShadowFactory(factory)
sBackend := &config.Backend{ExtraConfig: extraCfg}
backend := &config.Backend{}
endpointConfig := &config.EndpointConfig{Backend: []*config.Backend{sBackend, backend}}
serviceConfig := config.ServiceConfig{
Version: config.ConfigVersion,
Endpoints: []*config.EndpointConfig{endpointConfig},
Timeout: 100 * time.Millisecond,
Host: []string{"dummy"},
}
if err = serviceConfig.Init(); err != nil {
t.Errorf("Error during the config init: %s\n", err.Error())
}
p, err := f.New(endpointConfig)
if err != nil {
t.Error(err)
}
_, err = p(context.Background(), &Request{})
if err != nil {
t.Error(err)
}
time.Sleep(100 * time.Millisecond)
if atomic.LoadUint64(&counter) != 2 {
t.Errorf("The shadow proxy should have been called 2 times, not %d", counter)
}
}
func TestShadowMiddleware_erroredBackend(t *testing.T) {
timeout := 100 * time.Millisecond
p := ShadowMiddleware(
delayedProxy(t, timeout, &Response{Data: map[string]interface{}{"supu": 42}, IsComplete: true}),
func(_ context.Context, _ *Request) (*Response, error) {
return nil, errors.New("ignore me")
},
)
mustEnd := time.After(time.Duration(5 * timeout))
out, err := p(context.Background(), &Request{Params: map[string]string{}})
if err != nil {
t.Errorf("unexpected error: %s\n", err.Error())
return
}
if out == nil {
t.Errorf("The proxy returned a null result\n")
return
}
select {
case <-mustEnd:
t.Errorf("We were expecting a response but we got none\n")
default:
if len(out.Data) != 1 {
t.Errorf("We weren't expecting a partial response but we got %v!\n", out)
}
if !out.IsComplete {
t.Errorf("We were expecting a completed response!\n")
}
}
}
func TestShadowMiddleware_partialTimeout(t *testing.T) {
timeout := 200 * time.Millisecond
p := ShadowMiddleware(
delayedProxy(t, time.Duration(5*timeout), &Response{Data: map[string]interface{}{"supu": 42}}),
delayedProxy(t, time.Duration(timeout/2), &Response{Data: map[string]interface{}{"supu": 42}, IsComplete: true}))
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
out, err := p(ctx, &Request{})
if err == nil || err.Error() != "context deadline exceeded" {
t.Errorf("The middleware propagated an unexpected error: %s\n", err.Error())
}
if out != nil {
t.Errorf("The proxy did not return a null result: %+v\n", out)
return
}
}