Browse Source

watch: convert TestEventWatch to use channels

pull/3610/head
Frank Schroeder 7 years ago
parent
commit
a54b0994ed
No known key found for this signature in database
GPG Key ID: 4D65C6EAEC87DECD
  1. 46
      watch/funcs_test.go

46
watch/funcs_test.go

@ -459,40 +459,50 @@ func TestEventWatch(t *testing.T) {
a := agent.NewTestAgent(t.Name(), ``) a := agent.NewTestAgent(t.Name(), ``)
defer a.Shutdown() defer a.Shutdown()
invoke := make(chan error)
plan := mustParse(t, `{"type":"event", "name": "foo"}`) plan := mustParse(t, `{"type":"event", "name": "foo"}`)
invoke := 0
plan.Handler = func(idx uint64, raw interface{}) { plan.Handler = func(idx uint64, raw interface{}) {
if invoke == 0 { if raw == nil {
if raw == nil { return
return }
} v, ok := raw.([]*consulapi.UserEvent)
v, ok := raw.([]*consulapi.UserEvent) if !ok || len(v) == 0 {
if !ok || len(v) == 0 || string(v[len(v)-1].Name) != "foo" { return // ignore
t.Fatalf("Bad: %#v", raw)
}
invoke++
} }
if string(v[len(v)-1].Name) != "foo" {
invoke <- errBadContent
return
}
invoke <- nil
} }
var wg sync.WaitGroup
wg.Add(1)
go func() { go func() {
defer plan.Stop() defer wg.Done()
time.Sleep(20 * time.Millisecond)
event := a.Client().Event() event := a.Client().Event()
time.Sleep(20 * time.Millisecond)
params := &consulapi.UserEvent{Name: "foo"} params := &consulapi.UserEvent{Name: "foo"}
if _, _, err := event.Fire(params, nil); err != nil { if _, _, err := event.Fire(params, nil); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
}() }()
err := plan.Run(a.HTTPAddr()) wg.Add(1)
if err != nil { go func() {
defer wg.Done()
if err := plan.Run(a.HTTPAddr()); err != nil {
t.Fatalf("err: %v", err)
}
}()
if err := <-invoke; err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
if invoke == 0 { plan.Stop()
t.Fatalf("bad: %v", invoke) wg.Wait()
}
} }
func mustParse(t *testing.T, q string) *watch.Plan { func mustParse(t *testing.T, q string) *watch.Plan {

Loading…
Cancel
Save