forked from ayufan-research/golang-kardianos-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_test.go
85 lines (73 loc) · 1.36 KB
/
service_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
// Copyright 2015 Daniel Theophanes.
// Use of this source code is governed by a zlib-style
// license that can be found in the LICENSE file.package service
package service_test
import (
"log"
"os"
"testing"
"github.com/kardianos/service"
)
const runAsServiceArg = "RunThisAsService"
var sc = &service.Config{
Name: "go_service_test",
Arguments: []string{runAsServiceArg},
}
func TestMain(m *testing.M) {
if len(os.Args) > 1 && os.Args[1] == runAsServiceArg {
runService()
return
}
os.Exit(m.Run())
}
func TestInstallRunRestartStopRemove(t *testing.T) {
p := &program{}
s, err := service.New(p, sc)
if err != nil {
t.Fatal(err)
}
_ = s.Uninstall()
err = s.Install()
if err != nil {
t.Fatal("install", err)
}
defer s.Uninstall()
err = s.Start()
if err != nil {
t.Fatal("start", err)
}
err = s.Restart()
if err != nil {
t.Fatal("restart", err)
}
err = s.Stop()
if err != nil {
t.Fatal("stop", err)
}
err = s.Uninstall()
if err != nil {
t.Fatal("stop", err)
}
}
func runService() {
p := &program{}
s, err := service.New(p, sc)
if err != nil {
log.Fatal(err)
}
err = s.Run()
if err != nil {
log.Fatal(err)
}
}
type program struct{}
func (p *program) Start(s service.Service) error {
go p.run()
return nil
}
func (p *program) run() {
// Do work here
}
func (p *program) Stop(s service.Service) error {
return nil
}