Files

87 lines
1.6 KiB
Go
Raw Permalink Normal View History

2015-03-07 09:42:36 -08:00
// Copyright 2015 Daniel Theophanes.
// Use of this source code is governed by a zlib-style
2015-09-01 09:34:29 -07:00
// license that can be found in the LICENSE file.
2015-03-07 09:42:36 -08:00
package service_test
import (
"os"
2015-03-07 09:42:36 -08:00
"testing"
"time"
2015-03-07 09:42:36 -08:00
"github.com/kardianos/service"
)
func TestRunInterrupt(t *testing.T) {
2015-03-07 09:42:36 -08:00
p := &program{}
2016-07-12 05:45:16 +01:00
sc := &service.Config{
Name: "go_service_test",
}
2015-03-07 09:42:36 -08:00
s, err := service.New(p, sc)
if err != nil {
t.Fatalf("New err: %s", err)
2015-03-07 09:42:36 -08:00
}
go func() {
<-time.After(1 * time.Second)
interruptProcess(t)
}()
go func() {
2016-07-12 05:45:16 +01:00
for i := 0; i < 25 && p.numStopped == 0; i++ {
<-time.After(200 * time.Millisecond)
}
if p.numStopped == 0 {
t.Fatal("Run() hasn't been stopped")
}
}()
if err = s.Run(); err != nil {
t.Fatalf("Run() err: %s", err)
2015-03-07 09:42:36 -08:00
}
}
const testInstallEnv = "TEST_USER_INSTALL"
2020-06-07 20:34:08 +02:00
// Should always run, without asking for any permission
func TestUserRunInterrupt(t *testing.T) {
if os.Getenv(testInstallEnv) != "1" {
t.Skipf("env %q is not set to 1", testInstallEnv)
}
2020-06-07 20:34:08 +02:00
p := &program{}
options := make(service.KeyValue)
options["UserService"] = true
sc := &service.Config{
Name: "go_user_service_test",
2020-06-07 20:34:08 +02:00
Option: options,
}
s, err := service.New(p, sc)
if err != nil {
t.Fatalf("New err: %s", err)
}
err = s.Install()
if err != nil {
t.Errorf("Install err: %s", err)
}
err = s.Uninstall()
if err != nil {
t.Fatalf("Uninstall err: %s", err)
}
}
type program struct {
2016-07-12 05:45:16 +01:00
numStopped int
}
2015-03-07 09:42:36 -08:00
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 {
2016-07-12 05:45:16 +01:00
p.numStopped++
2015-03-07 09:42:36 -08:00
return nil
}