forked from itcomusic/winsvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
winsvc_test.go
55 lines (47 loc) · 989 Bytes
/
winsvc_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
// +build windows
package winsvc
import (
"context"
"fmt"
"os"
"sync"
"testing"
"time"
)
func TestRun_Interrupt(t *testing.T) {
ctxTest, cancelTest := context.WithCancel(context.Background())
go func() {
select {
case <-time.After(time.Second * 5):
t.Errorf("service has not been stopped")
case <-ctxTest.Done():
}
}()
start(func(ctx context.Context) {
<-ctx.Done()
cancelTest()
}, signalNotify(func(c chan<- os.Signal, sig ...os.Signal) { c <- os.Interrupt }))
}
func TestRun_Panic(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Fatal("exp: error")
return
}
exp := "exit from run function"
if got := fmt.Sprintf("%v", r); got != exp {
t.Errorf("exp: %s, got: %s", exp, got)
}
}()
start(func(_ context.Context) {})
}
func TestRun_DisablePanic(t *testing.T) {
runOnce = sync.Once{}
defer func() {
if r := recover(); r != nil {
t.Errorf("exp: nil")
}
}()
start(func(_ context.Context) {}, DisablePanic())
}