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{}{} +}