A lightweight mechanism to provide an *instant kickstart* to a Go web server instance upon changing any Go source files under the project directory (and its subdirectories).

syscall_linux_test.go 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.
  4. // +build linux
  5. package unix_test
  6. import (
  7. "io/ioutil"
  8. "os"
  9. "testing"
  10. "time"
  11. "golang.org/x/sys/unix"
  12. )
  13. func TestIoctlGetInt(t *testing.T) {
  14. f, err := os.Open("/dev/random")
  15. if err != nil {
  16. t.Fatalf("failed to open device: %v", err)
  17. }
  18. defer f.Close()
  19. v, err := unix.IoctlGetInt(int(f.Fd()), unix.RNDGETENTCNT)
  20. if err != nil {
  21. t.Fatalf("failed to perform ioctl: %v", err)
  22. }
  23. t.Logf("%d bits of entropy available", v)
  24. }
  25. func TestPoll(t *testing.T) {
  26. f, cleanup := mktmpfifo(t)
  27. defer cleanup()
  28. const timeout = 100
  29. ok := make(chan bool, 1)
  30. go func() {
  31. select {
  32. case <-time.After(10 * timeout * time.Millisecond):
  33. t.Errorf("Poll: failed to timeout after %d milliseconds", 10*timeout)
  34. case <-ok:
  35. }
  36. }()
  37. fds := []unix.PollFd{{Fd: int32(f.Fd()), Events: unix.POLLIN}}
  38. n, err := unix.Poll(fds, timeout)
  39. ok <- true
  40. if err != nil {
  41. t.Errorf("Poll: unexpected error: %v", err)
  42. return
  43. }
  44. if n != 0 {
  45. t.Errorf("Poll: wrong number of events: got %v, expected %v", n, 0)
  46. return
  47. }
  48. }
  49. func TestPpoll(t *testing.T) {
  50. f, cleanup := mktmpfifo(t)
  51. defer cleanup()
  52. const timeout = 100 * time.Millisecond
  53. ok := make(chan bool, 1)
  54. go func() {
  55. select {
  56. case <-time.After(10 * timeout):
  57. t.Errorf("Ppoll: failed to timeout after %d", 10*timeout)
  58. case <-ok:
  59. }
  60. }()
  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)
  64. ok <- true
  65. if err != nil {
  66. t.Errorf("Ppoll: unexpected error: %v", err)
  67. return
  68. }
  69. if n != 0 {
  70. t.Errorf("Ppoll: wrong number of events: got %v, expected %v", n, 0)
  71. return
  72. }
  73. }
  74. // mktmpfifo creates a temporary FIFO and provides a cleanup function.
  75. func mktmpfifo(t *testing.T) (*os.File, func()) {
  76. err := unix.Mkfifo("fifo", 0666)
  77. if err != nil {
  78. t.Fatalf("mktmpfifo: failed to create FIFO: %v", err)
  79. }
  80. f, err := os.OpenFile("fifo", os.O_RDWR, 0666)
  81. if err != nil {
  82. os.Remove("fifo")
  83. t.Fatalf("mktmpfifo: failed to open FIFO: %v", err)
  84. }
  85. return f, func() {
  86. f.Close()
  87. os.Remove("fifo")
  88. }
  89. }
  90. func TestTime(t *testing.T) {
  91. var ut unix.Time_t
  92. ut2, err := unix.Time(&ut)
  93. if err != nil {
  94. t.Fatalf("Time: %v", err)
  95. }
  96. if ut != ut2 {
  97. t.Errorf("Time: return value %v should be equal to argument %v", ut2, ut)
  98. }
  99. var now time.Time
  100. for i := 0; i < 10; i++ {
  101. ut, err = unix.Time(nil)
  102. if err != nil {
  103. t.Fatalf("Time: %v", err)
  104. }
  105. now = time.Now()
  106. if int64(ut) == now.Unix() {
  107. return
  108. }
  109. }
  110. t.Errorf("Time: return value %v should be nearly equal to time.Now().Unix() %v", ut, now.Unix())
  111. }
  112. func TestUtime(t *testing.T) {
  113. defer chtmpdir(t)()
  114. touch(t, "file1")
  115. buf := &unix.Utimbuf{
  116. Modtime: 12345,
  117. }
  118. err := unix.Utime("file1", buf)
  119. if err != nil {
  120. t.Fatalf("Utime: %v", err)
  121. }
  122. fi, err := os.Stat("file1")
  123. if err != nil {
  124. t.Fatal(err)
  125. }
  126. if fi.ModTime().Unix() != 12345 {
  127. t.Errorf("Utime: failed to change modtime: expected %v, got %v", 12345, fi.ModTime().Unix())
  128. }
  129. }
  130. func TestGetrlimit(t *testing.T) {
  131. var rlim unix.Rlimit
  132. err := unix.Getrlimit(unix.RLIMIT_AS, &rlim)
  133. if err != nil {
  134. t.Fatalf("Getrlimit: %v", err)
  135. }
  136. }
  137. // utilities taken from os/os_test.go
  138. func touch(t *testing.T, name string) {
  139. f, err := os.Create(name)
  140. if err != nil {
  141. t.Fatal(err)
  142. }
  143. if err := f.Close(); err != nil {
  144. t.Fatal(err)
  145. }
  146. }
  147. // chtmpdir changes the working directory to a new temporary directory and
  148. // provides a cleanup function. Used when PWD is read-only.
  149. func chtmpdir(t *testing.T) func() {
  150. oldwd, err := os.Getwd()
  151. if err != nil {
  152. t.Fatalf("chtmpdir: %v", err)
  153. }
  154. d, err := ioutil.TempDir("", "test")
  155. if err != nil {
  156. t.Fatalf("chtmpdir: %v", err)
  157. }
  158. if err := os.Chdir(d); err != nil {
  159. t.Fatalf("chtmpdir: %v", err)
  160. }
  161. return func() {
  162. if err := os.Chdir(oldwd); err != nil {
  163. t.Fatalf("chtmpdir: %v", err)
  164. }
  165. os.RemoveAll(d)
  166. }
  167. }