From fddf4918c0b48a5a0e8916b9de108293f7069a42 Mon Sep 17 00:00:00 2001 From: Nick Triller Date: Wed, 28 Apr 2021 17:00:07 +0200 Subject: [PATCH] Send empty targetgroup if nothing discovered Signed-off-by: Nick Triller --- discovery/consul/consul.go | 9 +++++++++ discovery/consul/consul_test.go | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/discovery/consul/consul.go b/discovery/consul/consul.go index ee2734def..fa3d32303 100644 --- a/discovery/consul/consul.go +++ b/discovery/consul/consul.go @@ -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. diff --git a/discovery/consul/consul_test.go b/discovery/consul/consul_test.go index 0a6b73518..afc387c0b 100644 --- a/discovery/consul/consul_test.go +++ b/discovery/consul/consul_test.go @@ -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)