| 1 |
package zoo_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"sync" |
| 6 |
"testing" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/gogo/protobuf/proto" |
| 10 |
log "github.com/golang/glog" |
| 11 |
"github.com/mesos/mesos-go/api/v0/detector" |
| 12 |
mock_zkdetector "github.com/mesos/mesos-go/api/v0/detector/zoo/mock" |
| 13 |
mesos "github.com/mesos/mesos-go/api/v0/mesosproto" |
| 14 |
util "github.com/mesos/mesos-go/api/v0/mesosutil" |
| 15 |
"github.com/samuel/go-zookeeper/zk" |
| 16 |
"github.com/stretchr/testify/assert" |
| 17 |
|
| 18 |
. "github.com/mesos/mesos-go/api/v0/detector/zoo" |
| 19 |
) |
| 20 |
|
| 21 |
const ( |
| 22 |
zkurl = "zk://127.0.0.1:2181/mesos" |
| 23 |
zkurl_bad = "zk://127.0.0.1:2181" |
| 24 |
test_zk_path = "/test" |
| 25 |
) |
| 26 |
|
| 27 |
func newTestMasterInfo(id int) []byte { |
| 28 |
miPb := util.NewMasterInfo(fmt.Sprintf("master(%d)@localhost:5050", id), 123456789, 400) |
| 29 |
data, err := proto.Marshal(miPb) |
| 30 |
if err != nil { |
| 31 |
panic(err) |
| 32 |
} |
| 33 |
return data |
| 34 |
} |
| 35 |
|
| 36 |
func TestMasterDetectorChildrenChanged(t *testing.T) { |
| 37 |
var ( |
| 38 |
path = test_zk_path |
| 39 |
snapDetected = make(chan struct{}) |
| 40 |
|
| 41 |
bf = func(client ZKInterface, done <-chan struct{}) (ZKInterface, error) { |
| 42 |
if client == nil { |
| 43 |
log.V(1).Infoln("bootstrapping detector") |
| 44 |
defer log.V(1).Infoln("bootstrapping detector ..finished") |
| 45 |
|
| 46 |
mocked, _, errs := mock_zkdetector.NewClient(test_zk_path, "info_0", "info_5", "info_10") |
| 47 |
client = mocked |
| 48 |
|
| 49 |
mocked.On("Data", fmt.Sprintf("%s/info_0", path)).Return(newTestMasterInfo(0), nil) |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
go func() { |
| 54 |
defer close(errs) |
| 55 |
select { |
| 56 |
case <-snapDetected: |
| 57 |
case <-done: |
| 58 |
t.Errorf("detector died before child snapshot") |
| 59 |
} |
| 60 |
}() |
| 61 |
} |
| 62 |
return client, nil |
| 63 |
} |
| 64 |
called = 0 |
| 65 |
lostMaster = make(chan struct{}) |
| 66 |
md, err = NewMasterDetector(zkurl, MinCyclePeriod(10*time.Millisecond), Bootstrap(bf)) |
| 67 |
) |
| 68 |
defer md.Cancel() |
| 69 |
assert.NoError(t, err) |
| 70 |
|
| 71 |
const expectedLeader = "master(0)@localhost:5050" |
| 72 |
err = md.Detect(detector.OnMasterChanged(func(master *mesos.MasterInfo) { |
| 73 |
|
| 74 |
|
| 75 |
switch called++; called { |
| 76 |
case 1: |
| 77 |
defer close(snapDetected) |
| 78 |
assert.NotNil(t, master) |
| 79 |
assert.Equal(t, expectedLeader, master.GetId()) |
| 80 |
case 2: |
| 81 |
md.Cancel() |
| 82 |
defer close(lostMaster) |
| 83 |
assert.Nil(t, master) |
| 84 |
default: |
| 85 |
t.Errorf("unexpected notification call attempt %d", called) |
| 86 |
} |
| 87 |
})) |
| 88 |
assert.NoError(t, err) |
| 89 |
|
| 90 |
fatalOn(t, 10*time.Second, lostMaster, "Waited too long for lost master") |
| 91 |
|
| 92 |
select { |
| 93 |
case <-md.Done(): |
| 94 |
assert.Equal(t, 2, called, "expected 2 detection callbacks instead of %d", called) |
| 95 |
case <-time.After(time.Second * 10): |
| 96 |
panic("Waited too long for detector shutdown...") |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
func TestMasterDetectorFlappyConnectionState(t *testing.T) { |
| 102 |
const ITERATIONS = 3 |
| 103 |
var ( |
| 104 |
path = test_zk_path |
| 105 |
|
| 106 |
bf = func(client ZKInterface, _ <-chan struct{}) (ZKInterface, error) { |
| 107 |
if client == nil { |
| 108 |
log.V(1).Infoln("bootstrapping detector") |
| 109 |
defer log.V(1).Infoln("bootstrapping detector ..finished") |
| 110 |
|
| 111 |
children := []string{"info_0", "info_5", "info_10"} |
| 112 |
mocked, snaps, errs := mock_zkdetector.NewClient(test_zk_path, children...) |
| 113 |
client = mocked |
| 114 |
|
| 115 |
mocked.On("Data", fmt.Sprintf("%s/info_0", path)).Return(newTestMasterInfo(0), nil) |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
go func() { |
| 120 |
defer close(errs) |
| 121 |
for attempt := 0; attempt < ITERATIONS; attempt++ { |
| 122 |
|
| 123 |
errs <- zk.ErrSessionExpired |
| 124 |
|
| 125 |
|
| 126 |
snaps <- children |
| 127 |
} |
| 128 |
}() |
| 129 |
} |
| 130 |
return client, nil |
| 131 |
} |
| 132 |
called = 0 |
| 133 |
lostMaster = make(chan struct{}) |
| 134 |
wg sync.WaitGroup |
| 135 |
md, err = NewMasterDetector(zkurl, MinCyclePeriod(10*time.Millisecond), Bootstrap(bf)) |
| 136 |
) |
| 137 |
defer md.Cancel() |
| 138 |
assert.NoError(t, err) |
| 139 |
|
| 140 |
wg.Add(1 + ITERATIONS) |
| 141 |
|
| 142 |
const EXPECTED_CALLS = (ITERATIONS * 2) + 2 |
| 143 |
err = md.Detect(detector.OnMasterChanged(func(master *mesos.MasterInfo) { |
| 144 |
called++ |
| 145 |
log.V(3).Infof("detector invoked: called %d", called) |
| 146 |
switch { |
| 147 |
case called < EXPECTED_CALLS: |
| 148 |
if master != nil { |
| 149 |
wg.Done() |
| 150 |
assert.Equal(t, master.GetId(), "master(0)@localhost:5050") |
| 151 |
} |
| 152 |
case called == EXPECTED_CALLS: |
| 153 |
md.Cancel() |
| 154 |
defer close(lostMaster) |
| 155 |
assert.Nil(t, master) |
| 156 |
default: |
| 157 |
t.Errorf("unexpected notification call attempt %d", called) |
| 158 |
} |
| 159 |
})) |
| 160 |
assert.NoError(t, err) |
| 161 |
|
| 162 |
fatalAfter(t, 10*time.Second, wg.Wait, "Waited too long for new-master alerts") |
| 163 |
fatalOn(t, 3*time.Second, lostMaster, "Waited too long for lost master") |
| 164 |
|
| 165 |
select { |
| 166 |
case <-md.Done(): |
| 167 |
assert.Equal(t, EXPECTED_CALLS, called, "expected %d detection callbacks instead of %d", EXPECTED_CALLS, called) |
| 168 |
case <-time.After(time.Second * 10): |
| 169 |
panic("Waited too long for detector shutdown...") |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
func TestMasterDetector_multipleLeadershipChanges(t *testing.T) { |
| 174 |
var ( |
| 175 |
leadershipChanges = [][]string{ |
| 176 |
{"info_014", "info_010", "info_005"}, |
| 177 |
{"info_005", "info_004", "info_022"}, |
| 178 |
{}, |
| 179 |
{"info_017", "info_099", "info_200"}, |
| 180 |
} |
| 181 |
|
| 182 |
ITERATIONS = len(leadershipChanges) |
| 183 |
|
| 184 |
EXPECTED_CALLS = (ITERATIONS + 2) |
| 185 |
path = test_zk_path |
| 186 |
|
| 187 |
bf = func(client ZKInterface, _ <-chan struct{}) (ZKInterface, error) { |
| 188 |
if client == nil { |
| 189 |
log.V(1).Infoln("bootstrapping detector") |
| 190 |
defer log.V(1).Infoln("bootstrapping detector ..finished") |
| 191 |
|
| 192 |
children := []string{"info_0", "info_5", "info_10"} |
| 193 |
mocked, snaps, errs := mock_zkdetector.NewClient(test_zk_path, children...) |
| 194 |
client = mocked |
| 195 |
|
| 196 |
mocked.On("Data", fmt.Sprintf("%s/info_0", path)).Return(newTestMasterInfo(0), nil) |
| 197 |
mocked.On("Data", fmt.Sprintf("%s/info_005", path)).Return(newTestMasterInfo(5), nil) |
| 198 |
mocked.On("Data", fmt.Sprintf("%s/info_004", path)).Return(newTestMasterInfo(4), nil) |
| 199 |
mocked.On("Data", fmt.Sprintf("%s/info_017", path)).Return(newTestMasterInfo(17), nil) |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
go func() { |
| 204 |
defer close(errs) |
| 205 |
for attempt := 0; attempt < ITERATIONS; attempt++ { |
| 206 |
snaps <- leadershipChanges[attempt] |
| 207 |
} |
| 208 |
}() |
| 209 |
} |
| 210 |
return client, nil |
| 211 |
} |
| 212 |
|
| 213 |
called = 0 |
| 214 |
lostMaster = make(chan struct{}) |
| 215 |
expectedLeaders = []int{0, 5, 4, 17} |
| 216 |
leaderIdx = 0 |
| 217 |
wg sync.WaitGroup |
| 218 |
md, err = NewMasterDetector(zkurl, MinCyclePeriod(10*time.Millisecond), Bootstrap(bf)) |
| 219 |
) |
| 220 |
defer md.Cancel() |
| 221 |
assert.NoError(t, err) |
| 222 |
|
| 223 |
wg.Add(ITERATIONS) |
| 224 |
err = md.Detect(detector.OnMasterChanged(func(master *mesos.MasterInfo) { |
| 225 |
called++ |
| 226 |
log.V(3).Infof("detector invoked: called %d", called) |
| 227 |
switch { |
| 228 |
case called < EXPECTED_CALLS: |
| 229 |
if master != nil { |
| 230 |
expectedLeader := fmt.Sprintf("master(%d)@localhost:5050", expectedLeaders[leaderIdx]) |
| 231 |
assert.Equal(t, expectedLeader, master.GetId()) |
| 232 |
leaderIdx++ |
| 233 |
wg.Done() |
| 234 |
} |
| 235 |
case called == EXPECTED_CALLS: |
| 236 |
md.Cancel() |
| 237 |
defer close(lostMaster) |
| 238 |
assert.Nil(t, master) |
| 239 |
default: |
| 240 |
t.Errorf("unexpected notification call attempt %d", called) |
| 241 |
} |
| 242 |
})) |
| 243 |
assert.NoError(t, err) |
| 244 |
|
| 245 |
fatalAfter(t, 10*time.Second, wg.Wait, "Waited too long for new-master alerts") |
| 246 |
fatalOn(t, 3*time.Second, lostMaster, "Waited too long for lost master") |
| 247 |
|
| 248 |
select { |
| 249 |
case <-md.Done(): |
| 250 |
assert.Equal(t, EXPECTED_CALLS, called, "expected %d detection callbacks instead of %d", EXPECTED_CALLS, called) |
| 251 |
case <-time.After(time.Second * 10): |
| 252 |
panic("Waited too long for detector shutdown...") |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
func afterFunc(f func()) <-chan struct{} { |
| 257 |
ch := make(chan struct{}) |
| 258 |
go func() { |
| 259 |
defer close(ch) |
| 260 |
f() |
| 261 |
}() |
| 262 |
return ch |
| 263 |
} |
| 264 |
|
| 265 |
func fatalAfter(t *testing.T, d time.Duration, f func(), msg string, args ...interface{}) { |
| 266 |
fatalOn(t, d, afterFunc(f), msg, args...) |
| 267 |
} |
| 268 |
|
| 269 |
func fatalOn(t *testing.T, d time.Duration, ch <-chan struct{}, msg string, args ...interface{}) { |
| 270 |
select { |
| 271 |
case <-ch: |
| 272 |
return |
| 273 |
case <-time.After(d): |
| 274 |
|
| 275 |
select { |
| 276 |
case <-ch: |
| 277 |
return |
| 278 |
default: |
| 279 |
t.Fatalf(msg, args...) |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|