Merge pull request #659 from vmarmol/remove-firstn

Remove unused FirstN() from StatsBuffer
This commit is contained in:
Rohit Jnagal 2015-04-20 19:32:31 -07:00
commit 229b39de2e
2 changed files with 12 additions and 32 deletions

View File

@ -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

View File

@ -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)