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() {
select {
case db.collectGarbageTrigger <- struct{}{}:
case <-db.close:
default:
}
}

View file

@ -29,12 +29,7 @@ import (
// TestDB_collectGarbageWorker tests garbage collection runs
// by uploading and syncing a number of chunks.
func TestDB_collectGarbageWorker(t *testing.T) {
db, cleanupFunc := newTestDB(t, &Options{
Capacity: 100,
})
defer cleanupFunc()
testDB_collectGarbageWorker(t, db)
testDB_collectGarbageWorker(t)
}
// TestDB_collectGarbageWorker_multipleBatches tests garbage
@ -46,20 +41,12 @@ func TestDB_collectGarbageWorker_multipleBatches(t *testing.T) {
defer func(s int64) { gcBatchSize = s }(gcBatchSize)
gcBatchSize = 2
db, cleanupFunc := newTestDB(t, &Options{
Capacity: 100,
})
defer cleanupFunc()
testDB_collectGarbageWorker(t, db)
testDB_collectGarbageWorker(t)
}
// testDB_collectGarbageWorker is a helper test function to test
// garbage collection runs by uploading and syncing a number of chunks.
func testDB_collectGarbageWorker(t *testing.T, db *DB) {
uploader := db.NewPutter(ModePutUpload)
syncer := db.NewSetter(ModeSetSync)
func testDB_collectGarbageWorker(t *testing.T) {
chunkCount := 150
testHookCollectGarbageChan := make(chan int64)
@ -67,6 +54,14 @@ func testDB_collectGarbageWorker(t *testing.T, db *DB) {
testHookCollectGarbageChan <- collectedCount
})()
db, cleanupFunc := newTestDB(t, &Options{
Capacity: 100,
})
defer cleanupFunc()
uploader := db.NewPutter(ModePutUpload)
syncer := db.NewSetter(ModeSetSync)
addrs := make([]storage.Address, 0)
// upload random chunks
@ -121,6 +116,15 @@ func testDB_collectGarbageWorker(t *testing.T, db *DB) {
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