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. 34
      watch/funcs_test.go

34
watch/funcs_test.go

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

Loading…
Cancel
Save