forked from docker-archive/deploykit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazy_test.go
236 lines (185 loc) · 5.35 KB
/
lazy_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
package group
import (
"errors"
"fmt"
"reflect"
"testing"
"time"
"github.com/docker/infrakit/pkg/spi/group"
"github.com/docker/infrakit/pkg/spi/instance"
"github.com/stretchr/testify/require"
)
func TestLazyBlockAndCancel(t *testing.T) {
called := make(chan struct{})
g := LazyConnect(func() (group.Plugin, error) {
close(called)
return nil, fmt.Errorf("boom")
}, 100*time.Second)
errs := make(chan error, 1)
go func() {
_, err := g.DescribeGroup(group.ID("test"))
errs <- err
close(errs)
}()
<-called
CancelWait(g)
require.Equal(t, "cancelled", (<-errs).Error())
}
func TestLazyNoBlock(t *testing.T) {
called := make(chan struct{})
g := LazyConnect(func() (group.Plugin, error) {
close(called)
return nil, fmt.Errorf("boom")
}, 0)
errs := make(chan error, 1)
go func() {
_, err := g.DescribeGroup(group.ID("test"))
errs <- err
close(errs)
}()
<-called
require.Equal(t, "boom", (<-errs).Error())
}
type fake chan []interface{}
func (f fake) CommitGroup(grp group.Spec, pretend bool) (string, error) {
f <- []interface{}{group.Plugin.CommitGroup, grp, pretend}
return "", nil
}
func (f fake) FreeGroup(id group.ID) error {
f <- []interface{}{group.Plugin.FreeGroup, id}
return nil
}
func (f fake) DescribeGroup(id group.ID) (group.Description, error) {
f <- []interface{}{group.Plugin.DescribeGroup, id}
return group.Description{}, nil
}
func (f fake) DestroyGroup(id group.ID) error {
f <- []interface{}{group.Plugin.DestroyGroup, id}
return nil
}
func (f fake) InspectGroups() ([]group.Spec, error) {
f <- []interface{}{group.Plugin.InspectGroups}
return nil, nil
}
func (f fake) DestroyInstances(id group.ID, sub []instance.ID) error {
f <- []interface{}{group.Plugin.DestroyInstances, id, sub}
return nil
}
func (f fake) Size(id group.ID) (int, error) {
f <- []interface{}{group.Plugin.Size, id}
return 100, nil
}
func (f fake) SetSize(id group.ID, sz int) error {
f <- []interface{}{group.Plugin.SetSize, id, sz}
return nil
}
func checkCalls(t *testing.T, ch chan []interface{}, args ...interface{}) {
found := <-ch
for i, a := range args {
if reflect.ValueOf(a) != reflect.ValueOf(found[i]) {
if a != found[i] {
t.Fatal("Not equal:", found[i], "vs", a)
}
}
}
}
func TestLazyNoBlockConnect(t *testing.T) {
called := make(chan struct{})
called2 := make(chan []interface{}, 2)
g := LazyConnect(func() (group.Plugin, error) {
close(called)
return fake(called2), nil
}, 0)
errs := make(chan error, 1)
go func() {
_, err := g.DescribeGroup(group.ID("test"))
errs <- err
close(errs)
g.Size(group.ID("test"))
close(called2)
}()
<-called
require.NoError(t, <-errs)
checkCalls(t, called2, group.Plugin.DescribeGroup, group.ID("test"))
checkCalls(t, called2, group.Plugin.Size, group.ID("test"))
}
func TestErrorOnCallsToNilPlugin(t *testing.T) {
errMessage := "no-plugin"
proxy := LazyConnect(func() (group.Plugin, error) {
return nil, errors.New(errMessage)
}, 0)
err := proxy.FreeGroup(group.ID("test"))
require.Error(t, err)
require.Equal(t, errMessage, err.Error())
}
type fakeGroupPlugin struct {
group.Plugin
commitGroup func(grp group.Spec, pretend bool) (string, error)
freeGroup func(id group.ID) error
}
func (f *fakeGroupPlugin) CommitGroup(grp group.Spec, pretend bool) (string, error) {
return f.commitGroup(grp, pretend)
}
func (f *fakeGroupPlugin) FreeGroup(id group.ID) error {
return f.freeGroup(id)
}
func TestDelayPluginLookupCallingMethod(t *testing.T) {
called := false
fake := &fakeGroupPlugin{
commitGroup: func(grp group.Spec, pretend bool) (string, error) {
called = true
require.Equal(t, group.Spec{ID: "foo"}, grp)
require.Equal(t, true, pretend)
return "some-response", nil
},
}
proxy := LazyConnect(func() (group.Plugin, error) { return fake, nil }, 0)
require.False(t, called)
actualStr, actualErr := proxy.CommitGroup(group.Spec{ID: "foo"}, true)
require.True(t, called)
require.NoError(t, actualErr)
require.Equal(t, "some-response", actualStr)
}
func TestDelayPluginLookupCallingMethodReturnsError(t *testing.T) {
called := false
fake := &fakeGroupPlugin{
freeGroup: func(id group.ID) error {
called = true
require.Equal(t, group.ID("foo"), id)
return errors.New("can't-free")
},
}
proxy := LazyConnect(func() (group.Plugin, error) { return fake, nil }, 0)
require.False(t, called)
actualErr := proxy.FreeGroup(group.ID("foo"))
require.True(t, called)
require.Error(t, actualErr)
require.Equal(t, "can't-free", actualErr.Error())
}
func TestDelayPluginLookupCallingMultipleMethods(t *testing.T) {
called := false
fake := &fakeGroupPlugin{
commitGroup: func(grp group.Spec, pretend bool) (string, error) {
called = true
require.Equal(t, group.Spec{ID: "foo"}, grp)
require.Equal(t, true, pretend)
return "some-response", nil
},
freeGroup: func(id group.ID) error {
called = true
require.Equal(t, group.ID("foo"), id)
return errors.New("can't-free")
},
}
proxy := LazyConnect(func() (group.Plugin, error) { return fake, nil }, 0)
require.False(t, called)
actualStr, actualErr := proxy.CommitGroup(group.Spec{ID: "foo"}, true)
require.True(t, called)
require.NoError(t, actualErr)
require.Equal(t, "some-response", actualStr)
called = false
actualErr = proxy.FreeGroup(group.ID("foo"))
require.True(t, called)
require.Error(t, actualErr)
require.Equal(t, "can't-free", actualErr.Error())
}