reloader_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2016, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package common
  20. import (
  21. "bytes"
  22. "io/ioutil"
  23. "os"
  24. "testing"
  25. "time"
  26. )
  27. func TestReloader(t *testing.T) {
  28. fileName := "reloader_test.dat"
  29. initialContents := []byte("contents1\n")
  30. modifiedContents := []byte("contents2\n")
  31. var file struct {
  32. ReloadableFile
  33. contents []byte
  34. }
  35. file.ReloadableFile = NewReloadableFile(
  36. fileName,
  37. func(filename string) error {
  38. contents, err := ioutil.ReadFile(filename)
  39. if err != nil {
  40. return err
  41. }
  42. file.contents = contents
  43. return nil
  44. })
  45. // Test: initial load
  46. err := ioutil.WriteFile(fileName, initialContents, 0600)
  47. if err != nil {
  48. t.Fatalf("WriteFile failed: %s", err)
  49. }
  50. time.Sleep(2 * time.Second)
  51. fileInfo, err := os.Stat(fileName)
  52. if err != nil {
  53. t.Fatalf("Stat failed: %s", err)
  54. }
  55. t.Logf("ModTime: %s", fileInfo.ModTime())
  56. reloaded, err := file.Reload()
  57. if err != nil {
  58. t.Fatalf("Reload failed: %s", err)
  59. }
  60. if !reloaded {
  61. t.Fatalf("Unexpected non-reload")
  62. }
  63. if bytes.Compare(file.contents, initialContents) != 0 {
  64. t.Fatalf("Unexpected contents")
  65. }
  66. // Test: reload unchanged file
  67. reloaded, err = file.Reload()
  68. if err != nil {
  69. t.Fatalf("Reload failed: %s", err)
  70. }
  71. if reloaded {
  72. t.Fatalf("Unexpected reload")
  73. }
  74. if bytes.Compare(file.contents, initialContents) != 0 {
  75. t.Fatalf("Unexpected contents")
  76. }
  77. // Test: reload changed file
  78. err = ioutil.WriteFile(fileName, modifiedContents, 0600)
  79. if err != nil {
  80. t.Fatalf("WriteFile failed: %s", err)
  81. }
  82. // TODO: without the sleeps, the os.Stat ModTime doesn't
  83. // change and IsFileChanged fails to detect the modification.
  84. time.Sleep(2 * time.Second)
  85. fileInfo, err = os.Stat(fileName)
  86. if err != nil {
  87. t.Fatalf("Stat failed: %s", err)
  88. }
  89. t.Logf("ModTime: %s", fileInfo.ModTime())
  90. reloaded, err = file.Reload()
  91. if err != nil {
  92. t.Fatalf("Reload failed: %s", err)
  93. }
  94. if !reloaded {
  95. t.Fatalf("Unexpected non-reload")
  96. }
  97. if bytes.Compare(file.contents, modifiedContents) != 0 {
  98. t.Fatalf("Unexpected contents")
  99. }
  100. }