Commit aba0ae35 authored by Matt Holt's avatar Matt Holt

Merge pull request #415 from abiosoft/appveyor-fix

Fix race condition in test for AppVeyor
parents 41bdd775 2e295b51
...@@ -13,14 +13,14 @@ func TestWatcher(t *testing.T) { ...@@ -13,14 +13,14 @@ func TestWatcher(t *testing.T) {
interval := time.Millisecond * 100 interval := time.Millisecond * 100
i := 0 i := 0
out := "" out := ""
syncChan := make(chan struct{})
stopChan := TickerFunc(interval, func() { stopChan := TickerFunc(interval, func() {
i++ i++
out += fmt.Sprint(i) out += fmt.Sprint(i)
syncChan <- struct{}{}
}) })
// wait little more because of concurrency sleepInSync(8, syncChan, stopChan)
time.Sleep(interval * 9) if out != expected {
stopChan <- struct{}{}
if !strings.HasPrefix(out, expected) {
t.Fatalf("Expected to have prefix %v, found %v", expected, out) t.Fatalf("Expected to have prefix %v, found %v", expected, out)
} }
out = "" out = ""
...@@ -31,8 +31,9 @@ func TestWatcher(t *testing.T) { ...@@ -31,8 +31,9 @@ func TestWatcher(t *testing.T) {
mu.Lock() mu.Lock()
out += fmt.Sprint(i) out += fmt.Sprint(i)
mu.Unlock() mu.Unlock()
syncChan <- struct{}{}
}) })
time.Sleep(interval * 10) sleepInSync(9, syncChan, stopChan)
mu.Lock() mu.Lock()
res := out res := out
mu.Unlock() mu.Unlock()
...@@ -40,3 +41,10 @@ func TestWatcher(t *testing.T) { ...@@ -40,3 +41,10 @@ func TestWatcher(t *testing.T) {
t.Fatalf("expected (%v) must be a proper prefix of out(%v).", expected, out) 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{}{}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment