From a4609fcec7ffb3213a617bf4d0510542efb3deab Mon Sep 17 00:00:00 2001 From: Victor Marmol Date: Mon, 20 Apr 2015 19:17:31 -0700 Subject: [PATCH] Remove unused FirstN() from StatsBuffer --- storage/memory/stats_buffer.go | 24 ------------------------ storage/memory/stats_buffer_test.go | 20 ++++++++++++-------- 2 files changed, 12 insertions(+), 32 deletions(-) diff --git a/storage/memory/stats_buffer.go b/storage/memory/stats_buffer.go index b71bfe2a..9089e30c 100644 --- a/storage/memory/stats_buffer.go +++ b/storage/memory/stats_buffer.go @@ -113,30 +113,6 @@ func (self *StatsBuffer) InTimeRange(start, end time.Time, maxResults int) []*in return result } -// TODO(vmarmol): Remove this function as it will no longer be neededt. -// Returns the first N elements in the buffer. If N > size of buffer, size of buffer elements are returned. -// Returns the elements in ascending timestamp order. -func (self *StatsBuffer) FirstN(n int) []*info.ContainerStats { - // Cap n at the number of elements we have. - if n > self.size { - n = self.size - } - - // index points to the latest element, get n before that one (keeping in mind we may have gone through 0). - start := self.index - (n - 1) - if start < 0 { - start += len(self.buffer) - } - - // Copy the elements. - res := make([]*info.ContainerStats, n) - for i := 0; i < n; i++ { - index := (start + i) % len(self.buffer) - res[i] = self.buffer[index] - } - return res -} - // Gets the element at the specified index. Note that elements are stored in LIFO order. func (self *StatsBuffer) Get(index int) *info.ContainerStats { calculatedIndex := self.index - index diff --git a/storage/memory/stats_buffer_test.go b/storage/memory/stats_buffer_test.go index f142e931..b466540d 100644 --- a/storage/memory/stats_buffer_test.go +++ b/storage/memory/stats_buffer_test.go @@ -44,8 +44,13 @@ func expectSize(t *testing.T, sb *StatsBuffer, expectedSize int) { } } -func expectFirstN(t *testing.T, sb *StatsBuffer, expected []int32) { - expectElements(t, sb.FirstN(sb.Size()), expected) +func expectAllElements(t *testing.T, sb *StatsBuffer, expected []int32) { + size := sb.Size() + els := make([]*info.ContainerStats, size) + for i := 0; i < size; i++ { + els[i] = sb.Get(size - i - 1) + } + expectElements(t, els, expected) } func expectElements(t *testing.T, actual []*info.ContainerStats, expected []int32) { @@ -71,13 +76,13 @@ func expectElement(t *testing.T, stat *info.ContainerStats, expected int32) { } } -func TestAddAndFirstN(t *testing.T) { +func TestAdd(t *testing.T) { sb := NewStatsBuffer(5) // Add 1. sb.Add(createStats(1)) expectSize(t, sb, 1) - expectFirstN(t, sb, []int32{1}) + expectAllElements(t, sb, []int32{1}) // Fill the buffer. for i := 1; i <= 5; i++ { @@ -85,19 +90,19 @@ func TestAddAndFirstN(t *testing.T) { sb.Add(createStats(int32(i))) } expectSize(t, sb, 5) - expectFirstN(t, sb, []int32{1, 2, 3, 4, 5}) + expectAllElements(t, sb, []int32{1, 2, 3, 4, 5}) // Add more than is available in the buffer sb.Add(createStats(6)) expectSize(t, sb, 5) - expectFirstN(t, sb, []int32{2, 3, 4, 5, 6}) + expectAllElements(t, sb, []int32{2, 3, 4, 5, 6}) // Replace all elements. for i := 7; i <= 10; i++ { sb.Add(createStats(int32(i))) } expectSize(t, sb, 5) - expectFirstN(t, sb, []int32{6, 7, 8, 9, 10}) + expectAllElements(t, sb, []int32{6, 7, 8, 9, 10}) } func TestGet(t *testing.T) { @@ -106,7 +111,6 @@ func TestGet(t *testing.T) { sb.Add(createStats(2)) sb.Add(createStats(3)) expectSize(t, sb, 3) - expectFirstN(t, sb, []int32{1, 2, 3}) expectElement(t, sb.Get(0), 3) expectElement(t, sb.Get(1), 2)