timeoutcond_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. wg.Go(func() {
  41. d := time.Duration(i) * timeMult * time.Millisecond
  42. t.Logf("Routine %d waits for %v\n", i, d)
  43. succ, fail := runLocks(t, iterations, c, d)
  44. results[i][0] = succ
  45. results[i][1] = fail
  46. })
  47. }
  48. wg.Wait()
  49. // Print a table of routine number: successes, failures.
  50. for i, v := range results {
  51. t.Logf("%4d: %4d %4d\n", i, v[0], v[1])
  52. }
  53. }
  54. func runLocks(t *testing.T, iterations int, c *TimeoutCond, d time.Duration) (succ, fail int) {
  55. for i := 0; i < iterations; i++ {
  56. c.L.Lock()
  57. // The thread may be stalled, so we can't test the 'succeeded late' case reliably.
  58. // Therefore make sure that we start t0 before starting the timeout, and only test
  59. // the 'failed early' case.
  60. t0 := time.Now()
  61. w := c.SetupWait(d)
  62. res := w.Wait()
  63. waited := time.Since(t0)
  64. // Allow 20% slide in either direction, and a five milliseconds of
  65. // scheduling delay... In tweaking these it was clear that things
  66. // worked like the should, so if this becomes a spurious failure
  67. // kind of thing feel free to remove or give significantly more
  68. // slack.
  69. if !res && waited < d*8/10 {
  70. t.Errorf("Wait failed early, %v < %v", waited, d)
  71. }
  72. if res && waited > d*11/10+5*time.Millisecond {
  73. // Ideally this would be t.Errorf
  74. t.Logf("WARNING: Wait succeeded late, %v > %v. This is probably a thread scheduling issue", waited, d)
  75. }
  76. w.Stop()
  77. if res {
  78. succ++
  79. } else {
  80. fail++
  81. }
  82. c.L.Unlock()
  83. }
  84. return
  85. }
  86. type testClock struct {
  87. time time.Time
  88. mut sync.Mutex
  89. }
  90. func newTestClock() *testClock {
  91. return &testClock{
  92. time: time.Now(),
  93. }
  94. }
  95. func (t *testClock) Now() time.Time {
  96. t.mut.Lock()
  97. now := t.time
  98. t.time = t.time.Add(time.Nanosecond)
  99. t.mut.Unlock()
  100. return now
  101. }
  102. func (t *testClock) wind(d time.Duration) {
  103. t.mut.Lock()
  104. t.time = t.time.Add(d)
  105. t.mut.Unlock()
  106. }