watch: convert TestKeyPrefixWatch to use channels

This commit is contained in:
Frank Schroeder 2017-10-24 15:12:08 +02:00
parent f24b06fe97
commit be1d91966a
No known key found for this signature in database
GPG Key ID: 4D65C6EAEC87DECD
1 changed files with 29 additions and 33 deletions

View File

@ -124,56 +124,52 @@ func TestKeyPrefixWatch(t *testing.T) {
a := agent.NewTestAgent(t.Name(), ``)
defer a.Shutdown()
invoke := make(chan error)
plan := mustParse(t, `{"type":"keyprefix", "prefix":"foo/"}`)
invoke := 0
plan.Handler = func(idx uint64, raw interface{}) {
if invoke == 0 {
if raw == nil {
return
}
v, ok := raw.(consulapi.KVPairs)
if ok && v == nil {
return
}
if !ok || v == nil || string(v[0].Key) != "foo/bar" {
t.Fatalf("Bad: %#v", raw)
}
invoke++
if raw == nil {
return // ignore
}
v, ok := raw.(consulapi.KVPairs)
if !ok || len(v) == 0 {
return
}
if string(v[0].Key) != "foo/bar" {
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()
kv := a.Client().KV()
time.Sleep(20 * time.Millisecond)
pair := &consulapi.KVPair{
Key: "foo/bar",
}
_, err := kv.Put(pair, nil)
if err != nil {
t.Fatalf("err: %v", err)
}
// Wait for the query to run
time.Sleep(20 * time.Millisecond)
plan.Stop()
// Delete the key
_, err = kv.Delete("foo/bar", nil)
if err != nil {
if _, err := kv.Put(pair, 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 err := <-invoke; err != nil {
t.Fatalf("err: %v", err)
}
if invoke == 0 {
t.Fatalf("bad: %v", invoke)
}
plan.Stop()
wg.Wait()
}
func TestServicesWatch(t *testing.T) {