reloader_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. "path/filepath"
  25. "testing"
  26. "time"
  27. )
  28. func TestReloader(t *testing.T) {
  29. dirname, err := ioutil.TempDir("", "psiphon-reloader-test")
  30. if err != nil {
  31. t.Fatalf("TempDir failed: %s", err)
  32. }
  33. defer os.RemoveAll(dirname)
  34. filename := filepath.Join(dirname, "reloader_test.dat")
  35. initialContents := []byte("contents1\n")
  36. modifiedContents := []byte("contents2\n")
  37. var file struct {
  38. ReloadableFile
  39. contents []byte
  40. }
  41. file.ReloadableFile = NewReloadableFile(
  42. filename,
  43. true,
  44. func(fileContent []byte, _ time.Time) error {
  45. file.contents = fileContent
  46. return nil
  47. })
  48. // Test: initial load
  49. err = ioutil.WriteFile(filename, initialContents, 0600)
  50. if err != nil {
  51. t.Fatalf("WriteFile failed: %s", err)
  52. }
  53. reloaded, err := file.Reload()
  54. if err != nil {
  55. t.Fatalf("Reload failed: %s", err)
  56. }
  57. if !reloaded {
  58. t.Fatalf("Unexpected non-reload")
  59. }
  60. if !bytes.Equal(file.contents, initialContents) {
  61. t.Fatalf("Unexpected contents")
  62. }
  63. // Test: reload unchanged file
  64. reloaded, err = file.Reload()
  65. if err != nil {
  66. t.Fatalf("Reload failed: %s", err)
  67. }
  68. if reloaded {
  69. t.Fatalf("Unexpected reload")
  70. }
  71. if !bytes.Equal(file.contents, initialContents) {
  72. t.Fatalf("Unexpected contents")
  73. }
  74. // Test: reload changed file
  75. err = ioutil.WriteFile(filename, modifiedContents, 0600)
  76. if err != nil {
  77. t.Fatalf("WriteFile failed: %s", err)
  78. }
  79. reloaded, err = file.Reload()
  80. if err != nil {
  81. t.Fatalf("Reload failed: %s", err)
  82. }
  83. if !reloaded {
  84. t.Fatalf("Unexpected non-reload")
  85. }
  86. if !bytes.Equal(file.contents, modifiedContents) {
  87. t.Fatalf("Unexpected contents")
  88. }
  89. }