1 | 1 |
|
2 | 2 |
import (
|
3 | 3 |
"context"
|
|
4 |
"fmt"
|
4 | 5 |
"io"
|
5 | 6 |
"testing"
|
6 | 7 |
"time"
|
|
260 | 261 |
t.Error("failed, to receive call in time")
|
261 | 262 |
}
|
262 | 263 |
}
|
|
264 |
|
|
265 |
type indexTestClient struct {
|
|
266 |
client *testClient
|
|
267 |
index uint64
|
|
268 |
errs chan error
|
|
269 |
}
|
|
270 |
|
|
271 |
func newIndexTestClient(c *testClient, errs chan error) *indexTestClient {
|
|
272 |
return &indexTestClient{
|
|
273 |
client: c,
|
|
274 |
index: 0,
|
|
275 |
errs: errs,
|
|
276 |
}
|
|
277 |
}
|
|
278 |
|
|
279 |
func (i *indexTestClient) Register(r *consul.AgentServiceRegistration) error {
|
|
280 |
return i.client.Register(r)
|
|
281 |
}
|
|
282 |
|
|
283 |
func (i *indexTestClient) Deregister(r *consul.AgentServiceRegistration) error {
|
|
284 |
return i.client.Deregister(r)
|
|
285 |
}
|
|
286 |
|
|
287 |
func (i *indexTestClient) Service(service, tag string, passingOnly bool, queryOpts *consul.QueryOptions) ([]*consul.ServiceEntry, *consul.QueryMeta, error) {
|
|
288 |
|
|
289 |
// Assumes this is the first call Service, loop hasn't begun running yet
|
|
290 |
if i.index == 0 && queryOpts.WaitIndex == 0 {
|
|
291 |
i.index = 100
|
|
292 |
entries, meta, err := i.client.Service(service, tag, passingOnly, queryOpts)
|
|
293 |
meta.LastIndex = i.index
|
|
294 |
return entries, meta, err
|
|
295 |
}
|
|
296 |
|
|
297 |
if queryOpts.WaitIndex < i.index {
|
|
298 |
i.errs <- fmt.Errorf("wait index %d is less than or equal to previous value", queryOpts.WaitIndex)
|
|
299 |
}
|
|
300 |
|
|
301 |
entries, meta, err := i.client.Service(service, tag, passingOnly, queryOpts)
|
|
302 |
i.index++
|
|
303 |
meta.LastIndex = i.index
|
|
304 |
return entries, meta, err
|
|
305 |
}
|
|
306 |
|
|
307 |
func TestInstancerLoopIndex(t *testing.T) {
|
|
308 |
|
|
309 |
var (
|
|
310 |
errs = make(chan error, 1)
|
|
311 |
logger = log.NewNopLogger()
|
|
312 |
client = newIndexTestClient(newTestClient(consulState), errs)
|
|
313 |
)
|
|
314 |
|
|
315 |
go func() {
|
|
316 |
for err := range errs {
|
|
317 |
t.Error(err)
|
|
318 |
t.FailNow()
|
|
319 |
}
|
|
320 |
}()
|
|
321 |
|
|
322 |
instancer := NewInstancer(client, logger, "search", []string{"api"}, true)
|
|
323 |
defer instancer.Stop()
|
|
324 |
|
|
325 |
time.Sleep(2 * time.Second)
|
|
326 |
}
|