File size: 1,794 Bytes
e36aeda | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | // Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package os_test
import (
"internal/testenv"
"os"
"os/signal"
"runtime"
"syscall"
"testing"
"time"
)
func TestProcessLiteral(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Process literals do not work on Windows. FindProcess/etc must initialize the process handle")
}
if runtime.GOARCH == "wasm" {
t.Skip("Signals send + notify not fully supported om wasm port")
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
defer signal.Stop(c)
p := &os.Process{Pid: os.Getpid()}
if err := p.Signal(os.Interrupt); err != nil {
t.Fatalf("Signal got err %v, want nil", err)
}
// Verify we actually received the signal.
select {
case <-time.After(1 * time.Second):
t.Error("timeout waiting for signal")
case <-c:
// Good
}
}
func TestProcessReleaseTwice(t *testing.T) {
testenv.MustHaveGoBuild(t)
t.Parallel()
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Pipe() got err %v, want nil", err)
}
defer r.Close()
defer w.Close()
p, err := os.StartProcess(testenv.GoToolPath(t), []string{"go"}, &os.ProcAttr{
// N.B. On Windows, StartProcess requires exactly 3 Files. Pass
// in a dummy pipe to avoid irrelevant output on the test stdout.
Files: []*os.File{r, w, w},
})
if err != nil {
t.Fatalf("starting test process: %v", err)
}
if err := p.Release(); err != nil {
t.Fatalf("first Release: got err %v, want nil", err)
}
err = p.Release()
// We want EINVAL from a second Release call only on Windows.
var want error
if runtime.GOOS == "windows" {
want = syscall.EINVAL
}
if err != want {
t.Fatalf("second Release: got err %v, want %v", err, want)
}
}
|