Skip to content

Commit

Permalink
Add collector cache test
Browse files Browse the repository at this point in the history
Signed-off-by: Ananya Kumar Mallik <[email protected]>
  • Loading branch information
ananya-mallik-ps committed Dec 3, 2024
1 parent bc1fa87 commit d9499df
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions collectors/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,56 @@ func TestDescriptorCache(t *testing.T) {
t.Error("cache entries should have expired")
}
}

func TestCollectorCache(t *testing.T) {
createCollector := func(id string) *MonitoringCollector {
return &MonitoringCollector{
projectID: id,
}
}

t.Run("basic cache Op", func(t *testing.T) {
ttl := 1 * time.Second
cache := NewCollectorCache(ttl)
collector := createCollector("test-project")
key := "test-key"

cache.Store(key, collector)

if _, found := cache.Get("test-key"); !found {
t.Error("Collector should be available in cache before TTL")
}

time.Sleep(2 * ttl)
if _, found := cache.Get("test-key"); found {
t.Error("Collector should have expired")
}
})

t.Run("multiple collectors", func(t *testing.T) {
ttl := 1 * time.Second
cache := NewCollectorCache(ttl)

collectors := map[string]*MonitoringCollector{
"test-key-1": createCollector("test-project-1"),
"test-key-2": createCollector("test-project-2"),
"test-key-3": createCollector("test-project-3"),
}

for k, v := range collectors {
cache.Store(k, v)
}

for k, original := range collectors {
cached, found := cache.Get(k)
if !found {
t.Errorf("Collector %s not found in cache", k)
continue
}

if cached.projectID != original.projectID {
t.Errorf("Wrong collector for key %s. Got projectId %s, want %s", k, cached.projectID, original.projectID)
}
}
})
}

0 comments on commit d9499df

Please sign in to comment.