Skip to content

Commit

Permalink
procstats: fix tests on Macs
Browse files Browse the repository at this point in the history
Previously they would fail with an anticipated error; instead, skip
the failing test on Darwin.
  • Loading branch information
kevinburkesegment committed Jun 19, 2024
1 parent cf67f29 commit 74e2696
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
10 changes: 9 additions & 1 deletion procstats/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,19 @@ type ProcInfo struct {
Threads ThreadInfo
}

// CollectProcInfo return a ProcInfo and error (if any) for a given PID.
// CollectProcInfo returns a ProcInfo and error (if any) for a given PID.
func CollectProcInfo(pid int) (ProcInfo, error) {
return collectProcInfo(pid)
}

type OSUnsupportedError struct {
Msg string
}

func (o *OSUnsupportedError) Error() string {
return o.Msg
}

// CPUInfo holds statistics and configuration details for a process.
type CPUInfo struct {
User time.Duration // user cpu time used by the process
Expand Down
3 changes: 1 addition & 2 deletions procstats/proc_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ static mach_port_t mach_task_self() { return mach_task_self_; }
import "C"

import (
"errors"
"fmt"
"os"
"syscall"
Expand All @@ -35,7 +34,7 @@ func collectProcInfo(pid int) (info ProcInfo, err error) {
defer func() { err = convertPanicToError(recover()) }()

if pid != os.Getpid() {
panic(errors.New("on darwin systems only metrics of the current process can be collected"))
panic(&OSUnsupportedError{Msg: "on darwin systems only metrics of the current process can be collected"})
}

self := C.mach_port_name_t(C.mach_task_self())
Expand Down
25 changes: 20 additions & 5 deletions procstats/proc_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package procstats

import (
"io/ioutil"
"errors"
"io"
"os"
"os/exec"
"runtime"
"testing"
"time"

Expand All @@ -18,23 +20,36 @@ func TestProcMetrics(t *testing.T) {
t.Run("child", func(t *testing.T) {
cmd := exec.Command("yes")
cmd.Stdin = os.Stdin
cmd.Stdout = ioutil.Discard
cmd.Stderr = ioutil.Discard
cmd.Stdout = io.Discard
cmd.Stderr = io.Discard

cmd.Start()
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
time.Sleep(200 * time.Millisecond)
testProcMetrics(t, cmd.Process.Pid)
cmd.Process.Signal(os.Interrupt)
cmd.Wait()
if err := cmd.Wait(); err != nil {
t.Fatal(err)
}
})
}

func testProcMetrics(t *testing.T, pid int) {
t.Helper()
h := &statstest.Handler{}
e := stats.NewEngine("", h)

proc := NewProcMetricsWith(e, pid)

// for darwin - catch the "can't collect child metrics" error before
// starting the test
_, err := CollectProcInfo(proc.pid)
var o *OSUnsupportedError
if errors.As(err, &o) {
t.Skipf("can't run test because current OS is unsupported: %v", runtime.GOOS)
}

for i := 0; i != 10; i++ {
t.Logf("collect number %d", i)
proc.Collect()
Expand Down

0 comments on commit 74e2696

Please sign in to comment.