-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunked_file_test.go
219 lines (207 loc) · 5.8 KB
/
chunked_file_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package chunked_file
import (
"bytes"
"io"
"math/rand"
"os"
"path/filepath"
"testing"
)
func TestChunkedReader(t *testing.T) {
r, e := NewChunkedReader("test_data/part1.txt", "test_data/part2.txt",
"test_data/part3.txt")
if e != nil {
t.Logf("Failed opening chunked reader: %s\n", e)
t.FailNow()
}
totalSize, e := r.Seek(0, io.SeekEnd)
if e != nil {
t.Logf("Failed seeking to end of chunked reader: %s\n", e)
t.FailNow()
}
if totalSize != 17 {
t.Logf("Got incorrect combined size: Expected %d bytes, got %d\n",
17, totalSize)
t.FailNow()
}
_, e = r.Seek(0, io.SeekStart)
if e != nil {
t.Logf("Failed rewinding to start of reader: %s\n", e)
t.FailNow()
}
// We'll intentionally oversize this buffer, to ensure that we only read
// the correct amount and get an EOF.
dstBuffer := [45]byte{}
_, e = r.Seek(3, io.SeekStart)
if e != nil {
t.Logf("Failed seeking to offset 3 in reader: %s\n", e)
t.FailNow()
}
bytesRead, e := r.Read(dstBuffer[0:5])
if e != nil {
t.Logf("Failed reading 5 bytes from reader: %s\n", e)
t.FailNow()
}
if bytesRead != 5 {
t.Logf("Incorrect amount returned by reader.Read: %d\n", bytesRead)
t.FailNow()
}
if string(dstBuffer[:5]) != "there" {
t.Logf("Incorrect Read result, got %s\n", dstBuffer[:5])
t.FailNow()
}
_, e = r.Seek(0, io.SeekStart)
if e != nil {
t.Logf("Failed rewinding (second time): %s\n", e)
t.FailNow()
}
bytesRead, e = r.Read(dstBuffer[:])
if e != io.EOF {
t.Logf("Didn't get expected EOF error.\n")
t.Fail()
if e != nil {
t.Logf("Got unexpected non-EOF error: %s\n", e)
t.FailNow()
}
}
t.Logf("Got expected EOF error: %s\n", e)
if int64(bytesRead) != totalSize {
t.Logf("Bad full read size. Expected %d, got %d\n", totalSize,
bytesRead)
t.FailNow()
}
if string(dstBuffer[:17]) != "Hi there, friend!" {
t.Logf("Got incorrect Read() content: %s\n", dstBuffer[:17])
t.FailNow()
}
}
// Used for cleaning up test data, doesn't cause failures itself, but will log
// any deletion errors that occur.
func deleteAllFiles(filenames []string, t *testing.T) {
for _, f := range filenames {
e := os.Remove(f)
if e != nil {
t.Logf("Failed deleting %s: %s\n", f, e)
}
}
}
// Opens, reads, and returns the content of the file at the given path. Causes
// a test failure on error. Closes the file before returning.
func getFileContent(path string, t *testing.T) []byte {
f, e := os.Open(path)
if e != nil {
t.Logf("Failed opening %s: %s\n", path, e)
t.FailNow()
}
toReturn, e := io.ReadAll(f)
if e != nil {
t.Logf("Failed reading %s: %s\n", path, e)
f.Close()
t.FailNow()
}
f.Close()
return toReturn
}
func TestChunkedWriter(t *testing.T) {
rng := rand.New(rand.NewSource(1337))
testData := make([]byte, 512)
pathPrefix := "test_data/test_output"
for i := range testData {
testData[i] = byte(rng.Int())
}
w, e := NewChunkedWriter(100, pathPrefix)
if e != nil {
t.Logf("Failed creating ChunkedWriter: %s\n", e)
t.FailNow()
}
offset, e := w.Seek(210, io.SeekStart)
if e != nil {
t.Logf("Failed seeking to offset 210: %s\n", e)
t.FailNow()
}
if offset != 210 {
t.Logf("Seek to offset 210 returned %d instead\n", offset)
t.FailNow()
}
bytesWritten, e := w.Write(testData)
if e != nil {
t.Logf("Failed writing test data: %s\n", e)
t.FailNow()
}
if bytesWritten != len(testData) {
t.Logf("Expected to write %d bytes, but Write() returned %d\n",
len(testData), bytesWritten)
t.FailNow()
}
filenames, e := filepath.Glob("test_data/test_output.part.*")
if e != nil {
t.Logf("Failed getting output filenames: %s\n", e)
t.FailNow()
}
if len(filenames) != 8 {
t.Logf("Expected to get 8 part files, but got %d\n", len(filenames))
deleteAllFiles(filenames, t)
t.FailNow()
}
// Make sure the last file wasn't padded, and that it contains the final
// bytes written.
fileContent := getFileContent(filenames[7], t)
if len(fileContent) != 22 {
t.Logf("Expected %s to contain 22 bytes, but got %d\n",
filenames[7], len(fileContent))
deleteAllFiles(filenames, t)
t.FailNow()
}
if !bytes.Equal(fileContent, testData[490:]) {
t.Logf("Didn't get expected test data content in last file. Expected"+
"'% x', got '% x'\n", testData[490:], fileContent)
t.FailNow()
}
// Make sure the second file only contains 0s
fileContent = getFileContent(filenames[1], t)
if len(fileContent) != 100 {
t.Logf("%d-byte file %s doens't match the chunk size of 100\n",
len(fileContent), filenames[1])
deleteAllFiles(filenames, t)
t.FailNow()
}
for i, b := range fileContent {
if b != 0 {
t.Logf("Found non-zero byte at offset %d of %s\n", i, filenames[1])
deleteAllFiles(filenames, t)
t.FailNow()
}
}
// Make sure the third file contains the content we expect, after the 10
// bytes of additional padding we inserted before it by the 210-byte seek.
fileContent = getFileContent(filenames[2], t)
for i, b := range fileContent[:10] {
if b != 0 {
t.Logf("Found non-zero byte at offset %d of %s\n", i, filenames[2])
deleteAllFiles(filenames, t)
t.FailNow()
}
}
if !bytes.Equal(fileContent[10:], testData[0:90]) {
t.Logf("First 90 bytes of test data don't match %s content (expected"+
"% x..., got % x...)\n", filenames[2], testData[0:10],
fileContent[10:20])
deleteAllFiles(filenames, t)
t.FailNow()
}
// Quick test to make sure we can't overwrite existing files with a new
// writer.
tmpWriter, _ := NewChunkedWriter(100, "test_data/test_output")
tmpWriter.OverwriteFiles = false
_, e = tmpWriter.Seek(1, io.SeekStart)
if e == nil {
t.Logf("Didn't get expected error when overwriting existing files\n")
// Remember to call deleteAllFiles if we ever change this to FailNow()
t.Fail()
}
t.Logf("Got expected error when overwriting existing files: %s\n", e)
tmpWriter.Close()
// Remove the remaining files.
deleteAllFiles(filenames, t)
tmpWriter = nil
}