From 37eedf5cdc6a714130cfb5fb2a7615516018d6a1 Mon Sep 17 00:00:00 2001 From: Alexander Morozov Date: Fri, 11 Sep 2015 08:23:08 -0700 Subject: [PATCH] Fix race in test Signed-off-by: Alexander Morozov --- middleware/markdown/watcher_test.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/middleware/markdown/watcher_test.go b/middleware/markdown/watcher_test.go index 6aa7364b..c04f8e4f 100644 --- a/middleware/markdown/watcher_test.go +++ b/middleware/markdown/watcher_test.go @@ -3,6 +3,7 @@ package markdown import ( "fmt" "strings" + "sync" "testing" "time" ) @@ -23,12 +24,18 @@ func TestWatcher(t *testing.T) { } out = "" i = 0 + var mu sync.Mutex stopChan = TickerFunc(interval, func() { i++ + mu.Lock() out += fmt.Sprint(i) + mu.Unlock() }) time.Sleep(interval * 10) - if !strings.HasPrefix(out, expected) || out == expected { + mu.Lock() + res := out + mu.Unlock() + if !strings.HasPrefix(res, expected) || res == expected { t.Fatalf("expected (%v) must be a proper prefix of out(%v).", expected, out) } }