github.com/Andyfoo/golang/x/sys@v0.0.0-20190901054642-57c1bf301704/unix/syscall_bsd_test.go (about) 1 // Copyright 2014 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. 4 5 // +build darwin dragonfly freebsd openbsd 6 7 package unix_test 8 9 import ( 10 "os/exec" 11 "runtime" 12 "testing" 13 "time" 14 15 "github.com/Andyfoo/golang/x/sys/unix" 16 ) 17 18 func TestGetfsstat(t *testing.T) { 19 n, err := unix.Getfsstat(nil, unix.MNT_NOWAIT) 20 if err != nil { 21 t.Fatal(err) 22 } 23 24 data := make([]unix.Statfs_t, n) 25 n2, err := unix.Getfsstat(data, unix.MNT_NOWAIT) 26 if err != nil { 27 t.Fatal(err) 28 } 29 if n != n2 { 30 t.Errorf("Getfsstat(nil) = %d, but subsequent Getfsstat(slice) = %d", n, n2) 31 } 32 for i, stat := range data { 33 if stat == (unix.Statfs_t{}) { 34 t.Errorf("index %v is an empty Statfs_t struct", i) 35 } 36 } 37 if t.Failed() { 38 for i, stat := range data[:n2] { 39 t.Logf("data[%v] = %+v", i, stat) 40 } 41 mount, err := exec.Command("mount").CombinedOutput() 42 if err != nil { 43 t.Logf("mount: %v\n%s", err, mount) 44 } else { 45 t.Logf("mount: %s", mount) 46 } 47 } 48 } 49 50 func TestSelect(t *testing.T) { 51 err := unix.Select(0, nil, nil, nil, &unix.Timeval{Sec: 0, Usec: 0}) 52 if err != nil { 53 t.Fatalf("Select: %v", err) 54 } 55 56 dur := 250 * time.Millisecond 57 tv := unix.NsecToTimeval(int64(dur)) 58 start := time.Now() 59 err = unix.Select(0, nil, nil, nil, &tv) 60 took := time.Since(start) 61 if err != nil { 62 t.Fatalf("Select: %v", err) 63 } 64 65 // On some BSDs the actual timeout might also be slightly less than the requested. 66 // Add an acceptable margin to avoid flaky tests. 67 if took < dur*2/3 { 68 t.Errorf("Select: timeout should have been at least %v, got %v", dur, took) 69 } 70 } 71 72 func TestSysctlRaw(t *testing.T) { 73 if runtime.GOOS == "openbsd" { 74 t.Skip("kern.proc.pid does not exist on OpenBSD") 75 } 76 77 _, err := unix.SysctlRaw("kern.proc.pid", unix.Getpid()) 78 if err != nil { 79 t.Fatal(err) 80 } 81 } 82 83 func TestSysctlUint32(t *testing.T) { 84 maxproc, err := unix.SysctlUint32("kern.maxproc") 85 if err != nil { 86 t.Fatal(err) 87 } 88 t.Logf("kern.maxproc: %v", maxproc) 89 }