2015-08-04 17:35:09 -05:00
|
|
|
package markdown
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2015-09-11 10:23:08 -05:00
|
|
|
"sync"
|
2015-08-04 17:35:09 -05: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-11 12:22:41 -05:00
|
|
|
// wait little more because of concurrency
|
|
|
|
time.Sleep(interval * 9)
|
2015-08-04 17:35:09 -05:00
|
|
|
stopChan <- struct{}{}
|
2015-09-11 12:22:41 -05:00
|
|
|
if !strings.HasPrefix(out, expected) {
|
|
|
|
t.Fatalf("Expected to have prefix %v, found %v", expected, out)
|
2015-08-04 17:35:09 -05:00
|
|
|
}
|
|
|
|
out = ""
|
|
|
|
i = 0
|
2015-09-11 10:23:08 -05:00
|
|
|
var mu sync.Mutex
|
2015-08-04 17:35:09 -05:00
|
|
|
stopChan = TickerFunc(interval, func() {
|
|
|
|
i++
|
2015-09-11 10:23:08 -05:00
|
|
|
mu.Lock()
|
2015-08-04 17:35:09 -05:00
|
|
|
out += fmt.Sprint(i)
|
2015-09-11 10:23:08 -05:00
|
|
|
mu.Unlock()
|
2015-08-04 17:35:09 -05:00
|
|
|
})
|
|
|
|
time.Sleep(interval * 10)
|
2015-09-11 10:23:08 -05:00
|
|
|
mu.Lock()
|
|
|
|
res := out
|
|
|
|
mu.Unlock()
|
|
|
|
if !strings.HasPrefix(res, expected) || res == expected {
|
2015-08-04 17:35:09 -05:00
|
|
|
t.Fatalf("expected (%v) must be a proper prefix of out(%v).", expected, out)
|
|
|
|
}
|
|
|
|
}
|