sync_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright (C) 2015 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package sync
  7. import (
  8. "strings"
  9. "sync"
  10. "testing"
  11. "time"
  12. "github.com/syncthing/syncthing/lib/logger"
  13. )
  14. const (
  15. logThreshold = 100 * time.Millisecond
  16. shortWait = 5 * time.Millisecond
  17. longWait = 125 * time.Millisecond
  18. )
  19. func TestTypes(t *testing.T) {
  20. debug = false
  21. l.SetDebug("sync", false)
  22. if _, ok := NewMutex().(*sync.Mutex); !ok {
  23. t.Error("Wrong type")
  24. }
  25. if _, ok := NewRWMutex().(*sync.RWMutex); !ok {
  26. t.Error("Wrong type")
  27. }
  28. if _, ok := NewWaitGroup().(*sync.WaitGroup); !ok {
  29. t.Error("Wrong type")
  30. }
  31. debug = true
  32. l.SetDebug("sync", true)
  33. if _, ok := NewMutex().(*loggedMutex); !ok {
  34. t.Error("Wrong type")
  35. }
  36. if _, ok := NewRWMutex().(*loggedRWMutex); !ok {
  37. t.Error("Wrong type")
  38. }
  39. if _, ok := NewWaitGroup().(*loggedWaitGroup); !ok {
  40. t.Error("Wrong type")
  41. }
  42. debug = false
  43. l.SetDebug("sync", false)
  44. }
  45. func TestMutex(t *testing.T) {
  46. debug = true
  47. l.SetDebug("sync", true)
  48. threshold = logThreshold
  49. msgmut := sync.Mutex{}
  50. var messages []string
  51. l.AddHandler(logger.LevelDebug, func(_ logger.LogLevel, message string) {
  52. msgmut.Lock()
  53. messages = append(messages, message)
  54. msgmut.Unlock()
  55. })
  56. mut := NewMutex()
  57. mut.Lock()
  58. time.Sleep(shortWait)
  59. mut.Unlock()
  60. if len(messages) > 0 {
  61. t.Errorf("Unexpected message count")
  62. }
  63. mut.Lock()
  64. time.Sleep(longWait)
  65. mut.Unlock()
  66. if len(messages) != 1 {
  67. t.Errorf("Unexpected message count")
  68. }
  69. debug = false
  70. l.SetDebug("sync", false)
  71. }
  72. func TestRWMutex(t *testing.T) {
  73. debug = true
  74. l.SetDebug("sync", true)
  75. threshold = logThreshold
  76. msgmut := sync.Mutex{}
  77. var messages []string
  78. l.AddHandler(logger.LevelDebug, func(_ logger.LogLevel, message string) {
  79. msgmut.Lock()
  80. messages = append(messages, message)
  81. msgmut.Unlock()
  82. })
  83. mut := NewRWMutex()
  84. mut.Lock()
  85. time.Sleep(shortWait)
  86. mut.Unlock()
  87. if len(messages) > 0 {
  88. t.Errorf("Unexpected message count")
  89. }
  90. mut.Lock()
  91. time.Sleep(longWait)
  92. mut.Unlock()
  93. if len(messages) != 1 {
  94. t.Errorf("Unexpected message count")
  95. }
  96. // Testing rlocker logging
  97. mut.RLock()
  98. go func() {
  99. time.Sleep(longWait)
  100. mut.RUnlock()
  101. }()
  102. mut.Lock()
  103. mut.Unlock()
  104. if len(messages) != 2 {
  105. t.Errorf("Unexpected message count")
  106. }
  107. if !strings.Contains(messages[1], "RUnlockers while locking: sync") || !strings.Contains(messages[1], "sync_test.go:") {
  108. t.Error("Unexpected message")
  109. }
  110. // Testing multiple rlockers
  111. mut.RLock()
  112. mut.RLock()
  113. mut.RLock()
  114. mut.RUnlock()
  115. mut.RUnlock()
  116. mut.RUnlock()
  117. debug = false
  118. l.SetDebug("sync", false)
  119. }
  120. func TestWaitGroup(t *testing.T) {
  121. debug = true
  122. l.SetDebug("sync", true)
  123. threshold = logThreshold
  124. msgmut := sync.Mutex{}
  125. var messages []string
  126. l.AddHandler(logger.LevelDebug, func(_ logger.LogLevel, message string) {
  127. msgmut.Lock()
  128. messages = append(messages, message)
  129. msgmut.Unlock()
  130. })
  131. wg := NewWaitGroup()
  132. wg.Add(1)
  133. go func() {
  134. time.Sleep(shortWait)
  135. wg.Done()
  136. }()
  137. wg.Wait()
  138. if len(messages) > 0 {
  139. t.Errorf("Unexpected message count")
  140. }
  141. wg = NewWaitGroup()
  142. wg.Add(1)
  143. go func() {
  144. time.Sleep(longWait)
  145. wg.Done()
  146. }()
  147. wg.Wait()
  148. if len(messages) != 1 {
  149. t.Errorf("Unexpected message count")
  150. }
  151. debug = false
  152. l.SetDebug("sync", false)
  153. }