1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 "golang.org/x/sys/unix"
18 func TestPoll(t *testing.T) {
19 f, cleanup := mktmpfifo(t)
24 ok := make(chan bool, 1)
27 case <-time.After(10 * timeout * time.Millisecond):
28 t.Errorf("Poll: failed to timeout after %d milliseconds", 10*timeout)
33 fds := []unix.PollFd{{Fd: int32(f.Fd()), Events: unix.POLLIN}}
34 n, err := unix.Poll(fds, timeout)
37 t.Errorf("Poll: unexpected error: %v", err)
41 t.Errorf("Poll: wrong number of events: got %v, expected %v", n, 0)
46 func TestPpoll(t *testing.T) {
47 f, cleanup := mktmpfifo(t)
50 const timeout = 100 * time.Millisecond
52 ok := make(chan bool, 1)
55 case <-time.After(10 * timeout):
56 t.Errorf("Ppoll: failed to timeout after %d", 10*timeout)
61 fds := []unix.PollFd{{Fd: int32(f.Fd()), Events: unix.POLLIN}}
62 timeoutTs := unix.NsecToTimespec(int64(timeout))
63 n, err := unix.Ppoll(fds, &timeoutTs, nil)
66 t.Errorf("Ppoll: unexpected error: %v", err)
70 t.Errorf("Ppoll: wrong number of events: got %v, expected %v", n, 0)
75 // mktmpfifo creates a temporary FIFO and provides a cleanup function.
76 func mktmpfifo(t *testing.T) (*os.File, func()) {
77 err := unix.Mkfifo("fifo", 0666)
79 t.Fatalf("mktmpfifo: failed to create FIFO: %v", err)
82 f, err := os.OpenFile("fifo", os.O_RDWR, 0666)
85 t.Fatalf("mktmpfifo: failed to open FIFO: %v", err)
94 func TestTime(t *testing.T) {
96 ut2, err := unix.Time(&ut)
98 t.Fatalf("Time: %v", err)
101 t.Errorf("Time: return value %v should be equal to argument %v", ut2, ut)
106 for i := 0; i < 10; i++ {
107 ut, err = unix.Time(nil)
109 t.Fatalf("Time: %v", err)
114 if int64(ut) == now.Unix() {
119 t.Errorf("Time: return value %v should be nearly equal to time.Now().Unix() %v", ut, now.Unix())
122 func TestUtime(t *testing.T) {
127 buf := &unix.Utimbuf{
131 err := unix.Utime("file1", buf)
133 t.Fatalf("Utime: %v", err)
136 fi, err := os.Stat("file1")
141 if fi.ModTime().Unix() != 12345 {
142 t.Errorf("Utime: failed to change modtime: expected %v, got %v", 12345, fi.ModTime().Unix())
146 func TestGetrlimit(t *testing.T) {
148 err := unix.Getrlimit(unix.RLIMIT_AS, &rlim)
150 t.Fatalf("Getrlimit: %v", err)
154 // utilities taken from os/os_test.go
156 func touch(t *testing.T, name string) {
157 f, err := os.Create(name)
161 if err := f.Close(); err != nil {
166 // chtmpdir changes the working directory to a new temporary directory and
167 // provides a cleanup function. Used when PWD is read-only.
168 func chtmpdir(t *testing.T) func() {
169 oldwd, err := os.Getwd()
171 t.Fatalf("chtmpdir: %v", err)
173 d, err := ioutil.TempDir("", "test")
175 t.Fatalf("chtmpdir: %v", err)
177 if err := os.Chdir(d); err != nil {
178 t.Fatalf("chtmpdir: %v", err)
181 if err := os.Chdir(oldwd); err != nil {
182 t.Fatalf("chtmpdir: %v", err)