consul/api/watch/plan_test.go

154 lines
2.7 KiB
Go
Raw Permalink Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
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) (BlockingParamVal, interface{}, error) {
idx := WaitIndexVal(0)
if i, ok := p.lastParamVal.(WaitIndexVal); ok {
idx = i
}
return idx + 1, uint64(idx + 1), nil
2014-08-20 20:45:34 +00:00
}
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{})
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)
}
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)
}
}
func TestRun_Stop_Hybrid(t *testing.T) {
t.Parallel()
plan := mustParse(t, `{"type":"noop"}`)
var expect uint64 = 1
doneCh := make(chan struct{})
plan.HybridHandler = func(blockParamVal BlockingParamVal, val interface{}) {
idxVal, ok := blockParamVal.(WaitIndexVal)
if !ok {
t.Fatalf("expected index-based watch")
}
idx := uint64(idxVal)
2014-08-20 20:45:34 +00:00
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)
}
}
func TestRunWithClientAndLogger_NilLogger(t *testing.T) {
t.Parallel()
plan := mustParse(t, `{"type":"noop"}`)
errCh := make(chan error, 1)
go func() {
errCh <- plan.RunWithClientAndHclog(nil, nil)
}()
plan.Stop()
select {
case err := <-errCh:
if err != nil {
t.Fatalf("err: %v", err)
}
case <-time.After(1 * time.Second):
t.Fatalf("watcher didn't exit")
}
}