-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapplication_test.go
734 lines (716 loc) · 15.3 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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
package appctl
import (
"context"
"errors"
"io"
"os"
"sync"
"sync/atomic"
"syscall"
"testing"
"time"
)
type (
brokenPingService struct{}
brokenPingServiceError struct{}
)
func (*brokenPingService) Init(context.Context) error {
return nil
}
func (*brokenPingService) Watch(context.Context) error {
<-make(chan struct{})
return nil
}
func (*brokenPingService) Stop() {
}
func (*brokenPingService) Release() error {
return brokenPingServiceError{}
}
func (brokenPingServiceError) Error() string {
return "brokenPingServiceError"
}
func TestApplication_Deadline(t *testing.T) {
t.Parallel()
var a Application
dl, ok := a.Deadline()
if ok {
t.Error("application has deadline")
}
if dl != (time.Time{}) {
t.Error("wrong result")
}
}
func TestApplication_Done(t *testing.T) {
// do not put t.Parallel
var a = Application{
halt: make(chan struct{}),
done: make(chan struct{}),
}
t.Run("test value", func(t *testing.T) {
if a.Done() != a.done {
t.Error("wrong value")
}
})
t.Run("test chan state", func(t *testing.T) {
select {
case <-a.Done():
t.Error("wrong channel state")
default:
}
})
t.Run("test chan close", func(t *testing.T) {
close(a.done)
select {
case <-a.Done():
default:
t.Error("wrong channel state")
}
})
}
func TestApplication_Err(t *testing.T) {
t.Parallel()
type testCase struct {
name string
app Application
needErr error
}
var testCases = []testCase{
{
name: "on init state",
app: Application{appState: appStateInit},
needErr: nil,
},
{
name: "on running state",
app: Application{appState: appStateRunning},
needErr: nil,
},
{
name: "on halt state",
app: Application{appState: appStateHalt},
needErr: nil,
},
{
name: "on shutdown state",
app: Application{appState: appStateShutdown},
needErr: ErrShutdown,
},
{
name: "application.err in status",
app: Application{
appState: appStateInit,
err: io.EOF,
},
needErr: io.EOF,
},
{
name: "application.err",
app: Application{
appState: appStateShutdown,
err: io.EOF,
},
needErr: io.EOF,
},
}
for i := range testCases {
test := testCases[i]
t.Run(test.name, func(t *testing.T) {
if e := test.app.Err(); e != test.needErr {
t.Errorf("need: %v, got: %v", test.needErr, e)
}
})
}
}
func TestApplication_Halt(t *testing.T) {
t.Run("close channel", func(t *testing.T) {
var a = Application{
halt: make(chan struct{}),
done: make(chan struct{}),
appState: appStateRunning,
}
a.Shutdown()
select {
case <-a.done:
t.Error("the done chan is closed")
default:
select {
case <-a.halt:
if a.appState != appStateHalt {
t.Error("wrong app state")
}
default:
t.Error("the halt chan is open")
}
}
})
t.Run("wrong state", func(t *testing.T) {
var apps = []Application{
{
halt: make(chan struct{}),
done: make(chan struct{}),
appState: appStateInit,
},
{
halt: make(chan struct{}),
done: make(chan struct{}),
appState: appStateHalt,
},
{
halt: make(chan struct{}),
done: make(chan struct{}),
appState: appStateShutdown,
},
}
for i := range apps {
a := apps[i]
a.Shutdown()
select {
case <-a.done:
t.Error("the done chan is closed")
default:
select {
case <-a.halt:
t.Error("the halt chan is closed")
default:
// good case
}
}
}
})
}
func TestApplication_Run(t *testing.T) {
t.Parallel()
t.Run("Run stages", func(t *testing.T) {
var a = Application{
halt: make(chan struct{}),
done: make(chan struct{}),
appState: appStateInit,
MainFunc: func(context.Context, <-chan struct{}) error {
return nil
},
}
if err := a.Run(); err != nil {
t.Error(err)
}
})
t.Run("empty main", func(t *testing.T) {
var a = Application{}
if err := a.Run(); err != ErrMainOmitted {
t.Errorf("want: %v, got: %v", ErrMainOmitted, err)
}
})
t.Run("running twice", func(t *testing.T) {
var result = false
var a = Application{
halt: make(chan struct{}),
done: make(chan struct{}),
appState: appStateRunning,
MainFunc: func(context.Context, <-chan struct{}) error {
result = true
return nil
},
}
if err := a.Run(); err != ErrWrongState {
t.Errorf("want: %v, got: %v", ErrWrongState, err)
}
if result {
t.Error("wrong logic")
}
})
t.Run("wrong init", func(t *testing.T) {
var result = false
var a = Application{
halt: make(chan struct{}),
done: make(chan struct{}),
appState: appStateInit,
Resources: &ServiceKeeper{
Services: []Service{
&dummyService{brokeOnInit: true},
},
},
MainFunc: func(context.Context, <-chan struct{}) error {
result = true
return nil
},
}
err := a.Run()
if err == nil {
t.Errorf("want error, got: %v", err)
}
if result {
t.Error("wrong logic")
}
if a.err == nil {
t.Errorf("a.err == expected: %v, got: nil", err)
}
if a.appState != appStateShutdown {
t.Error("expected appStateShutdown state")
}
})
t.Run("break services", func(t *testing.T) {
var a = Application{
halt: make(chan struct{}),
done: make(chan struct{}),
appState: appStateInit,
Resources: &ServiceKeeper{
Services: []Service{
&dummyService{
throttling: time.Millisecond * 25,
brokeOnPing: true,
},
},
PingPeriod: time.Millisecond * 25,
},
MainFunc: func(context.Context, <-chan struct{}) error {
<-time.After(time.Second * 100)
return nil
},
}
if err := a.Run(); err == nil {
t.Error("expected error here")
}
})
}
func TestApplication_Shutdown(t *testing.T) {
t.Parallel()
checkBothChannels := func(app Application) error {
select {
case <-app.halt:
select {
case <-app.done:
// good case
return nil
default:
return errors.New("not doned")
}
default:
return errors.New("not halted")
}
}
type testCase struct {
name string
app Application
needError error
check func(Application) error
}
var tests = []testCase{
{
name: "implicit shutdown",
app: Application{
MainFunc: func(context.Context, <-chan struct{}) error {
// exit with Shutdown automatic call
return nil
},
},
needError: nil,
check: checkBothChannels,
},
{
name: "shutdown after halt",
app: Application{
MainFunc: func(ctx context.Context, halt <-chan struct{}) error {
go func() {
<-time.After(time.Millisecond * 5)
ctx.Value(AppContext{}).(*Application).Shutdown()
}()
<-halt
return nil
},
},
needError: nil,
check: checkBothChannels,
},
{
name: "explicit shutdown",
app: Application{
MainFunc: func(ctx context.Context, halt <-chan struct{}) error {
go func() {
<-time.After(time.Millisecond * 5)
ctx.Value(AppContext{}).(*Application).Close()
}()
<-halt
return nil
},
},
needError: nil,
check: checkBothChannels,
},
}
for i := range tests {
test := tests[i]
t.Run(test.name, func(t *testing.T) {
a := test.app
if err := a.Run(); err != test.needError {
t.Errorf("want: %v, got: %v", test.needError, err)
}
if err := test.check(a); err != nil {
t.Error(err)
}
})
}
t.Run("wrong state", func(t *testing.T) {
var result = false
var a = Application{
halt: make(chan struct{}),
done: make(chan struct{}),
appState: appStateInit,
MainFunc: func(ctx context.Context, halt <-chan struct{}) error {
result = true
<-halt
t.Error(errors.New("test error"))
return nil
},
}
a.Close()
if result {
t.Error("no any action expected")
}
select {
case <-a.halt:
t.Error("halt channel closed")
case <-a.done:
t.Error("done channel closed")
default:
// good case
}
})
t.Run("halt state", func(t *testing.T) {
var result = false
var a = Application{
halt: make(chan struct{}),
done: make(chan struct{}),
appState: appStateHalt,
MainFunc: func(ctx context.Context, halt <-chan struct{}) error {
result = true
<-halt
t.Error(errors.New("test error"))
return nil
},
}
a.Close()
if result {
t.Error("no any action expected")
}
select {
case <-a.halt:
t.Error("halt channel closed")
default:
select {
case <-a.done:
// good case
default:
t.Error("done channel closed")
}
}
})
t.Run("bad resources release", func(t *testing.T) {
var a = Application{
Resources: &brokenPingService{},
MainFunc: func(context.Context, <-chan struct{}) error {
return nil
},
TerminationTimeout: time.Millisecond * 5,
}
err := a.Run()
if !errors.Is(err, brokenPingServiceError{}) {
t.Errorf("need brokenPingServiceError, got: %v", err)
}
})
}
func TestApplication_Value(t *testing.T) {
t.Parallel()
var a = Application{
MainFunc: func(ctx context.Context, halt <-chan struct{}) error {
if _, ok := ctx.Value(AppContext{}).(*Application); !ok {
t.Error("wrong context")
}
return nil
},
}
if err := a.Run(); err != nil {
t.Error(err)
}
}
func TestApplication_checkState(t *testing.T) {
t.Parallel()
type fields struct {
appState int32
}
type args struct {
old int32
new int32
}
tests := []struct {
name string
fields fields
args args
want bool
}{
{
name: "0 -> 1",
fields: fields{appState: 0},
args: args{old: 0, new: 1},
want: true,
},
{
name: "1 -> 3",
fields: fields{appState: 1},
args: args{old: 1, new: 3},
want: true,
},
{
name: "4 -> 2",
fields: fields{appState: 4},
args: args{old: 4, new: 2},
want: true,
},
{
name: "1 -> 1",
fields: fields{appState: 1},
args: args{old: 1, new: 1},
want: true,
},
{
name: "err",
fields: fields{appState: 5},
args: args{old: 1, new: 1},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := Application{
appState: tt.fields.appState,
}
if got := a.checkState(tt.args.old, tt.args.new); got != tt.want {
t.Errorf("checkState() = %v, want %v", got, tt.want)
}
})
}
}
func TestApplication_run(t *testing.T) {
t.Parallel()
t.Run("exit by SIGINT", func(t *testing.T) {
var status int32
var a = Application{
appState: appStateRunning,
halt: make(chan struct{}),
done: make(chan struct{}),
MainFunc: func(ctx context.Context, halt <-chan struct{}) error {
<-halt
atomic.CompareAndSwapInt32(&status, 1, 2)
return nil
},
TerminationTimeout: time.Millisecond * 500,
}
var sig = make(chan os.Signal)
go func() {
<-time.After(time.Millisecond * 5)
atomic.CompareAndSwapInt32(&status, 0, 1)
sig <- syscall.SIGINT
}()
go func() {
<-time.After(time.Millisecond * 100)
select {
case <-a.halt:
return
case <-a.done:
return
default:
t.Error("timeout")
close(a.halt)
close(a.done)
}
}()
if err := a.run(sig); err != nil {
t.Error(err)
}
if atomic.LoadInt32(&status) != 2 {
t.Error("unexpected exit")
}
})
t.Run("exit by SIGINT and timeout", func(t *testing.T) {
var status int32
var a = Application{
appState: appStateRunning,
halt: make(chan struct{}),
done: make(chan struct{}),
MainFunc: func(ctx context.Context, halt <-chan struct{}) error {
<-time.After(time.Second * 30)
atomic.CompareAndSwapInt32(&status, 0, 2) // never happens
return nil
},
TerminationTimeout: time.Millisecond * 10,
}
var sig = make(chan os.Signal)
go func() {
<-time.After(time.Millisecond * 5)
sig <- syscall.SIGINT
}()
go func() {
<-time.After(time.Millisecond * 500)
select {
case <-a.halt:
return
case <-a.done:
return
default:
t.Error("test timeout")
}
}()
if err := a.run(sig); err != ErrTermTimeout {
t.Errorf("expected: %v, got: %v", ErrTermTimeout, err)
}
if atomic.LoadInt32(&status) != 0 {
t.Error("unexpected exit")
}
})
t.Run("exit by main", func(t *testing.T) {
var status int32
var a = Application{
appState: appStateRunning,
halt: make(chan struct{}),
done: make(chan struct{}),
MainFunc: func(context.Context, <-chan struct{}) error {
atomic.CompareAndSwapInt32(&status, 0, 1)
return nil
},
TerminationTimeout: time.Millisecond * 500,
}
var sig = make(chan os.Signal)
go func() {
<-time.After(time.Millisecond * 100)
select {
case <-a.done:
return
default:
t.Error("timeout")
close(a.halt)
close(a.done)
}
}()
if err := a.run(sig); err != nil {
t.Error(err)
}
if atomic.LoadInt32(&status) != 1 {
t.Error("unexpected exit")
}
})
t.Run("exit by error", func(t *testing.T) {
var status int32
var a = Application{
appState: appStateRunning,
halt: make(chan struct{}),
done: make(chan struct{}),
MainFunc: func(context.Context, <-chan struct{}) error {
atomic.CompareAndSwapInt32(&status, 0, 1)
return errors.New("error")
},
TerminationTimeout: time.Millisecond * 500,
}
var sig = make(chan os.Signal)
go func() {
<-time.After(time.Millisecond * 100)
select {
case <-a.done:
return
default:
t.Error("timeout")
close(a.halt)
close(a.done)
}
}()
if err := a.run(sig); err == nil {
t.Error("expected error here")
}
if atomic.LoadInt32(&status) != 1 {
t.Error("unexpected exit")
}
})
}
func TestApplication_setError(t *testing.T) {
t.Parallel()
t.Run("normal setError", func(t *testing.T) {
var e = errors.New("test 1")
var a = Application{
appState: appStateRunning,
halt: make(chan struct{}),
done: make(chan struct{}),
}
a.setError(e)
if a.err != e {
t.Errorf("expected: %v, got: %v", e, a.err)
}
})
t.Run("nil setError", func(t *testing.T) {
var e error
var a = Application{
appState: appStateRunning,
halt: make(chan struct{}),
done: make(chan struct{}),
}
a.setError(e)
if a.err != nil {
t.Errorf("expected: %v, got: %v", e, a.err)
}
})
t.Run("concurrent setError", func(t *testing.T) {
var e = errors.New("test error 2")
var a = Application{
appState: appStateRunning,
halt: make(chan struct{}),
done: make(chan struct{}),
}
var wg sync.WaitGroup
wg.Add(1000)
for nn := 0; nn < 1000; nn++ {
go func(i int) {
if i == 50 {
a.setError(e)
} else {
if a.checkState(appStateRunning, appStateRunning) {
a.setError(nil)
} else {
a.setError(errors.New("test error 4"))
}
}
wg.Done()
}(nn)
}
wg.Wait()
if a.err != e {
t.Errorf("expected: %v, got: %v", e, a.err)
}
})
}
func TestApplication_init(t *testing.T) {
t.Parallel()
t.Run("init timeout", func(t *testing.T) {
var a = Application{
appState: appStateRunning,
Resources: &ServiceKeeper{
Services: []Service{
&dummyService{throttling: time.Second},
},
},
InitializationTimeout: time.Millisecond * 2,
}
if err := a.init(); err == nil {
t.Error("expected timeout error here")
}
})
t.Run("defaults", func(t *testing.T) {
var a = Application{
appState: appStateRunning,
}
if err := a.init(); err != nil {
t.Error(err)
}
if a.InitializationTimeout != defaultInitializationTimeout || a.TerminationTimeout != defaultTerminationTimeout {
t.Error("incorrect default values")
}
if a.done == nil || a.halt == nil {
t.Error("channels is not initialized")
}
})
}