Skip to content

Commit

Permalink
Fix Consul SD loop exiting on EOF (#788)
Browse files Browse the repository at this point in the history
* switched io.EOF for quit to errStopped

* adding unit test for consul sd EOF
  • Loading branch information
kcajmagic authored and peterbourgon committed Nov 7, 2018
1 parent 81e1437 commit 33fad1e
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
9 changes: 6 additions & 3 deletions sd/consul/instancer.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package consul

import (
"errors"
"fmt"
"io"
"time"

consul "github.com/hashicorp/consul/api"
Expand All @@ -15,6 +15,9 @@ import (

const defaultIndex = 0

// errStopped notifies the loop to quit. aka stopped via quitc
var errStopped = errors.New("quit and closed consul instancer")

// Instancer yields instances for a service in Consul.
type Instancer struct {
cache *instance.Cache
Expand Down Expand Up @@ -66,7 +69,7 @@ func (s *Instancer) loop(lastIndex uint64) {
for {
instances, lastIndex, err = s.getInstances(lastIndex, s.quitc)
switch {
case err == io.EOF:
case err == errStopped:
return // stopped via quitc
case err != nil:
s.logger.Log("err", err)
Expand Down Expand Up @@ -125,7 +128,7 @@ func (s *Instancer) getInstances(lastIndex uint64, interruptc chan struct{}) ([]
case res := <-resc:
return res.instances, res.index, nil
case <-interruptc:
return nil, 0, io.EOF
return nil, 0, errStopped
}
}

Expand Down
75 changes: 73 additions & 2 deletions sd/consul/instancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package consul

import (
"context"
"testing"

consul "github.com/hashicorp/consul/api"
"io"
"testing"
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/sd"
Expand Down Expand Up @@ -131,3 +132,73 @@ func TestInstancerAddressOverride(t *testing.T) {
t.Errorf("want %q, have %q", want, have)
}
}

type eofTestClient struct {
client *testClient
eofSig chan bool
called chan struct{}
}

func neweofTestClient(client *testClient, sig chan bool, called chan struct{}) Client {
return &eofTestClient{client: client, eofSig: sig, called: called}
}

func (c *eofTestClient) Register(r *consul.AgentServiceRegistration) error {
return c.client.Register(r)
}

func (c *eofTestClient) Deregister(r *consul.AgentServiceRegistration) error {
return c.client.Deregister(r)
}

func (c *eofTestClient) Service(service, tag string, passingOnly bool, queryOpts *consul.QueryOptions) ([]*consul.ServiceEntry, *consul.QueryMeta, error) {
c.called <- struct{}{}
shouldEOF := <-c.eofSig
if shouldEOF {
return nil, &consul.QueryMeta{}, io.EOF
}
return c.client.Service(service, tag, passingOnly, queryOpts)
}

func TestInstancerWithEOF(t *testing.T) {
var (
sig = make(chan bool, 1)
called = make(chan struct{}, 1)
logger = log.NewNopLogger()
client = neweofTestClient(newTestClient(consulState), sig, called)
)

sig <- false
s := NewInstancer(client, logger, "search", []string{"api"}, true)
defer s.Stop()

select {
case <-called:
case <-time.Tick(time.Millisecond * 500):
t.Error("failed, to receive call")
}

state := s.cache.State()
if want, have := 2, len(state.Instances); want != have {
t.Errorf("want %d, have %d", want, have)
}

// some error occurred resulting in io.EOF
sig <- true

// Service Called Once
select {
case <-called:
case <-time.Tick(time.Millisecond * 500):
t.Error("failed, to receive call in time")
}

sig <- false

// loop should continue
select {
case <-called:
case <-time.Tick(time.Millisecond * 500):
t.Error("failed, to receive call in time")
}
}

0 comments on commit 33fad1e

Please sign in to comment.