From 59dbea768c75a7ce07453010fcf6c392738771b8 Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Wed, 9 Dec 2015 19:22:20 +0100 Subject: [PATCH 1/2] Fix race condition on AppVeyor. Increase timeout a bit. --- middleware/markdown/watcher_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/middleware/markdown/watcher_test.go b/middleware/markdown/watcher_test.go index d09d07c6..2f7ca4cb 100644 --- a/middleware/markdown/watcher_test.go +++ b/middleware/markdown/watcher_test.go @@ -18,7 +18,7 @@ func TestWatcher(t *testing.T) { out += fmt.Sprint(i) }) // wait little more because of concurrency - time.Sleep(interval * 9) + time.Sleep(interval * 12) stopChan <- struct{}{} if !strings.HasPrefix(out, expected) { t.Fatalf("Expected to have prefix %v, found %v", expected, out) @@ -32,7 +32,7 @@ func TestWatcher(t *testing.T) { out += fmt.Sprint(i) mu.Unlock() }) - time.Sleep(interval * 10) + time.Sleep(interval * 15) mu.Lock() res := out mu.Unlock() From 2e295b51b3344ead5a980827a8f26d9ec9c9b1de Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Wed, 9 Dec 2015 20:18:18 +0100 Subject: [PATCH 2/2] Use channel instead for a synchronous interval. --- middleware/markdown/watcher_test.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/middleware/markdown/watcher_test.go b/middleware/markdown/watcher_test.go index 2f7ca4cb..8a89e007 100644 --- a/middleware/markdown/watcher_test.go +++ b/middleware/markdown/watcher_test.go @@ -13,14 +13,14 @@ func TestWatcher(t *testing.T) { interval := time.Millisecond * 100 i := 0 out := "" + syncChan := make(chan struct{}) stopChan := TickerFunc(interval, func() { i++ out += fmt.Sprint(i) + syncChan <- struct{}{} }) - // wait little more because of concurrency - time.Sleep(interval * 12) - stopChan <- struct{}{} - if !strings.HasPrefix(out, expected) { + sleepInSync(8, syncChan, stopChan) + if out != expected { t.Fatalf("Expected to have prefix %v, found %v", expected, out) } out = "" @@ -31,8 +31,9 @@ func TestWatcher(t *testing.T) { mu.Lock() out += fmt.Sprint(i) mu.Unlock() + syncChan <- struct{}{} }) - time.Sleep(interval * 15) + sleepInSync(9, syncChan, stopChan) mu.Lock() res := out mu.Unlock() @@ -40,3 +41,10 @@ func TestWatcher(t *testing.T) { t.Fatalf("expected (%v) must be a proper prefix of out(%v).", expected, out) } } + +func sleepInSync(times int, syncChan chan struct{}, stopChan chan struct{}) { + for i := 0; i < times; i++ { + <-syncChan + } + stopChan <- struct{}{} +}