This repository has been archived by the owner on Jan 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
chunkaligned_test.go
175 lines (148 loc) · 3.7 KB
/
chunkaligned_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
// Copyright 2015 Letv Cloud Computing. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package chunkaligned
import (
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"reflect"
"strings"
"testing"
)
func TestNewChunkAlignedReaderAt(t *testing.T) {
s := "0123" + "4567" + "8"
sr := io.NewSectionReader(strings.NewReader(s), 0, int64(len(s)))
r, err := NewChunkAlignedReaderAt(sr, 4)
if err != nil {
t.Fatalf("NewChunkAlignedReaderAt: %v", err)
}
if r.Size() != int64(len(s)) {
t.Fatalf("Size() is %d, want %d", r.Size(), len(s))
}
buf := make([]byte, 9)
for i := 1; i < len(buf); i++ {
for off := 0; off < len(s); off++ {
if (i + off) > len(s) {
continue
}
readP := buf[:i]
_, err = r.ReadAt(readP, int64(off))
if err != nil {
t.Fatalf("r.ReadAt: %v", err)
}
if !reflect.DeepEqual(s[off:off+len(readP)], string(readP)) {
t.Errorf("Want %s, got %s", s[off:off+len(readP)], readP)
}
}
}
_, err = r.ReadAt(buf, int64(len(s)))
if err != io.EOF {
t.Fatalf("Want %v, got %v", io.EOF, err)
}
}
func TestIntergration(t *testing.T) {
tf, err := sizeTempFile(1 * 1024 * 1024)
if err != nil {
t.Errorf("sizeTempFile: %v", err)
return
}
defer os.Remove(tf.Name())
defer tf.Close()
fg, err := newFileGetter(tf.Name())
if err != nil {
t.Errorf("newFileGetter: %v", err)
return
}
defer fg.Close()
// testing with different combinations of chunkSize, bufSize and offset
for i := 0; i < 6; i++ {
chunkSize := 32 * 1024 * (1 << uint(i))
cara, err := NewChunkAlignedReaderAt(&fg, chunkSize)
if err != nil {
t.Errorf("NewChunkAlignedReaderAt: %v", err)
return
}
for j := 0; j < 4; j++ {
bufSize := 32 * 1024 * (1 << uint(j))
for k := 0; k < 100; k++ {
offset := rand.Int63n(fg.Size())
t.Logf("chunkSize: %d, bufSize: %d, offset: %d",
chunkSize, bufSize, offset)
wantN := bufSize
if fg.Size()-offset < int64(bufSize) {
wantN = int(fg.Size() - offset)
}
bufActual := make([]byte, wantN)
_, err = cara.ReadAt(bufActual, offset)
if err != nil {
t.Errorf("cara.ReadAt: %v", err)
return
}
bufExpected := make([]byte, wantN)
_, err = tf.ReadAt(bufExpected, offset)
if err != nil {
t.Errorf("tf.ReadAt: %v", err)
return
}
if !reflect.DeepEqual(bufActual, bufExpected) {
t.Errorf("ReadAt did not work properly")
return
}
}
}
}
}
func TestChunkSizeLimitExceeded(t *testing.T) {
s := "very large buffer"
r := io.NewSectionReader(strings.NewReader(s), 0, int64(len(s)))
_, err := NewChunkAlignedReaderAt(r, chunkSizeLimit+1)
if err.Error() != "chunkaligned: chunk size limit exceeded" {
t.Errorf("Limitting chunkSize didn't work properly")
}
}
type fileGetter struct {
size int64
file *os.File
}
func (fg *fileGetter) Size() int64 {
return fg.size
}
func (fg *fileGetter) ReadAt(p []byte, off int64) (n int, err error) {
return fg.file.ReadAt(p, off)
}
func (fg *fileGetter) Close() error {
return fg.file.Close()
}
func newFileGetter(path string) (fileGetter, error) {
file, err := os.Open(path)
if err != nil {
return fileGetter{}, err
}
fs, err := file.Stat()
if err != nil {
return fileGetter{}, err
}
return fileGetter{fs.Size(), file}, nil
}
func sizeTempFile(size int64) (f *os.File, err error) {
tf, err := ioutil.TempFile("", "_chunkaligned_")
if err != nil {
err = fmt.Errorf("TempFile: %v", err)
return
}
rf, err := os.Open("/dev/urandom")
if err != nil {
err = fmt.Errorf("Open /dev/urandom: %v", err)
return
}
defer rf.Close()
_, err = io.CopyN(tf, rf, size)
if err != nil {
err = fmt.Errorf("Failed to write content: %v", err)
return
}
return tf, nil
}