open-consul/watch/plan_test.go

75 lines
1.2 KiB
Go
Raw Normal View History

2014-08-20 20:45:34 +00:00
package watch
import (
"testing"
"time"
)
func init() {
watchFuncFactory["noop"] = noopWatch
}
2017-04-21 00:46:29 +00:00
func noopWatch(params map[string]interface{}) (WatcherFunc, error) {
fn := func(p *Plan) (uint64, interface{}, error) {
2014-08-20 20:45:34 +00:00
idx := p.lastIndex + 1
return idx, idx, nil
}
return fn, nil
}
2017-04-21 00:46:29 +00:00
func mustParse(t *testing.T, q string) *Plan {
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) {
2017-10-24 13:24:57 +00:00
t.Parallel()
2014-08-21 18:38:44 +00:00
plan := mustParse(t, `{"type":"noop"}`)
2017-04-17 05:14:55 +00:00
2014-08-20 20:45:34 +00:00
var expect uint64 = 1
2017-04-17 05:14:55 +00:00
doneCh := make(chan struct{})
2014-08-20 20:45:34 +00:00
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)
}
2017-04-17 05:14:55 +00:00
if expect == 1 {
close(doneCh)
}
2014-08-20 20:45:34 +00:00
expect++
}
2017-04-17 05:14:55 +00:00
errCh := make(chan error, 1)
go func() {
errCh <- plan.Run("127.0.0.1:8500")
}()
select {
case <-doneCh:
2014-08-20 20:45:34 +00:00
plan.Stop()
2017-04-17 05:14:55 +00:00
case <-time.After(1 * time.Second):
t.Fatalf("handler never ran")
}
select {
case err := <-errCh:
if err != nil {
t.Fatalf("err: %v", err)
}
case <-time.After(1 * time.Second):
t.Fatalf("watcher didn't exit")
2014-08-20 20:45:34 +00:00
}
if expect == 1 {
t.Fatalf("Bad: %d", expect)
}
}