-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor_stop_test.go
More file actions
89 lines (80 loc) · 2.12 KB
/
Copy pathexecutor_stop_test.go
File metadata and controls
89 lines (80 loc) · 2.12 KB
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
//go:build !windows
package main
import (
"errors"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
"testing"
"time"
)
func TestStopEntryTerminatesProcessGroup(t *testing.T) {
service := newTestService(t)
group, err := service.CreateGroup(GroupInput{Name: "终止测试"})
if err != nil {
t.Fatal(err)
}
directory := t.TempDir()
childPIDPath := filepath.Join(directory, "child.pid")
scriptPath := filepath.Join(directory, "long-running.sh")
script := "#!/bin/sh\nsleep 30 &\nchild=$!\nprintf '%s' \"$child\" > \"$1\"\nwait \"$child\"\n"
if err := os.WriteFile(scriptPath, []byte(script), 0o755); err != nil {
t.Fatal(err)
}
entry, err := service.CreateEntry(EntryInput{
GroupID: group.ID,
Name: "长时间任务",
ScriptPath: scriptPath,
Arguments: shellJoin([]string{childPIDPath}),
})
if err != nil {
t.Fatal(err)
}
resultChannel := make(chan ExecutionResult, 1)
go func() {
resultChannel <- service.ExecuteEntry(entry.ID)
}()
var childPID int
deadline := time.Now().Add(3 * time.Second)
for time.Now().Before(deadline) {
contents, readErr := os.ReadFile(childPIDPath)
if readErr == nil {
childPID, err = strconv.Atoi(strings.TrimSpace(string(contents)))
if err == nil && childPID > 0 {
break
}
}
time.Sleep(20 * time.Millisecond)
}
if childPID <= 0 {
t.Fatal("child process did not start")
}
if err := service.StopEntry(entry.ID); err != nil {
t.Fatal(err)
}
select {
case result := <-resultChannel:
if result.Status != "stopped" || result.Message != "运行已终止" {
t.Fatalf("unexpected stopped result: %#v", result)
}
case <-time.After(4 * time.Second):
t.Fatal("execution did not finish after stop")
}
deadline = time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
err = syscall.Kill(childPID, 0)
if errors.Is(err, syscall.ESRCH) {
return
}
time.Sleep(20 * time.Millisecond)
}
t.Fatalf("child process %d is still running", childPID)
}
func TestStopEntryRejectsInactiveEntry(t *testing.T) {
service := newTestService(t)
if err := service.StopEntry(42); err == nil {
t.Fatal("expected an error for an inactive entry")
}
}