This repository has been archived by the owner on Oct 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
appender.go
179 lines (144 loc) · 3.89 KB
/
appender.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
package tests
import (
"bytes"
"crypto/sha256"
"io"
"io/ioutil"
"math/rand"
"testing"
"github.com/google/uuid"
. "github.com/smartystreets/goconvey/convey"
"github.com/beyondstorage/go-storage/v4/pairs"
"github.com/beyondstorage/go-storage/v4/pkg/randbytes"
"github.com/beyondstorage/go-storage/v4/types"
)
func TestAppender(t *testing.T, store types.Storager) {
Convey("Given a basic Storager", t, func() {
ap, ok := store.(types.Appender)
So(ok, ShouldBeTrue)
Convey("When CreateAppend", func() {
path := uuid.NewString()
o, err := ap.CreateAppend(path)
defer func() {
err := store.Delete(path)
if err != nil {
t.Error(err)
}
}()
Convey("The error should be nil", func() {
So(err, ShouldBeNil)
})
Convey("The Object Mode should be appendable", func() {
// Append object's mode must be appendable.
So(o.Mode.IsAppend(), ShouldBeTrue)
})
})
Convey("When CreateAppend with an existing object", func() {
path := uuid.NewString()
o, err := ap.CreateAppend(path)
defer func() {
err := store.Delete(path)
if err != nil {
t.Error(err)
}
}()
Convey("The first returned error should be nil", func() {
So(err, ShouldBeNil)
})
size := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
r := io.LimitReader(randbytes.NewRand(), size)
_, err = ap.WriteAppend(o, r, size)
if err != nil {
t.Fatal(err)
}
err = ap.CommitAppend(o)
if err != nil {
t.Fatal(err)
}
o, err = ap.CreateAppend(path)
Convey("The second returned error also should be nil", func() {
So(err, ShouldBeNil)
})
Convey("The Object Mode should be appendable", func() {
// Append object's mode must be appendable.
So(o.Mode.IsAppend(), ShouldBeTrue)
})
Convey("The object append offset should be 0", func() {
So(o.MustGetAppendOffset(), ShouldBeZeroValue)
})
})
Convey("When Delete", func() {
path := uuid.NewString()
_, err := ap.CreateAppend(path)
if err != nil {
t.Error(err)
}
err = store.Delete(path)
Convey("The first returned error should be nil", func() {
So(err, ShouldBeNil)
})
err = store.Delete(path)
Convey("The second returned error also should be nil", func() {
So(err, ShouldBeNil)
})
})
Convey("When WriteAppend", func() {
path := uuid.NewString()
o, err := ap.CreateAppend(path)
if err != nil {
t.Error(err)
}
defer func() {
err := store.Delete(path)
if err != nil {
t.Error(err)
}
}()
size := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
content, _ := ioutil.ReadAll(io.LimitReader(randbytes.NewRand(), size))
r := bytes.NewReader(content)
n, err := ap.WriteAppend(o, r, size)
Convey("WriteAppend error should be nil", func() {
So(err, ShouldBeNil)
})
Convey("WriteAppend size should be equal to n", func() {
So(n, ShouldEqual, size)
})
})
Convey("When CommitAppend", func() {
path := uuid.NewString()
o, err := ap.CreateAppend(path)
if err != nil {
t.Error(err)
}
defer func() {
err := store.Delete(path)
if err != nil {
t.Error(err)
}
}()
size := rand.Int63n(4 * 1024 * 1024) // Max file size is 4MB
content, _ := ioutil.ReadAll(io.LimitReader(randbytes.NewRand(), size))
_, err = ap.WriteAppend(o, bytes.NewReader(content), size)
if err != nil {
t.Error(err)
}
_, err = ap.WriteAppend(o, bytes.NewReader(content), size)
if err != nil {
t.Error(err)
}
err = ap.CommitAppend(o)
Convey("CommitAppend error should be nil", func() {
So(err, ShouldBeNil)
})
var buf bytes.Buffer
_, err = store.Read(path, &buf, pairs.WithSize(size*2))
Convey("Read error should be nil", func() {
So(err, ShouldBeNil)
})
Convey("The content should be match", func() {
So(sha256.Sum256(buf.Bytes()), ShouldResemble, sha256.Sum256(bytes.Repeat(content, 2)))
})
})
})
}