選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

94 行
2.0 KiB

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