-
Notifications
You must be signed in to change notification settings - Fork 2
/
golet_test.go
163 lines (140 loc) · 3.02 KB
/
golet_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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
package golet
import (
"context"
"os"
"os/exec"
"os/signal"
"path/filepath"
"syscall"
"testing"
"time"
colorable "github.com/mattn/go-colorable"
"github.com/stretchr/testify/assert"
)
var ctx = context.Background()
func TestDefault(t *testing.T) {
p := New(ctx)
assert.Equal(t, time.Duration(0), p.(*config).interval)
assert.Equal(t, false, p.(*config).color)
assert.Equal(t, colorable.NewColorableStderr(), p.(*config).logger)
assert.Equal(t, true, p.(*config).logWorker)
assert.Equal(t, true, p.(*config).execNotice)
}
func TestEnv(t *testing.T) {
maps := []map[string]string{
{"Hello": "World"},
{
"Year": "2017",
"Month": "02",
"Day": "08",
},
}
for _, m := range maps {
New(ctx).Env(m)
for k := range m {
env := os.Getenv(k)
if m[k] != env {
assert.Equal(t, m[k], env)
}
}
}
}
func TestAdd(t *testing.T) {
st := ServiceGen()
p1, p2 := New(ctx), New(ctx)
for _, v := range st {
if err := p1.Add(v); err != nil {
t.Fatalf(err.Error())
}
}
if err := p2.Add(st...); err != nil {
t.Fatalf(err.Error())
}
p1s := p1.(*config).services
p2s := p2.(*config).services
assert.Equal(t, len(p1s), len(p2s))
ln := len(p1s)
for i := 0; i < ln; i++ {
assert.Equal(t, p1s[i].Exec, p2s[i].Exec)
assert.Equal(t, p1s[i].Tag, p2s[i].Tag)
assert.Equal(t, p1s[i].Every, p2s[i].Every)
assert.Equal(t, p1s[i].Worker, p2s[i].Worker)
}
}
func TestWait(t *testing.T) {
c := exec.Command("go", "build", "-o", "sleep", "sleep.go")
c.Dir = "_testdata"
defer os.Remove(filepath.Join(c.Dir, "sleep"))
if err := c.Run(); err != nil {
t.Fatalf(err.Error())
}
_ctx, cancel := context.WithTimeout(ctx, time.Second*5)
defer cancel()
p := New(_ctx)
chps := make(chan *os.Process, 1)
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGINT)
times := 3
go p.(*config).waitSignals(chps, times)
finch := make(chan bool)
for i := 0; i < times; i++ {
go func() {
cmd := exec.Command("./sleep", "5")
cmd.Dir = "_testdata"
if err := run(cmd, chps); err == nil {
panic(err)
}
finch <- true
}()
}
// Send a signal to self test
go func() {
time.Sleep(time.Second * 1)
syscall.Kill(syscall.Getpid(), syscall.SIGHUP)
}()
i := 0
for i < times {
select {
case <-finch:
i++
case <-_ctx.Done():
t.Fatalf("Timeout: Could not send signals to all processes")
}
}
p.(*config).wait(chps)
assert.Equal(t, times, i, "Could not send signals to all processes")
}
func ServiceGen() []Service {
return []Service{
{
Exec: "ping google.com",
Tag: "ping",
},
{
Code: func(c context.Context) error {
return nil
},
Tag: "code",
},
{
Exec: "ping google.com",
Worker: 4,
Tag: "ping-worker",
},
{
Exec: "ping google.com",
Every: "@hourly",
Worker: 10,
Tag: "ping-cron",
},
{
Code: func(c context.Context) error {
return nil
},
Every: "@every 20s",
Worker: -100,
Tag: "code-cron",
},
}
}