forked from cosmos/iavl
-
Notifications
You must be signed in to change notification settings - Fork 3
/
batch_test.go
70 lines (58 loc) · 1.52 KB
/
batch_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
package iavl
import (
"encoding/binary"
"fmt"
"os"
"path/filepath"
"testing"
dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/require"
)
func cleanupDBDir(dir, name string) {
err := os.RemoveAll(filepath.Join(dir, name) + ".db")
if err != nil {
panic(err)
}
}
var bytesArrayOfSize10KB = [10000]byte{}
func makeKey(n uint16) []byte {
key := make([]byte, 2)
binary.BigEndian.PutUint16(key, n)
return key
}
func TestBatchWithFlusher(t *testing.T) {
testedBackends := []dbm.BackendType{
dbm.GoLevelDBBackend,
}
for _, backend := range testedBackends {
testBatchWithFlusher(t, backend)
}
}
func testBatchWithFlusher(t *testing.T, backend dbm.BackendType) {
name := fmt.Sprintf("test_%x", randstr(12))
dir := t.TempDir()
db, err := dbm.NewDB(name, backend, dir)
require.NoError(t, err)
defer cleanupDBDir(dir, name)
batchWithFlusher := NewBatchWithFlusher(db, DefaultOptions().FlushThreshold)
// we'll try to to commit 10MBs (1000 * 10KBs each entries) of data into the db
for keyNonce := uint16(0); keyNonce < 1000; keyNonce++ {
// each value is 10 KBs of zero bytes
key := makeKey(keyNonce)
err := batchWithFlusher.Set(key, bytesArrayOfSize10KB[:])
if err != nil {
panic(err)
}
}
require.NoError(t, batchWithFlusher.Write())
itr, err := db.Iterator(nil, nil)
require.NoError(t, err)
var keyNonce uint16
for itr.Valid() {
expectedKey := makeKey(keyNonce)
require.Equal(t, expectedKey, itr.Key())
require.Equal(t, bytesArrayOfSize10KB[:], itr.Value())
itr.Next()
keyNonce++
}
}