Debian Codegraph @

api/v0/healthchecker/slave_health_checker_test.go

raw symbols SCIP
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package healthchecker
20
21 import (
22 "fmt"
23 "net/http"
24 "net/http/httptest"
25 "sync"
26 "sync/atomic"
27 "testing"
28 "time"
29
30 "github.com/mesos/mesos-go/api/v0/upid"
31 "github.com/stretchr/testify/assert"
32 )
33
34 type thresholdMonitor struct {
35 cnt int32
36 threshold int32
37 }
38
39 func newThresholdMonitor(threshold int) *thresholdMonitor {
40 return &thresholdMonitor{threshold: int32(threshold)}
41 }
42
43 // incAndTest returns true if the threshold is reached.
44 func (t *thresholdMonitor) incAndTest() bool {
45 if atomic.AddInt32(&t.cnt, 1) >= t.threshold {
46 return false
47 }
48 return true
49 }
50
51 // blockedServer replies only threshold times, after that
52 // it will block.
53 type blockedServer struct {
54 th *thresholdMonitor
55 ch chan struct{}
56 stopOnce sync.Once
57 }
58
59 func newBlockedServer(threshold int) *blockedServer {
60 return &blockedServer{
61 th: newThresholdMonitor(threshold),
62 ch: make(chan struct{}),
63 }
64 }
65
66 func (s *blockedServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
67 if s.th.incAndTest() {
68 return
69 }
70 <-s.ch
71 }
72
73 func (s *blockedServer) stop() {
74 s.stopOnce.Do(func() { close(s.ch) })
75 }
76
77 // eofServer will close the connection after it replies for threshold times.
78 // Thus the health checker will get an EOF error.
79 type eofServer struct {
80 th *thresholdMonitor
81 }
82
83 func newEOFServer(threshold int) *eofServer {
84 return &eofServer{newThresholdMonitor(threshold)}
85 }
86
87 func (s *eofServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
88 if s.th.incAndTest() {
89 return
90 }
91 hj := w.(http.Hijacker)
92 conn, _, err := hj.Hijack()
93 if err != nil {
94 panic("Cannot hijack")
95 }
96 conn.Close()
97 }
98
99 // errorStatusCodeServer will reply error status code (e.g. 503) after
100 // it replies for threhold time.
101 type errorStatusCodeServer struct {
102 th *thresholdMonitor
103 }
104
105 func newErrorStatusServer(threshold int) *errorStatusCodeServer {
106 return &errorStatusCodeServer{newThresholdMonitor(threshold)}
107 }
108
109 func (s *errorStatusCodeServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
110 if s.th.incAndTest() {
111 return
112 }
113 w.WriteHeader(http.StatusServiceUnavailable)
114 }
115
116 // goodServer always returns status ok.
117 type goodServer bool
118
119 func (s *goodServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {}
120
121 // partitionedServer returns status ok at some first requests.
122 // Then it will block for a while, and then reply again.
123 type partitionedServer struct {
124 healthyCnt int32
125 partitionCnt int32
126 cnt int32
127 mutex *sync.Mutex
128 cond *sync.Cond
129 }
130
131 func newPartitionedServer(healthyCnt, partitionCnt int) *partitionedServer {
132 mutex := new(sync.Mutex)
133 cond := sync.NewCond(mutex)
134 return &partitionedServer{
135 healthyCnt: int32(healthyCnt),
136 partitionCnt: int32(partitionCnt),
137 mutex: mutex,
138 cond: cond,
139 }
140 }
141
142 func (s *partitionedServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
143 cnt := atomic.AddInt32(&s.cnt, 1)
144 if cnt < s.healthyCnt {
145 return
146 }
147 if cnt < s.healthyCnt+s.partitionCnt {
148 s.mutex.Lock()
149 defer s.mutex.Unlock()
150 s.cond.Wait()
151 return
152 }
153 s.mutex.Lock()
154 defer s.mutex.Unlock()
155 s.cond.Broadcast()
156 }
157
158 func TestSlaveHealthCheckerFailedOnBlockedSlave(t *testing.T) {
159 s := newBlockedServer(5)
160 ts := httptest.NewUnstartedServer(s)
161 ts.Start()
162 defer ts.Close()
163 defer s.stop()
164
165 upid, err := upid.Parse(fmt.Sprintf("slave@%s", ts.Listener.Addr().String()))
166 assert.NoError(t, err)
167
168 checker := NewSlaveHealthChecker(upid, 10, time.Millisecond*10, time.Millisecond*10)
169 ch := checker.Start()
170 defer checker.Stop()
171
172 select {
173 case <-time.After(time.Second):
174 t.Error("timeout")
175 case <-ch:
176 assert.True(t, atomic.LoadInt32(&s.th.cnt) > 10)
177 }
178
179 // TODO(jdef) hack: this sucks, but there's a data race in httptest's handler when Close()
180 // and ServeHTTP() are invoked (WaitGroup DATA RACE). Sleeping here to attempt to avoid that.
181 // I think this is supposed to be fixed in go1.6
182 time.Sleep(5 * time.Second)
183 }
184
185 func TestSlaveHealthCheckerFailedOnEOFSlave(t *testing.T) {
186 s := newEOFServer(5)
187 ts := httptest.NewUnstartedServer(s)
188 ts.Start()
189 defer ts.Close()
190
191 upid, err := upid.Parse(fmt.Sprintf("slave@%s", ts.Listener.Addr().String()))
192 assert.NoError(t, err)
193
194 checker := NewSlaveHealthChecker(upid, 10, time.Millisecond*10, time.Millisecond*10)
195 ch := checker.Start()
196 defer checker.Stop()
197
198 select {
199 case <-time.After(time.Second):
200 t.Fatal("timeout")
201 case <-ch:
202 assert.True(t, atomic.LoadInt32(&s.th.cnt) > 10)
203 }
204 }
205
206 func TestSlaveHealthCheckerFailedOnErrorStatusSlave(t *testing.T) {
207 s := newErrorStatusServer(5)
208 ts := httptest.NewUnstartedServer(s)
209 ts.Start()
210 defer ts.Close()
211
212 upid, err := upid.Parse(fmt.Sprintf("slave@%s", ts.Listener.Addr().String()))
213 assert.NoError(t, err)
214
215 checker := NewSlaveHealthChecker(upid, 10, time.Millisecond*10, time.Millisecond*10)
216 ch := checker.Start()
217 defer checker.Stop()
218
219 select {
220 case <-time.After(time.Second):
221 t.Fatal("timeout")
222 case <-ch:
223 assert.True(t, atomic.LoadInt32(&s.th.cnt) > 10)
224 }
225 }
226
227 func TestSlaveHealthCheckerSucceed(t *testing.T) {
228 t.Skip("skipping known flaky test (fails on busy CI servers, should use a fake clock)")
229
230 s := new(goodServer)
231 ts := httptest.NewUnstartedServer(s)
232 ts.Start()
233 defer ts.Close()
234
235 upid, err := upid.Parse(fmt.Sprintf("slave@%s", ts.Listener.Addr().String()))
236 assert.NoError(t, err)
237
238 checker := NewSlaveHealthChecker(upid, 10, time.Millisecond*10, time.Millisecond*10)
239 ch := checker.Start()
240 defer checker.Stop()
241
242 select {
243 case <-time.After(time.Second):
244 assert.EqualValues(t, 0, atomic.LoadInt32(&checker.continuousUnhealthyCount))
245 case <-ch:
246 t.Fatal("Shouldn't get unhealthy notification")
247 }
248 }
249
250 func TestSlaveHealthCheckerPartitonedSlave(t *testing.T) {
251 t.Skip("skipping known flaky test (fails on busy CI servers, should use a fake clock)")
252
253 s := newPartitionedServer(5, 9)
254 ts := httptest.NewUnstartedServer(s)
255 ts.Start()
256 defer ts.Close()
257
258 t.Log("test server listening on", ts.Listener.Addr())
259 upid, err := upid.Parse(fmt.Sprintf("slave@%s", ts.Listener.Addr().String()))
260 assert.NoError(t, err)
261
262 checker := NewSlaveHealthChecker(upid, 10, time.Millisecond*10, time.Millisecond*10)
263 ch := checker.Start()
264 defer func() {
265 checker.Stop()
266 <-checker.stop
267 }()
268
269 select {
270 case <-time.After(2 * time.Second):
271 actual := atomic.LoadInt32(&checker.continuousUnhealthyCount)
272 assert.EqualValues(t, 0, actual, "expected 0 unhealthy counts instead of %d", actual)
273 case <-ch:
274 t.Fatal("Shouldn't get unhealthy notification")
275 }
276 }