forked from ergo-services/ergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application_test.go
505 lines (419 loc) · 12.5 KB
/
application_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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package ergo
import (
"context"
"fmt"
"testing"
"time"
"github.com/halturin/ergo/etf"
)
type testApplication struct {
Application
}
func (a *testApplication) Load(args ...interface{}) (ApplicationSpec, error) {
lifeSpan := args[0].(time.Duration)
name := args[1].(string)
nameGS := "testAppGS"
if len(args) == 3 {
nameGS = args[2].(string)
}
return ApplicationSpec{
Name: name,
Description: "My Test Applicatoin",
Version: "v.0.1",
Environment: map[string]interface{}{
"envName1": 123,
"envName2": "Hello world",
},
Children: []ApplicationChildSpec{
ApplicationChildSpec{
Child: &testAppGenServer{},
Name: nameGS,
},
},
Lifespan: lifeSpan,
}, nil
}
func (a *testApplication) Start(p *Process, args ...interface{}) {
//p.SetEnv("env123", 456)
}
// test GenServer
type testAppGenServer struct {
GenServer
}
func (gs *testAppGenServer) Init(p *Process, args ...interface{}) interface{} {
//fmt.Println("STARTING TEST GS IN APP")
p.SetEnv("env123", 456)
return nil
}
func (gs *testAppGenServer) HandleCast(message etf.Term, state interface{}) (string, interface{}) {
return "noreply", state
}
func (gs *testAppGenServer) HandleCall(from etf.Tuple, message etf.Term, state interface{}) (string, etf.Term, interface{}) {
return "stop", message, nil
}
func (gs *testAppGenServer) HandleInfo(message etf.Term, state interface{}) (string, interface{}) {
return "noreply", state
}
func (gs *testAppGenServer) Terminate(reason string, state interface{}) {
//fmt.Println("TERMINATING TEST GS IN APP with reason:", reason)
}
// testing application
func TestApplicationBasics(t *testing.T) {
fmt.Printf("\n=== Test Application load/unload/start/stop\n")
fmt.Printf("\nStarting node nodeTestAplication@localhost:")
ctx := context.Background()
node := CreateNodeWithContext(ctx, "nodeTestApplication@localhost", "cookies", NodeOptions{})
if node == nil {
t.Fatal("can't start node")
} else {
fmt.Println("OK")
}
app := &testApplication{}
lifeSpan := 0 * time.Second
//
// case 1: loading/unloading app
//
fmt.Printf("... loading application: ")
err := node.ApplicationLoad(app, lifeSpan, "testapp")
if err != nil {
t.Fatal(err)
}
la := node.LoadedApplications()
if len(la) != 1 {
t.Fatal("total number of loaded application mismatch")
}
if la[0].Name != "testapp" {
t.Fatal("can't load application")
}
fmt.Println("OK")
wa := node.WhichApplications()
if len(wa) > 0 {
t.Fatal("total number of running application mismatch")
}
fmt.Printf("... unloading application: ")
if err := node.ApplicationUnload("testapp"); err != nil {
t.Fatal(err)
}
la = node.LoadedApplications()
if len(la) > 0 {
t.Fatal("total number of loaded application mismatch")
}
fmt.Println("OK")
//
// case 2: start(and try to unload running app)/stop(normal) application
//
fmt.Printf("... starting application: ")
if err := node.ApplicationLoad(app, lifeSpan, "testapp1", "testAppGS1"); err != nil {
t.Fatal(err)
}
p, e := node.ApplicationStart("testapp1")
if e != nil {
t.Fatal(e)
}
fmt.Println("OK")
fmt.Printf("... try to unload started application (shouldn't be able): ")
if e := node.ApplicationUnload("testapp1"); e != ErrAppAlreadyStarted {
t.Fatal(e)
}
fmt.Println("OK")
fmt.Printf("... check total number of running applications (should be 1): ")
wa = node.WhichApplications()
if n := len(wa); n != 1 {
t.Fatal(n)
}
fmt.Println("OK")
fmt.Printf("... check the name of running application (should be 'testapp1'): ")
if wa[0].Name != "testapp1" {
t.Fatal(wa[0].Name)
}
fmt.Println("OK")
// case 2.1: test env vars
fmt.Printf("... application's environment variables: ")
p.SetEnv("env123", 123)
p.SetEnv("envStr", "123")
gs := node.GetProcessByName("testAppGS1")
env := gs.GetEnv("env123")
if env == nil {
t.Fatal("incorrect environment variable: not found")
}
if env.(int) != 456 {
t.Fatal("incorrect environment variable: value should be overrided by child process")
}
if envUnknown := gs.GetEnv("unknown"); envUnknown != nil {
t.Fatal("incorrect environment variable: undefined variable should have nil value")
}
envs := gs.ListEnv()
if x, ok := envs["env123"]; !ok || x != 456 {
t.Fatal("incorrect environment variable: list of variables has no env123 value or its wrong")
}
if x, ok := envs["envStr"]; !ok || x != "123" {
t.Fatal("incorrect environment variable: list of variables has no envStr value or its wrong")
}
fmt.Println("OK")
// case 2.2: get list of children' pid
fmt.Printf("... application's children list: ")
list := p.GetChildren()
if len(list) != 1 || list[0] != gs.Self() {
t.Fatal("incorrect children list")
}
fmt.Println("OK")
// case 2.3: get application info
fmt.Printf("... getting application info: ")
info, errInfo := node.GetApplicationInfo("testapp1")
if errInfo != nil {
t.Fatal(errInfo)
}
if p.Self() != info.PID {
t.Fatal("incorrect pid in application info")
}
fmt.Println("OK")
fmt.Printf("... stopping application: ")
if e := node.ApplicationStop("testapp1"); e != nil {
t.Fatal(e)
}
fmt.Println("OK")
if e := p.WaitWithTimeout(100 * time.Millisecond); e != nil {
t.Fatal("timed out")
}
wa = node.WhichApplications()
if len(wa) != 0 {
fmt.Println("waa: ", wa)
t.Fatal("total number of running application mismatch")
}
//
// case 3: start/stop (brutal) application
//
fmt.Printf("... starting application for brutal kill: ")
if err := node.ApplicationLoad(app, lifeSpan, "testappBrutal", "testAppGS2Brutal"); err != nil {
t.Fatal(err)
}
p, e = node.ApplicationStart("testappBrutal")
if e != nil {
t.Fatal(e)
}
fmt.Println("OK")
fmt.Printf("... kill application: ")
p.Kill()
if e := p.WaitWithTimeout(100 * time.Millisecond); e != nil {
t.Fatal("timed out")
}
fmt.Println("OK")
node.ApplicationUnload("testappBrutal")
//
// case 4: start with limited lifespan
//
fmt.Printf("... starting application with lifespan 150ms: ")
lifeSpan = 150 * time.Millisecond
if err := node.ApplicationLoad(app, lifeSpan, "testapp2", "testAppGS2"); err != nil {
t.Fatal(err)
}
tStart := time.Now()
p, e = node.ApplicationStart("testapp2")
if e != nil {
t.Fatal(e)
}
// due to small lifespantiming it is ok to get real lifespan longer almost twice
if e := p.WaitWithTimeout(300 * time.Millisecond); e != nil {
t.Fatal("application lifespan was longer than 150ms")
}
fmt.Println("OK")
tLifeSpan := time.Since(tStart)
fmt.Printf("... application should be self stopped in 150ms: ")
if node.IsProcessAlive(p.Self()) {
t.Fatal("still alive")
}
if tLifeSpan < lifeSpan {
t.Fatal("lifespan was shorter(", tLifeSpan, ") than ", lifeSpan)
}
fmt.Println("OK [ real lifespan:", tLifeSpan, "]")
node.Stop()
}
func TestApplicationTypePermanent(t *testing.T) {
fmt.Printf("\n=== Test Application type Permanent\n")
fmt.Printf("\nStarting node nodeTestAplicationPermanent@localhost:")
ctx := context.Background()
node := CreateNodeWithContext(ctx, "nodeTestApplicationPermanent@localhost", "cookies", NodeOptions{})
if node == nil {
t.Fatal("can't start node")
} else {
fmt.Println("OK")
}
fmt.Printf("... starting application: ")
app := &testApplication{}
lifeSpan := time.Duration(0)
if err := node.ApplicationLoad(app, lifeSpan, "testapp"); err != nil {
t.Fatal(err)
}
p, e := node.ApplicationStartPermanent("testapp")
if e != nil {
t.Fatal(e)
}
fmt.Println("OK")
gs := node.GetProcessByName("testAppGS")
fmt.Printf("... stop child with 'abnormal' reason: ")
gs.Exit(p.Self(), "abnormal")
if e := gs.WaitWithTimeout(100 * time.Millisecond); e != nil {
t.Fatal("timeout on waiting child")
}
fmt.Println("OK")
if e := p.WaitWithTimeout(1 * time.Second); e != nil {
t.Fatal("timeout on waiting application stopping")
}
if e := node.WaitWithTimeout(1 * time.Second); e != nil {
t.Fatal("node shouldn't be alive here")
}
if node.IsAlive() {
t.Fatal("node shouldn't be alive here")
}
}
func TestApplicationTypeTransient(t *testing.T) {
fmt.Printf("\n=== Test Application type Transient\n")
fmt.Printf("\nStarting node nodeTestAplicationTypeTransient@localhost:")
ctx := context.Background()
node := CreateNodeWithContext(ctx, "nodeTestApplicationTypeTransient@localhost", "cookies", NodeOptions{})
if node == nil {
t.Fatal("can't start node")
} else {
fmt.Println("OK")
}
app1 := &testApplication{}
app2 := &testApplication{}
lifeSpan := time.Duration(0)
if err := node.ApplicationLoad(app1, lifeSpan, "testapp1", "testAppGS1"); err != nil {
t.Fatal(err)
}
if err := node.ApplicationLoad(app2, lifeSpan, "testapp2", "testAppGS2"); err != nil {
t.Fatal(err)
}
fmt.Printf("... starting application testapp1: ")
p1, e1 := node.ApplicationStartTransient("testapp1")
if e1 != nil {
t.Fatal(e1)
}
fmt.Println("OK")
fmt.Printf("... starting application testapp2: ")
p2, e2 := node.ApplicationStartTransient("testapp2")
if e2 != nil {
t.Fatal(e2)
}
fmt.Println("OK")
fmt.Printf("... stopping testAppGS1 with 'normal' reason (shouldn't affect testAppGS2): ")
gs := node.GetProcessByName("testAppGS1")
gs.Exit(gs.Self(), "normal")
if e := gs.WaitWithTimeout(100 * time.Millisecond); e != nil {
t.Fatal(e)
}
if e := p1.WaitWithTimeout(100 * time.Millisecond); e != ErrTimeout {
t.Fatal("application testapp1 should be alive here")
}
p1.Kill()
p2.WaitWithTimeout(100 * time.Millisecond)
if !p2.IsAlive() {
t.Fatal("testAppGS2 should be alive here")
}
if !node.IsAlive() {
t.Fatal("node should be alive here")
}
fmt.Println("OK")
fmt.Printf("... starting application testapp1: ")
p1, e1 = node.ApplicationStartTransient("testapp1")
if e1 != nil {
t.Fatal(e1)
}
fmt.Println("OK")
fmt.Printf("... stopping testAppGS1 with 'abnormal' reason (node will shotdown): ")
gs = node.GetProcessByName("testAppGS1")
gs.Exit(gs.Self(), "abnormal")
if e := gs.WaitWithTimeout(100 * time.Millisecond); e != nil {
t.Fatal(e)
}
if e := p1.WaitWithTimeout(100 * time.Millisecond); e != nil {
t.Fatal("testapp1 shouldn't be alive here")
}
if e := p2.WaitWithTimeout(100 * time.Millisecond); e != nil {
t.Fatal("testapp2 shouldn't be alive here")
}
if e := node.WaitWithTimeout(100 * time.Millisecond); e != nil {
t.Fatal("node shouldn't be alive here")
}
fmt.Println("OK")
}
func TestApplicationTypeTemporary(t *testing.T) {
fmt.Printf("\n=== Test Application type Temporary\n")
fmt.Printf("\nStarting node nodeTestAplicationStop@localhost:")
ctx := context.Background()
node := CreateNodeWithContext(ctx, "nodeTestApplicationStop@localhost", "cookies", NodeOptions{})
if node == nil {
t.Fatal("can't start node")
} else {
fmt.Println("OK")
}
fmt.Printf("... starting application: ")
app := &testApplication{}
lifeSpan := time.Duration(0)
if err := node.ApplicationLoad(app, lifeSpan, "testapp"); err != nil {
t.Fatal(err)
}
p, e := node.ApplicationStart("testapp") // default start type is Temporary
if e != nil {
t.Fatal(e)
}
fmt.Println("OK")
fmt.Printf("... stopping testAppGS with 'normal' reason: ")
gs := node.GetProcessByName("testAppGS")
gs.Exit(p.Self(), "normal")
if e := gs.WaitWithTimeout(100 * time.Millisecond); e != nil {
t.Fatal(e)
}
if e := node.WaitWithTimeout(100 * time.Millisecond); e != ErrTimeout {
t.Fatal("node should be alive here")
}
if !node.IsAlive() {
t.Fatal("node should be alive here")
}
fmt.Println("OK")
node.Stop()
}
func TestApplicationStop(t *testing.T) {
fmt.Printf("\n=== Test Application stopping\n")
fmt.Printf("\nStarting node nodeTestAplicationTypeTemporary@localhost:")
ctx := context.Background()
node := CreateNodeWithContext(ctx, "nodeTestApplicationTypeTemporary@localhost", "cookies", NodeOptions{})
if node == nil {
t.Fatal("can't start node")
} else {
fmt.Println("OK")
}
fmt.Printf("... starting applications testapp1, testapp2: ")
lifeSpan := time.Duration(0)
app := &testApplication{}
if e := node.ApplicationLoad(app, lifeSpan, "testapp1", "testAppGS1"); e != nil {
t.Fatal(e)
}
app1 := &testApplication{}
if e := node.ApplicationLoad(app1, lifeSpan, "testapp2", "testAppGS2"); e != nil {
t.Fatal(e)
}
_, e1 := node.ApplicationStartPermanent("testapp1")
if e1 != nil {
t.Fatal(e1)
}
p2, e2 := node.ApplicationStartPermanent("testapp2")
if e2 != nil {
t.Fatal(e2)
}
fmt.Println("OK")
// case 1: stopping via node.ApplicatoinStop
fmt.Printf("... stopping testapp1 via node.ApplicationStop (shouldn't affect testapp2):")
if e := node.ApplicationStop("testapp1"); e != nil {
t.Fatal("can't stop application via node.ApplicationStop", e)
}
if !p2.IsAlive() {
t.Fatal("testapp2 should be alive here")
}
if !node.IsAlive() {
t.Fatal("node should be alive here")
}
fmt.Println("OK")
node.Stop()
}