Repository navigation
Expand file tree
/
Copy pathgovernor_test.go
More file actions
93 lines (88 loc) · 3.34 KB
/
Copy pathgovernor_test.go
File metadata and controls
93 lines (88 loc) · 3.34 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
90
91
92
93
package exec
import (
"context"
"os"
"strings"
"sync/atomic"
"testing"
)
// The regression this file exists for. A leaf is a goroutine parked on a
// socket: it puts no load on this machine, so what this machine's load average
// says is no evidence about it whatsoever. Measured before the fix, gating
// leaves on load average pinned live concurrency at three in seven of eight
// runs — a starvation floor was the only admission that ever happened, on a
// sixteen-core host that was idle.
//
// The law is stronger than "the leaves are admitted": the host is not asked at
// all, and there is nothing here that could ask it. A reading taken here is a
// reading that can gate here, so the absence is the assertion.
func TestALeafIsAdmittedWithoutTheHostBeingAskedAnything(t *testing.T) {
governor := NewGovernor()
for inFlight := range GovernorInFlightCeiling {
if !governor.Admit(inFlight) {
t.Fatalf("a socket-parked leaf was refused at %d in flight on an idle machine's behalf", inFlight)
}
}
source, err := os.ReadFile("governor.go")
if err != nil {
t.Fatal(err)
}
for _, reading := range []string{"loadAverage", "NumCPU", "getloadavg", "/proc/loadavg"} {
if strings.Contains(string(source), reading) {
t.Errorf("the admission gate reads %s — a reading taken here is a reading that can gate here, "+
"and every leaf this process runs is a socket rather than a core", reading)
}
}
}
// The backstop is the only bound, and it is a backstop: it names a resource
// this process really can run out of — handles, goroutines — at a width no
// honest plan reaches.
func TestTheBackstopIsTheOnlyCeilingOnLeaves(t *testing.T) {
governor := NewGovernor()
if !governor.Admit(GovernorInFlightCeiling - 1) {
t.Fatal("the leaf under the backstop was refused")
}
if governor.Admit(GovernorInFlightCeiling) {
t.Fatal("the backstop let a leaf through above itself")
}
if GovernorInFlightCeiling <= 12 {
t.Fatalf("the backstop is %d, which is a scheduler rather than a backstop",
GovernorInFlightCeiling)
}
// A nil gate is not a closed one: the runner that never installed a
// governor still runs work.
var absent *Governor
if !absent.Admit(1) {
t.Fatal("a nil governor refused a claim")
}
}
func TestBackgroundJobYieldsTheInteractiveMachine(t *testing.T) {
var pgid, priority atomic.Int64
original := setProcessGroupPriority
setProcessGroupPriority = func(group, value int) {
pgid.Store(int64(group))
priority.Store(int64(value))
original(group, value)
}
t.Cleanup(func() { setProcessGroupPriority = original })
tools, _ := backgroundToolbox(t)
result := tools.Execute(context.Background(), "sh", `{"cmd":"sleep 30","bg":true}`)
if result.IsError {
t.Fatalf("background start failed: %s", result.Content)
}
// The real syscall is not asserted: a sandbox may forbid renicing, and the
// job must start either way. What is asserted is that the whole detached
// group is the thing asked to yield, and by how much.
tools.jobs.mutex.Lock()
job := tools.jobs.jobs[1]
tools.jobs.mutex.Unlock()
if job == nil {
t.Fatal("job 1 not registered")
}
if got := int(pgid.Load()); got != job.cmd.Process.Pid {
t.Fatalf("renice targeted %d, want the job's process group %d", got, job.cmd.Process.Pid)
}
if got := int(priority.Load()); got != backgroundJobNice {
t.Fatalf("background job priority = %d, want %d", got, backgroundJobNice)
}
}