Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

procstats: fix tests on Macs #158

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
28 changes: 23 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,39 @@ 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()
waitErr := cmd.Wait()
if exitErr, ok := waitErr.(*exec.ExitError); ok && exitErr.Error() == "signal: interrupt" {
// This is expected from stopping the process
} else {
t.Fatal(waitErr)
}
})
}

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
Loading