Skip to content

Commit

Permalink
watch: node watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
armon committed Aug 20, 2014
1 parent 00358ba commit 8d70128
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
16 changes: 15 additions & 1 deletion watch/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func init() {
"key": keyWatch,
"keyprefix": keyPrefixWatch,
"services": servicesWatch,
"nodes": nil,
"nodes": nodesWatch,
"service": nil,
"checks": nil,
}
Expand Down Expand Up @@ -82,3 +82,17 @@ func servicesWatch(params map[string][]string) (WatchFunc, error) {
}
return fn, nil
}

// nodesWatch is used to watch the list of available nodes
func nodesWatch(params map[string][]string) (WatchFunc, error) {
fn := func(p *WatchPlan) (uint64, interface{}, error) {
catalog := p.client.Catalog()
opts := consulapi.QueryOptions{WaitIndex: p.lastIndex}
nodes, meta, err := catalog.Nodes(&opts)
if err != nil {
return 0, nil, err
}
return meta.LastIndex, nodes, err
}
return fn, nil
}
49 changes: 49 additions & 0 deletions watch/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,52 @@ func TestServicesWatch(t *testing.T) {
t.Fatalf("bad: %v", invoke)
}
}

func TestNodesWatch(t *testing.T) {
if consulAddr == "" {
t.Skip()
}
plan := mustParse(t, "type:nodes")
invoke := 0
plan.Handler = func(idx uint64, raw interface{}) {
if invoke == 0 {
if raw == nil {
return
}
v, ok := raw.([]*consulapi.Node)
if !ok || len(v) == 0 {
t.Fatalf("Bad: %#v", raw)
}
invoke++
}
}

go func() {
time.Sleep(20 * time.Millisecond)
plan.Stop()

catalog := plan.client.Catalog()
reg := &consulapi.CatalogRegistration{
Node: "foobar",
Address: "1.1.1.1",
Datacenter: "dc1",
}
catalog.Register(reg, nil)
time.Sleep(20 * time.Millisecond)
dereg := &consulapi.CatalogDeregistration{
Node: "foobar",
Address: "1.1.1.1",
Datacenter: "dc1",
}
catalog.Deregister(dereg, nil)
}()

err := plan.Run(consulAddr)
if err != nil {
t.Fatalf("err: %v", err)
}

if invoke == 0 {
t.Fatalf("bad: %v", invoke)
}
}

0 comments on commit 8d70128

Please sign in to comment.