purge_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package fileutil
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "path"
  20. "reflect"
  21. "testing"
  22. "time"
  23. )
  24. func TestPurgeFile(t *testing.T) {
  25. dir, err := ioutil.TempDir("", "purgefile")
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. defer os.RemoveAll(dir)
  30. // minimal file set
  31. for i := 0; i < 3; i++ {
  32. f, ferr := os.Create(path.Join(dir, fmt.Sprintf("%d.test", i)))
  33. if ferr != nil {
  34. t.Fatal(err)
  35. }
  36. f.Close()
  37. }
  38. stop, purgec := make(chan struct{}), make(chan string, 10)
  39. // keep 3 most recent files
  40. errch := purgeFile(dir, "test", 3, time.Millisecond, stop, purgec)
  41. select {
  42. case f := <-purgec:
  43. t.Errorf("unexpected purge on %q", f)
  44. case <-time.After(10 * time.Millisecond):
  45. }
  46. // rest of the files
  47. for i := 4; i < 10; i++ {
  48. go func(n int) {
  49. f, ferr := os.Create(path.Join(dir, fmt.Sprintf("%d.test", n)))
  50. if ferr != nil {
  51. t.Fatal(err)
  52. }
  53. f.Close()
  54. }(i)
  55. }
  56. // watch files purge away
  57. for i := 4; i < 10; i++ {
  58. select {
  59. case <-purgec:
  60. case <-time.After(time.Second):
  61. t.Errorf("purge took too long")
  62. }
  63. }
  64. fnames, rerr := ReadDir(dir)
  65. if rerr != nil {
  66. t.Fatal(rerr)
  67. }
  68. wnames := []string{"7.test", "8.test", "9.test"}
  69. if !reflect.DeepEqual(fnames, wnames) {
  70. t.Errorf("filenames = %v, want %v", fnames, wnames)
  71. }
  72. // no error should be reported from purge routine
  73. select {
  74. case f := <-purgec:
  75. t.Errorf("unexpected purge on %q", f)
  76. case err := <-errch:
  77. t.Errorf("unexpected purge error %v", err)
  78. case <-time.After(10 * time.Millisecond):
  79. }
  80. close(stop)
  81. }
  82. func TestPurgeFileHoldingLockFile(t *testing.T) {
  83. dir, err := ioutil.TempDir("", "purgefile")
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. defer os.RemoveAll(dir)
  88. for i := 0; i < 10; i++ {
  89. var f *os.File
  90. f, err = os.Create(path.Join(dir, fmt.Sprintf("%d.test", i)))
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. f.Close()
  95. }
  96. // create a purge barrier at 5
  97. p := path.Join(dir, fmt.Sprintf("%d.test", 5))
  98. l, err := LockFile(p, os.O_WRONLY, PrivateFileMode)
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. stop, purgec := make(chan struct{}), make(chan string, 10)
  103. errch := purgeFile(dir, "test", 3, time.Millisecond, stop, purgec)
  104. for i := 0; i < 5; i++ {
  105. select {
  106. case <-purgec:
  107. case <-time.After(time.Second):
  108. t.Fatalf("purge took too long")
  109. }
  110. }
  111. fnames, rerr := ReadDir(dir)
  112. if rerr != nil {
  113. t.Fatal(rerr)
  114. }
  115. wnames := []string{"5.test", "6.test", "7.test", "8.test", "9.test"}
  116. if !reflect.DeepEqual(fnames, wnames) {
  117. t.Errorf("filenames = %v, want %v", fnames, wnames)
  118. }
  119. select {
  120. case s := <-purgec:
  121. t.Errorf("unexpected purge %q", s)
  122. case err = <-errch:
  123. t.Errorf("unexpected purge error %v", err)
  124. case <-time.After(10 * time.Millisecond):
  125. }
  126. // remove the purge barrier
  127. if err = l.Close(); err != nil {
  128. t.Fatal(err)
  129. }
  130. // wait for rest of purges (5, 6)
  131. for i := 0; i < 2; i++ {
  132. select {
  133. case <-purgec:
  134. case <-time.After(time.Second):
  135. t.Fatalf("purge took too long")
  136. }
  137. }
  138. fnames, rerr = ReadDir(dir)
  139. if rerr != nil {
  140. t.Fatal(rerr)
  141. }
  142. wnames = []string{"7.test", "8.test", "9.test"}
  143. if !reflect.DeepEqual(fnames, wnames) {
  144. t.Errorf("filenames = %v, want %v", fnames, wnames)
  145. }
  146. select {
  147. case f := <-purgec:
  148. t.Errorf("unexpected purge on %q", f)
  149. case err := <-errch:
  150. t.Errorf("unexpected purge error %v", err)
  151. case <-time.After(10 * time.Millisecond):
  152. }
  153. close(stop)
  154. }