Skip to content

Commit 289d012

Browse files
committed
Fix Proc.Limits limit name matching
I was working on improving this algorithm to reduce the number of allocations when I found out that with the addition of the additional test cases, `Max processes` was failing to match the `switch` statement as for some reason the limit name has a trailing whitespace. By trimming the spaces it now matches all cases. Signed-off-by: Leandro López (inkel) <[email protected]>
1 parent a49c6d2 commit 289d012

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

Diff for: proc_limits.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"os"
2020
"regexp"
2121
"strconv"
22+
"strings"
2223
)
2324

2425
// ProcLimits represents the soft limits for each of the process's resource
@@ -106,7 +107,7 @@ func (p Proc) Limits() (ProcLimits, error) {
106107
return ProcLimits{}, fmt.Errorf("%w: couldn't parse %q line %q", ErrFileParse, f.Name(), s.Text())
107108
}
108109

109-
switch fields[1] {
110+
switch strings.TrimSpace(fields[1]) {
110111
case "Max cpu time":
111112
l.CPUTime, err = parseUint(fields[2])
112113
case "Max file size":

Diff for: proc_limits_test.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,21 @@ func TestLimits(t *testing.T) {
3232
have uint64
3333
}{
3434
{name: "cpu time", want: 18446744073709551615, have: l.CPUTime},
35+
{name: "file size", want: 18446744073709551615, have: l.FileSize},
36+
{name: "data size", want: 18446744073709551615, have: l.DataSize},
37+
{name: "stack size", want: 8388608, have: l.StackSize},
38+
{name: "core file size", want: 0, have: l.CoreFileSize},
39+
{name: "resident set", want: 18446744073709551615, have: l.ResidentSet},
40+
{name: "processes", want: 62898, have: l.Processes},
3541
{name: "open files", want: 2048, have: l.OpenFiles},
42+
{name: "locked memory", want: 18446744073708503040, have: l.LockedMemory},
43+
{name: "address space", want: 8589934592, have: l.AddressSpace},
44+
{name: "file locks", want: 18446744073709551615, have: l.FileLocks},
45+
{name: "pending signals", want: 62898, have: l.PendingSignals},
3646
{name: "msgqueue size", want: 819200, have: l.MsqqueueSize},
3747
{name: "nice priority", want: 0, have: l.NicePriority},
38-
{name: "address space", want: 8589934592, have: l.AddressSpace},
48+
{name: "realtime priority", want: 0, have: l.RealtimePriority},
49+
{name: "realtime timeout", want: 18446744073709551615, have: l.RealtimeTimeout},
3950
} {
4051
if test.want != test.have {
4152
t.Errorf("want %s %d, have %d", test.name, test.want, test.have)

0 commit comments

Comments
 (0)