-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshard_test.go
186 lines (160 loc) · 5.04 KB
/
shard_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"testing"
)
func TestNewFile(t *testing.T) {
startApplication()
// Register on volume
b := datastore.NewBlock()
// Get shard
shard := b.DataShards[0]
// == FIRST FILE ==
// Fake file
fileBytes := []byte("Hello File")
fileMeta := newFileMeta("/images/robin/hello.txt")
// Add file
updatedFileMeta, err := shard.AddFile(fileMeta, fileBytes)
// No errors
if err != nil {
t.Error("Failed to add file")
}
// Validate size
if updatedFileMeta.Size != uint32(len(fileBytes)) {
t.Error("Failed size validation")
}
// Validate offset (first file in test)
if updatedFileMeta.StartOffset != 0 {
t.Error("Start offset must be 0 for first file")
}
// == SECOND FILE ==
// Fake another file
fileBytes2 := []byte("Hello number two")
fileMeta2 := newFileMeta("/images/robin/hello2.txt")
// Add another file
updatedFileMeta2, err2 := shard.AddFile(fileMeta2, fileBytes2)
if err2 != nil {
t.Error(err2)
}
// Offset of file 2 should be after file 1
if updatedFileMeta2.StartOffset != uint32(len(fileBytes)) {
t.Error("Should start after file 1")
}
// Validate shard meta
if shard.FileCount() != 2 {
t.Error("Shard should contain 2 files")
}
// Validate shard index
if shard.shardIndex.Test("/non-existing") == true {
t.Error("Shard index false positive")
}
if shard.shardIndex.Test(fileMeta.FullName) == false {
t.Error("Shard index does not contain file 1")
}
if shard.TestContainsFile(fileMeta.FullName) == false {
t.Error("Shard index (via shard) does not contain file 1")
}
if shard.shardIndex.Test(fileMeta2.FullName) == false {
t.Error("Shard index does not contain file 2")
}
// Validate shard meta (de)serialization
shardFileMetaBytes := shard.shardFileMeta.Bytes()
if len(shardFileMetaBytes) < 1 {
t.Error("Shard file meta bytes must be non-zero")
}
// Load these bytes into new to confirm it's still intact
newShardFileMetaInstance := newShardFileMeta()
if len(newShardFileMetaInstance.FileMeta) != 0 {
t.Error("Empty shard file meta must be empty")
}
newShardFileMetaInstance.FromBytes(shardFileMetaBytes)
if len(newShardFileMetaInstance.FileMeta) != 2 {
t.Error("Loaded shard file meta must be 2")
}
// Read file by name (from memory)
fileAbytes, fileAerr, fileAfromMemory := shard.ReadFile(fileMeta.FullName)
if fileAfromMemory == false {
t.Error("File must be read from memory buffers here")
}
if fileAerr != nil {
t.Errorf("Unexpected error while reading file: %s", fileAerr)
}
if fileAbytes == nil || len(fileAbytes) < 1 {
t.Error("No bytes from read file")
}
if string(fileAbytes) != "Hello File" {
t.Error("Read file contents are not correct")
}
// Persist shard to disk
shard.Persist()
// Test contains file after persist
if shard.TestContainsFile(fileMeta.FullName) == false {
t.Error("Shard index (via shard) does not contain file 1 after persist")
}
// Reset pointers in this shard
shard.contents = nil
shard.shardIndex = nil
shard.shardMeta = nil
shard.shardFileMeta = nil
// Force reload
shard.ResetLoaded()
// Read from disk
loadRes, loadErr := shard.Load()
if !loadRes {
t.Error("Failed to load shard")
}
if loadErr != nil {
t.Errorf("Failed to load shard: %s", loadErr)
}
// Validate (no need to check contents as those are not used directly)
if shard.shardIndex == nil {
t.Error("Failed to load shard index")
}
if shard.shardMeta == nil {
t.Error("Failed to load shard meta")
}
if shard.shardFileMeta == nil {
t.Error("Failed to load shard file meta")
}
// Functional validaton on the index
if shard.shardIndex.Test(fileMeta.FullName) == false {
t.Error("Shard index (after persist, nil, load) does not contain file 1")
}
// Function validation on shard meta
if shard.FileCount() != 2 {
t.Error("Shard (after persist, nil, load) should contain 2 files")
}
if shard.shardFileMeta.FileMeta[0].FullName != fileMeta.FullName {
t.Error("Failed to read file meta name after shard load")
}
if shard.shardFileMeta.FileMeta[0].Size < 1 {
t.Error("Failed to read file meta size after shard load")
}
if len(shard.shardFileMeta.FileMeta[0].Id) != 16 {
t.Error("Failed to read file meta id after shard load")
}
if shard.shardFileMeta.FileMeta[0].Created < 1 {
t.Error("Failed to read file meta created after shard load")
}
if shard.shardFileMeta.FileMeta[1].StartOffset < 1 {
t.Error("Failed to read file meta start offset after shard load")
}
// Read file by name (from disk)
fileAbytes, fileAerr, fileAfromMemory = shard.ReadFile(fileMeta.FullName)
if fileAfromMemory {
t.Error("File must be read from disk here")
}
if fileAerr != nil {
t.Errorf("Unexpected error while reading file: %s", fileAerr)
}
if fileAbytes == nil || len(fileAbytes) < 1 {
t.Error("No bytes from read file")
}
if string(fileAbytes) != "Hello File" {
t.Error("Read file contents are not correct")
}
// Read non-existing file
nonExistingBytes, nonExistingErr, _ := shard.ReadFile("/non-existing")
if nonExistingBytes != nil || len(nonExistingBytes) > 0 || nonExistingErr == nil {
t.Error("Reading non-existing file should throw error without bytes")
}
}