forked from deanishe/awgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background_test.go
55 lines (49 loc) · 1.3 KB
/
background_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
// Copyright (c) 2018 Dean Jackson <[email protected]>
// MIT Licence - http://opensource.org/licenses/MIT
package aw
import (
"os/exec"
"testing"
"github.com/deanishe/awgo/util"
)
// TestRunInBackground ensures background jobs work.
func TestRunInBackground(t *testing.T) {
t.Parallel()
wf := New()
cmd := exec.Command("sleep", "5")
if wf.IsRunning("sleep") {
t.Fatalf("Job 'sleep' is already running")
}
if err := wf.RunInBackground("sleep", cmd); err != nil {
t.Fatalf("Error starting job 'sleep': %s", err)
}
if !wf.IsRunning("sleep") {
t.Fatalf("Job 'sleep' is not running")
}
p := wf.pidFile("sleep")
if !util.PathExists(p) {
t.Fatalf("No PID file for 'sleep'")
}
// Duplicate jobs fail
cmd = exec.Command("sleep", "5")
err := wf.RunInBackground("sleep", cmd)
if err == nil {
t.Fatal("Starting duplicate 'sleep' job didn't error")
}
if _, ok := err.(ErrJobExists); !ok {
t.Fatal("RunInBackground didn't return ErrAlreadyRunning")
}
if !IsJobExists(err) {
t.Errorf("IsAlreadyRunning didn't identify ErrAlreadyRunning")
}
// Job killed OK
if err := wf.Kill("sleep"); err != nil {
t.Fatalf("Error killing 'sleep' job: %s", err)
}
if wf.IsRunning("sleep") {
t.Fatal("'sleep' job still running")
}
if util.PathExists(p) {
t.Fatal("'sleep' PID file not deleted")
}
}