-
Notifications
You must be signed in to change notification settings - Fork 0
/
subprocess_test.go
69 lines (58 loc) · 1.53 KB
/
subprocess_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
package main
import (
"context"
"fmt"
"github.com/phith0n/vindicator"
"os/exec"
"testing"
"time"
)
type ProcessWorker struct {
isRunning bool
}
// Work must be a blocking function
func (pw *ProcessWorker) Work(ctx context.Context) error {
cmd := exec.CommandContext(ctx, "sleep", "1h")
if err := cmd.Start(); err != nil {
return err
}
if err := cmd.Wait(); err != nil {
return err
}
return nil
}
func (pw *ProcessWorker) SetRunning(run bool) {
pw.isRunning = run
}
func (pw *ProcessWorker) GetRunning() bool {
return pw.isRunning
}
func TestSubprocess(t *testing.T) {
ctx := context.Background()
worker := ProcessWorker{}
v := vindicator.NewVindicator(&worker, 2)
v.On("monitor:start", func(v *vindicator.Vindicator, args ...interface{}) {
fmt.Println("start monitor")
})
v.On("monitor:working", func(v *vindicator.Vindicator, args ...interface{}) {
fmt.Println("process is working normally...")
})
v.On("monitor:interrupt", func(v *vindicator.Vindicator, args ...interface{}) {
fmt.Println("process is stopped unexpected, try to restart it...")
})
v.On("monitor:stop", func(v *vindicator.Vindicator, args ...interface{}) {
fmt.Println("stop monitor")
})
v.On("worker:start", func(v *vindicator.Vindicator, args ...interface{}) {
fmt.Println("start worker")
})
v.On("worker:stop", func(v *vindicator.Vindicator, args ...interface{}) {
fmt.Println("stop worker")
})
go v.Start(ctx)
go v.Monitor(ctx)
timer := time.NewTimer(time.Second * 10)
<-timer.C
// stop the worker and the monitor manual
v.Stop()
}