-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemrecorder_test.go
127 lines (103 loc) · 2.22 KB
/
memrecorder_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
package main
import (
"fmt"
"testing"
"time"
)
func TestMatchesInterface(t *testing.T) {
var _ Recorder = NewMemRecorder()
}
func TestSawImageTagAt(t *testing.T) {
r := NewMemRecorder()
tag := "flibbit"
now := time.Now()
then := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
r.SawImageTagAt(tag, then)
r.Sweep(func(_ string, w time.Time) bool {
if w != then {
t.Error("The timestamp wasn't stored for the tag")
}
return false
})
r.SawImageTagAt(tag, now)
r.Sweep(func(_ string, w time.Time) bool {
if w != now {
t.Errorf("The timestamp should be now, not %v", w)
}
return false
})
r.SawImageTagAt(tag, then)
r.Sweep(func(_ string, w time.Time) bool {
if w != now {
t.Errorf("The timestamp should remain now, not %v", w)
}
return false
})
}
func TestSawImageTag(t *testing.T) {
r := NewMemRecorder()
tag := "frobnitz"
r.SawImageTag(tag)
r.Sweep(func(found string, _ time.Time) bool {
if found != tag {
t.Error("I expected to find the tag")
}
return false
})
}
func TestForget(t *testing.T) {
r := NewMemRecorder()
expectedTag := "frobnitz"
unexpectedTag := "foobar"
r.SawImageTag(expectedTag)
r.SawImageTag(unexpectedTag)
r.Forget(unexpectedTag)
r.Sweep(func(found string, _ time.Time) bool {
if found != expectedTag {
t.Errorf("I expected to find the tag %v; found %v", expectedTag, found)
}
return false
})
}
func TestSweep(t *testing.T) {
r := NewMemRecorder()
tag := "frobnitz"
r.SawImageTag(tag)
r.Sweep(func(_ string, _ time.Time) bool {
return false
})
r.Sweep(func(found string, _ time.Time) bool {
if found != tag {
t.Error("I expected to find the tag")
}
return true
})
r.Sweep(func(_ string, _ time.Time) bool {
t.Error("I shouldn't find any tags")
return false
})
}
func TestSawAndSweep(t *testing.T) {
r := NewMemRecorder()
const iterations = 1000
// Writer 1
go func() {
for i := 0; i < iterations/2; i++ {
r.SawImageTag(fmt.Sprintf("foo%d", i))
}
}()
// Writer 2
go func() {
for i := 0; i < iterations/2; i++ {
r.SawImageTag(fmt.Sprintf("bar%d", i))
}
}()
// Reader/Deleter
go func() {
for i := 0; i < iterations; i++ {
r.Sweep(func(_ string, _ time.Time) bool {
return i%2 == 0
})
}
}()
}