-
Notifications
You must be signed in to change notification settings - Fork 14
/
slow_wrapper.go
77 lines (68 loc) · 1.76 KB
/
slow_wrapper.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
package storage
import (
"context"
"io"
"time"
)
// NewSlowWrapper creates an artificially slow FS.
// Probably only useful for testing.
func NewSlowWrapper(fs FS, readDelay time.Duration, writeDelay time.Duration) FS {
return &slowWrapper{
fs: fs,
readDelay: readDelay,
writeDelay: writeDelay,
}
}
type slowWrapper struct {
fs FS
readDelay time.Duration
writeDelay time.Duration
}
func (fs *slowWrapper) Open(ctx context.Context, path string, options *ReaderOptions) (*File, error) {
select {
case <-time.After(fs.readDelay):
return fs.fs.Open(ctx, path, options)
case <-ctx.Done():
return nil, ctx.Err()
}
}
func (fs *slowWrapper) Walk(ctx context.Context, path string, fn WalkFn) error {
select {
case <-time.After(fs.readDelay):
return fs.fs.Walk(ctx, path, fn)
case <-ctx.Done():
return ctx.Err()
}
}
func (fs *slowWrapper) Attributes(ctx context.Context, path string, options *ReaderOptions) (*Attributes, error) {
select {
case <-time.After(fs.readDelay):
return fs.fs.Attributes(ctx, path, options)
case <-ctx.Done():
return nil, ctx.Err()
}
}
func (fs *slowWrapper) Create(ctx context.Context, path string, options *WriterOptions) (io.WriteCloser, error) {
select {
case <-time.After(fs.writeDelay):
return fs.fs.Create(ctx, path, options)
case <-ctx.Done():
return nil, ctx.Err()
}
}
func (fs *slowWrapper) Delete(ctx context.Context, path string) error {
select {
case <-time.After(fs.writeDelay):
return fs.fs.Delete(ctx, path)
case <-ctx.Done():
return ctx.Err()
}
}
func (fs *slowWrapper) URL(ctx context.Context, path string, options *SignedURLOptions) (string, error) {
select {
case <-time.After(fs.readDelay):
return fs.fs.URL(ctx, path, options)
case <-ctx.Done():
return "", ctx.Err()
}
}