-
Notifications
You must be signed in to change notification settings - Fork 3
/
bond_test.go
124 lines (101 loc) · 3.09 KB
/
bond_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
118
119
120
121
122
123
124
package bond
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/cockroachdb/pebble"
"github.com/stretchr/testify/require"
)
const dbName = "test_db"
func setupDatabase(serializer ...Serializer[any]) DB {
return setupDB(dbName, serializer...)
}
func setupDB(name string, serializer ...Serializer[any]) DB {
options := &Options{}
if len(serializer) > 0 && serializer[0] != nil {
options.Serializer = serializer[0]
}
db, err := Open(name, options)
if err != nil {
panic(err)
}
return db
}
func tearDownDatabase(db DB) {
tearDownDB(dbName, db)
}
func tearDownDB(name string, db DB) {
// NOTE: we intentionally panic on Close here, as it will catch any
// leaking iterators which would otherwise be difficult to debug,
// and are relevant to ensure we close.
err := db.Close()
if err != nil {
panic(err)
}
err = os.RemoveAll(name)
if err != nil {
panic(err)
}
}
func TestBond_Open(t *testing.T) {
db, err := Open(dbName, &Options{})
defer func() { _ = os.RemoveAll(dbName) }()
require.NoError(t, err)
require.NotNil(t, db)
err = db.Close()
require.NoError(t, err)
}
func TestBond_VersionCheck(t *testing.T) {
defer func() { _ = os.RemoveAll(dbName) }()
pebbleOpts := DefaultPebbleOptions()
pebbleOpts.FormatMajorVersion = pebble.FormatPrePebblev1MarkedCompacted
opts := DefaultOptions()
opts.PebbleOptions = pebbleOpts
db, err := Open(dbName, opts)
require.NoError(t, err)
err = db.Close()
require.NoError(t, err)
// simluate the db where VERSION file don't exist.
pebbelVersionPath := filepath.Join(dbName, "bond", PebbleFormatFile)
err = os.Remove(pebbelVersionPath)
require.NoError(t, err)
// opening db should create a version file
db, err = Open(dbName, opts)
require.NoError(t, err)
err = db.Close()
require.NoError(t, err)
_, err = os.Stat(pebbelVersionPath)
require.NoError(t, err)
buf, err := os.ReadFile(pebbelVersionPath)
require.NoError(t, err)
require.Equal(t, fmt.Sprintf("%d", opts.PebbleOptions.FormatMajorVersion), string(buf))
// rewrite the version with some other version.
err = os.Remove(pebbelVersionPath)
require.NoError(t, err)
err = os.WriteFile(pebbelVersionPath,
[]byte(fmt.Sprintf("%d", opts.PebbleOptions.FormatMajorVersion-1)), os.ModePerm)
require.NoError(t, err)
// throw an error since db is written in different version.
_, err = Open(dbName, opts)
require.Error(t, err)
}
func Test_BondVersionMigrate(t *testing.T) {
defer func() { _ = os.RemoveAll(dbName) }()
pebbleOpts := DefaultPebbleOptions()
pebbleOpts.FormatMajorVersion = pebble.FormatPrePebblev1MarkedCompacted
opts := DefaultOptions()
opts.PebbleOptions = pebbleOpts
db, err := Open(dbName, opts)
require.NoError(t, err)
err = db.Close()
require.NoError(t, err)
version, err := PebbleFormatVersion(dbName)
require.NoError(t, err)
require.Equal(t, uint64(pebble.FormatPrePebblev1MarkedCompacted), uint64(version))
err = MigratePebbleFormatVersion(dbName, uint64(pebble.FormatVirtualSSTables))
require.NoError(t, err)
version, err = PebbleFormatVersion(dbName)
require.NoError(t, err)
require.Equal(t, uint64(pebble.FormatVirtualSSTables), uint64(version))
}