purge_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. errch := PurgeFile(dir, "test", 3, time.Millisecond, stop)
  38. for i := 5; i < 10; i++ {
  39. _, err := os.Create(path.Join(dir, fmt.Sprintf("%d.test", i)))
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. time.Sleep(2 * time.Millisecond)
  44. }
  45. fnames, err := ReadDir(dir)
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. wnames := []string{"7.test", "8.test", "9.test"}
  50. if !reflect.DeepEqual(fnames, wnames) {
  51. t.Errorf("filenames = %v, want %v", fnames, wnames)
  52. }
  53. select {
  54. case err := <-errch:
  55. t.Errorf("unexpected purge error %v", err)
  56. case <-time.After(time.Millisecond):
  57. }
  58. close(stop)
  59. }
  60. func TestPurgeFileHoldingLock(t *testing.T) {
  61. dir, err := ioutil.TempDir("", "purgefile")
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. defer os.RemoveAll(dir)
  66. for i := 0; i < 10; i++ {
  67. _, err := os.Create(path.Join(dir, fmt.Sprintf("%d.test", i)))
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. }
  72. // create a purge barrier at 5
  73. l, err := NewLock(path.Join(dir, fmt.Sprintf("%d.test", 5)))
  74. err = l.Lock()
  75. if err != nil {
  76. t.Fatal(err)
  77. }
  78. stop := make(chan struct{})
  79. errch := PurgeFile(dir, "test", 3, time.Millisecond, stop)
  80. time.Sleep(5 * time.Millisecond)
  81. fnames, err := ReadDir(dir)
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. wnames := []string{"5.test", "6.test", "7.test", "8.test", "9.test"}
  86. if !reflect.DeepEqual(fnames, wnames) {
  87. t.Errorf("filenames = %v, want %v", fnames, wnames)
  88. }
  89. select {
  90. case err := <-errch:
  91. t.Errorf("unexpected purge error %v", err)
  92. case <-time.After(time.Millisecond):
  93. }
  94. // remove the purge barrier
  95. err = l.Unlock()
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. err = l.Destroy()
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. time.Sleep(5 * time.Millisecond)
  104. fnames, err = ReadDir(dir)
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. wnames = []string{"7.test", "8.test", "9.test"}
  109. if !reflect.DeepEqual(fnames, wnames) {
  110. t.Errorf("filenames = %v, want %v", fnames, wnames)
  111. }
  112. select {
  113. case err := <-errch:
  114. t.Errorf("unexpected purge error %v", err)
  115. case <-time.After(time.Millisecond):
  116. }
  117. close(stop)
  118. }