lockfile_test.go 2.7 KB

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