Skip to content

Commit

Permalink
storage: enhance test for keyIndex.Get and keyIndex.Compact
Browse files Browse the repository at this point in the history
It covers the case that one key is set multiple times in one main
revision now.
  • Loading branch information
yichengq committed Sep 18, 2015
1 parent c4b3ad7 commit be80d11
Showing 1 changed file with 126 additions and 41 deletions.
167 changes: 126 additions & 41 deletions storage/key_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,50 +20,66 @@ import (
)

func TestKeyIndexGet(t *testing.T) {
// key: "foo"
// rev: 12
// key: "foo"
// rev: 16
// generations:
// {empty}
// {8[1], 10[2], 12(t)[3]}
// {4[2], 6(t)[3]}
// {{14, 0}[1], {14, 1}[2], {16, 0}(t)[3]}
// {{8, 0}[1], {10, 0}[2], {12, 0}(t)[3]}
// {{2, 0}[1], {4, 0}[2], {6, 0}(t)[3]}
ki := newTestKeyIndex()
ki.compact(4, make(map[revision]struct{}))

tests := []struct {
rev int64

wrev int64
werr error
wmod revision
wcreat revision
wver int64
werr error
}{
{13, 0, ErrRevisionNotFound},
{12, 0, ErrRevisionNotFound},
{17, revision{}, revision{}, 0, ErrRevisionNotFound},
{16, revision{}, revision{}, 0, ErrRevisionNotFound},

// get on generation 3
{15, revision{14, 1}, revision{14, 0}, 2, nil},
{14, revision{14, 1}, revision{14, 0}, 2, nil},

{13, revision{}, revision{}, 0, ErrRevisionNotFound},
{12, revision{}, revision{}, 0, ErrRevisionNotFound},

// get on generation 2
{11, 10, nil},
{10, 10, nil},
{9, 8, nil},
{8, 8, nil},
{11, revision{10, 0}, revision{8, 0}, 2, nil},
{10, revision{10, 0}, revision{8, 0}, 2, nil},
{9, revision{8, 0}, revision{8, 0}, 1, nil},
{8, revision{8, 0}, revision{8, 0}, 1, nil},

{7, 0, ErrRevisionNotFound},
{6, 0, ErrRevisionNotFound},
{7, revision{}, revision{}, 0, ErrRevisionNotFound},
{6, revision{}, revision{}, 0, ErrRevisionNotFound},

// get on generation 1
{5, 4, nil},
{4, 4, nil},
{5, revision{4, 0}, revision{2, 0}, 2, nil},
{4, revision{4, 0}, revision{2, 0}, 2, nil},

{3, 0, ErrRevisionNotFound},
{2, 0, ErrRevisionNotFound},
{1, 0, ErrRevisionNotFound},
{0, 0, ErrRevisionNotFound},
{3, revision{}, revision{}, 0, ErrRevisionNotFound},
{2, revision{}, revision{}, 0, ErrRevisionNotFound},
{1, revision{}, revision{}, 0, ErrRevisionNotFound},
{0, revision{}, revision{}, 0, ErrRevisionNotFound},
}

for i, tt := range tests {
rev, _, _, err := ki.get(tt.rev)
mod, creat, ver, err := ki.get(tt.rev)
if err != tt.werr {
t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
}
if rev.main != tt.wrev {
t.Errorf("#%d: rev = %d, want %d", i, rev.main, tt.rev)
if mod != tt.wmod {
t.Errorf("#%d: modified = %+v, want %+v", i, mod, tt.wmod)
}
if creat != tt.wcreat {
t.Errorf("#%d: created = %+v, want %+v", i, creat, tt.wcreat)
}
if ver != tt.wver {
t.Errorf("#%d: version = %d, want %d", i, ver, tt.wver)
}
}
}
Expand Down Expand Up @@ -162,10 +178,11 @@ func TestKeyIndexCompact(t *testing.T) {
1,
&keyIndex{
key: []byte("foo"),
modified: revision{12, 0},
modified: revision{16, 0},
generations: []generation{
{created: revision{2, 0}, ver: 3, revs: []revision{{main: 2}, {main: 4}, {main: 6}}},
{created: revision{8, 0}, ver: 3, revs: []revision{{main: 8}, {main: 10}, {main: 12}}},
{created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
{},
},
},
Expand All @@ -175,10 +192,11 @@ func TestKeyIndexCompact(t *testing.T) {
2,
&keyIndex{
key: []byte("foo"),
modified: revision{12, 0},
modified: revision{16, 0},
generations: []generation{
{created: revision{2, 0}, ver: 3, revs: []revision{{main: 2}, {main: 4}, {main: 6}}},
{created: revision{8, 0}, ver: 3, revs: []revision{{main: 8}, {main: 10}, {main: 12}}},
{created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
{},
},
},
Expand All @@ -190,10 +208,11 @@ func TestKeyIndexCompact(t *testing.T) {
3,
&keyIndex{
key: []byte("foo"),
modified: revision{12, 0},
modified: revision{16, 0},
generations: []generation{
{created: revision{2, 0}, ver: 3, revs: []revision{{main: 2}, {main: 4}, {main: 6}}},
{created: revision{8, 0}, ver: 3, revs: []revision{{main: 8}, {main: 10}, {main: 12}}},
{created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
{},
},
},
Expand All @@ -205,10 +224,11 @@ func TestKeyIndexCompact(t *testing.T) {
4,
&keyIndex{
key: []byte("foo"),
modified: revision{12, 0},
modified: revision{16, 0},
generations: []generation{
{created: revision{2, 0}, ver: 3, revs: []revision{{main: 4}, {main: 6}}},
{created: revision{8, 0}, ver: 3, revs: []revision{{main: 8}, {main: 10}, {main: 12}}},
{created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
{},
},
},
Expand All @@ -220,10 +240,11 @@ func TestKeyIndexCompact(t *testing.T) {
5,
&keyIndex{
key: []byte("foo"),
modified: revision{12, 0},
modified: revision{16, 0},
generations: []generation{
{created: revision{2, 0}, ver: 3, revs: []revision{{main: 4}, {main: 6}}},
{created: revision{8, 0}, ver: 3, revs: []revision{{main: 8}, {main: 10}, {main: 12}}},
{created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
{},
},
},
Expand All @@ -235,9 +256,10 @@ func TestKeyIndexCompact(t *testing.T) {
6,
&keyIndex{
key: []byte("foo"),
modified: revision{12, 0},
modified: revision{16, 0},
generations: []generation{
{created: revision{8, 0}, ver: 3, revs: []revision{{main: 8}, {main: 10}, {main: 12}}},
{created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
{},
},
},
Expand All @@ -247,9 +269,10 @@ func TestKeyIndexCompact(t *testing.T) {
7,
&keyIndex{
key: []byte("foo"),
modified: revision{12, 0},
modified: revision{16, 0},
generations: []generation{
{created: revision{8, 0}, ver: 3, revs: []revision{{main: 8}, {main: 10}, {main: 12}}},
{created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
{},
},
},
Expand All @@ -259,9 +282,10 @@ func TestKeyIndexCompact(t *testing.T) {
8,
&keyIndex{
key: []byte("foo"),
modified: revision{12, 0},
modified: revision{16, 0},
generations: []generation{
{created: revision{8, 0}, ver: 3, revs: []revision{{main: 8}, {main: 10}, {main: 12}}},
{created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
{},
},
},
Expand All @@ -273,9 +297,10 @@ func TestKeyIndexCompact(t *testing.T) {
9,
&keyIndex{
key: []byte("foo"),
modified: revision{12, 0},
modified: revision{16, 0},
generations: []generation{
{created: revision{8, 0}, ver: 3, revs: []revision{{main: 8}, {main: 10}, {main: 12}}},
{created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
{},
},
},
Expand All @@ -287,9 +312,10 @@ func TestKeyIndexCompact(t *testing.T) {
10,
&keyIndex{
key: []byte("foo"),
modified: revision{12, 0},
modified: revision{16, 0},
generations: []generation{
{created: revision{8, 0}, ver: 3, revs: []revision{{main: 10}, {main: 12}}},
{created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
{},
},
},
Expand All @@ -301,9 +327,10 @@ func TestKeyIndexCompact(t *testing.T) {
11,
&keyIndex{
key: []byte("foo"),
modified: revision{12, 0},
modified: revision{16, 0},
generations: []generation{
{created: revision{8, 0}, ver: 3, revs: []revision{{main: 10}, {main: 12}}},
{created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
{},
},
},
Expand All @@ -314,9 +341,63 @@ func TestKeyIndexCompact(t *testing.T) {
{
12,
&keyIndex{
key: []byte("foo"),
modified: revision{12, 0},
generations: []generation{{}},
key: []byte("foo"),
modified: revision{16, 0},
generations: []generation{
{created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
{},
},
},
map[revision]struct{}{},
},
{
13,
&keyIndex{
key: []byte("foo"),
modified: revision{16, 0},
generations: []generation{
{created: revision{14, 0}, ver: 3, revs: []revision{{main: 14}, {main: 14, sub: 1}, {main: 16}}},
{},
},
},
map[revision]struct{}{},
},
{
14,
&keyIndex{
key: []byte("foo"),
modified: revision{16, 0},
generations: []generation{
{created: revision{14, 0}, ver: 3, revs: []revision{{main: 14, sub: 1}, {main: 16}}},
{},
},
},
map[revision]struct{}{
revision{main: 14, sub: 1}: {},
},
},
{
15,
&keyIndex{
key: []byte("foo"),
modified: revision{16, 0},
generations: []generation{
{created: revision{14, 0}, ver: 3, revs: []revision{{main: 14, sub: 1}, {main: 16}}},
{},
},
},
map[revision]struct{}{
revision{main: 14, sub: 1}: {},
},
},
{
16,
&keyIndex{
key: []byte("foo"),
modified: revision{16, 0},
generations: []generation{
{},
},
},
map[revision]struct{}{},
},
Expand Down Expand Up @@ -513,12 +594,13 @@ func TestGenerationWalk(t *testing.T) {
}

func newTestKeyIndex() *keyIndex {
// key: "foo"
// rev: 12
// key: "foo"
// rev: 16
// generations:
// {empty}
// {8[1], 10[2], 12(t)[3]}
// {2[1], 4[2], 6(t)[3]}
// {{14, 0}[1], {14, 1}[2], {16, 0}(t)[3]}
// {{8, 0}[1], {10, 0}[2], {12, 0}(t)[3]}
// {{2, 0}[1], {4, 0}[2], {6, 0}(t)[3]}

ki := &keyIndex{key: []byte("foo")}
ki.put(2, 0)
Expand All @@ -527,5 +609,8 @@ func newTestKeyIndex() *keyIndex {
ki.put(8, 0)
ki.put(10, 0)
ki.tombstone(12, 0)
ki.put(14, 0)
ki.put(14, 1)
ki.tombstone(16, 0)
return ki
}

0 comments on commit be80d11

Please sign in to comment.