MCPcopy
hub / github.com/prometheus/prometheus / TestDisableAutoCompactions

Function TestDisableAutoCompactions

tsdb/compact_test.go:1321–1367  ·  view source on GitHub ↗

TestDisableAutoCompactions checks that we can disable and enable the auto compaction. This is needed for unit tests that rely on checking state before and after a compaction.

(t *testing.T)

Source from the content-addressed store, hash-verified

1319// This is needed for unit tests that rely on
1320// checking state before and after a compaction.
1321func TestDisableAutoCompactions(t *testing.T) {
1322 db := newTestDB(t)
1323
1324 blockRange := db.compactor.(*LeveledCompactor).ranges[0]
1325 label := labels.FromStrings("foo", "bar")
1326
1327 // Trigger a compaction to check that it was skipped and
1328 // no new blocks were created when compaction is disabled.
1329 db.DisableCompactions()
1330 app := db.Appender(context.Background())
1331 for i := range int64(3) {
1332 _, err := app.Append(0, label, i*blockRange, 0)
1333 require.NoError(t, err)
1334 _, err = app.Append(0, label, i*blockRange+1000, 0)
1335 require.NoError(t, err)
1336 }
1337 require.NoError(t, app.Commit())
1338
1339 select {
1340 case db.compactc <- struct{}{}:
1341 default:
1342 }
1343
1344 for range 10 {
1345 if prom_testutil.ToFloat64(db.metrics.compactionsSkipped) > 0.0 {
1346 break
1347 }
1348 time.Sleep(10 * time.Millisecond)
1349 }
1350
1351 require.Greater(t, prom_testutil.ToFloat64(db.metrics.compactionsSkipped), 0.0, "No compaction was skipped after the set timeout.")
1352 require.Empty(t, db.blocks)
1353
1354 // Enable the compaction, trigger it and check that the block is persisted.
1355 db.EnableCompactions()
1356 select {
1357 case db.compactc <- struct{}{}:
1358 default:
1359 }
1360 for range 100 {
1361 if len(db.Blocks()) > 0 {
1362 break
1363 }
1364 time.Sleep(100 * time.Millisecond)
1365 }
1366 require.NotEmpty(t, db.Blocks(), "No block was persisted after the set timeout.")
1367}
1368
1369// TestCancelCompactions ensures that when the db is closed
1370// any running compaction is cancelled to unblock closing the db.

Callers

nothing calls this directly

Calls 8

FromStringsFunction · 0.92
newTestDBFunction · 0.85
DisableCompactionsMethod · 0.80
EnableCompactionsMethod · 0.80
AppenderMethod · 0.65
AppendMethod · 0.65
CommitMethod · 0.65
BlocksMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…