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 := ""
|
2015-12-09 14:18:18 -05:00
|
|
|
syncChan := make(chan struct{})
|
2015-08-04 17:35:09 -05:00
|
|
|
stopChan := TickerFunc(interval, func() {
|
|
|
|
i++
|
|
|
|
out += fmt.Sprint(i)
|
2015-12-09 14:18:18 -05:00
|
|
|
syncChan <- struct{}{}
|
2015-08-04 17:35:09 -05:00
|
|
|
})
|
2015-12-09 14:18:18 -05:00
|
|
|
sleepInSync(8, syncChan, stopChan)
|
|
|
|
if out != expected {
|
2015-09-11 12:22:41 -05:00
|
|
|
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-12-09 14:18:18 -05:00
|
|
|
syncChan <- struct{}{}
|
2015-08-04 17:35:09 -05:00
|
|
|
})
|
2015-12-09 14:18:18 -05:00
|
|
|
sleepInSync(9, syncChan, stopChan)
|
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)
|
|
|
|
}
|
|
|
|
}
|
2015-12-09 14:18:18 -05:00
|
|
|
|
|
|
|
func sleepInSync(times int, syncChan chan struct{}, stopChan chan struct{}) {
|
|
|
|
for i := 0; i < times; i++ {
|
|
|
|
<-syncChan
|
|
|
|
}
|
|
|
|
stopChan <- struct{}{}
|
|
|
|
}
|