1
0

timeoutcond_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 https://mozilla.org/MPL/2.0/.
  6. package syncutil
  7. import (
  8. "sync"
  9. "testing"
  10. "time"
  11. )
  12. func TestTimeoutCond(t *testing.T) {
  13. // WARNING this test relies heavily on threads not being stalled at particular points.
  14. // As such, it's pretty unstable on the build server. It has been left in as it still
  15. // exercises the deadlock detector, and one of the two things it tests is still functional.
  16. // See the comments in runLocks
  17. const (
  18. // Low values to avoid being intrusive in continuous testing. Can be
  19. // increased significantly for stress testing.
  20. iterations = 100
  21. routines = 10
  22. timeMult = 2
  23. )
  24. c := NewTimeoutCond(new(sync.Mutex))
  25. // Start a routine to periodically broadcast on the cond.
  26. go func() {
  27. d := time.Duration(routines) * timeMult * time.Millisecond / 2
  28. t.Log("Broadcasting every", d)
  29. for i := 0; i < iterations; i++ {
  30. time.Sleep(d)
  31. c.L.Lock()
  32. c.Broadcast()
  33. c.L.Unlock()
  34. }
  35. }()
  36. // Start several routines that wait on it with different timeouts.
  37. var results [routines][2]int
  38. var wg sync.WaitGroup
  39. for i := 0; i < routines; i++ {
  40. i := i
  41. wg.Add(1)
  42. go func() {
  43. d := time.Duration(i) * timeMult * time.Millisecond
  44. t.Logf("Routine %d waits for %v\n", i, d)
  45. succ, fail := runLocks(t, iterations, c, d)
  46. results[i][0] = succ
  47. results[i][1] = fail
  48. wg.Done()
  49. }()
  50. }
  51. wg.Wait()
  52. // Print a table of routine number: successes, failures.
  53. for i, v := range results {
  54. t.Logf("%4d: %4d %4d\n", i, v[0], v[1])
  55. }
  56. }
  57. func runLocks(t *testing.T, iterations int, c *TimeoutCond, d time.Duration) (succ, fail int) {
  58. for i := 0; i < iterations; i++ {
  59. c.L.Lock()
  60. // The thread may be stalled, so we can't test the 'succeeded late' case reliably.
  61. // Therefore make sure that we start t0 before starting the timeout, and only test
  62. // the 'failed early' case.
  63. t0 := time.Now()
  64. w := c.SetupWait(d)
  65. res := w.Wait()
  66. waited := time.Since(t0)
  67. // Allow 20% slide in either direction, and a five milliseconds of
  68. // scheduling delay... In tweaking these it was clear that things
  69. // worked like the should, so if this becomes a spurious failure
  70. // kind of thing feel free to remove or give significantly more
  71. // slack.
  72. if !res && waited < d*8/10 {
  73. t.Errorf("Wait failed early, %v < %v", waited, d)
  74. }
  75. if res && waited > d*11/10+5*time.Millisecond {
  76. // Ideally this would be t.Errorf
  77. t.Logf("WARNING: Wait succeeded late, %v > %v. This is probably a thread scheduling issue", waited, d)
  78. }
  79. w.Stop()
  80. if res {
  81. succ++
  82. } else {
  83. fail++
  84. }
  85. c.L.Unlock()
  86. }
  87. return
  88. }
  89. type testClock struct {
  90. time time.Time
  91. mut sync.Mutex
  92. }
  93. func newTestClock() *testClock {
  94. return &testClock{
  95. time: time.Now(),
  96. }
  97. }
  98. func (t *testClock) Now() time.Time {
  99. t.mut.Lock()
  100. now := t.time
  101. t.time = t.time.Add(time.Nanosecond)
  102. t.mut.Unlock()
  103. return now
  104. }
  105. func (t *testClock) wind(d time.Duration) {
  106. t.mut.Lock()
  107. t.time = t.time.Add(d)
  108. t.mut.Unlock()
  109. }