open-consul/watch/plan_test.go
Paul Banks 6f566f750e
Basic watch support for connect proxy config and certificate endpoints.
- Includes some bug fixes for previous `api` work and `agent` that weren't tested
 - Needed somewhat pervasive changes to support hash based blocking - some TODOs left in our watch toolchain that will explicitly fail on hash-based watches.
 - Integration into `connect` is partially done here but still WIP
2018-06-14 09:42:05 -07:00

83 lines
1.4 KiB
Go

package watch
import (
"testing"
"time"
)
func init() {
watchFuncFactory["noop"] = noopWatch
}
func noopWatch(params map[string]interface{}) (WatcherFunc, error) {
fn := func(p *Plan) (BlockingParam, interface{}, error) {
idx := WaitIndexVal(0)
if i, ok := p.lastParamVal.(WaitIndexVal); ok {
idx = i
}
return idx + 1, uint64(idx + 1), nil
}
return fn, nil
}
func mustParse(t *testing.T, q string) *Plan {
params := makeParams(t, q)
plan, err := Parse(params)
if err != nil {
t.Fatalf("err: %v", err)
}
return plan
}
func TestRun_Stop(t *testing.T) {
t.Parallel()
plan := mustParse(t, `{"type":"noop"}`)
var expect uint64 = 1
doneCh := make(chan struct{})
plan.Handler = func(blockParamVal BlockingParam, val interface{}) {
idxVal, ok := blockParamVal.(WaitIndexVal)
if !ok {
t.Fatalf("Expected index-based watch")
}
idx := uint64(idxVal)
if idx != expect {
t.Fatalf("Bad: %d %d", expect, idx)
}
if val != expect {
t.Fatalf("Bad: %d %d", expect, val)
}
if expect == 1 {
close(doneCh)
}
expect++
}
errCh := make(chan error, 1)
go func() {
errCh <- plan.Run("127.0.0.1:8500")
}()
select {
case <-doneCh:
plan.Stop()
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")
}
if expect == 1 {
t.Fatalf("Bad: %d", expect)
}
}