From 0ff311356d910d41e9d746f4ec38a0f9b7da93c6 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Mon, 15 Jun 2015 13:48:54 +0100 Subject: [PATCH] Flush the background updater goroutines to make tests reliable --- common/sched_queue_test.go | 6 ++++++ nameserver/mocks_test.go | 6 ++++++ nameserver/server_cache_test.go | 4 ++++ nameserver/zone_lookup_test.go | 6 ++++++ 4 files changed, 22 insertions(+) diff --git a/common/sched_queue_test.go b/common/sched_queue_test.go index 63d4812b..32e05f3b 100644 --- a/common/sched_queue_test.go +++ b/common/sched_queue_test.go @@ -24,8 +24,10 @@ func TestSchedCallsBasic(t *testing.T) { } schedQueue.Add(c, clk.Now().Add(time.Second)) + schedQueue.Flush() for i := 0; i < testSecs; i++ { clk.Add(time.Second) + schedQueue.Flush() } t.Logf("Now: %s - calls: %d", clk.Now(), schedQueue.Count()) @@ -51,8 +53,10 @@ func TestSchedCallsGap(t *testing.T) { } schedQueue.Add(c2, clk.Now().Add(time.Second)) + schedQueue.Flush() for i := 0; i < testSecs; i++ { clk.Add(time.Second) + schedQueue.Flush() } t.Logf("Now: %s - calls: %d", clk.Now(), schedQueue.Count()) @@ -77,8 +81,10 @@ func TestSchedCallsStop(t *testing.T) { } schedQueue.Add(c2, clk.Now().Add(time.Second)) + schedQueue.Flush() for i := 0; i < testSecs; i++ { clk.Add(time.Second) + schedQueue.Flush() } t.Logf("Now: %s - calls: %d", clk.Now(), schedQueue.Count()) diff --git a/nameserver/mocks_test.go b/nameserver/mocks_test.go index d2cb9810..c6a73f5a 100644 --- a/nameserver/mocks_test.go +++ b/nameserver/mocks_test.go @@ -291,6 +291,12 @@ func (zbs zoneDbsWithMockedMDns) Stop() { } } +func (zbs zoneDbsWithMockedMDns) Flush() { + for _, entry := range zbs { + entry.Zone.refreshScheds.Flush() + } +} + ////////////////////////////////////////////////////////////////// // A mocked cache where we never find a single thing... ;) diff --git a/nameserver/server_cache_test.go b/nameserver/server_cache_test.go index 16934db1..5b366928 100644 --- a/nameserver/server_cache_test.go +++ b/nameserver/server_cache_test.go @@ -297,6 +297,7 @@ func TestServerCacheRefresh(t *testing.T) { Debug.Printf("Adding an IPs to %s", testName1) dbs[1].Zone.AddRecord(containerID, testName1, net.ParseIP("10.2.2.1")) + dbs.Flush() // ensure the immediate refresh is done // Zone database #2 at this point: // first.weave.local = 10.2.2.1 @@ -308,6 +309,7 @@ func TestServerCacheRefresh(t *testing.T) { assertNotLocalInCache(t, cache, qName2, "after asking for second name") clk.Forward(refreshInterval / 2) + dbs.Flush() Debug.Printf("Adding an IP to %s and to %s", testName1, testName2) dbs[1].Zone.AddRecord(containerID, testName1, net.ParseIP("10.2.2.2")) @@ -317,6 +319,7 @@ func TestServerCacheRefresh(t *testing.T) { // first.weave.local = 10.2.2.1 10.2.2.2 // second.weave.local = 10.9.9.2 clk.Forward(refreshInterval/2 + 2) + dbs.Flush() // at this point, the testName1 should have been refreshed // so it should have two IPs, and the cache entry should have been invalidated @@ -332,6 +335,7 @@ func TestServerCacheRefresh(t *testing.T) { dbs[1].Zone.DeleteRecords(containerID, "", net.ParseIP("10.2.2.1")) dbs[1].Zone.DeleteRecords(containerID, "", net.ParseIP("10.2.2.2")) clk.Forward(refreshInterval + 1) + dbs.Flush() qName1, _ = assertExchange(t, testName1, dns.TypeA, testPort, 0, 0, dns.RcodeNameError) qName2, _ = assertExchange(t, testName2, dns.TypeA, testPort, 0, 0, dns.RcodeNameError) diff --git a/nameserver/zone_lookup_test.go b/nameserver/zone_lookup_test.go index 43007cb9..c93b7aa0 100644 --- a/nameserver/zone_lookup_test.go +++ b/nameserver/zone_lookup_test.go @@ -56,6 +56,7 @@ func TestZoneRefresh(t *testing.T) { wt.AssertEqualInt(t, len(res), 1, "lookup result") clk.Forward(refreshInterval / 2) + dbs.Flush() Debug.Printf("A couple of seconds later, we should still have one IP for that name") res, err = dbs[0].Zone.DomainLookupName(name) wt.AssertNoErr(t, err) @@ -63,6 +64,7 @@ func TestZoneRefresh(t *testing.T) { Debug.Printf("And then we add 2 IPs for that name at ZoneDb 2") clk.Forward(1) + dbs.Flush() Debug.Printf("Adding 2 IPs to '%s' in Db #2", name) dbs[1].Zone.AddRecord("someident", name, net.ParseIP(addr2)) dbs[1].Zone.AddRecord("someident", name, net.ParseIP(addr3)) @@ -72,6 +74,7 @@ func TestZoneRefresh(t *testing.T) { Debug.Printf("Wait for a while, until a refresh is performed...") clk.Forward(refreshInterval + 1) + dbs.Flush() Debug.Printf("A refresh should have been scheduled now: we should have 3 IPs:") Debug.Printf("the first (local) IP and the others obtained from zone2 with a mDNS query") Debug.Printf("Asking for '%s' again... we should have 3 IPs now", name) @@ -82,6 +85,7 @@ func TestZoneRefresh(t *testing.T) { Debug.Printf("We will not ask for `name` for a while, so it will become irrelevant and will be removed...") clk.Forward(refreshInterval + relevantTime + 1) + dbs.Flush() // the name should be irrelevant now, and all remote info should have been // removed from the zone database @@ -92,6 +96,7 @@ func TestZoneRefresh(t *testing.T) { Debug.Printf("There is no remote info about this name at zone 1: a new IP appears remotely meanwhile...") clk.Forward(1) + dbs.Flush() Debug.Printf("Adding '%s' to Db #2", name) dbs[1].Zone.AddRecord("someident", name, net.ParseIP(addr4)) @@ -103,6 +108,7 @@ func TestZoneRefresh(t *testing.T) { Debug.Printf("... but a second lookup should return all the results in the network") clk.Forward(refreshInterval + 1) + dbs.Flush() res, err = dbs[0].Zone.DomainLookupName(name) Debug.Printf("Got: %s", res)