From 7322c46b8ebda444f6925e29e6c7720d4e35b022 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Fri, 30 Dec 2016 10:45:56 +0100 Subject: [PATCH] storage: add mock iterator for test --- storage/buffer_test.go | 58 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/storage/buffer_test.go b/storage/buffer_test.go index e4e82cb10..a719b60ed 100644 --- a/storage/buffer_test.go +++ b/storage/buffer_test.go @@ -2,8 +2,10 @@ package storage import ( "math/rand" + "sort" "testing" + "github.com/fabxc/tsdb/labels" "github.com/stretchr/testify/require" ) @@ -121,3 +123,59 @@ func TestBufferedSeriesIterator(t *testing.T) { require.False(t, it.Next(), "next succeeded unexpectedly") } + +type mockSeriesIterator struct { + seek func(int64) bool + values func() (int64, float64) + next func() bool + err func() error +} + +func (m *mockSeriesIterator) Seek(t int64) bool { return m.seek(t) } +func (m *mockSeriesIterator) Values() (int64, float64) { return m.values() } +func (m *mockSeriesIterator) Next() bool { return m.next() } +func (m *mockSeriesIterator) Err() error { return m.err() } + +type mockSeries struct { + labels func() labels.Labels + iterator func() SeriesIterator +} + +func (m *mockSeries) Labels() labels.Labels { return m.labels() } +func (m *mockSeries) Iterator() SeriesIterator { return m.iterator() } + +type listSeriesIterator struct { + list []sample + idx int +} + +func newListSeriesIterator(list []sample) *listSeriesIterator { + return &listSeriesIterator{list: list, idx: -1} +} + +func (it *listSeriesIterator) Values() (int64, float64) { + s := it.list[it.idx] + return s.t, s.v +} + +func (it *listSeriesIterator) Next() bool { + it.idx++ + return it.idx < len(it.list) +} + +func (it *listSeriesIterator) Seek(t int64) bool { + if it.idx == -1 { + it.idx = 0 + } + // Do binary search between current position and end. + it.idx = sort.Search(len(it.list)-it.idx, func(i int) bool { + s := it.list[i+it.idx] + return s.t >= t + }) + + return it.idx < len(it.list) +} + +func (it *listSeriesIterator) Err() error { + return nil +}