-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathpluginManagerSwapper_test.go
More file actions
257 lines (238 loc) · 6.83 KB
/
pluginManagerSwapper_test.go
File metadata and controls
257 lines (238 loc) · 6.83 KB
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
257
package hotswap
import (
"fmt"
"math/rand"
"strings"
"testing"
"time"
"github.com/edwingeng/hotswap/internal/hutils"
)
func newSwapper(pluginDir string, opts ...Option) *PluginManagerSwapper {
n := len(opts)
opts = append(opts[:n:n], WithFreeDelay(time.Second), WithExtensionNewer(nilNewer))
return NewPluginManagerSwapper(pluginDir, opts...)
}
func TestPluginManagerSwapper_LoadPlugins(t *testing.T) {
oldMinFreeDelay := minFreeDelay
minFreeDelay = time.Second
defer func() {
minFreeDelay = oldMinFreeDelay
}()
log := newScavenger()
swapper1 := newSwapper("/helloXYZ", WithLogger(log))
if _, err := swapper1.LoadPlugins(log); err == nil {
t.Fatal("LoadPlugins should fail when the plugin directory does not exist")
} else if !strings.Contains(err.Error(), "no such file or directory") {
t.Fatalf("unexpected error: %v", err)
}
if _, err := swapper1.Reload(log); err == nil {
t.Fatal("Reload should fail when it is called before a successful call of LoadPlugins")
} else if !strings.Contains(err.Error(), "no such file or directory") {
t.Fatalf("unexpected error: %v", err)
}
if swapper1.Current() != nil {
t.Fatal("swapper1.Current() != nil")
}
pluginNames := []string{"arya", "snow", "stubborn"}
outputDir := preparePluginGroup(t, nil, "LoadPlugins", pluginNames...)
swapper2 := newSwapper(outputDir, WithLogger(log))
prepareEnv(t, "")
details1, err := swapper2.LoadPlugins(log)
if err != nil {
t.Fatal(err)
}
for k, v := range details1 {
if v != "ok" {
t.Fatalf(`unexpected result. file: %s, result: %s`, k, v)
}
}
if swapper2.Current() == nil {
t.Fatal("swapper2.Current() == nil")
}
currentPlugins1 := swapper2.Current().Plugins()
if len(currentPlugins1) != 3 {
t.Fatal("len(currentPlugins1) != 3")
}
for _, p := range currentPlugins1 {
compileTimeString := ""
if err := p.Lookup("CompileTimeString", &compileTimeString); err != nil {
t.Fatal(err)
} else if compileTimeString != "" {
t.Fatal("unexpected CompileTimeString: " + compileTimeString)
}
switch p.Name {
case "arya", "snow", "stubborn":
default:
panic("impossible")
}
}
log.Reset()
oldMgr := swapper2.Current()
newPluginNames := []string{"snow"}
buildArgs := []string{"--", "-ldflags", "-X main.CompileTimeString=stark"}
preparePluginGroupImpl(t, buildArgs, "LoadPlugins", false, newPluginNames...)
details2, err := swapper2.Reload(log)
if err != nil {
t.Fatal(err)
} else if log.StringExists("invoking snow.OnFree") {
t.Fatal("OnFree should not run this early")
}
for k, v := range details2 {
switch name2key(pluginName(k)) {
case "arya":
if v != "unchanged" {
t.Fatalf(`unexpected result. file: %s, result: %s`, k, v)
}
case "snow":
if v != "ok" {
t.Fatalf(`unexpected result. file: %s, result: %s`, k, v)
}
case "stubborn":
if v != "not reloadable" {
t.Fatalf(`unexpected result. file: %s, result: %s`, k, v)
}
default:
panic("impossible")
}
}
if swapper2.Current() == oldMgr {
t.Fatal("swapper2.Current() == oldMgr")
}
if swapper2.Current() == nil {
t.Fatal("swapper2.Current() == nil")
}
currentPlugins2 := swapper2.Current().Plugins()
if len(currentPlugins2) != 3 {
t.Fatal("len(currentPlugins2) != 3")
}
for _, p := range currentPlugins2 {
compileTimeString := ""
if err := p.Lookup("CompileTimeString", &compileTimeString); err != nil {
t.Fatal(err)
}
switch p.Name {
case "arya", "stubborn":
if compileTimeString != "" {
t.Fatal("unexpected CompileTimeString: " + compileTimeString)
}
case "snow":
if compileTimeString != "stark" {
t.Fatal("unexpected CompileTimeString: " + compileTimeString)
}
default:
panic("impossible")
}
}
time.Sleep(time.Millisecond * 1200)
for _, pName := range pluginNames {
str := fmt.Sprintf("invoking %s.OnFree", pName)
switch pName {
case "arya", "stubborn":
if log.StringExists(str) {
t.Fatal("unexpected message: " + str)
}
case "snow":
if !log.StringExists(str) {
t.Fatal("snow.OnFree should have been invoked")
}
default:
panic("impossible")
}
}
}
func TestPluginManagerSwapper_ReloadWithCallback(t *testing.T) {
var counter int
cb := func(newManager, oldManager *PluginManager) error {
counter++
switch counter {
case 1:
newManager.Info("Reloaded: 1")
case 2:
newManager.Info("Reloaded: 2")
case 3:
return fmt.Errorf("%s", "Reloaded: unreasonable error")
case 4:
panic("Reloaded: panic")
default:
newManager.Infof("Reloaded: %d", counter)
}
return nil
}
pluginNames := []string{"snow"}
outputDir := preparePluginGroup(t, nil, "ReloadWithCallback", pluginNames...)
log := newScavenger()
swapper := newSwapper(outputDir, WithLogger(log), WithReloadCallback(cb))
prepareEnv(t, "")
_, err := swapper.LoadPlugins(log)
if err != nil {
t.Fatal(err)
}
if !log.StringExists("Reloaded: 1") {
t.Fatal("ReloadCallback does not work as expected")
}
log.Reset()
preparePluginGroup(t, nil, "ReloadWithCallback", pluginNames...)
if _, err := swapper.Reload(log); err != nil {
t.Fatal(err)
}
if !log.StringExists("Reloaded: 2") {
t.Fatal("ReloadCallback does not work as expected")
}
log.Reset()
preparePluginGroup(t, nil, "ReloadWithCallback", pluginNames...)
if _, err := swapper.Reload(log); err == nil {
t.Fatal("Reload should fail when ReloadCallback returns an error")
}
if !log.StringExists("invoking snow.OnFree") {
t.Fatal("snow.OnFree should have been invoked")
}
log.Reset()
preparePluginGroup(t, nil, "ReloadWithCallback", pluginNames...)
if _, err := swapper.Reload(log); err == nil {
t.Fatal("Reload should fail when ReloadCallback panics")
}
if !log.StringExists("invoking snow.OnFree") {
t.Fatal("snow.OnFree should have been invoked")
}
log.Reset()
preparePluginGroup(t, nil, "ReloadWithCallback", pluginNames...)
extra := func(newManager, oldManager *PluginManager) error {
newManager.Info("Reloaded: done")
return nil
}
if _, err := swapper.ReloadWithCallback(log, extra); err != nil {
t.Fatalf("unexpected error: %v", err)
}
seq := []string{
"Reloaded: 5",
"Reloaded: done",
}
if _, ok := log.FindStringSequence(seq); !ok {
t.Fatal("cannot find the error message sequence")
}
}
func TestFormatDetails(t *testing.T) {
keys := []string{"a", "b" + hutils.FileNameExt, "c" + hutils.FileNameExt}
vals := []string{"not reloadable", "ok", "unchanged"}
var expected string
for i := 0; i < len(keys); i++ {
expected = fmt.Sprintf("%s, %s: %s", expected,
strings.TrimSuffix(keys[i], hutils.FileNameExt), vals[i])
}
expected = expected[2:]
for i := 0; i < 100; i++ {
for j := 0; j < len(keys); j++ {
idx := rand.Intn(len(keys))
keys[j], keys[idx] = keys[idx], keys[j]
vals[j], vals[idx] = vals[idx], vals[j]
}
m := make(map[string]string)
for j := 0; j < len(keys); j++ {
m[keys[j]] = vals[j]
}
actual := Details(m).String()
if actual != expected {
t.Fatal("actual != expected")
}
}
}