watcher_test.go 809 Bytes
Newer Older
1 2 3 4 5
package markdown

import (
	"fmt"
	"strings"
Alexander Morozov's avatar
Alexander Morozov committed
6
	"sync"
7 8 9 10 11 12 13 14 15 16 17 18 19
	"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)
	})
20 21
	// wait little more because of concurrency
	time.Sleep(interval * 9)
22
	stopChan <- struct{}{}
23 24
	if !strings.HasPrefix(out, expected) {
		t.Fatalf("Expected to have prefix %v, found %v", expected, out)
25 26 27
	}
	out = ""
	i = 0
Alexander Morozov's avatar
Alexander Morozov committed
28
	var mu sync.Mutex
29 30
	stopChan = TickerFunc(interval, func() {
		i++
Alexander Morozov's avatar
Alexander Morozov committed
31
		mu.Lock()
32
		out += fmt.Sprint(i)
Alexander Morozov's avatar
Alexander Morozov committed
33
		mu.Unlock()
34 35
	})
	time.Sleep(interval * 10)
Alexander Morozov's avatar
Alexander Morozov committed
36 37 38 39
	mu.Lock()
	res := out
	mu.Unlock()
	if !strings.HasPrefix(res, expected) || res == expected {
40 41 42
		t.Fatalf("expected (%v) must be a proper prefix of out(%v).", expected, out)
	}
}