purge_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2015 CoreOS, Inc.
  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. for i := 0; i < 5; i++ {
  31. _, err = os.Create(path.Join(dir, fmt.Sprintf("%d.test", i)))
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. }
  36. stop := make(chan struct{})
  37. // keep at most 3 most recent files
  38. errch := PurgeFile(dir, "test", 3, time.Millisecond, stop)
  39. // create 5 more files
  40. for i := 5; i < 10; i++ {
  41. _, err = os.Create(path.Join(dir, fmt.Sprintf("%d.test", i)))
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. time.Sleep(10 * time.Millisecond)
  46. }
  47. // purge routine should purge 7 out of 10 files and only keep the
  48. // 3 most recent ones.
  49. // wait for purging for at most 100ms.
  50. var fnames []string
  51. for i := 0; i < 10; i++ {
  52. fnames, err = ReadDir(dir)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. if len(fnames) <= 3 {
  57. break
  58. }
  59. time.Sleep(10 * time.Millisecond)
  60. }
  61. wnames := []string{"7.test", "8.test", "9.test"}
  62. if !reflect.DeepEqual(fnames, wnames) {
  63. t.Errorf("filenames = %v, want %v", fnames, wnames)
  64. }
  65. // no error should be reported from purge routine
  66. select {
  67. case err := <-errch:
  68. t.Errorf("unexpected purge error %v", err)
  69. case <-time.After(time.Millisecond):
  70. }
  71. close(stop)
  72. }
  73. func TestPurgeFileHoldingLock(t *testing.T) {
  74. dir, err := ioutil.TempDir("", "purgefile")
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. defer os.RemoveAll(dir)
  79. for i := 0; i < 10; i++ {
  80. _, err = os.Create(path.Join(dir, fmt.Sprintf("%d.test", i)))
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. }
  85. // create a purge barrier at 5
  86. l, err := NewLock(path.Join(dir, fmt.Sprintf("%d.test", 5)))
  87. err = l.Lock()
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. stop := make(chan struct{})
  92. errch := PurgeFile(dir, "test", 3, time.Millisecond, stop)
  93. var fnames []string
  94. for i := 0; i < 10; i++ {
  95. fnames, err = ReadDir(dir)
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. if len(fnames) <= 5 {
  100. break
  101. }
  102. time.Sleep(10 * time.Millisecond)
  103. }
  104. wnames := []string{"5.test", "6.test", "7.test", "8.test", "9.test"}
  105. if !reflect.DeepEqual(fnames, wnames) {
  106. t.Errorf("filenames = %v, want %v", fnames, wnames)
  107. }
  108. select {
  109. case err = <-errch:
  110. t.Errorf("unexpected purge error %v", err)
  111. case <-time.After(time.Millisecond):
  112. }
  113. // remove the purge barrier
  114. err = l.Unlock()
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. err = l.Destroy()
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. for i := 0; i < 10; i++ {
  123. fnames, err = ReadDir(dir)
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. if len(fnames) <= 3 {
  128. break
  129. }
  130. time.Sleep(10 * time.Millisecond)
  131. }
  132. wnames = []string{"7.test", "8.test", "9.test"}
  133. if !reflect.DeepEqual(fnames, wnames) {
  134. t.Errorf("filenames = %v, want %v", fnames, wnames)
  135. }
  136. select {
  137. case err := <-errch:
  138. t.Errorf("unexpected purge error %v", err)
  139. case <-time.After(time.Millisecond):
  140. }
  141. close(stop)
  142. }