-
Notifications
You must be signed in to change notification settings - Fork 452
/
flush_test.go
117 lines (102 loc) · 2.76 KB
/
flush_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2018 The LevelDB-Go and Pebble Authors. All rights reserved. Use
// of this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
package pebble
import (
"fmt"
"testing"
"time"
"github.com/cockroachdb/datadriven"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/pebble/vfs"
"github.com/stretchr/testify/require"
)
func TestManualFlush(t *testing.T) {
getOptions := func() *Options {
opts := &Options{
FS: vfs.NewMem(),
L0CompactionThreshold: 10,
}
opts.DisableAutomaticCompactions = true
return opts
}
d, err := Open("", getOptions())
require.NoError(t, err)
defer func() {
require.NoError(t, d.Close())
}()
datadriven.RunTest(t, "testdata/manual_flush", func(t *testing.T, td *datadriven.TestData) string {
switch td.Cmd {
case "batch":
b := d.NewBatch()
if err := runBatchDefineCmd(td, b); err != nil {
return err.Error()
}
b.Commit(nil)
return ""
case "flush":
if err := d.Flush(); err != nil {
return err.Error()
}
d.mu.Lock()
s := d.mu.versions.currentVersion().String()
d.mu.Unlock()
return s
case "async-flush":
d.mu.Lock()
cur := d.mu.versions.currentVersion()
d.mu.Unlock()
if _, err := d.AsyncFlush(); err != nil {
return err.Error()
}
err := try(100*time.Microsecond, 20*time.Second, func() error {
d.mu.Lock()
defer d.mu.Unlock()
if cur == d.mu.versions.currentVersion() {
return errors.New("flush has not occurred")
}
return nil
})
if err != nil {
return err.Error()
}
d.mu.Lock()
s := d.mu.versions.currentVersion().String()
d.mu.Unlock()
return s
case "reset":
if err := d.Close(); err != nil {
return err.Error()
}
d, err = Open("", getOptions())
if err != nil {
return err.Error()
}
return ""
default:
return fmt.Sprintf("unknown command: %s", td.Cmd)
}
})
}
// TestFlushDelRangeEmptyKey tests flushing a range tombstone that begins with
// an empty key. The empty key is a valid key but can be confused with nil.
func TestFlushDelRangeEmptyKey(t *testing.T) {
d, err := Open("", &Options{FS: vfs.NewMem()})
require.NoError(t, err)
require.NoError(t, d.DeleteRange([]byte{}, []byte("z"), nil))
require.NoError(t, d.Flush())
require.NoError(t, d.Close())
}
// TestFlushEmptyKey tests that flushing an empty key does not trigger that key
// order invariant assertions.
func TestFlushEmptyKey(t *testing.T) {
d, err := Open("", &Options{FS: vfs.NewMem()})
require.NoError(t, err)
require.NoError(t, d.Set(nil, []byte("hello"), nil))
require.NoError(t, d.Flush())
val, closer, err := d.Get(nil)
require.NoError(t, err)
require.Equal(t, val, []byte("hello"))
require.NoError(t, closer.Close())
require.NoError(t, d.Close())
}