-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting.go
110 lines (76 loc) · 1.92 KB
/
testing.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
package blob
// Eventually this will be deprecated and all of these methods will be publicly
// available in go-cache/testing.go
import (
"bytes"
"context"
"fmt"
"github.com/whosonfirst/go-cache"
"io"
_ "log"
)
type testCacheOptions struct {
AllowCacheMiss bool
}
func testCache(ctx context.Context, c cache.Cache, opts *testCacheOptions) error {
k := "test"
v := "test"
fh1, err := cache.ReadSeekCloserFromString(v)
if err != nil {
return fmt.Errorf("Failed to create ReadSeekCloser, %v", err)
}
fh2, err := c.Set(ctx, k, fh1)
if err != nil {
return fmt.Errorf("Failed to set %s (%s), %v", k, v, err)
}
equals, err := compareReadSeekClosers(fh1, fh2)
if err != nil {
return fmt.Errorf("Failed to compare filehandles, %v", err)
}
if !equals {
return fmt.Errorf("Filehandles 1 and 2 failed equality test")
}
fh3, err := c.Get(ctx, k)
if err != nil && !cache.IsCacheMiss(err) {
return fmt.Errorf("Failed to get cache value for '%s', %v", k, err)
}
if err != nil {
if !opts.AllowCacheMiss {
return fmt.Errorf("Unexpected cache miss for '%s'", k)
}
} else {
equals, err = compareReadSeekClosers(fh1, fh3)
if err != nil {
return fmt.Errorf("Failed to compare filehandles 1 and 3, %v", err)
}
if !equals {
return fmt.Errorf("Filehandles 1 and 3 failed equality test")
}
}
return nil
}
func compareReadSeekClosers(fh1 io.ReadSeekCloser, fh2 io.ReadSeekCloser) (bool, error) {
fh1.Seek(0, 0)
fh2.Seek(0, 0)
defer func() {
fh1.Seek(0, 0)
fh2.Seek(0, 0)
}()
b1, err := io.ReadAll(fh1)
if err != nil {
return false, fmt.Errorf("Failed to read fh1, %v", err)
}
fh1.Seek(0, 0)
b2, err := io.ReadAll(fh2)
if err != nil {
return false, fmt.Errorf("Failed to read fh2, %v", err)
}
fh2.Seek(0, 0)
// log.Printf("1 '%s'\n", string(b1))
// log.Printf("2 '%s'\n", string(b2))
equals := bytes.Compare(b1, b2)
if equals != 0 {
return false, nil
}
return true, nil
}