sync_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/calmh/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. if _, ok := NewMutex().(*sync.Mutex); !ok {
  22. t.Error("Wrong type")
  23. }
  24. if _, ok := NewRWMutex().(*sync.RWMutex); !ok {
  25. t.Error("Wrong type")
  26. }
  27. if _, ok := NewWaitGroup().(*sync.WaitGroup); !ok {
  28. t.Error("Wrong type")
  29. }
  30. debug = true
  31. if _, ok := NewMutex().(*loggedMutex); !ok {
  32. t.Error("Wrong type")
  33. }
  34. if _, ok := NewRWMutex().(*loggedRWMutex); !ok {
  35. t.Error("Wrong type")
  36. }
  37. if _, ok := NewWaitGroup().(*loggedWaitGroup); !ok {
  38. t.Error("Wrong type")
  39. }
  40. debug = false
  41. }
  42. func TestMutex(t *testing.T) {
  43. debug = true
  44. threshold = logThreshold
  45. msgmut := sync.Mutex{}
  46. var messages []string
  47. l.AddHandler(logger.LevelDebug, func(_ logger.LogLevel, message string) {
  48. msgmut.Lock()
  49. messages = append(messages, message)
  50. msgmut.Unlock()
  51. })
  52. mut := NewMutex()
  53. mut.Lock()
  54. time.Sleep(shortWait)
  55. mut.Unlock()
  56. if len(messages) > 0 {
  57. t.Errorf("Unexpected message count")
  58. }
  59. mut.Lock()
  60. time.Sleep(longWait)
  61. mut.Unlock()
  62. if len(messages) != 1 {
  63. t.Errorf("Unexpected message count")
  64. }
  65. debug = false
  66. }
  67. func TestRWMutex(t *testing.T) {
  68. debug = true
  69. threshold = logThreshold
  70. msgmut := sync.Mutex{}
  71. var messages []string
  72. l.AddHandler(logger.LevelDebug, func(_ logger.LogLevel, message string) {
  73. msgmut.Lock()
  74. messages = append(messages, message)
  75. msgmut.Unlock()
  76. })
  77. mut := NewRWMutex()
  78. mut.Lock()
  79. time.Sleep(shortWait)
  80. mut.Unlock()
  81. if len(messages) > 0 {
  82. t.Errorf("Unexpected message count")
  83. }
  84. mut.Lock()
  85. time.Sleep(longWait)
  86. mut.Unlock()
  87. if len(messages) != 1 {
  88. t.Errorf("Unexpected message count")
  89. }
  90. // Testing rlocker logging
  91. mut.RLock()
  92. go func() {
  93. time.Sleep(longWait)
  94. mut.RUnlock()
  95. }()
  96. mut.Lock()
  97. mut.Unlock()
  98. if len(messages) != 2 {
  99. t.Errorf("Unexpected message count")
  100. }
  101. if !strings.Contains(messages[1], "RUnlockers while locking: sync") || !strings.Contains(messages[1], "sync_test.go:") {
  102. t.Error("Unexpected message")
  103. }
  104. // Testing multiple rlockers
  105. mut.RLock()
  106. mut.RLock()
  107. mut.RLock()
  108. mut.RUnlock()
  109. mut.RUnlock()
  110. mut.RUnlock()
  111. debug = false
  112. }
  113. func TestWaitGroup(t *testing.T) {
  114. debug = true
  115. threshold = logThreshold
  116. msgmut := sync.Mutex{}
  117. var messages []string
  118. l.AddHandler(logger.LevelDebug, func(_ logger.LogLevel, message string) {
  119. msgmut.Lock()
  120. messages = append(messages, message)
  121. msgmut.Unlock()
  122. })
  123. wg := NewWaitGroup()
  124. wg.Add(1)
  125. go func() {
  126. time.Sleep(shortWait)
  127. wg.Done()
  128. }()
  129. wg.Wait()
  130. if len(messages) > 0 {
  131. t.Errorf("Unexpected message count")
  132. }
  133. wg = NewWaitGroup()
  134. wg.Add(1)
  135. go func() {
  136. time.Sleep(longWait)
  137. wg.Done()
  138. }()
  139. wg.Wait()
  140. if len(messages) != 1 {
  141. t.Errorf("Unexpected message count")
  142. }
  143. debug = false
  144. }