Skip to content

Commit

Permalink
[chore] [internal/aws] Change TestMapWithExpiry to explicitly test cl…
Browse files Browse the repository at this point in the history
…eanup (#25097)

**Description:** Changes the behavior of the specific test case to be
more aligned with its behavior pre #25066

**Link to tracking Issue:** #25095
  • Loading branch information
bryan-aguilar authored Aug 8, 2023
1 parent 21f9b03 commit df0c744
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions internal/aws/metrics/metric_calculator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,16 @@ func TestMapWithExpiryAdd(t *testing.T) {
}

func TestMapWithExpiryCleanup(t *testing.T) {
store := NewMapWithExpiry(time.Second)
// This test is meant to explicitly test the CleanUp method. We do not need to use NewMapWithExpiry().
// Instead, manually create a Map Object, sleep, and then call cleanup to ensure that entries are erased.
// The sweep method is tested in a later unit test.
// Explicitly testing CleanUp allows us to avoid test race conditions when the sweep ticker may not fire within
// the allotted sleep time.
store := &MapWithExpiry{
ttl: time.Millisecond,
entries: make(map[interface{}]*MetricValue),
lock: &sync.Mutex{},
}
value1 := rand.Float64()
store.Lock()
store.Set(Key{MetricMetadata: "key1"}, MetricValue{RawValue: value1, Timestamp: time.Now()})
Expand All @@ -130,14 +139,14 @@ func TestMapWithExpiryCleanup(t *testing.T) {
assert.Equal(t, 1, store.Size())
store.Unlock()

time.Sleep(time.Second + time.Millisecond)
time.Sleep(time.Millisecond * 2)
store.CleanUp(time.Now())
store.Lock()
val, ok = store.Get(Key{MetricMetadata: "key1"})
assert.Equal(t, false, ok)
assert.True(t, val == nil)
assert.Equal(t, 0, store.Size())
store.Unlock()
require.NoError(t, store.Shutdown())
}

func TestMapWithExpiryConcurrency(t *testing.T) {
Expand Down

0 comments on commit df0c744

Please sign in to comment.