syscall_bsd_test.go 2.0 KB

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