watch: convert TestServiceWatch to use channels

This commit is contained in:
Frank Schroeder 2017-10-24 14:23:57 +02:00
parent 375fbcb643
commit ef4e8b5811
No known key found for this signature in database
GPG Key ID: 4D65C6EAEC87DECD
1 changed files with 32 additions and 26 deletions

View File

@ -281,49 +281,55 @@ func TestServiceWatch(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":"service", "service":"foo", "tag":"bar", "passingonly":true}`) plan := mustParse(t, `{"type":"service", "service":"foo", "tag":"bar", "passingonly":true}`)
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 // ignore
} }
v, ok := raw.([]*consulapi.ServiceEntry) v, ok := raw.([]*consulapi.ServiceEntry)
if ok && len(v) == 0 { if !ok || len(v) == 0 {
return // ignore
}
if v[0].Service.ID != "foo" {
invoke <- errBadContent
return return
} }
if !ok || v[0].Service.ID != "foo" { invoke <- nil
t.Fatalf("Bad: %#v", raw)
}
invoke++
}
} }
var wg sync.WaitGroup
wg.Add(1)
go func() { go func() {
time.Sleep(20 * time.Millisecond) defer wg.Done()
agent := a.Client().Agent() agent := a.Client().Agent()
time.Sleep(20 * time.Millisecond)
reg := &consulapi.AgentServiceRegistration{ reg := &consulapi.AgentServiceRegistration{
ID: "foo", ID: "foo",
Name: "foo", Name: "foo",
Tags: []string{"bar"}, Tags: []string{"bar"},
} }
agent.ServiceRegister(reg) if err := agent.ServiceRegister(reg); err != nil {
t.Fatalf("err: %v", err)
time.Sleep(20 * time.Millisecond) }
plan.Stop()
agent.ServiceDeregister("foo")
}() }()
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 TestChecksWatch_State(t *testing.T) { func TestChecksWatch_State(t *testing.T) {