reloader_test.go 2.3 KB

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