This repository has been archived by the owner on Feb 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
version_queue_test.go
256 lines (225 loc) · 7.26 KB
/
version_queue_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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package gps
import (
"fmt"
"testing"
)
// just need a ListVersions method
type fakeBridge struct {
*bridge
vl []Version
}
var fakevl = []Version{
NewVersion("v2.0.0").Is("200rev"),
NewVersion("v1.1.1").Is("111rev"),
NewVersion("v1.1.0").Is("110rev"),
NewVersion("v1.0.0").Is("100rev"),
NewBranch("master").Is("masterrev"),
}
func init() {
SortForUpgrade(fakevl)
}
func (fb *fakeBridge) ListVersions(id ProjectIdentifier) ([]PairedVersion, error) {
return nil, nil
}
func (fb *fakeBridge) listVersions(id ProjectIdentifier) ([]Version, error) {
// it's a fixture, we only ever do the one, regardless of id
return fb.vl, nil
}
type fakeFailBridge struct {
*bridge
}
var errVQ = fmt.Errorf("vqerr")
func (fb *fakeFailBridge) ListVersions(id ProjectIdentifier) ([]PairedVersion, error) {
return nil, nil
}
func (fb *fakeFailBridge) listVersions(id ProjectIdentifier) ([]Version, error) {
return nil, errVQ
}
func TestVersionQueueSetup(t *testing.T) {
id := ProjectIdentifier{ProjectRoot: ProjectRoot("foo")}.normalize()
// shouldn't even need to embed a real bridge
fb := &fakeBridge{vl: fakevl}
ffb := &fakeFailBridge{}
_, err := newVersionQueue(id, nil, nil, ffb)
if err == nil {
t.Error("Expected err when providing no prefv or lockv, and injected bridge returns err from ListVersions()")
}
vq, err := newVersionQueue(id, nil, nil, fb)
if err != nil {
t.Errorf("Unexpected err on vq create: %s", err)
} else {
if len(vq.pi) != 5 {
t.Errorf("Should have five versions from listVersions() when providing no prefv or lockv; got %v:\n\t%s", len(vq.pi), vq.String())
}
if !vq.allLoaded {
t.Errorf("allLoaded flag should be set, but wasn't")
}
if vq.prefv != nil || vq.lockv != nil {
t.Error("lockv and prefv should be nil")
}
if vq.current() != fakevl[0] {
t.Errorf("current should be head of fakevl (%s), got %s", fakevl[0], vq.current())
}
}
lockv := fakevl[0]
prefv := fakevl[1]
vq, err = newVersionQueue(id, lockv, nil, fb)
if err != nil {
t.Errorf("Unexpected err on vq create: %s", err)
} else {
if len(vq.pi) != 1 {
t.Errorf("Should have one version when providing only a lockv; got %v:\n\t%s", len(vq.pi), vq.String())
}
if vq.allLoaded {
t.Errorf("allLoaded flag should not be set")
}
if vq.lockv != lockv {
t.Errorf("lockv should be %s, was %s", lockv, vq.lockv)
}
if vq.current() != lockv {
t.Errorf("current should be lockv (%s), got %s", lockv, vq.current())
}
}
vq, err = newVersionQueue(id, nil, prefv, fb)
if err != nil {
t.Errorf("Unexpected err on vq create: %s", err)
} else {
if len(vq.pi) != 1 {
t.Errorf("Should have one version when providing only a prefv; got %v:\n\t%s", len(vq.pi), vq.String())
}
if vq.allLoaded {
t.Errorf("allLoaded flag should not be set")
}
if vq.prefv != prefv {
t.Errorf("prefv should be %s, was %s", prefv, vq.prefv)
}
if vq.current() != prefv {
t.Errorf("current should be prefv (%s), got %s", prefv, vq.current())
}
}
vq, err = newVersionQueue(id, lockv, prefv, fb)
if err != nil {
t.Errorf("Unexpected err on vq create: %s", err)
} else {
if len(vq.pi) != 2 {
t.Errorf("Should have two versions when providing both a prefv and lockv; got %v:\n\t%s", len(vq.pi), vq.String())
}
if vq.allLoaded {
t.Errorf("allLoaded flag should not be set")
}
if vq.prefv != prefv {
t.Errorf("prefv should be %s, was %s", prefv, vq.prefv)
}
if vq.lockv != lockv {
t.Errorf("lockv should be %s, was %s", lockv, vq.lockv)
}
if vq.current() != lockv {
t.Errorf("current should be lockv (%s), got %s", lockv, vq.current())
}
}
}
func TestVersionQueueAdvance(t *testing.T) {
fb := &fakeBridge{vl: fakevl}
id := ProjectIdentifier{ProjectRoot: ProjectRoot("foo")}.normalize()
// First with no prefv or lockv
vq, err := newVersionQueue(id, nil, nil, fb)
if err != nil {
t.Fatalf("Unexpected err on vq create: %s", err)
}
for k, v := range fakevl[1:] {
err = vq.advance(fmt.Errorf("advancment fail for %s", fakevl[k]))
if err != nil {
t.Errorf("error on advancing vq from %s to %s", fakevl[k], v)
break
}
if vq.current() != v {
t.Errorf("on advance() %v, current should be %s, got %s", k, v, vq.current())
}
}
if vq.isExhausted() {
t.Error("should not be exhausted until advancing 'past' the end")
}
if err = vq.advance(fmt.Errorf("final advance failure")); err != nil {
t.Errorf("should not error on advance, even past end, but got %s", err)
}
if !vq.isExhausted() {
t.Error("advanced past end, should now report exhaustion")
}
if vq.current() != nil {
t.Error("advanced past end, current should return nil")
}
// now, do one with both a prefv and lockv
lockv := fakevl[2]
prefv := fakevl[0]
vq, err = newVersionQueue(id, lockv, prefv, fb)
if vq.String() != "[v1.1.0, v2.0.0]" {
t.Error("stringifying vq did not have expected outcome, got", vq.String())
}
if vq.isExhausted() {
t.Error("can't be exhausted, we aren't even 'allLoaded' yet")
}
err = vq.advance(fmt.Errorf("dequeue lockv"))
if err != nil {
t.Error("unexpected error when advancing past lockv", err)
} else {
if vq.current() != prefv {
t.Errorf("current should be prefv (%s) after first advance, got %s", prefv, vq.current())
}
if len(vq.pi) != 1 {
t.Errorf("should have just prefv elem left in vq, but there are %v:\n\t%s", len(vq.pi), vq.String())
}
}
err = vq.advance(fmt.Errorf("dequeue prefv"))
if err != nil {
t.Error("unexpected error when advancing past prefv", err)
} else {
if !vq.allLoaded {
t.Error("allLoaded should now be true")
}
if len(vq.pi) != 3 {
t.Errorf("should have three remaining versions after removing prefv and lockv, but there are %v:\n\t%s", len(vq.pi), vq.String())
}
if vq.current() != fakevl[1] {
t.Errorf("current should be first elem of fakevl (%s) after advancing into all, got %s", fakevl[1], vq.current())
}
}
// make sure the queue ordering is still right even with a double-delete
vq.advance(nil)
if vq.current() != fakevl[3] {
t.Errorf("second elem after ListVersions() should be idx 3 of fakevl (%s), got %s", fakevl[3], vq.current())
}
vq.advance(nil)
if vq.current() != fakevl[4] {
t.Errorf("third elem after ListVersions() should be idx 4 of fakevl (%s), got %s", fakevl[4], vq.current())
}
vq.advance(nil)
if vq.current() != nil || !vq.isExhausted() {
t.Error("should be out of versions in the queue")
}
// Make sure we handle things correctly when listVersions adds nothing new
fb = &fakeBridge{vl: []Version{lockv, prefv}}
vq, err = newVersionQueue(id, lockv, prefv, fb)
vq.advance(nil)
vq.advance(nil)
if vq.current() != nil || !vq.isExhausted() {
t.Errorf("should have no versions left, as ListVersions() added nothing new, but still have %s", vq.String())
}
err = vq.advance(nil)
if err != nil {
t.Errorf("should be fine to advance on empty queue, per docs, but got err %s", err)
}
// Also handle it well when advancing calls ListVersions() and it gets an
// error
vq, err = newVersionQueue(id, lockv, nil, &fakeFailBridge{})
if err != nil {
t.Errorf("should not err on creation when preseeded with lockv, but got err %s", err)
}
err = vq.advance(nil)
if err == nil {
t.Error("advancing should trigger call to erroring bridge, but no err")
}
err = vq.advance(nil)
if err == nil {
t.Error("err should be stored for reuse on any subsequent calls")
}
}