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 := ""
|
|
|
|
stopChan := TickerFunc(interval, func() {
|
|
|
|
i++
|
|
|
|
out += fmt.Sprint(i)
|
|
|
|
})
|
2015-09-12 01:22:41 +08:00
|
|
|
// wait little more because of concurrency
|
|
|
|
time.Sleep(interval * 9)
|
2015-08-05 06:35:09 +08:00
|
|
|
stopChan <- struct{}{}
|
2015-09-12 01:22:41 +08:00
|
|
|
if !strings.HasPrefix(out, expected) {
|
|
|
|
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-08-05 06:35:09 +08:00
|
|
|
})
|
|
|
|
time.Sleep(interval * 10)
|
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)
|
|
|
|
}
|
|
|
|
}
|