forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost_checker_test.go
155 lines (133 loc) · 3.58 KB
/
host_checker_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
package main
import (
"bytes"
"sync"
"testing"
"text/template"
"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/config"
)
const sampleUptimeTestAPI = `{
"slug": "api",
"api_id": "test",
"use_keyless": true,
"version_data": {
"not_versioned": true,
"versions": {
"Default": {
"name": "Default"
}
}
},
"uptime_tests": {
"check_list": [
{
"url": "{{.ActiveHost}}/get",
"method": "GET"
},
{
"url": "{{.InactiveHost}}/get",
"method": "GET"
}
]
},
"proxy": {
"listen_path": "/",
"enable_load_balancing": true,
"check_host_against_uptime_tests": true,
"target_list": [
"{{.ActiveHost}}",
"{{.InactiveHost}}"
]
},
"active": true
}`
type testEventHandler struct {
cb func(config.EventMessage)
}
func (w *testEventHandler) Init(handlerConf interface{}) error {
return nil
}
func (w *testEventHandler) HandleEvent(em config.EventMessage) {
w.cb(em)
}
func TestHostChecker(t *testing.T) {
specTmpl := template.Must(template.New("spec").Parse(sampleUptimeTestAPI))
tmplData := struct {
ActiveHost string
InactiveHost string
}{
testHttpAny,
testHttpFailureAny,
}
specBuf := &bytes.Buffer{}
specTmpl.ExecuteTemplate(specBuf, specTmpl.Name(), &tmplData)
spec := createDefinitionFromString(specBuf.String())
// From api_loader.go#processSpec
sl := apidef.NewHostListFromList(spec.Proxy.Targets)
spec.Proxy.StructuredTargetList = sl
var eventWG sync.WaitGroup
// Should receive one HostDown event
eventWG.Add(1)
cb := func(em config.EventMessage) {
eventWG.Done()
}
spec.EventPaths = map[apidef.TykEvent][]config.TykEventHandler{
"HostDown": {&testEventHandler{cb}},
}
apisMu.Lock()
apisByID = map[string]*APISpec{spec.APIID: spec}
apisMu.Unlock()
GlobalHostChecker.checkerMu.Lock()
GlobalHostChecker.checker.sampleTriggerLimit = 1
GlobalHostChecker.checkerMu.Unlock()
defer func() {
apisMu.Lock()
apisByID = make(map[string]*APISpec)
apisMu.Unlock()
GlobalHostChecker.checkerMu.Lock()
GlobalHostChecker.checker.sampleTriggerLimit = defaultSampletTriggerLimit
GlobalHostChecker.checkerMu.Unlock()
}()
SetCheckerHostList()
GlobalHostChecker.checkerMu.Lock()
if len(GlobalHostChecker.currentHostList) != 2 {
t.Error("Should update hosts manager check list", GlobalHostChecker.currentHostList)
}
if len(GlobalHostChecker.checker.newList) != 2 {
t.Error("Should update host checker check list")
}
GlobalHostChecker.checkerMu.Unlock()
hostCheckTicker <- struct{}{}
eventWG.Wait()
if GlobalHostChecker.IsHostDown(testHttpAny) {
t.Error("Should not mark as down")
}
if !GlobalHostChecker.IsHostDown(testHttpFailureAny) {
t.Error("Should mark as down")
}
// Test it many times concurrently, to simulate concurrent and
// parallel requests to the API. This will catch bugs in those
// scenarios, like data races.
var targetWG sync.WaitGroup
for i := 0; i < 10; i++ {
targetWG.Add(1)
go func() {
host := GetNextTarget(spec.Proxy.StructuredTargetList, spec)
if host != testHttpAny {
t.Error("Should return only active host, got", host)
}
targetWG.Done()
}()
}
targetWG.Wait()
GlobalHostChecker.checkerMu.Lock()
if GlobalHostChecker.checker.checkTimeout != defaultTimeout {
t.Error("Should set defaults", GlobalHostChecker.checker.checkTimeout)
}
redisStore := GlobalHostChecker.store.(*RedisClusterStorageManager)
if ttl, _ := redisStore.GetKeyTTL(PoolerHostSentinelKeyPrefix + testHttpFailure); int(ttl) != GlobalHostChecker.checker.checkTimeout+1 {
t.Error("HostDown expiration key should be checkTimeout + 1", ttl)
}
GlobalHostChecker.checkerMu.Unlock()
}