use test utils in head_test

This commit is contained in:
Callum Styan 2017-12-06 16:00:14 -08:00
parent ae8bb5fd16
commit 5aa2af255b
1 changed files with 61 additions and 62 deletions

View File

@ -26,16 +26,15 @@ import (
promlabels "github.com/prometheus/prometheus/pkg/labels" promlabels "github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/pkg/textparse" "github.com/prometheus/prometheus/pkg/textparse"
"github.com/stretchr/testify/require"
) )
func BenchmarkCreateSeries(b *testing.B) { func BenchmarkCreateSeries(b *testing.B) {
lbls, err := readPrometheusLabels("testdata/all.series", b.N) lbls, err := readPrometheusLabels("testdata/all.series", b.N)
require.NoError(b, err) Ok(b, err)
h, err := NewHead(nil, nil, nil, 10000) h, err := NewHead(nil, nil, nil, 10000)
if err != nil { if err != nil {
require.NoError(b, err) Ok(b, err)
} }
defer h.Close() defer h.Close()
@ -131,40 +130,40 @@ func TestHead_ReadWAL(t *testing.T) {
wal := &memoryWAL{entries: entries} wal := &memoryWAL{entries: entries}
head, err := NewHead(nil, nil, wal, 1000) head, err := NewHead(nil, nil, wal, 1000)
require.NoError(t, err) Ok(t, err)
defer head.Close() defer head.Close()
require.NoError(t, head.ReadWAL()) Ok(t, head.ReadWAL())
require.Equal(t, uint64(100), head.lastSeriesID) Equals(t, uint64(100), head.lastSeriesID)
s10 := head.series.getByID(10) s10 := head.series.getByID(10)
s11 := head.series.getByID(11) s11 := head.series.getByID(11)
s50 := head.series.getByID(50) s50 := head.series.getByID(50)
s100 := head.series.getByID(100) s100 := head.series.getByID(100)
require.Equal(t, labels.FromStrings("a", "1"), s10.lset) Equals(t, labels.FromStrings("a", "1"), s10.lset)
require.Equal(t, labels.FromStrings("a", "2"), s11.lset) Equals(t, labels.FromStrings("a", "2"), s11.lset)
require.Equal(t, labels.FromStrings("a", "4"), s50.lset) Equals(t, labels.FromStrings("a", "4"), s50.lset)
require.Equal(t, labels.FromStrings("a", "3"), s100.lset) Equals(t, labels.FromStrings("a", "3"), s100.lset)
expandChunk := func(c chunks.Iterator) (x []sample) { expandChunk := func(c chunks.Iterator) (x []sample) {
for c.Next() { for c.Next() {
t, v := c.At() t, v := c.At()
x = append(x, sample{t: t, v: v}) x = append(x, sample{t: t, v: v})
} }
require.NoError(t, c.Err()) Ok(t, c.Err())
return x return x
} }
require.Equal(t, []sample{{100, 2}, {101, 5}}, expandChunk(s10.iterator(0))) Equals(t, []sample{{100, 2}, {101, 5}}, expandChunk(s10.iterator(0)))
require.Equal(t, 0, len(s11.chunks)) Equals(t, 0, len(s11.chunks))
require.Equal(t, []sample{{101, 6}}, expandChunk(s50.iterator(0))) Equals(t, []sample{{101, 6}}, expandChunk(s50.iterator(0)))
require.Equal(t, []sample{{100, 3}}, expandChunk(s100.iterator(0))) Equals(t, []sample{{100, 3}}, expandChunk(s100.iterator(0)))
} }
func TestHead_Truncate(t *testing.T) { func TestHead_Truncate(t *testing.T) {
h, err := NewHead(nil, nil, nil, 1000) h, err := NewHead(nil, nil, nil, 1000)
require.NoError(t, err) Ok(t, err)
defer h.Close() defer h.Close()
h.initTime(0) h.initTime(0)
@ -191,21 +190,21 @@ func TestHead_Truncate(t *testing.T) {
s4.chunks = []*memChunk{} s4.chunks = []*memChunk{}
// Truncation need not be aligned. // Truncation need not be aligned.
require.NoError(t, h.Truncate(1)) Ok(t, h.Truncate(1))
h.Truncate(2000) h.Truncate(2000)
require.Equal(t, []*memChunk{ Equals(t, []*memChunk{
{minTime: 2000, maxTime: 2999}, {minTime: 2000, maxTime: 2999},
}, h.series.getByID(s1.ref).chunks) }, h.series.getByID(s1.ref).chunks)
require.Equal(t, []*memChunk{ Equals(t, []*memChunk{
{minTime: 2000, maxTime: 2999}, {minTime: 2000, maxTime: 2999},
{minTime: 3000, maxTime: 3999}, {minTime: 3000, maxTime: 3999},
}, h.series.getByID(s2.ref).chunks) }, h.series.getByID(s2.ref).chunks)
require.Nil(t, h.series.getByID(s3.ref)) Nil(t, h.series.getByID(s3.ref), "")
require.Nil(t, h.series.getByID(s4.ref)) Nil(t, h.series.getByID(s4.ref), "")
postingsA1, _ := expandPostings(h.postings.get("a", "1")) postingsA1, _ := expandPostings(h.postings.get("a", "1"))
postingsA2, _ := expandPostings(h.postings.get("a", "2")) postingsA2, _ := expandPostings(h.postings.get("a", "2"))
@ -214,14 +213,14 @@ func TestHead_Truncate(t *testing.T) {
postingsC1, _ := expandPostings(h.postings.get("c", "1")) postingsC1, _ := expandPostings(h.postings.get("c", "1"))
postingsAll, _ := expandPostings(h.postings.get("", "")) postingsAll, _ := expandPostings(h.postings.get("", ""))
require.Equal(t, []uint64{s1.ref}, postingsA1) Equals(t, []uint64{s1.ref}, postingsA1)
require.Equal(t, []uint64{s2.ref}, postingsA2) Equals(t, []uint64{s2.ref}, postingsA2)
require.Equal(t, []uint64{s1.ref, s2.ref}, postingsB1) Equals(t, []uint64{s1.ref, s2.ref}, postingsB1)
require.Equal(t, []uint64{s1.ref, s2.ref}, postingsAll) Equals(t, []uint64{s1.ref, s2.ref}, postingsAll)
require.Nil(t, postingsB2) Nil(t, postingsB2, "")
require.Nil(t, postingsC1) Nil(t, postingsC1, "")
require.Equal(t, map[string]struct{}{ Equals(t, map[string]struct{}{
"": struct{}{}, // from 'all' postings list "": struct{}{}, // from 'all' postings list
"a": struct{}{}, "a": struct{}{},
"b": struct{}{}, "b": struct{}{},
@ -229,7 +228,7 @@ func TestHead_Truncate(t *testing.T) {
"2": struct{}{}, "2": struct{}{},
}, h.symbols) }, h.symbols)
require.Equal(t, map[string]stringset{ Equals(t, map[string]stringset{
"a": stringset{"1": struct{}{}, "2": struct{}{}}, "a": stringset{"1": struct{}{}, "2": struct{}{}},
"b": stringset{"1": struct{}{}}, "b": stringset{"1": struct{}{}},
"": stringset{"": struct{}{}}, "": stringset{"": struct{}{}},
@ -243,7 +242,7 @@ func TestMemSeries_truncateChunks(t *testing.T) {
for i := 0; i < 4000; i += 5 { for i := 0; i < 4000; i += 5 {
ok, _ := s.append(int64(i), float64(i)) ok, _ := s.append(int64(i), float64(i))
require.True(t, ok, "sample appen failed") Assert(t, ok == true, "sample append failed")
} }
// Check that truncate removes half of the chunks and afterwards // Check that truncate removes half of the chunks and afterwards
@ -252,32 +251,32 @@ func TestMemSeries_truncateChunks(t *testing.T) {
lastID := s.chunkID(countBefore - 1) lastID := s.chunkID(countBefore - 1)
lastChunk := s.chunk(lastID) lastChunk := s.chunk(lastID)
require.NotNil(t, s.chunk(0)) NotNil(t, s.chunk(0), "")
require.NotNil(t, lastChunk) NotNil(t, lastChunk, "")
s.truncateChunksBefore(2000) s.truncateChunksBefore(2000)
require.Equal(t, int64(2000), s.chunks[0].minTime, "unexpected start time of first chunks") Equals(t, int64(2000), s.chunks[0].minTime)
require.Nil(t, s.chunk(0), "first chunk not gone") Nil(t, s.chunk(0), "first chunks not gone")
require.Equal(t, countBefore/2, len(s.chunks), "chunks not truncated correctly") Equals(t, countBefore/2, len(s.chunks))
require.Equal(t, lastChunk, s.chunk(lastID), "last chunk does not match") Equals(t, lastChunk, s.chunk(lastID))
// Validate that the series' sample buffer is applied correctly to the last chunk // Validate that the series' sample buffer is applied correctly to the last chunk
// after truncation. // after truncation.
it1 := s.iterator(s.chunkID(len(s.chunks) - 1)) it1 := s.iterator(s.chunkID(len(s.chunks) - 1))
_, ok := it1.(*memSafeIterator) _, ok := it1.(*memSafeIterator)
require.True(t, ok, "last chunk not wrapped with sample buffer") Assert(t, ok == true, "")
it2 := s.iterator(s.chunkID(len(s.chunks) - 2)) it2 := s.iterator(s.chunkID(len(s.chunks) - 2))
_, ok = it2.(*memSafeIterator) _, ok = it2.(*memSafeIterator)
require.False(t, ok, "non-last chunk incorrectly wrapped with sample buffer") Assert(t, ok == false, "non-last chunk incorrectly wrapped with sample buffer")
} }
func TestHeadDeleteSimple(t *testing.T) { func TestHeadDeleteSimple(t *testing.T) {
numSamples := int64(10) numSamples := int64(10)
head, err := NewHead(nil, nil, nil, 1000) head, err := NewHead(nil, nil, nil, 1000)
require.NoError(t, err) Ok(t, err)
defer head.Close() defer head.Close()
app := head.Appender() app := head.Appender()
@ -288,7 +287,7 @@ func TestHeadDeleteSimple(t *testing.T) {
app.Add(labels.Labels{{"a", "b"}}, i, smpls[i]) app.Add(labels.Labels{{"a", "b"}}, i, smpls[i])
} }
require.NoError(t, app.Commit()) Ok(t, app.Commit())
cases := []struct { cases := []struct {
intervals Intervals intervals Intervals
remaint []int64 remaint []int64
@ -322,14 +321,14 @@ Outer:
// Delete the ranges. // Delete the ranges.
for _, r := range c.intervals { for _, r := range c.intervals {
require.NoError(t, head.Delete(r.Mint, r.Maxt, labels.NewEqualMatcher("a", "b"))) Ok(t, head.Delete(r.Mint, r.Maxt, labels.NewEqualMatcher("a", "b")))
} }
// Compare the result. // Compare the result.
q, err := NewBlockQuerier(head, head.MinTime(), head.MaxTime()) q, err := NewBlockQuerier(head, head.MinTime(), head.MaxTime())
require.NoError(t, err) Ok(t, err)
res, err := q.Select(labels.NewEqualMatcher("a", "b")) res, err := q.Select(labels.NewEqualMatcher("a", "b"))
require.NoError(t, err) Ok(t, err)
expSamples := make([]sample, 0, len(c.remaint)) expSamples := make([]sample, 0, len(c.remaint))
for _, ts := range c.remaint { for _, ts := range c.remaint {
@ -341,13 +340,13 @@ Outer:
}) })
if len(expSamples) == 0 { if len(expSamples) == 0 {
require.False(t, res.Next()) Assert(t, res.Next() == false, "")
continue continue
} }
for { for {
eok, rok := expss.Next(), res.Next() eok, rok := expss.Next(), res.Next()
require.Equal(t, eok, rok, "next") Equals(t, eok, rok)
if !eok { if !eok {
continue Outer continue Outer
@ -355,13 +354,13 @@ Outer:
sexp := expss.At() sexp := expss.At()
sres := res.At() sres := res.At()
require.Equal(t, sexp.Labels(), sres.Labels(), "labels") Equals(t, sexp.Labels(), sres.Labels())
smplExp, errExp := expandSeriesIterator(sexp.Iterator()) smplExp, errExp := expandSeriesIterator(sexp.Iterator())
smplRes, errRes := expandSeriesIterator(sres.Iterator()) smplRes, errRes := expandSeriesIterator(sres.Iterator())
require.Equal(t, errExp, errRes, "samples error") Equals(t, errExp, errRes)
require.Equal(t, smplExp, smplRes, "samples") Equals(t, smplExp, smplRes)
} }
} }
} }
@ -381,12 +380,12 @@ Outer:
// app.Add(labels.Labels{{"a", "b"}}, i, smpls[i]) // app.Add(labels.Labels{{"a", "b"}}, i, smpls[i])
// } // }
// require.NoError(t, app.Commit()) // Ok(t, app.Commit())
// require.NoError(t, hb.Delete(0, 10000, labels.NewEqualMatcher("a", "b"))) // Ok(t, hb.Delete(0, 10000, labels.NewEqualMatcher("a", "b")))
// app = hb.Appender() // app = hb.Appender()
// _, err := app.Add(labels.Labels{{"a", "b"}}, 11, 1) // _, err := app.Add(labels.Labels{{"a", "b"}}, 11, 1)
// require.NoError(t, err) // Ok(t, err)
// require.NoError(t, app.Commit()) // Ok(t, app.Commit())
// q := hb.Querier(0, 100000) // q := hb.Querier(0, 100000)
// res := q.Select(labels.NewEqualMatcher("a", "b")) // res := q.Select(labels.NewEqualMatcher("a", "b"))
@ -395,8 +394,8 @@ Outer:
// exps := res.At() // exps := res.At()
// it := exps.Iterator() // it := exps.Iterator()
// ressmpls, err := expandSeriesIterator(it) // ressmpls, err := expandSeriesIterator(it)
// require.NoError(t, err) // Ok(t, err)
// require.Equal(t, []sample{{11, 1}}, ressmpls) // Equals(t, []sample{{11, 1}}, ressmpls)
// } // }
// func TestDelete_e2e(t *testing.T) { // func TestDelete_e2e(t *testing.T) {
@ -473,9 +472,9 @@ Outer:
// _, err := app.Add(ls, ts, v) // _, err := app.Add(ls, ts, v)
// if ts >= minTime && ts <= maxTime { // if ts >= minTime && ts <= maxTime {
// require.NoError(t, err) // Ok(t, err)
// } else { // } else {
// require.EqualError(t, err, ErrOutOfBounds.Error()) // EqualsError(t, err, ErrOutOfBounds.Error())
// } // }
// ts += rand.Int63n(timeInterval) + 1 // ts += rand.Int63n(timeInterval) + 1
@ -484,7 +483,7 @@ Outer:
// seriesMap[labels.New(l...).String()] = series // seriesMap[labels.New(l...).String()] = series
// } // }
// require.NoError(t, app.Commit()) // Ok(t, app.Commit())
// // Delete a time-range from each-selector. // // Delete a time-range from each-selector.
// dels := []struct { // dels := []struct {
@ -519,7 +518,7 @@ Outer:
// hb.tombstones = newEmptyTombstoneReader() // hb.tombstones = newEmptyTombstoneReader()
// for _, r := range del.drange { // for _, r := range del.drange {
// require.NoError(t, hb.Delete(r.Mint, r.Maxt, del.ms...)) // Ok(t, hb.Delete(r.Mint, r.Maxt, del.ms...))
// } // }
// matched := labels.Slice{} // matched := labels.Slice{}
@ -570,7 +569,7 @@ Outer:
// } // }
// } // }
// } // }
// require.Equal(t, eok, rok, "next") // Equals(t, eok, rok, "next")
// if !eok { // if !eok {
// break // break
@ -578,13 +577,13 @@ Outer:
// sexp := expSs.At() // sexp := expSs.At()
// sres := ss.At() // sres := ss.At()
// require.Equal(t, sexp.Labels(), sres.Labels(), "labels") // Equals(t, sexp.Labels(), sres.Labels(), "labels")
// smplExp, errExp := expandSeriesIterator(sexp.Iterator()) // smplExp, errExp := expandSeriesIterator(sexp.Iterator())
// smplRes, errRes := expandSeriesIterator(sres.Iterator()) // smplRes, errRes := expandSeriesIterator(sres.Iterator())
// require.Equal(t, errExp, errRes, "samples error") // Equals(t, errExp, errRes, "samples error")
// require.Equal(t, smplExp, smplRes, "samples") // Equals(t, smplExp, smplRes, "samples")
// } // }
// } // }
// } // }