-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsegment_integration_test.go
More file actions
87 lines (72 loc) · 2.16 KB
/
segment_integration_test.go
File metadata and controls
87 lines (72 loc) · 2.16 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
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
package blobcache
import (
"fmt"
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestCache_SegmentMode(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "segment-integration-*")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
// Create cache with segments and Bitcask index
cache, err := New(tmpDir)
require.NoError(t, err)
defer cache.Close()
// Write multiple keys
for i := 0; i < 10; i++ {
key := fmt.Appendf(nil, "key-%d", i)
value := make([]byte, 1024*1024) // 1MB each
for j := range value {
value[j] = byte((i + j) % 256)
}
require.NoError(t, cache.Put(key, value))
}
cache.Drain()
// Read them back and verify
for i := 0; i < 10; i++ {
key := fmt.Appendf(nil, "key-%d", i)
value, found := cache.Get(key)
require.True(t, found, "key-%d should be found", i)
// 1. Check size once
require.Equal(t, 1024*1024, len(value), "key-%d size mismatch", i)
// 2. Build the expected buffer once per key
expected := make([]byte, 1024*1024)
for j := range expected {
expected[j] = byte((i + j) % 256)
}
// 3. ONE assertion for the whole 1MB slice (Super fast)
require.Equal(t, expected, value, "key-%d data mismatch", i)
}
}
func TestCache_SegmentModeWithDirectIO(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "segment-directio-integration-*")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
// Create cache with segments, DirectIO, and Bitcask
cache, err := New(tmpDir)
require.NoError(t, err)
defer cache.Close()
// Write keys with various sizes (test alignment handling)
sizes := []int{1024, 5000, 1024 * 1024, 999, 4096}
for i, size := range sizes {
key := fmt.Appendf(nil, "key-%d", i)
value := make([]byte, size)
for j := range value {
value[j] = byte(j % 256)
}
require.NoError(t, cache.Put(key, value))
}
cache.Drain()
// Verify all keys
for i, size := range sizes {
key := fmt.Appendf(nil, "key-%d", i)
value, found := cache.Get(key)
require.True(t, found, "key-%d should be found", i)
require.Equal(t, size, len(value), "key-%d size mismatch", i)
// Verify data
for j := 0; j < len(value); j++ {
require.Equal(t, byte(j%256), value[j], "key-%d byte %d mismatch", i, j)
}
}
}