swarm/storage/localstore: fix a race in testDB_collectGarbageWorker

This commit is contained in:
Janos Guljas 2019-01-14 13:52:33 +01:00
parent 1dae999982
commit eda338a319
2 changed files with 21 additions and 16 deletions

View file

@ -159,6 +159,7 @@ func (db *DB) getGCSize() (count int64) {
func (db *DB) triggerGarbageCollection() { func (db *DB) triggerGarbageCollection() {
select { select {
case db.collectGarbageTrigger <- struct{}{}: case db.collectGarbageTrigger <- struct{}{}:
case <-db.close:
default: default:
} }
} }

View file

@ -29,12 +29,7 @@ import (
// TestDB_collectGarbageWorker tests garbage collection runs // TestDB_collectGarbageWorker tests garbage collection runs
// by uploading and syncing a number of chunks. // by uploading and syncing a number of chunks.
func TestDB_collectGarbageWorker(t *testing.T) { func TestDB_collectGarbageWorker(t *testing.T) {
db, cleanupFunc := newTestDB(t, &Options{ testDB_collectGarbageWorker(t)
Capacity: 100,
})
defer cleanupFunc()
testDB_collectGarbageWorker(t, db)
} }
// TestDB_collectGarbageWorker_multipleBatches tests garbage // TestDB_collectGarbageWorker_multipleBatches tests garbage
@ -46,20 +41,12 @@ func TestDB_collectGarbageWorker_multipleBatches(t *testing.T) {
defer func(s int64) { gcBatchSize = s }(gcBatchSize) defer func(s int64) { gcBatchSize = s }(gcBatchSize)
gcBatchSize = 2 gcBatchSize = 2
db, cleanupFunc := newTestDB(t, &Options{ testDB_collectGarbageWorker(t)
Capacity: 100,
})
defer cleanupFunc()
testDB_collectGarbageWorker(t, db)
} }
// testDB_collectGarbageWorker is a helper test function to test // testDB_collectGarbageWorker is a helper test function to test
// garbage collection runs by uploading and syncing a number of chunks. // garbage collection runs by uploading and syncing a number of chunks.
func testDB_collectGarbageWorker(t *testing.T, db *DB) { func testDB_collectGarbageWorker(t *testing.T) {
uploader := db.NewPutter(ModePutUpload)
syncer := db.NewSetter(ModeSetSync)
chunkCount := 150 chunkCount := 150
testHookCollectGarbageChan := make(chan int64) testHookCollectGarbageChan := make(chan int64)
@ -67,6 +54,14 @@ func testDB_collectGarbageWorker(t *testing.T, db *DB) {
testHookCollectGarbageChan <- collectedCount testHookCollectGarbageChan <- collectedCount
})() })()
db, cleanupFunc := newTestDB(t, &Options{
Capacity: 100,
})
defer cleanupFunc()
uploader := db.NewPutter(ModePutUpload)
syncer := db.NewSetter(ModeSetSync)
addrs := make([]storage.Address, 0) addrs := make([]storage.Address, 0)
// upload random chunks // upload random chunks
@ -121,6 +116,15 @@ func testDB_collectGarbageWorker(t *testing.T, db *DB) {
t.Fatal(err) t.Fatal(err)
} }
}) })
// cleanup: drain the last testHookCollectGarbageChan
// element before calling deferred functions not to block
// collectGarbageWorker loop, preventing the race in
// setting testHookCollectGarbage function
select {
case <-testHookCollectGarbageChan:
default:
}
} }
// TestDB_collectGarbageWorker_withRequests is a helper test function // TestDB_collectGarbageWorker_withRequests is a helper test function