Skip to content

Commit

Permalink
Merge pull request prometheus#7385 from brancz/fix-flaky-kube-test
Browse files Browse the repository at this point in the history
discovery/kubernetes: Fix incorrect premature break of reading results
  • Loading branch information
brancz authored Jun 11, 2020
2 parents b71c00e + 7b1c0d6 commit 7c31fe1
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions discovery/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,36 +107,30 @@ func (d k8sDiscoveryTest) Run(t *testing.T) {
// readResultWithTimeout reads all targegroups from channel with timeout.
// It merges targegroups by source and sends the result to result channel.
func readResultWithTimeout(t *testing.T, ch <-chan []*targetgroup.Group, max int, timeout time.Duration, resChan chan<- map[string]*targetgroup.Group) {
allTgs := make([][]*targetgroup.Group, 0)

res := make(map[string]*targetgroup.Group)
Loop:
for {
select {
case tgs := <-ch:
allTgs = append(allTgs, tgs)
if len(allTgs) == max {
for _, tg := range tgs {
if tg == nil {
continue
}
res[tg.Source] = tg
}
if len(res) == max {
// Reached max target groups we may get, break fast.
break Loop
}
case <-time.After(timeout):
// Because we use queue, an object that is created then
// deleted or updated may be processed only once.
// So possibly we may skip events, timed out here.
t.Logf("timed out, got %d (max: %d) items, some events are skipped", len(allTgs), max)
t.Logf("timed out, got %d (max: %d) items, some events are skipped", len(res), max)
break Loop
}
}

// Merge by source and sent it to channel.
res := make(map[string]*targetgroup.Group)
for _, tgs := range allTgs {
for _, tg := range tgs {
if tg == nil {
continue
}
res[tg.Source] = tg
}
}
resChan <- res
}

Expand Down

0 comments on commit 7c31fe1

Please sign in to comment.