From 289d012cca8246ae353e38af564c38ab7ce5cbc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leandro=20L=C3=B3pez=20=28inkel=29?= Date: Thu, 19 Sep 2024 15:42:57 -0300 Subject: [PATCH] Fix Proc.Limits limit name matching MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- proc_limits.go | 3 ++- proc_limits_test.go | 13 ++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/proc_limits.go b/proc_limits.go index 9530b14bc..7dc6c43f8 100644 --- a/proc_limits.go +++ b/proc_limits.go @@ -19,6 +19,7 @@ import ( "os" "regexp" "strconv" + "strings" ) // ProcLimits represents the soft limits for each of the process's resource @@ -106,7 +107,7 @@ func (p Proc) Limits() (ProcLimits, error) { return ProcLimits{}, fmt.Errorf("%w: couldn't parse %q line %q", ErrFileParse, f.Name(), s.Text()) } - switch fields[1] { + switch strings.TrimSpace(fields[1]) { case "Max cpu time": l.CPUTime, err = parseUint(fields[2]) case "Max file size": diff --git a/proc_limits_test.go b/proc_limits_test.go index bb8ae61b5..ba41a267e 100644 --- a/proc_limits_test.go +++ b/proc_limits_test.go @@ -32,10 +32,21 @@ func TestLimits(t *testing.T) { have uint64 }{ {name: "cpu time", want: 18446744073709551615, have: l.CPUTime}, + {name: "file size", want: 18446744073709551615, have: l.FileSize}, + {name: "data size", want: 18446744073709551615, have: l.DataSize}, + {name: "stack size", want: 8388608, have: l.StackSize}, + {name: "core file size", want: 0, have: l.CoreFileSize}, + {name: "resident set", want: 18446744073709551615, have: l.ResidentSet}, + {name: "processes", want: 62898, have: l.Processes}, {name: "open files", want: 2048, have: l.OpenFiles}, + {name: "locked memory", want: 18446744073708503040, have: l.LockedMemory}, + {name: "address space", want: 8589934592, have: l.AddressSpace}, + {name: "file locks", want: 18446744073709551615, have: l.FileLocks}, + {name: "pending signals", want: 62898, have: l.PendingSignals}, {name: "msgqueue size", want: 819200, have: l.MsqqueueSize}, {name: "nice priority", want: 0, have: l.NicePriority}, - {name: "address space", want: 8589934592, have: l.AddressSpace}, + {name: "realtime priority", want: 0, have: l.RealtimePriority}, + {name: "realtime timeout", want: 18446744073709551615, have: l.RealtimeTimeout}, } { if test.want != test.have { t.Errorf("want %s %d, have %d", test.name, test.want, test.have)