This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
generated from ContainerSSH/library-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlifecycle_test.go
81 lines (75 loc) · 1.79 KB
/
lifecycle_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
package service_test
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/containerssh/service"
)
func TestLifecycle(t *testing.T) {
l := service.NewLifecycle(newTestService("Test service"))
starting := make(chan bool)
running := make(chan bool)
stopping := make(chan bool)
stopped := make(chan bool)
stopExited := make(chan bool)
l.OnStarting(func(s service.Service, l service.Lifecycle) {
starting <- true
})
l.OnRunning(func(s service.Service, l service.Lifecycle) {
running <- true
})
l.OnStopping(func(s service.Service, l service.Lifecycle, shutdownContext context.Context) {
stopping <- true
})
l.OnStopped(func(s service.Service, l service.Lifecycle) {
stopped <- true
})
l.OnCrashed(func(s service.Service, l service.Lifecycle, err error) {
t.Fail()
})
go func() {
if err := l.Run(); err != nil {
assert.Fail(t, "service crashed", err)
}
}()
<-starting
<-running
go func() {
l.Stop(context.Background())
stopExited <- true
}()
<-stopping
<-stopped
<-stopExited
}
func TestCrash(t *testing.T) {
s := newTestService("Test service")
l := service.NewLifecycle(s)
starting := make(chan bool)
running := make(chan bool)
crashed := make(chan bool, 1)
l.OnStarting(func(s service.Service, l service.Lifecycle) {
starting <- true
})
l.OnRunning(func(s service.Service, l service.Lifecycle) {
running <- true
})
l.OnCrashed(func(s service.Service, l service.Lifecycle, err error) {
crashed <- true
})
l.OnStopped(func(s service.Service, l service.Lifecycle) {
t.Fail()
})
l.OnStopping(func(s service.Service, l service.Lifecycle, shutdownContext context.Context) {
t.Fail()
})
go func() {
if err := l.Run(); err == nil {
assert.Fail(t, "service did not crash")
}
}()
<-starting
<-running
s.Crash()
<-crashed
}