Skip to content

Commit

Permalink
fix(tests): check PinProccessToCPU err
Browse files Browse the repository at this point in the history
  • Loading branch information
geyslan committed Oct 31, 2023
1 parent 15c624b commit 6262f09
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
8 changes: 6 additions & 2 deletions tests/integration/syscaller/cmd/syscaller.go
Expand Up @@ -11,7 +11,11 @@ import (
)

func main() {
testutils.PinProccessToCPU()
err := testutils.PinProccessToCPU()
if err != nil {
fmt.Fprintf(os.Stderr, "PinProccessToCPU: %v\n", err)
os.Exit(1)
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()

Expand All @@ -31,7 +35,7 @@ func main() {
syscallsToCall = append(syscallsToCall, events.ID(syscallNum))
}

err := changeOwnComm(callerComm)
err = changeOwnComm(callerComm)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
Expand Down
5 changes: 3 additions & 2 deletions tests/testutils/cpu.go
Expand Up @@ -7,7 +7,7 @@ import (
const CPUForTests = 0 // CPU to pin test processes to

// PinProccessToCPU pins the current process to a specific CPU
func PinProccessToCPU(id ...int) {
func PinProccessToCPU(id ...int) error {
if len(id) == 0 {
id = append(id, CPUForTests)
}
Expand All @@ -16,5 +16,6 @@ func PinProccessToCPU(id ...int) {
for _, i := range id {
cpuMask.Set(i)
}
_ = unix.SchedSetaffinity(0, &cpuMask)

return unix.SchedSetaffinity(0, &cpuMask)
}
10 changes: 10 additions & 0 deletions tests/testutils/errors.go
Expand Up @@ -52,6 +52,16 @@ func (e *commandFailed) Error() string {
return fmt.Sprintf("command '%s' failed with error: %s", e.command, e.err)
}

// failedToPinProcessToCPU is returned when a command fails to pin to a CPU.
type failedToPinProcessToCPU struct {
command string
err error
}

func (e *failedToPinProcessToCPU) Error() string {
return fmt.Sprintf("failed to pin command '%s' to CPU: %s", e.command, e.err)
}

// failedToParseCmd is returned when a command fails to parse.
type failedToParseCmd struct {
command string
Expand Down
5 changes: 4 additions & 1 deletion tests/testutils/exec.go
Expand Up @@ -49,7 +49,10 @@ func ParseCmd(fullCmd string) (string, []string, error) {

// ExecPinnedCmdWithTimeout executes a cmd with a timeout and returns the PID of the process.
func ExecPinnedCmdWithTimeout(command string, timeout time.Duration) (int, error) {
PinProccessToCPU() // pin this goroutine to a specific CPU
err := PinProccessToCPU() // pin this goroutine to a specific CPU
if err != nil {
return 0, &failedToPinProcessToCPU{command: command, err: err}
}
runtime.LockOSThread() // wire this goroutine to a specific OS thread
defer runtime.UnlockOSThread() // unlock the thread when we're done

Expand Down

0 comments on commit 6262f09

Please sign in to comment.