-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper_test.go
109 lines (93 loc) · 1.72 KB
/
wrapper_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
package glcm
import (
"sync"
"testing"
"time"
)
func TestWrapper_Start(t *testing.T) {
tests := []struct {
name string
preHooks []Hook
postHooks []Hook
}{
{
name: "No hooks",
preHooks: nil,
postHooks: nil,
},
{
name: "With pre-hooks",
preHooks: []Hook{
&mockHook{name: "pre-hook-1"},
&mockHook{name: "pre-hook-2"},
},
postHooks: nil,
},
{
name: "With post-hooks",
preHooks: nil,
postHooks: []Hook{
&mockHook{name: "post-hook-1"},
&mockHook{name: "post-hook-2"},
},
},
{
name: "With pre and post-hooks",
preHooks: []Hook{
&mockHook{name: "pre-hook-1"},
&mockHook{name: "pre-hook-2"},
},
postHooks: []Hook{
&mockHook{name: "post-hook-1"},
&mockHook{name: "post-hook-2"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
wg := &sync.WaitGroup{}
svc := &mockService{}
w := NewWrapper(svc, wg, ServiceOptions{
PreHooks: tt.preHooks,
PostHooks: tt.postHooks,
})
go w.Start()
<-time.After(time.Second)
if !svc.started {
t.Errorf("Service was not started")
}
if svc.stopped {
t.Errorf("Service was stopped prematurely")
}
w.StopAndWait()
if !svc.stopped {
t.Errorf("Service was not stopped")
}
})
}
}
type mockService struct {
started bool
stopped bool
}
func (m *mockService) Start(t Terminator) {
m.started = true
<-t.TermCh()
m.started = false
m.stopped = true
}
func (m *mockService) Name() string {
return "mockService"
}
func (m *mockService) Status() string {
return "mockServiceStatus"
}
type mockHook struct {
name string
}
func (m *mockHook) Execute() error {
return nil
}
func (m *mockHook) Name() string {
return m.name
}