-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage_cache_test.go
More file actions
58 lines (56 loc) · 1.37 KB
/
page_cache_test.go
File metadata and controls
58 lines (56 loc) · 1.37 KB
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
package sokv
import (
"github.com/stretchr/testify/require"
"testing"
)
func TestPageCache(t *testing.T) {
createReadTx := func() *txHeader {
return &txHeader{
seq: 0,
isRead: true,
isRollback: false,
isCommit: false,
}
}
createWriteTx := func() *txHeader {
return &txHeader{
seq: 1,
isRead: false,
isRollback: false,
isCommit: false,
}
}
readTx := createReadTx()
writeTx := createWriteTx()
c := newLFUCache(1024)
for i := 0; i < 1024; i++ {
c.putPage(readTx, uint64(i), cachePage{
txSeq: uint64(i),
data: make([]byte, i),
pgId: createPageIdFromUint64(uint64(i)),
})
}
for i := 0; i < 1024; i++ {
cacheVal, found := c.getPage(readTx, uint64(i))
require.True(t, found)
require.Equal(t, cacheVal.txSeq, uint64(i))
require.Equal(t, cacheVal.pgId.ToUint64(), uint64(i))
require.Equal(t, cacheVal.data, make([]byte, i))
}
c.createShadowPage(writeTx)
f, ok := c.getPage(writeTx, 1023)
require.True(t, ok)
c.opShadowPage(writeTx, f, func(p cachePage) (cachePage, bool) {
p.data[0] = 100
p.data[1] = 102
p.data[2] = 103
p.data[3] = 104
return p, true
})
readP, ok := c.getPage(readTx, 1023)
require.True(t, ok)
writeReadP, ok := c.getPage(writeTx, 1023)
require.True(t, ok)
require.Equal(t, readP.data[:4], []byte{0, 0, 0, 0})
require.Equal(t, writeReadP.data[:4], []byte{100, 102, 103, 104})
}