consul/watch/plan_test.go

75 lines
1.2 KiB
Go
Raw Normal View History

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