From 21bc8e09963399c5bb1429d088a9d1e3312bbc36 Mon Sep 17 00:00:00 2001 From: James Phillips Date: Tue, 6 Oct 2015 20:09:35 -0700 Subject: [PATCH] Adds a watch tester helper that helps cut the cruft. --- consul/state/state_store_test.go | 45 ++++++++++++++------------------ 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/consul/state/state_store_test.go b/consul/state/state_store_test.go index e944e9bfdf..4ca7aea1d9 100644 --- a/consul/state/state_store_test.go +++ b/consul/state/state_store_test.go @@ -106,6 +106,20 @@ func testSetKey(t *testing.T, s *StateStore, idx uint64, key, value string) { } } +// verifyWatch will set up a watch channel, call the given function, and then +// make sure the watch fires within a fixed time period. +func verifyWatch(t *testing.T, watch Watch, fn func()) { + ch := make(chan struct{}) + watch.Wait(ch) + go fn() + + select { + case <-ch: + case <-time.After(100 * time.Millisecond): + t.Fatalf("watch was not notified") + } +} + func TestStateStore_maxIndex(t *testing.T) { s := testStateStore(t) @@ -1837,41 +1851,22 @@ func TestStateStore_ACLDelete(t *testing.T) { func TestStateStore_ACL_Watches(t *testing.T) { s := testStateStore(t) - ch := make(chan struct{}) - s.GetTableWatch("acls").Wait(ch) - go func() { + verifyWatch(t, s.GetTableWatch("acls"), func() { if err := s.ACLSet(1, &structs.ACL{ID: "acl1"}); err != nil { t.Fatalf("err: %s", err) } - }() - select { - case <-ch: - case <-time.After(1 * time.Second): - t.Fatalf("watch was not notified") - } + }) - s.GetTableWatch("acls").Wait(ch) - go func() { + verifyWatch(t, s.GetTableWatch("acls"), func() { if err := s.ACLDelete(2, "acl1"); err != nil { t.Fatalf("err: %s", err) } - }() - select { - case <-ch: - case <-time.After(1 * time.Second): - t.Fatalf("watch was not notified") - } + }) - s.GetTableWatch("acls").Wait(ch) - go func() { + verifyWatch(t, s.GetTableWatch("acls"), func() { if err := s.ACLRestore(&structs.ACL{ID: "acl1"}); err != nil { t.Fatalf("err: %s", err) } - }() - select { - case <-ch: - case <-time.After(1 * time.Second): - t.Fatalf("watch was not notified") - } + }) }