Skip to content

Commit

Permalink
mvcc: add TestHashKVWhenCompacting to kvstore_test
Browse files Browse the repository at this point in the history
  • Loading branch information
fanminshi committed Jul 25, 2017
1 parent b4ab1d8 commit 767a217
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions mvcc/kvstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
mrand "math/rand"
"os"
"reflect"
"strconv"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -510,6 +512,82 @@ func TestRestoreContinueUnfinishedCompaction(t *testing.T) {
t.Errorf("key for rev %+v still exists, want deleted", bytesToRev(revbytes))
}

// TestHashKVWhenCompacting ensures that HashKV returns correct hash when compacting.
func TestHashKVWhenCompacting(t *testing.T) {
b, tmpPath := backend.NewDefaultTmpBackend()
s := NewStore(b, &lease.FakeLessor{}, nil)
defer os.Remove(tmpPath)

rev := 1000
for i := 2; i <= rev; i++ {
s.Put([]byte("foo"), []byte("bar"+strconv.Itoa(i)), lease.NoLease)
}

hashCompactc := make(chan struct {
hash uint32
compactRev int64
}, 1)

donec := make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
hash, _, compactRev, err := s.HashByRev(int64(rev))
if err != nil {
t.Fatal(err)
}
select {
case <-donec:
return
case hashCompactc <- struct {
hash uint32
compactRev int64
}{hash, compactRev}:
}
}
}()
}

go func() {
defer close(donec)
revHash := make(map[int64]uint32)
for round := 0; round < 1000; round++ {
select {
case r := <-hashCompactc:
if revHash[r.compactRev] == 0 {
revHash[r.compactRev] = r.hash
break
}
if r.hash != revHash[r.compactRev] {
t.Fatalf("Hashes differ (current %v) != (saved %v)", r.hash, revHash[r.compactRev])
}
}
}
}()

wg.Add(1)
go func() {
defer wg.Done()
for i := 100; i >= 0; i-- {
_, err := s.Compact(int64(rev - 1 - i))
if err != nil {
t.Fatal(err)
}
time.Sleep(10)
}
}()

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

func TestTxnPut(t *testing.T) {
// assign arbitrary size
bytesN := 30
Expand Down

0 comments on commit 767a217

Please sign in to comment.