From 01c6943f2b80ae53db6ad85b9195026caaa5c48a Mon Sep 17 00:00:00 2001 From: Marco Pracucci Date: Wed, 22 Jan 2020 11:27:56 +0100 Subject: [PATCH] Fix flaky tests in compactor Signed-off-by: Marco Pracucci --- pkg/compactor/compactor_test.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/pkg/compactor/compactor_test.go b/pkg/compactor/compactor_test.go index 74abf5100ad..59e14a8e2ca 100644 --- a/pkg/compactor/compactor_test.go +++ b/pkg/compactor/compactor_test.go @@ -69,11 +69,12 @@ func TestConfig_ShouldSupportCliFlags(t *testing.T) { func TestCompactor_ShouldDoNothingOnNoUserBlocks(t *testing.T) { t.Parallel() - c, bucketClient, _, logs, registry := prepare(t) - // No user blocks stored in the bucket. + bucketClient := &cortex_tsdb.BucketClientMock{} bucketClient.MockIter("", []string{}, nil) + c, _, logs, registry := prepare(t, bucketClient) + // Wait until a run has completed. cortex_testutil.Poll(t, time.Second, 1.0, func() interface{} { return prom_testutil.ToFloat64(c.compactionRunsCompleted) @@ -104,11 +105,12 @@ func TestCompactor_ShouldDoNothingOnNoUserBlocks(t *testing.T) { func TestCompactor_ShouldRetryOnFailureWhileDiscoveringUsersFromBucket(t *testing.T) { t.Parallel() - c, bucketClient, _, logs, registry := prepare(t) - // Fail to iterate over the bucket while discovering users. + bucketClient := &cortex_tsdb.BucketClientMock{} bucketClient.MockIter("", nil, errors.New("failed to iterate the bucket")) + c, _, logs, registry := prepare(t, bucketClient) + // Wait until all retry attempts have completed. cortex_testutil.Poll(t, time.Second, 1.0, func() interface{} { return prom_testutil.ToFloat64(c.compactionRunsFailed) @@ -146,15 +148,16 @@ func TestCompactor_ShouldRetryOnFailureWhileDiscoveringUsersFromBucket(t *testin func TestCompactor_ShouldIterateOverUsersAndRunCompaction(t *testing.T) { t.Parallel() - c, bucketClient, tsdbCompactor, logs, registry := prepare(t) - // Mock the bucket to contain two users, each one with one block. + bucketClient := &cortex_tsdb.BucketClientMock{} bucketClient.MockIter("", []string{"user-1", "user-2"}, nil) bucketClient.MockIter("user-1/", []string{"user-1/01DTVP434PA9VFXSW2JKB3392D"}, nil) bucketClient.MockIter("user-2/", []string{"user-2/01DTW0ZCPDDNV4BV83Q2SV4QAZ"}, nil) bucketClient.MockGet("user-1/01DTVP434PA9VFXSW2JKB3392D/meta.json", mockBlockMetaJSON("01DTVP434PA9VFXSW2JKB3392D"), nil) bucketClient.MockGet("user-2/01DTW0ZCPDDNV4BV83Q2SV4QAZ/meta.json", mockBlockMetaJSON("01DTW0ZCPDDNV4BV83Q2SV4QAZ"), nil) + c, tsdbCompactor, logs, registry := prepare(t, bucketClient) + // Mock the compactor as if there's no compaction to do, // in order to simplify tests (all in all, we just want to // test our logic and not TSDB compactor which we expect to @@ -203,14 +206,13 @@ func TestCompactor_ShouldIterateOverUsersAndRunCompaction(t *testing.T) { `))) } -func prepare(t *testing.T) (*Compactor, *cortex_tsdb.BucketClientMock, *tsdbCompactorMock, *bytes.Buffer, prometheus.Gatherer) { +func prepare(t *testing.T, bucketClient *cortex_tsdb.BucketClientMock) (*Compactor, *tsdbCompactorMock, *bytes.Buffer, prometheus.Gatherer) { compactorCfg := Config{} storageCfg := cortex_tsdb.Config{} flagext.DefaultValues(&compactorCfg, &storageCfg) compactorCfg.retryMinBackoff = 0 compactorCfg.retryMaxBackoff = 0 - bucketClient := &cortex_tsdb.BucketClientMock{} tsdbCompactor := &tsdbCompactorMock{} logs := &bytes.Buffer{} logger := log.NewLogfmtLogger(logs) @@ -220,7 +222,7 @@ func prepare(t *testing.T) (*Compactor, *cortex_tsdb.BucketClientMock, *tsdbComp c, err := newCompactor(ctx, cancelCtx, compactorCfg, storageCfg, bucketClient, tsdbCompactor, logger, registry) require.NoError(t, err) - return c, bucketClient, tsdbCompactor, logs, registry + return c, tsdbCompactor, logs, registry } type tsdbCompactorMock struct {