2015-08-05 06:35:09 +08:00
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2015-09-11 23:23:08 +08:00
|
|
|
"sync"
|
2015-08-05 06:35:09 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWatcher(t *testing.T) {
|
|
|
|
expected := "12345678"
|
|
|
|
interval := time.Millisecond * 100
|
|
|
|
i := 0
|
|
|
|
out := ""
|
2015-12-10 03:18:18 +08:00
|
|
|
syncChan := make(chan struct{})
|
2015-08-05 06:35:09 +08:00
|
|
|
stopChan := TickerFunc(interval, func() {
|
|
|
|
i++
|
|
|
|
out += fmt.Sprint(i)
|
2015-12-10 03:18:18 +08:00
|
|
|
syncChan <- struct{}{}
|
2015-08-05 06:35:09 +08:00
|
|
|
})
|
2015-12-10 03:18:18 +08:00
|
|
|
sleepInSync(8, syncChan, stopChan)
|
|
|
|
if out != expected {
|
2015-09-12 01:22:41 +08:00
|
|
|
t.Fatalf("Expected to have prefix %v, found %v", expected, out)
|
2015-08-05 06:35:09 +08:00
|
|
|
}
|
|
|
|
out = ""
|
|
|
|
i = 0
|
2015-09-11 23:23:08 +08:00
|
|
|
var mu sync.Mutex
|
2015-08-05 06:35:09 +08:00
|
|
|
stopChan = TickerFunc(interval, func() {
|
|
|
|
i++
|
2015-09-11 23:23:08 +08:00
|
|
|
mu.Lock()
|
2015-08-05 06:35:09 +08:00
|
|
|
out += fmt.Sprint(i)
|
2015-09-11 23:23:08 +08:00
|
|
|
mu.Unlock()
|
2015-12-10 03:18:18 +08:00
|
|
|
syncChan <- struct{}{}
|
2015-08-05 06:35:09 +08:00
|
|
|
})
|
2015-12-10 03:18:18 +08:00
|
|
|
sleepInSync(9, syncChan, stopChan)
|
2015-09-11 23:23:08 +08:00
|
|
|
mu.Lock()
|
|
|
|
res := out
|
|
|
|
mu.Unlock()
|
|
|
|
if !strings.HasPrefix(res, expected) || res == expected {
|
2015-08-05 06:35:09 +08:00
|
|
|
t.Fatalf("expected (%v) must be a proper prefix of out(%v).", expected, out)
|
|
|
|
}
|
|
|
|
}
|
2015-12-10 03:18:18 +08:00
|
|
|
|
|
|
|
func sleepInSync(times int, syncChan chan struct{}, stopChan chan struct{}) {
|
|
|
|
for i := 0; i < times; i++ {
|
|
|
|
<-syncChan
|
|
|
|
}
|
|
|
|
stopChan <- struct{}{}
|
|
|
|
}
|