fix unit test

Signed-off-by: jessicagreben <jessicagrebens@gmail.com>
This commit is contained in:
jessicagreben 2020-11-30 11:02:45 -08:00
parent cec3515fa3
commit e32e4fcc53
2 changed files with 18 additions and 12 deletions

View file

@ -185,10 +185,10 @@ func (m *multipleAppender) flushAndCommit(ctx context.Context) error {
if m.currentSampleCount == 0 { if m.currentSampleCount == 0 {
return nil return nil
} }
if _, err := m.writer.Flush(ctx); err != nil { if err := m.appender.Commit(); err != nil {
return err return err
} }
if err := m.appender.Commit(); err != nil { if _, err := m.writer.Flush(ctx); err != nil {
return err return err
} }
m.appender = m.writer.Appender(ctx) m.appender = m.writer.Appender(ctx)

View file

@ -30,7 +30,12 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
const testMaxSampleCount = 500 const (
testMaxSampleCount = 50
testValue = 123
)
var testTime = model.Time(time.Now().Add(-20 * time.Minute).Unix())
type mockQueryRangeAPI struct{} type mockQueryRangeAPI struct{}
@ -38,12 +43,12 @@ func (mockAPI mockQueryRangeAPI) QueryRange(ctx context.Context, query string, r
var testMatrix model.Matrix = []*model.SampleStream{ var testMatrix model.Matrix = []*model.SampleStream{
{ {
Metric: model.Metric{ Metric: model.Metric{
"labelname1": "val1", "name1": "val1",
}, },
Values: []model.SamplePair{ Values: []model.SamplePair{
{ {
Timestamp: model.Time(123456789123), Timestamp: testTime,
Value: 123, Value: testValue,
}, },
}, },
}, },
@ -71,6 +76,7 @@ func TestBackfillRuleIntegration(t *testing.T) {
// Execute test two times in a row to simulate running the rule importer twice with the same data. // Execute test two times in a row to simulate running the rule importer twice with the same data.
// We expect that duplicate blocks with the same series are created when run more than once. // We expect that duplicate blocks with the same series are created when run more than once.
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
ruleImporter, err := newTestRuleImporter(ctx, start, tmpDir) ruleImporter, err := newTestRuleImporter(ctx, start, tmpDir)
require.NoError(t, err) require.NoError(t, err)
path := tmpDir + "/test.file" path := tmpDir + "/test.file"
@ -122,14 +128,15 @@ func TestBackfillRuleIntegration(t *testing.T) {
it := series.Iterator() it := series.Iterator()
for it.Next() { for it.Next() {
samplesCount++ samplesCount++
_, v := it.At() ts, v := it.At()
require.Equal(t, 123, v) require.Equal(t, float64(testValue), v)
require.Equal(t, int64(testTime), ts)
} }
require.NoError(t, it.Err()) require.NoError(t, it.Err())
} }
require.NoError(t, selectedSeries.Err()) require.NoError(t, selectedSeries.Err())
require.Equal(t, 1, seriesCount) require.Equal(t, 1, seriesCount)
require.Equal(t, 2, samplesCount) require.Equal(t, 1, samplesCount)
require.NoError(t, q.Close()) require.NoError(t, q.Close())
require.NoError(t, db.Close()) require.NoError(t, db.Close())
} }
@ -156,8 +163,7 @@ func newTestRuleImporter(ctx context.Context, start time.Time, tmpDir string) (*
} }
func createTestFiles(groupName, ruleName, ruleExpr, ruleLabels, path string) error { func createTestFiles(groupName, ruleName, ruleExpr, ruleLabels, path string) error {
x := fmt.Sprintf(` recordingRules := fmt.Sprintf(`groups:
groups:
- name: %s - name: %s
rules: rules:
- record: %s - record: %s
@ -167,5 +173,5 @@ groups:
`, `,
groupName, ruleName, ruleExpr, ruleLabels, groupName, ruleName, ruleExpr, ruleLabels,
) )
return ioutil.WriteFile(path, []byte(x), 0777) return ioutil.WriteFile(path, []byte(recordingRules), 0777)
} }