| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 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 |
|
| 52 |
|
| 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 |
|
| 78 |
|
| 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 |
|
| 100 |
|
| 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 |
|
| 117 |
type goodServer bool |
| 118 |
|
| 119 |
func (s *goodServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {} |
| 120 |
|
| 121 |
|
| 122 |
|
| 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 |
|
| 180 |
|
| 181 |
|
| 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 |
} |