Send empty targetgroup if nothing discovered

Signed-off-by: Nick Triller <nicktriller@gmail.com>
This commit is contained in:
Nick Triller 2021-04-28 17:00:07 +02:00
parent f3b2d2a998
commit fddf4918c0
No known key found for this signature in database
GPG key ID: FE4DB87C22F6A4E3
2 changed files with 26 additions and 0 deletions

View file

@ -426,6 +426,15 @@ func (d *Discovery) watchServices(ctx context.Context, ch chan<- []*targetgroup.
}
}
}
// Send targetgroup with no targets if nothing was discovered.
if len(services) == 0 {
select {
case <-ctx.Done():
return
case ch <- []*targetgroup.Group{{}}:
}
}
}
// consulService contains data belonging to the same service.

View file

@ -298,6 +298,23 @@ func TestAllServices(t *testing.T) {
<-ch
}
// targetgroup with no targets is emitted if no services were discovered.
func TestNoTargets(t *testing.T) {
stub, config := newServer(t)
defer stub.Close()
config.ServiceTags = []string{"missing"}
d := newDiscovery(t, config)
ctx, cancel := context.WithCancel(context.Background())
ch := make(chan []*targetgroup.Group)
go d.Run(ctx, ch)
targets := (<-ch)[0].Targets
require.Equal(t, 0, len(targets))
cancel()
}
// Watch only the test service.
func TestOneService(t *testing.T) {
stub, config := newServer(t)