-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrecord_test.go
147 lines (128 loc) · 3.57 KB
/
record_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package bitcask
import (
"testing"
"time"
"github.com./stretchr/testify/assert"
)
func TestRecord_EmptyNs(t *testing.T) {
// mock global options
oldOpts := gOpts
gOpts = &Options{
NsSize: 0,
EtagSize: 0,
}
defer func() {
gOpts = oldOpts
}()
// testcase
record := &Record{
Ns: nil,
Key: []byte("test-key"),
Value: []byte("test-value"),
Meta: NewMeta(nil),
}
backStore := make([]byte, DefaultRecordBufferSize)
baseTime := uint64(time.Now().Unix())
encoded, err := record.Encode(backStore, baseTime)
assert.Nil(t, err)
decoded, err := RecordFromBytes(encoded, baseTime)
assert.Nil(t, err)
// check
assert.Equal(t, len(decoded.Ns), 0)
assert.Equal(t, decoded.Key, record.Key)
assert.Equal(t, decoded.Value, record.Value)
}
func TestRecord_EncodingDecoding(t *testing.T) {
// mock global options
oldOpts := gOpts
gOpts = &Options{
NsSize: DefaultNsSize,
EtagSize: DefaultEtagSize,
}
defer func() {
gOpts = oldOpts
}()
// testcase
ns := sha1Bytes("test-ns")
etag := sha1Bytes("etag")
baseTime := uint64(time.Now().Unix())
tests := []struct {
name string
record *Record
wantErr bool
}{
{
name: "Normal case with full metadata and value",
record: &Record{
Ns: ns[:],
Key: []byte("test-key"),
Meta: NewMeta(map[string]string{"foo": "bar"}).SetExpire(baseTime + 60).SetEtag(etag[:]).SetTombstone(true),
Value: []byte("hello world"),
},
wantErr: false,
},
{
name: "Nil APP Meta",
record: &Record{
Ns: ns[:],
Key: []byte("test-key"),
Meta: NewMeta(nil).SetExpire(baseTime + 61).SetEtag(etag[:]).SetTombstone(false),
Value: []byte("hello world"),
},
wantErr: false,
},
{
name: "Nil Value",
record: &Record{
Ns: ns[:],
Key: []byte("test-key"),
Meta: NewMeta(map[string]string{"foo": "bar"}).SetExpire(baseTime + 62).SetEtag(etag[:]).SetTombstone(true),
Value: []byte{},
},
wantErr: false,
},
{
name: "Nil APP Meta, Nil Value, Nil Etag and No Expire",
record: &Record{
Ns: ns[:],
Key: []byte("test-key"),
Meta: NewMeta(nil),
Value: []byte{},
},
wantErr: false,
},
}
backStore := make([]byte, DefaultRecordBufferSize)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
encoded, err := tt.record.Encode(backStore, baseTime)
if (err != nil) != tt.wantErr {
t.Errorf("Encode() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr {
return // No need to proceed if encoding is expected to fail
}
decodedRecord, err := RecordFromBytes(encoded, baseTime)
assert.NoError(t, err, "RecordFromBytes should not fail")
// Ensure Namespace is correctly restored
assert.Equal(t, tt.record.Ns, decodedRecord.Ns, "Namespace mismatch")
// Ensure Key is correctly restored
assert.Equal(t, tt.record.Key, decodedRecord.Key, "Key mismatch")
// Ensure Value is correctly restored
assert.Equal(t, tt.record.Value, decodedRecord.Value, "Value mismatch")
// Check Meta field
assert.NotNil(t, decodedRecord.Meta, "Meta should not be nil")
assert.Equal(t, tt.record.Meta.Flags, decodedRecord.Meta.Flags, "Flags mismatch")
assert.Equal(t, tt.record.Meta.Expire, decodedRecord.Meta.Expire, "Expire mismatch")
if len(tt.record.Meta.Etag) != 0 {
assert.Equal(t, tt.record.Meta.Etag, decodedRecord.Meta.Etag, "Etag mismatch")
}
if tt.record.Meta.AppMeta == nil {
assert.Nil(t, decodedRecord.Meta.AppMeta, "AppMeta should be nil")
} else {
assert.Equal(t, tt.record.Meta.AppMeta, decodedRecord.Meta.AppMeta, "AppMeta mismatch")
}
})
}
}