consul/watch/plan_test.go

55 lines
960 B
Go
Raw Normal View History

2014-08-20 20:45:34 +00:00
package watch
import (
"testing"
"time"
)
func init() {
watchFuncFactory["noop"] = noopWatch
}
2014-08-21 18:38:44 +00:00
func noopWatch(params map[string]interface{}) (WatchFunc, error) {
2014-08-20 20:45:34 +00:00
fn := func(p *WatchPlan) (uint64, interface{}, error) {
idx := p.lastIndex + 1
return idx, idx, nil
}
return fn, nil
}
2014-08-20 22:18:08 +00:00
func mustParse(t *testing.T, q string) *WatchPlan {
2014-08-21 18:38:44 +00:00
params := makeParams(t, q)
plan, err := Parse(params)
2014-08-20 20:45:34 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
2014-08-20 22:18:08 +00:00
return plan
}
func TestRun_Stop(t *testing.T) {
2014-08-21 18:38:44 +00:00
plan := mustParse(t, `{"type":"noop"}`)
2014-08-20 20:45:34 +00:00
var expect uint64 = 1
plan.Handler = func(idx uint64, val interface{}) {
if idx != expect {
t.Fatalf("Bad: %d %d", expect, idx)
}
if val != expect {
t.Fatalf("Bad: %d %d", expect, val)
}
expect++
}
time.AfterFunc(10*time.Millisecond, func() {
plan.Stop()
})
2014-08-20 22:18:08 +00:00
err := plan.Run("127.0.0.1:8500")
2014-08-20 20:45:34 +00:00
if err != nil {
t.Fatalf("err: %v", err)
}
if expect == 1 {
t.Fatalf("Bad: %d", expect)
}
}