Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(kuma-cp): fix wait for goroutine to be done (backport #5638) #5646

Merged
28 changes: 16 additions & 12 deletions pkg/core/resources/manager/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package manager_test
import (
"context"
"sync"
"sync/atomic"
"time"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -22,12 +23,12 @@ import (

type countingResourcesManager struct {
store core_store.ResourceStore
getQueries int
listQueries int
getQueries uint32
listQueries uint32
}

func (c *countingResourcesManager) Get(ctx context.Context, res core_model.Resource, fn ...core_store.GetOptionsFunc) error {
c.getQueries++
atomic.AddUint32(&c.getQueries, 1)
return c.store.Get(ctx, res, fn...)
}

Expand All @@ -36,7 +37,7 @@ func (c *countingResourcesManager) List(ctx context.Context, list core_model.Res
if list.GetItemType() == core_mesh.TrafficLogType && opts.Mesh == "slow" {
time.Sleep(10 * time.Second)
}
c.listQueries++
atomic.AddUint32(&c.listQueries, 1)
return c.store.List(ctx, list, fn...)
}

Expand Down Expand Up @@ -102,14 +103,14 @@ var _ = Describe("Cached Resource Manager", func() {

// then real manager should be called only once
Expect(fetch().Spec).To(MatchProto(res.Spec))
Expect(countingManager.getQueries).To(Equal(1))
Expect(int(countingManager.getQueries)).To(Equal(1))

// when
time.Sleep(expiration)

// then
Expect(fetch().Spec).To(MatchProto(res.Spec))
Expect(countingManager.getQueries).To(Equal(2))
Expect(int(countingManager.getQueries)).To(Equal(2))

// and metrics are published
Expect(test_metrics.FindMetric(metrics, "store_cache", "operation", "get", "result", "miss").Counter.GetValue()).To(Equal(2.0))
Expand Down Expand Up @@ -139,7 +140,7 @@ var _ = Describe("Cached Resource Manager", func() {
wg.Wait()

// then real manager should be called every time
Expect(countingManager.getQueries).To(Equal(100))
Expect(int(countingManager.getQueries)).To(Equal(100))
})

It("should cache List() queries", func() {
Expand All @@ -165,7 +166,7 @@ var _ = Describe("Cached Resource Manager", func() {
list := fetch()
Expect(list.Items).To(HaveLen(1))
Expect(list.Items[0].GetSpec()).To(MatchProto(res.Spec))
Expect(countingManager.listQueries).To(Equal(1))
Expect(int(countingManager.listQueries)).To(Equal(1))

// when
time.Sleep(expiration)
Expand All @@ -174,7 +175,7 @@ var _ = Describe("Cached Resource Manager", func() {
list = fetch()
Expect(list.Items).To(HaveLen(1))
Expect(list.Items[0].GetSpec()).To(MatchProto(res.Spec))
Expect(countingManager.listQueries).To(Equal(2))
Expect(int(countingManager.listQueries)).To(Equal(2))

// and metrics are published
Expect(test_metrics.FindMetric(metrics, "store_cache", "operation", "list", "result", "miss").Counter.GetValue()).To(Equal(2.0))
Expand All @@ -187,12 +188,14 @@ var _ = Describe("Cached Resource Manager", func() {
Expect(hits + hitWaits).To(Equal(100.0))
})

It("should let concurrent List() queries for different types and meshes", test.Within(5*time.Second, func() {
It("should let concurrent List() queries for different types and meshes", test.Within(15*time.Second, func() {
// given ongoing TrafficLog from mesh slow that takes a lot of time to complete
done := make(chan struct{})
go func() {
fetched := core_mesh.TrafficLogResourceList{}
err := cachedManager.List(context.Background(), &fetched, core_store.ListByMesh("slow"))
Expect(err).ToNot(HaveOccurred())
close(done)
}()

// when trying to fetch TrafficLog from different mesh that takes normal time to response
Expand All @@ -208,6 +211,7 @@ var _ = Describe("Cached Resource Manager", func() {

// then first request does not block request for other type
Expect(err).ToNot(HaveOccurred())
<-done
}))

It("should cache List() at different key when ordered", test.Within(5*time.Second, func() {
Expand Down Expand Up @@ -238,15 +242,15 @@ var _ = Describe("Cached Resource Manager", func() {
list := fetch(false)
Expect(list.Items).To(HaveLen(1))
Expect(list.Items[0].GetSpec()).To(MatchProto(res.Spec))
Expect(countingManager.listQueries).To(Equal(1))
Expect(int(countingManager.listQueries)).To(Equal(1))

// when call for ordered data
list = fetch(true)

// then real manager should be called
Expect(list.Items).To(HaveLen(1))
Expect(list.Items[0].GetSpec()).To(MatchProto(res.Spec))
Expect(countingManager.listQueries).To(Equal(2))
Expect(int(countingManager.listQueries)).To(Equal(2))

// and metrics are published
Expect(test_metrics.FindMetric(metrics, "store_cache", "operation", "list", "result", "miss").Counter.GetValue()).To(Equal(2.0))
Expand Down