simclock.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2018 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package mclock
  17. import (
  18. "sync"
  19. "time"
  20. )
  21. // Simulated implements a virtual Clock for reproducible time-sensitive tests. It
  22. // simulates a scheduler on a virtual timescale where actual processing takes zero time.
  23. //
  24. // The virtual clock doesn't advance on its own, call Run to advance it and execute timers.
  25. // Since there is no way to influence the Go scheduler, testing timeout behaviour involving
  26. // goroutines needs special care. A good way to test such timeouts is as follows: First
  27. // perform the action that is supposed to time out. Ensure that the timer you want to test
  28. // is created. Then run the clock until after the timeout. Finally observe the effect of
  29. // the timeout using a channel or semaphore.
  30. type Simulated struct {
  31. now AbsTime
  32. scheduled []event
  33. mu sync.RWMutex
  34. cond *sync.Cond
  35. }
  36. type event struct {
  37. do func()
  38. at AbsTime
  39. }
  40. // Run moves the clock by the given duration, executing all timers before that duration.
  41. func (s *Simulated) Run(d time.Duration) {
  42. s.mu.Lock()
  43. defer s.mu.Unlock()
  44. s.init()
  45. end := s.now + AbsTime(d)
  46. for len(s.scheduled) > 0 {
  47. ev := s.scheduled[0]
  48. if ev.at > end {
  49. break
  50. }
  51. s.now = ev.at
  52. ev.do()
  53. s.scheduled = s.scheduled[1:]
  54. }
  55. s.now = end
  56. }
  57. func (s *Simulated) ActiveTimers() int {
  58. s.mu.RLock()
  59. defer s.mu.RUnlock()
  60. return len(s.scheduled)
  61. }
  62. func (s *Simulated) WaitForTimers(n int) {
  63. s.mu.Lock()
  64. defer s.mu.Unlock()
  65. s.init()
  66. for len(s.scheduled) < n {
  67. s.cond.Wait()
  68. }
  69. }
  70. // Now implements Clock.
  71. func (s *Simulated) Now() AbsTime {
  72. s.mu.RLock()
  73. defer s.mu.RUnlock()
  74. return s.now
  75. }
  76. // Sleep implements Clock.
  77. func (s *Simulated) Sleep(d time.Duration) {
  78. <-s.After(d)
  79. }
  80. // After implements Clock.
  81. func (s *Simulated) After(d time.Duration) <-chan time.Time {
  82. after := make(chan time.Time, 1)
  83. s.insert(d, func() {
  84. after <- (time.Time{}).Add(time.Duration(s.now))
  85. })
  86. return after
  87. }
  88. func (s *Simulated) insert(d time.Duration, do func()) {
  89. s.mu.Lock()
  90. defer s.mu.Unlock()
  91. s.init()
  92. at := s.now + AbsTime(d)
  93. l, h := 0, len(s.scheduled)
  94. ll := h
  95. for l != h {
  96. m := (l + h) / 2
  97. if at < s.scheduled[m].at {
  98. h = m
  99. } else {
  100. l = m + 1
  101. }
  102. }
  103. s.scheduled = append(s.scheduled, event{})
  104. copy(s.scheduled[l+1:], s.scheduled[l:ll])
  105. s.scheduled[l] = event{do: do, at: at}
  106. s.cond.Broadcast()
  107. }
  108. func (s *Simulated) init() {
  109. if s.cond == nil {
  110. s.cond = sync.NewCond(&s.mu)
  111. }
  112. }