Skip to content

Commit 953b8c4

Browse files
committed
Fix flaky tests
Signed-off-by: Alvin Lin <[email protected]>
1 parent 195f863 commit 953b8c4

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

pkg/compactor/compactor_test.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ func TestCompactor_ShouldIterateOverUsersAndRunCompaction(t *testing.T) {
526526
// Ensure a plan has been executed for the blocks of each user.
527527
tsdbPlanner.AssertNumberOfCalls(t, "Plan", 2)
528528

529-
assert.Len(t, tsdbPlanner.noCompactMarkFilter.NoCompactMarkedBlocks(), 0)
529+
assert.Len(t, tsdbPlanner.getNoCompactBlocks(), 0)
530530

531531
assert.ElementsMatch(t, []string{
532532
`level=info component=cleaner msg="started blocks cleanup and maintenance"`,
@@ -765,7 +765,7 @@ func TestCompactor_ShouldNotCompactBlocksMarkedForSkipCompact(t *testing.T) {
765765

766766
require.NoError(t, services.StartAndAwaitRunning(context.Background(), c))
767767

768-
cortex_testutil.Poll(t, 30*time.Second, 1.0, func() interface{} {
768+
cortex_testutil.Poll(t, time.Second, 1.0, func() interface{} {
769769
return prom_testutil.ToFloat64(c.compactionRunsCompleted)
770770
})
771771

@@ -774,7 +774,7 @@ func TestCompactor_ShouldNotCompactBlocksMarkedForSkipCompact(t *testing.T) {
774774
// Planner still called for user with all blocks makred for skip compaction.
775775
tsdbPlanner.AssertNumberOfCalls(t, "Plan", 2)
776776

777-
assert.Len(t, tsdbPlanner.noCompactMarkFilter.NoCompactMarkedBlocks(), 2)
777+
assert.ElementsMatch(t, []string{"01DTVP434PA9VFXSW2JKB3392D", "01FN6CDF3PNEWWRY5MPGJPE3EX"}, tsdbPlanner.getNoCompactBlocks())
778778

779779
testedMetrics := []string{"cortex_compactor_blocks_marked_for_no_compaction_total"}
780780

@@ -1297,7 +1297,9 @@ func prepare(t *testing.T, compactorCfg Config, bucketClient objstore.Bucket) (*
12971297
})
12981298

12991299
tsdbCompactor := &tsdbCompactorMock{}
1300-
tsdbPlanner := &tsdbPlannerMock{}
1300+
tsdbPlanner := &tsdbPlannerMock{
1301+
noCompactMarkFilters: []*compact.GatherNoCompactionMarkFilter{},
1302+
}
13011303
logs := &concurrency.SyncBuffer{}
13021304
logger := log.NewLogfmtLogger(logs)
13031305
registry := prometheus.NewRegistry()
@@ -1314,7 +1316,7 @@ func prepare(t *testing.T, compactorCfg Config, bucketClient objstore.Bucket) (*
13141316
blocksCompactorFactory := func(ctx context.Context, cfg Config, logger log.Logger, reg prometheus.Registerer) (compact.Compactor, PlannerFactory, error) {
13151317
return tsdbCompactor,
13161318
func(_ log.Logger, _ Config, noCompactMarkFilter *compact.GatherNoCompactionMarkFilter) compact.Planner {
1317-
tsdbPlanner.noCompactMarkFilter = noCompactMarkFilter
1319+
tsdbPlanner.noCompactMarkFilters = append(tsdbPlanner.noCompactMarkFilters, noCompactMarkFilter)
13181320
return tsdbPlanner
13191321
},
13201322
nil
@@ -1347,14 +1349,27 @@ func (m *tsdbCompactorMock) Compact(dest string, dirs []string, open []*tsdb.Blo
13471349

13481350
type tsdbPlannerMock struct {
13491351
mock.Mock
1350-
noCompactMarkFilter *compact.GatherNoCompactionMarkFilter
1352+
noCompactMarkFilters []*compact.GatherNoCompactionMarkFilter
13511353
}
13521354

13531355
func (m *tsdbPlannerMock) Plan(ctx context.Context, metasByMinTime []*metadata.Meta) ([]*metadata.Meta, error) {
13541356
args := m.Called(ctx, metasByMinTime)
13551357
return args.Get(0).([]*metadata.Meta), args.Error(1)
13561358
}
13571359

1360+
func (m *tsdbPlannerMock) getNoCompactBlocks() []string {
1361+
1362+
result := []string{}
1363+
1364+
for _, noCompactMarkFilter := range m.noCompactMarkFilters {
1365+
for _, mark := range noCompactMarkFilter.NoCompactMarkedBlocks() {
1366+
result = append(result, mark.ID.String())
1367+
}
1368+
}
1369+
1370+
return result
1371+
}
1372+
13581373
func mockBlockMetaJSON(id string) string {
13591374
meta := tsdb.BlockMeta{
13601375
Version: 1,

0 commit comments

Comments
 (0)