-
Notifications
You must be signed in to change notification settings - Fork 336
/
recovery_reader_test.go
68 lines (55 loc) · 1.63 KB
/
recovery_reader_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
package nutsdb
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"os"
"testing"
)
func Test_readEntry(t *testing.T) {
path := "/tmp/test_read_entry"
fd, err := os.OpenFile(path, os.O_TRUNC|os.O_CREATE|os.O_RDWR, os.ModePerm)
require.NoError(t, err)
meta := NewMetaData().WithKeySize(uint32(len("key"))).
WithValueSize(uint32(len("val"))).WithTimeStamp(1547707905).
WithTTL(Persistent).WithFlag(DataSetFlag).WithBucketId(1)
expect := NewEntry().WithKey([]byte("key")).WithMeta(meta).WithValue([]byte("val"))
_, err = fd.Write(expect.Encode())
require.NoError(t, err)
f, err := newFileRecovery(path, 4096)
require.NoError(t, err)
entry, err := f.readEntry(0)
require.NoError(t, err)
assert.Equal(t, expect.Encode(), entry.Encode())
err = fd.Close()
require.NoError(t, err)
}
func Test_fileRecovery_readBucket(t *testing.T) {
filePath := "bucket_test_data"
bucket := &Bucket{
Meta: &BucketMeta{
Op: BucketInsertOperation,
},
Id: 1,
Ds: DataStructureBTree,
Name: "bucket_1",
}
bytes := bucket.Encode()
fd, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, os.ModePerm)
defer func() {
err = fd.Close()
assert.Nil(t, err)
err = os.Remove(filePath)
assert.Nil(t, nil)
}()
assert.Nil(t, err)
_, err = fd.Write(bytes)
assert.Nil(t, err)
fr, err := newFileRecovery(filePath, 4*MB)
assert.Nil(t, err)
readBucket, err := fr.readBucket()
assert.Nil(t, err)
assert.Equal(t, readBucket.Meta.Op, BucketInsertOperation)
assert.Equal(t, int64(8+2+8), int64(readBucket.Meta.Size))
assert.Equal(t, BucketId(1), readBucket.Id)
assert.Equal(t, readBucket.Name, "bucket_1")
}