lockfile_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package lockutil
  10. import (
  11. "flag"
  12. "fmt"
  13. "os"
  14. "testing"
  15. "time"
  16. "devt.de/krotik/common/fileutil"
  17. )
  18. const lfdir = "lockfiletest"
  19. const invalidFileName = "**" + string(0x0)
  20. func TestMain(m *testing.M) {
  21. flag.Parse()
  22. // Setup
  23. if res, _ := fileutil.PathExists(lfdir); res {
  24. os.RemoveAll(lfdir)
  25. }
  26. err := os.Mkdir(lfdir, 0770)
  27. if err != nil {
  28. fmt.Print("Could not create test directory:", err.Error())
  29. os.Exit(1)
  30. }
  31. // Run the tests
  32. res := m.Run()
  33. // Teardown
  34. err = os.RemoveAll(lfdir)
  35. if err != nil {
  36. fmt.Print("Could not remove test directory:", err.Error())
  37. }
  38. os.Exit(res)
  39. }
  40. func TestLockFile(t *testing.T) {
  41. duration := time.Duration(3) * time.Millisecond
  42. // Straight case
  43. lf := NewLockFile(lfdir+"/test1.lck", duration)
  44. if err := lf.Start(); err != nil {
  45. t.Error(err)
  46. return
  47. }
  48. if err := lf.Finish(); err != nil {
  49. t.Error(err)
  50. return
  51. }
  52. // Simulate 2 process opening the same lockfile
  53. lf1 := &LockFile{lfdir + "/test2.lck", 1, duration, nil, false}
  54. if err := lf1.Start(); err != nil {
  55. t.Error(err)
  56. return
  57. }
  58. lf2 := &LockFile{lfdir + "/test2.lck", 2, duration, nil, false}
  59. if err := lf2.Start(); err == nil {
  60. t.Error("Unexpected result while starting lockfile watch:", err)
  61. return
  62. }
  63. if err := lf1.Finish(); err != nil {
  64. t.Error(err)
  65. return
  66. }
  67. // Test error cases
  68. lf3 := &LockFile{lfdir + "/" + invalidFileName, 1, duration, nil, false}
  69. if err := lf3.Start(); err == nil {
  70. t.Error("Unexpected result while starting lockfile watch:", err)
  71. return
  72. }
  73. lf = &LockFile{lfdir + "/test3.lck", 1, duration, nil, false}
  74. if err := lf.Start(); err != nil {
  75. t.Error(err)
  76. return
  77. }
  78. // Calling start twice should have no effect
  79. if err := lf.Start(); err != nil {
  80. t.Error(err)
  81. return
  82. }
  83. lf.filename = lfdir + "/" + invalidFileName
  84. for lf.WatcherRunning() {
  85. time.Sleep(lf.interval * 2)
  86. }
  87. if lf.WatcherRunning() {
  88. t.Error("Watcher is still running")
  89. return
  90. }
  91. if err := lf.Finish(); err == nil {
  92. t.Error("Unexpected finish result")
  93. return
  94. }
  95. file, err := os.OpenFile(lfdir+"/test4.lck", os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0660)
  96. if err != nil {
  97. t.Error(err)
  98. return
  99. }
  100. file.Write(make([]byte, 3))
  101. file.Close()
  102. lf = &LockFile{lfdir + "/test4.lck", 1, duration, nil, false}
  103. if _, err := lf.checkLockfile(); err == nil || err.Error() != "Unexpected timestamp value found in lockfile:[0 0 0 0 0 0 0 0]" {
  104. t.Error("Unexpected checkLockfile result:", err)
  105. return
  106. }
  107. }