Skip to content

Commit 35422d5

Browse files
committed
FFM-11660 Test for safe maps
1 parent 4f4a14a commit 35422d5

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

analyticsservice/analytics.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ type AnalyticsService struct {
6565
analyticsChan chan analyticsEvent
6666
evaluationAnalytics SafeAnalyticsCache[string, analyticsEvent]
6767
targetAnalytics SafeAnalyticsCache[string, evaluation.Target]
68-
seenTargets SafeAnalyticsCache[string, bool]
68+
seenTargets SafeSeenTargetsCache[string, bool]
6969
logEvaluationLimitReached atomic.Bool
7070
logTargetLimitReached atomic.Bool
7171
timeout time.Duration
@@ -162,8 +162,15 @@ func (as *AnalyticsService) listener() {
162162
continue
163163
}
164164

165+
// Check if seen targets limit has been hit
166+
limitExceeded := as.seenTargets.isLimitExceeded()
167+
168+
if limitExceeded {
169+
continue
170+
}
171+
165172
// Update seen targets
166-
as.seenTargets.set(ad.target.Identifier, true)
173+
as.seenTargets.setWithLimit(ad.target.Identifier, true)
167174

168175
// Update target metrics
169176
if as.targetAnalytics.size() < maxTargetEntries {

analyticsservice/safe_maps_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"reflect"
66
"sync"
77
"testing"
8+
"time"
89

910
"github.com/harness/ff-golang-server-sdk/evaluation"
1011
)
@@ -172,4 +173,32 @@ func TestSafeSeenTargets(t *testing.T) {
172173
t.Errorf("Map size should be 0 after clearing, got %d", s.size())
173174
}
174175
})
176+
177+
// Add test for clearing based on interval
178+
t.Run("IntervalClearingTest", func(t *testing.T) {
179+
// Re-initialize the map with a clearing interval
180+
s = newSafeSeenTargets(10, 100*time.Millisecond)
181+
182+
for key, value := range testData {
183+
s.set(key, value)
184+
}
185+
186+
// Ensure the map has items initially
187+
if s.size() != len(testData) {
188+
t.Errorf("Expected map size to be %d, got %d", len(testData), s.size())
189+
}
190+
191+
// Wait for the clearing to clear the map
192+
time.Sleep(300 * time.Millisecond)
193+
194+
// Ensure the map is cleared after the interval
195+
if s.size() != 0 {
196+
t.Errorf("Expected map size to be 0 after clearing interval, got %d", s.size())
197+
}
198+
199+
// Ensure the limitExceeded flag is reset
200+
if s.isLimitExceeded() {
201+
t.Errorf("Expected limitExceeded to be reset after clearing")
202+
}
203+
})
175204
}

0 commit comments

Comments
 (0)