201 | 201 |
t.Error("failed, to receive call in time")
|
202 | 202 |
}
|
203 | 203 |
}
|
|
204 |
|
|
205 |
type badIndexTestClient struct {
|
|
206 |
client *testClient
|
|
207 |
called chan struct{}
|
|
208 |
}
|
|
209 |
|
|
210 |
func newBadIndexTestClient(client *testClient, called chan struct{}) Client {
|
|
211 |
return &badIndexTestClient{client: client, called: called}
|
|
212 |
}
|
|
213 |
|
|
214 |
func (c *badIndexTestClient) Register(r *consul.AgentServiceRegistration) error {
|
|
215 |
return c.client.Register(r)
|
|
216 |
}
|
|
217 |
|
|
218 |
func (c *badIndexTestClient) Deregister(r *consul.AgentServiceRegistration) error {
|
|
219 |
return c.client.Deregister(r)
|
|
220 |
}
|
|
221 |
|
|
222 |
func (c *badIndexTestClient) Service(service, tag string, passingOnly bool, queryOpts *consul.QueryOptions) ([]*consul.ServiceEntry, *consul.QueryMeta, error) {
|
|
223 |
switch {
|
|
224 |
case queryOpts.WaitIndex == 0:
|
|
225 |
queryOpts.WaitIndex = 100
|
|
226 |
case queryOpts.WaitIndex == 100:
|
|
227 |
queryOpts.WaitIndex = 99
|
|
228 |
default:
|
|
229 |
}
|
|
230 |
c.called <- struct{}{}
|
|
231 |
return c.client.Service(service, tag, passingOnly, queryOpts)
|
|
232 |
}
|
|
233 |
|
|
234 |
func TestInstancerWithInvalidIndex(t *testing.T) {
|
|
235 |
var (
|
|
236 |
called = make(chan struct{}, 1)
|
|
237 |
logger = log.NewNopLogger()
|
|
238 |
client = newBadIndexTestClient(newTestClient(consulState), called)
|
|
239 |
)
|
|
240 |
|
|
241 |
s := NewInstancer(client, logger, "search", []string{"api"}, true)
|
|
242 |
defer s.Stop()
|
|
243 |
|
|
244 |
select {
|
|
245 |
case <-called:
|
|
246 |
case <-time.Tick(time.Millisecond * 500):
|
|
247 |
t.Error("failed, to receive call")
|
|
248 |
}
|
|
249 |
|
|
250 |
state := s.cache.State()
|
|
251 |
if want, have := 2, len(state.Instances); want != have {
|
|
252 |
t.Errorf("want %d, have %d", want, have)
|
|
253 |
}
|
|
254 |
|
|
255 |
// loop should continue
|
|
256 |
select {
|
|
257 |
case <-called:
|
|
258 |
case <-time.Tick(time.Millisecond * 500):
|
|
259 |
t.Error("failed, to receive call in time")
|
|
260 |
}
|
|
261 |
}
|