Skip to content

Commit

Permalink
test: fix TestHashKVWhenCompacting: ensure all goroutine finished
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Wang <[email protected]>
  • Loading branch information
ahrtr committed Jan 25, 2024
1 parent a818d07 commit c647614
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions server/storage/mvcc/kvstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ func TestHashKVWhenCompacting(t *testing.T) {
hashCompactc := make(chan hashKVResult, 1)
var wg sync.WaitGroup
donec := make(chan struct{})
stopc := make(chan struct{})

// Call HashByRev(10000) in multiple goroutines until donec is closed
for i := 0; i < 10; i++ {
Expand All @@ -574,6 +575,8 @@ func TestHashKVWhenCompacting(t *testing.T) {
t.Error(err)
}
select {
case <-stopc:
return
case <-donec:
return
case hashCompactc <- hashKVResult{hash.Hash, hash.CompactRevision}:
Expand All @@ -597,16 +600,29 @@ func TestHashKVWhenCompacting(t *testing.T) {
if r.hash != revHash[r.compactRev] {
t.Errorf("Hashes differ (current %v) != (saved %v)", r.hash, revHash[r.compactRev])
}
case <-stopc:
return
case <-donec:
return
}
}
}()

// Compact the store in a goroutine, using RevisionTombstone 9900 to 10000 and close donec when finished
wg.Add(1)
go func() {
defer close(donec)
defer func() {
close(donec)
wg.Done()
}()

for i := 100; i >= 0; i-- {
select {
case <-stopc:
return
default:
}

_, err := s.Compact(traceutil.TODO(), int64(rev-i))
if err != nil {
t.Error(err)
Expand All @@ -620,10 +636,14 @@ func TestHashKVWhenCompacting(t *testing.T) {

select {
case <-donec:
wg.Wait()
case <-time.After(10 * time.Second):
close(stopc)
wg.Wait()
testutil.FatalStack(t, "timeout")
}

close(stopc)
wg.Wait()
}

// TestHashKVWithCompactedAndFutureRevisions ensures that HashKV returns a correct hash when called
Expand Down

0 comments on commit c647614

Please sign in to comment.