1 | 1 |
|
2 | 2 |
import (
|
3 | 3 |
"context"
|
|
4 |
consul "github.com/hashicorp/consul/api"
|
|
5 |
"io"
|
4 | 6 |
"testing"
|
5 | |
|
6 | |
consul "github.com/hashicorp/consul/api"
|
|
7 |
"time"
|
7 | 8 |
|
8 | 9 |
"github.com/go-kit/kit/log"
|
9 | 10 |
"github.com/go-kit/kit/sd"
|
|
130 | 131 |
t.Errorf("want %q, have %q", want, have)
|
131 | 132 |
}
|
132 | 133 |
}
|
|
134 |
|
|
135 |
type eofTestClient struct {
|
|
136 |
client *testClient
|
|
137 |
eofSig chan bool
|
|
138 |
called chan struct{}
|
|
139 |
}
|
|
140 |
|
|
141 |
func neweofTestClient(client *testClient, sig chan bool, called chan struct{}) Client {
|
|
142 |
return &eofTestClient{client: client, eofSig: sig, called: called}
|
|
143 |
}
|
|
144 |
|
|
145 |
func (c *eofTestClient) Register(r *consul.AgentServiceRegistration) error {
|
|
146 |
return c.client.Register(r)
|
|
147 |
}
|
|
148 |
|
|
149 |
func (c *eofTestClient) Deregister(r *consul.AgentServiceRegistration) error {
|
|
150 |
return c.client.Deregister(r)
|
|
151 |
}
|
|
152 |
|
|
153 |
func (c *eofTestClient) Service(service, tag string, passingOnly bool, queryOpts *consul.QueryOptions) ([]*consul.ServiceEntry, *consul.QueryMeta, error) {
|
|
154 |
c.called <- struct{}{}
|
|
155 |
shouldEOF := <-c.eofSig
|
|
156 |
if shouldEOF {
|
|
157 |
return nil, &consul.QueryMeta{}, io.EOF
|
|
158 |
}
|
|
159 |
return c.client.Service(service, tag, passingOnly, queryOpts)
|
|
160 |
}
|
|
161 |
|
|
162 |
func TestInstancerWithEOF(t *testing.T) {
|
|
163 |
var (
|
|
164 |
sig = make(chan bool, 1)
|
|
165 |
called = make(chan struct{}, 1)
|
|
166 |
logger = log.NewNopLogger()
|
|
167 |
client = neweofTestClient(newTestClient(consulState), sig, called)
|
|
168 |
)
|
|
169 |
|
|
170 |
sig <- false
|
|
171 |
s := NewInstancer(client, logger, "search", []string{"api"}, true)
|
|
172 |
defer s.Stop()
|
|
173 |
|
|
174 |
select {
|
|
175 |
case <-called:
|
|
176 |
case <-time.Tick(time.Millisecond * 500):
|
|
177 |
t.Error("failed, to receive call")
|
|
178 |
}
|
|
179 |
|
|
180 |
state := s.cache.State()
|
|
181 |
if want, have := 2, len(state.Instances); want != have {
|
|
182 |
t.Errorf("want %d, have %d", want, have)
|
|
183 |
}
|
|
184 |
|
|
185 |
// some error occurred resulting in io.EOF
|
|
186 |
sig <- true
|
|
187 |
|
|
188 |
// Service Called Once
|
|
189 |
select {
|
|
190 |
case <-called:
|
|
191 |
case <-time.Tick(time.Millisecond * 500):
|
|
192 |
t.Error("failed, to receive call in time")
|
|
193 |
}
|
|
194 |
|
|
195 |
sig <- false
|
|
196 |
|
|
197 |
// loop should continue
|
|
198 |
select {
|
|
199 |
case <-called:
|
|
200 |
case <-time.Tick(time.Millisecond * 500):
|
|
201 |
t.Error("failed, to receive call in time")
|
|
202 |
}
|
|
203 |
}
|