Commit a342f89c authored by acud's avatar acud Committed by GitHub

localstore: change pin gc test to reduce flakiness (#1491)

parent d9bb759d
...@@ -61,6 +61,13 @@ func testDBCollectGarbageWorker(t *testing.T) { ...@@ -61,6 +61,13 @@ func testDBCollectGarbageWorker(t *testing.T) {
var closed chan struct{} var closed chan struct{}
testHookCollectGarbageChan := make(chan uint64) testHookCollectGarbageChan := make(chan uint64)
t.Cleanup(setTestHookCollectGarbage(func(collectedCount uint64) { t.Cleanup(setTestHookCollectGarbage(func(collectedCount uint64) {
// don't trigger if we haven't collected anything - this may
// result in a race condition when we inspect the gcsize below,
// causing the database to shut down while the cleanup to happen
// before the correct signal has been communicated here.
if collectedCount == 0 {
return
}
select { select {
case testHookCollectGarbageChan <- collectedCount: case testHookCollectGarbageChan <- collectedCount:
case <-closed: case <-closed:
...@@ -151,6 +158,14 @@ func TestPinGC(t *testing.T) { ...@@ -151,6 +158,14 @@ func TestPinGC(t *testing.T) {
var closed chan struct{} var closed chan struct{}
testHookCollectGarbageChan := make(chan uint64) testHookCollectGarbageChan := make(chan uint64)
t.Cleanup(setTestHookCollectGarbage(func(collectedCount uint64) { t.Cleanup(setTestHookCollectGarbage(func(collectedCount uint64) {
// don't trigger if we haven't collected anything - this may
// result in a race condition when we inspect the gcsize below,
// causing the database to shut down while the cleanup to happen
// before the correct signal has been communicated here.
if collectedCount == 0 {
return
}
select { select {
case testHookCollectGarbageChan <- collectedCount: case testHookCollectGarbageChan <- collectedCount:
case <-closed: case <-closed:
...@@ -169,34 +184,31 @@ func TestPinGC(t *testing.T) { ...@@ -169,34 +184,31 @@ func TestPinGC(t *testing.T) {
for i := 0; i < chunkCount; i++ { for i := 0; i < chunkCount; i++ {
ch := generateTestRandomChunk() ch := generateTestRandomChunk()
_, err := db.Put(context.Background(), storage.ModePutUpload, ch) mode := storage.ModePutUpload
if err != nil { if i < pinChunksCount {
t.Fatal(err) mode = storage.ModePutUploadPin
pinAddrs = append(pinAddrs, ch.Address())
} }
err = db.Set(context.Background(), storage.ModeSetSync, ch.Address()) _, err := db.Put(context.Background(), mode, ch)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
addrs = append(addrs, ch.Address()) err = db.Set(context.Background(), storage.ModeSetSync, ch.Address())
// Pin the chunks at the beginning to make sure they are not removed by GC
if i < pinChunksCount {
err = db.Set(context.Background(), storage.ModeSetPin, ch.Address())
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
pinAddrs = append(pinAddrs, ch.Address()) addrs = append(addrs, ch.Address())
}
} }
gcTarget := db.gcTarget()
gcTarget := db.gcTarget()
t.Log(gcTarget)
for { for {
select { select {
case <-testHookCollectGarbageChan: case <-testHookCollectGarbageChan:
case <-time.After(10 * time.Second): case <-time.After(10 * time.Second):
t.Error("collect garbage timeout") t.Fatal("collect garbage timeout")
} }
gcSize, err := db.gcSize.Get() gcSize, err := db.gcSize.Get()
if err != nil { if err != nil {
...@@ -209,7 +221,7 @@ func TestPinGC(t *testing.T) { ...@@ -209,7 +221,7 @@ func TestPinGC(t *testing.T) {
t.Run("pin Index count", newItemsCountTest(db.pinIndex, pinChunksCount)) t.Run("pin Index count", newItemsCountTest(db.pinIndex, pinChunksCount))
t.Run("gc exclude index count", newItemsCountTest(db.gcExcludeIndex, 0)) t.Run("gc exclude index count", newItemsCountTest(db.gcExcludeIndex, pinChunksCount))
t.Run("pull index count", newItemsCountTest(db.pullIndex, int(gcTarget)+pinChunksCount)) t.Run("pull index count", newItemsCountTest(db.pullIndex, int(gcTarget)+pinChunksCount))
...@@ -308,6 +320,13 @@ func TestDB_collectGarbageWorker_withRequests(t *testing.T) { ...@@ -308,6 +320,13 @@ func TestDB_collectGarbageWorker_withRequests(t *testing.T) {
testHookCollectGarbageChan := make(chan uint64) testHookCollectGarbageChan := make(chan uint64)
defer setTestHookCollectGarbage(func(collectedCount uint64) { defer setTestHookCollectGarbage(func(collectedCount uint64) {
// don't trigger if we haven't collected anything - this may
// result in a race condition when we inspect the gcsize below,
// causing the database to shut down while the cleanup to happen
// before the correct signal has been communicated here.
if collectedCount == 0 {
return
}
testHookCollectGarbageChan <- collectedCount testHookCollectGarbageChan <- collectedCount
})() })()
...@@ -624,6 +643,13 @@ func TestPinSyncAndAccessPutSetChunkMultipleTimes(t *testing.T) { ...@@ -624,6 +643,13 @@ func TestPinSyncAndAccessPutSetChunkMultipleTimes(t *testing.T) {
var closed chan struct{} var closed chan struct{}
testHookCollectGarbageChan := make(chan uint64) testHookCollectGarbageChan := make(chan uint64)
t.Cleanup(setTestHookCollectGarbage(func(collectedCount uint64) { t.Cleanup(setTestHookCollectGarbage(func(collectedCount uint64) {
// don't trigger if we haven't collected anything - this may
// result in a race condition when we inspect the gcsize below,
// causing the database to shut down while the cleanup to happen
// before the correct signal has been communicated here.
if collectedCount == 0 {
return
}
select { select {
case testHookCollectGarbageChan <- collectedCount: case testHookCollectGarbageChan <- collectedCount:
case <-closed: case <-closed:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment