|
|
|
|
@ -136,7 +136,67 @@ func TestUsageStore_Update(t *testing.T) { |
|
|
|
|
// This test asserts that we update the correct rate buckets, and as rate
|
|
|
|
|
// buckets are implemented as a circular list, when we reach the end of
|
|
|
|
|
// list the next bucket is the start of the list.
|
|
|
|
|
func TestUsageStore_UpdateRateBuckets(t *testing.T) { |
|
|
|
|
// func TestUsageStore_UpdateRateBuckets(t *testing.T) {
|
|
|
|
|
// s, err := newUsageStore(15*time.Minute, 5*time.Minute, time.Minute, 1, prometheus.NewRegistry())
|
|
|
|
|
// require.NoError(t, err)
|
|
|
|
|
// clock := quartz.NewMock(t)
|
|
|
|
|
// s.clock = clock
|
|
|
|
|
// metadata := &proto.StreamMetadata{
|
|
|
|
|
// StreamHash: 0x1,
|
|
|
|
|
// TotalSize: 100,
|
|
|
|
|
// }
|
|
|
|
|
// // Metadata at clock.Now() should update the first rate bucket because
|
|
|
|
|
// // the mocked clock starts at 2024-01-01T00:00:00Z.
|
|
|
|
|
// time1 := clock.Now()
|
|
|
|
|
// require.NoError(t, s.Update("tenant", metadata, time1))
|
|
|
|
|
// stream, ok := s.getForTests("tenant", 0x1)
|
|
|
|
|
// require.True(t, ok)
|
|
|
|
|
// expected := newRateBuckets(5*time.Minute, time.Minute)
|
|
|
|
|
// expected[0].timestamp = time1.UnixNano()
|
|
|
|
|
// expected[0].size = 100
|
|
|
|
|
// require.Equal(t, expected, stream.rateBuckets)
|
|
|
|
|
// // Update the first bucket with the same metadata but 1 second later.
|
|
|
|
|
// clock.Advance(time.Second)
|
|
|
|
|
// time2 := clock.Now()
|
|
|
|
|
// require.NoError(t, s.Update("tenant", metadata, time2))
|
|
|
|
|
// expected[0].size = 200
|
|
|
|
|
// require.Equal(t, expected, stream.rateBuckets)
|
|
|
|
|
// // Advance the clock forward to the next bucket. Should update the second
|
|
|
|
|
// // bucket and leave the first bucket unmodified.
|
|
|
|
|
// clock.Advance(time.Minute)
|
|
|
|
|
// time3 := clock.Now()
|
|
|
|
|
// require.NoError(t, s.Update("tenant", metadata, time3))
|
|
|
|
|
// stream, ok = s.getForTests("tenant", 0x1)
|
|
|
|
|
// require.True(t, ok)
|
|
|
|
|
// // As the clock is now 1 second ahead of the bucket start time, we must
|
|
|
|
|
// // truncate the expected time to the start of the bucket.
|
|
|
|
|
// expected[1].timestamp = time3.Truncate(time.Minute).UnixNano()
|
|
|
|
|
// expected[1].size = 100
|
|
|
|
|
// require.Equal(t, expected, stream.rateBuckets)
|
|
|
|
|
// // Advance the clock to the last bucket.
|
|
|
|
|
// clock.Advance(3 * time.Minute)
|
|
|
|
|
// time4 := clock.Now()
|
|
|
|
|
// require.NoError(t, s.Update("tenant", metadata, time4))
|
|
|
|
|
// stream, ok = s.getForTests("tenant", 0x1)
|
|
|
|
|
// require.True(t, ok)
|
|
|
|
|
// expected[4].timestamp = time4.Truncate(time.Minute).UnixNano()
|
|
|
|
|
// expected[4].size = 100
|
|
|
|
|
// require.Equal(t, expected, stream.rateBuckets)
|
|
|
|
|
// // Advance the clock one last one. It should wrap around to the start of
|
|
|
|
|
// // the list and replace the original bucket with time1.
|
|
|
|
|
// clock.Advance(time.Minute)
|
|
|
|
|
// time5 := clock.Now()
|
|
|
|
|
// require.NoError(t, s.Update("tenant", metadata, time5))
|
|
|
|
|
// stream, ok = s.getForTests("tenant", 0x1)
|
|
|
|
|
// require.True(t, ok)
|
|
|
|
|
// expected[0].timestamp = time5.Truncate(time.Minute).UnixNano()
|
|
|
|
|
// expected[0].size = 100
|
|
|
|
|
// require.Equal(t, expected, stream.rateBuckets)
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// This test asserts that rate buckets are not updated while the TODOs are
|
|
|
|
|
// in place.
|
|
|
|
|
func TestUsageStore_RateBucketsAreNotUsed(t *testing.T) { |
|
|
|
|
s, err := newUsageStore(15*time.Minute, 5*time.Minute, time.Minute, 1, prometheus.NewRegistry()) |
|
|
|
|
require.NoError(t, err) |
|
|
|
|
clock := quartz.NewMock(t) |
|
|
|
|
@ -145,53 +205,12 @@ func TestUsageStore_UpdateRateBuckets(t *testing.T) { |
|
|
|
|
StreamHash: 0x1, |
|
|
|
|
TotalSize: 100, |
|
|
|
|
} |
|
|
|
|
// Metadata at clock.Now() should update the first rate bucket because
|
|
|
|
|
// the mocked clock starts at 2024-01-01T00:00:00Z.
|
|
|
|
|
time1 := clock.Now() |
|
|
|
|
require.NoError(t, s.Update("tenant", metadata, time1)) |
|
|
|
|
require.NoError(t, s.Update("tenant", metadata, clock.Now())) |
|
|
|
|
stream, ok := s.getForTests("tenant", 0x1) |
|
|
|
|
require.True(t, ok) |
|
|
|
|
expected := newRateBuckets(5*time.Minute, time.Minute) |
|
|
|
|
expected[0].timestamp = time1.UnixNano() |
|
|
|
|
expected[0].size = 100 |
|
|
|
|
require.Equal(t, expected, stream.rateBuckets) |
|
|
|
|
// Update the first bucket with the same metadata but 1 second later.
|
|
|
|
|
clock.Advance(time.Second) |
|
|
|
|
time2 := clock.Now() |
|
|
|
|
require.NoError(t, s.Update("tenant", metadata, time2)) |
|
|
|
|
expected[0].size = 200 |
|
|
|
|
require.Equal(t, expected, stream.rateBuckets) |
|
|
|
|
// Advance the clock forward to the next bucket. Should update the second
|
|
|
|
|
// bucket and leave the first bucket unmodified.
|
|
|
|
|
clock.Advance(time.Minute) |
|
|
|
|
time3 := clock.Now() |
|
|
|
|
require.NoError(t, s.Update("tenant", metadata, time3)) |
|
|
|
|
stream, ok = s.getForTests("tenant", 0x1) |
|
|
|
|
require.True(t, ok) |
|
|
|
|
// As the clock is now 1 second ahead of the bucket start time, we must
|
|
|
|
|
// truncate the expected time to the start of the bucket.
|
|
|
|
|
expected[1].timestamp = time3.Truncate(time.Minute).UnixNano() |
|
|
|
|
expected[1].size = 100 |
|
|
|
|
require.Equal(t, expected, stream.rateBuckets) |
|
|
|
|
// Advance the clock to the last bucket.
|
|
|
|
|
clock.Advance(3 * time.Minute) |
|
|
|
|
time4 := clock.Now() |
|
|
|
|
require.NoError(t, s.Update("tenant", metadata, time4)) |
|
|
|
|
stream, ok = s.getForTests("tenant", 0x1) |
|
|
|
|
require.True(t, ok) |
|
|
|
|
expected[4].timestamp = time4.Truncate(time.Minute).UnixNano() |
|
|
|
|
expected[4].size = 100 |
|
|
|
|
require.Equal(t, expected, stream.rateBuckets) |
|
|
|
|
// Advance the clock one last one. It should wrap around to the start of
|
|
|
|
|
// the list and replace the original bucket with time1.
|
|
|
|
|
clock.Advance(time.Minute) |
|
|
|
|
time5 := clock.Now() |
|
|
|
|
require.NoError(t, s.Update("tenant", metadata, time5)) |
|
|
|
|
stream, ok = s.getForTests("tenant", 0x1) |
|
|
|
|
require.True(t, ok) |
|
|
|
|
expected[0].timestamp = time5.Truncate(time.Minute).UnixNano() |
|
|
|
|
expected[0].size = 100 |
|
|
|
|
require.Equal(t, expected, stream.rateBuckets) |
|
|
|
|
|
|
|
|
|
require.Equal(t, uint64(0), stream.totalSize) |
|
|
|
|
require.Nil(t, stream.rateBuckets) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestUsageStore_UpdateCond(t *testing.T) { |
|
|
|
|
|