simclock.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. lastId uint64
  36. }
  37. type event struct {
  38. do func()
  39. at AbsTime
  40. id uint64
  41. }
  42. // SimulatedEvent implements Event for a virtual clock.
  43. type SimulatedEvent struct {
  44. at AbsTime
  45. id uint64
  46. s *Simulated
  47. }
  48. // Run moves the clock by the given duration, executing all timers before that duration.
  49. func (s *Simulated) Run(d time.Duration) {
  50. s.mu.Lock()
  51. s.init()
  52. end := s.now + AbsTime(d)
  53. var do []func()
  54. for len(s.scheduled) > 0 {
  55. ev := s.scheduled[0]
  56. if ev.at > end {
  57. break
  58. }
  59. s.now = ev.at
  60. do = append(do, ev.do)
  61. s.scheduled = s.scheduled[1:]
  62. }
  63. s.now = end
  64. s.mu.Unlock()
  65. for _, fn := range do {
  66. fn()
  67. }
  68. }
  69. func (s *Simulated) ActiveTimers() int {
  70. s.mu.RLock()
  71. defer s.mu.RUnlock()
  72. return len(s.scheduled)
  73. }
  74. func (s *Simulated) WaitForTimers(n int) {
  75. s.mu.Lock()
  76. defer s.mu.Unlock()
  77. s.init()
  78. for len(s.scheduled) < n {
  79. s.cond.Wait()
  80. }
  81. }
  82. // Now implements Clock.
  83. func (s *Simulated) Now() AbsTime {
  84. s.mu.RLock()
  85. defer s.mu.RUnlock()
  86. return s.now
  87. }
  88. // Sleep implements Clock.
  89. func (s *Simulated) Sleep(d time.Duration) {
  90. <-s.After(d)
  91. }
  92. // After implements Clock.
  93. func (s *Simulated) After(d time.Duration) <-chan time.Time {
  94. after := make(chan time.Time, 1)
  95. s.AfterFunc(d, func() {
  96. after <- (time.Time{}).Add(time.Duration(s.now))
  97. })
  98. return after
  99. }
  100. // AfterFunc implements Clock.
  101. func (s *Simulated) AfterFunc(d time.Duration, do func()) Event {
  102. s.mu.Lock()
  103. defer s.mu.Unlock()
  104. s.init()
  105. at := s.now + AbsTime(d)
  106. s.lastId++
  107. id := s.lastId
  108. l, h := 0, len(s.scheduled)
  109. ll := h
  110. for l != h {
  111. m := (l + h) / 2
  112. if (at < s.scheduled[m].at) || ((at == s.scheduled[m].at) && (id < s.scheduled[m].id)) {
  113. h = m
  114. } else {
  115. l = m + 1
  116. }
  117. }
  118. s.scheduled = append(s.scheduled, event{})
  119. copy(s.scheduled[l+1:], s.scheduled[l:ll])
  120. e := event{do: do, at: at, id: id}
  121. s.scheduled[l] = e
  122. s.cond.Broadcast()
  123. return &SimulatedEvent{at: at, id: id, s: s}
  124. }
  125. func (s *Simulated) init() {
  126. if s.cond == nil {
  127. s.cond = sync.NewCond(&s.mu)
  128. }
  129. }
  130. // Cancel implements Event.
  131. func (e *SimulatedEvent) Cancel() bool {
  132. s := e.s
  133. s.mu.Lock()
  134. defer s.mu.Unlock()
  135. l, h := 0, len(s.scheduled)
  136. ll := h
  137. for l != h {
  138. m := (l + h) / 2
  139. if e.id == s.scheduled[m].id {
  140. l = m
  141. break
  142. }
  143. if (e.at < s.scheduled[m].at) || ((e.at == s.scheduled[m].at) && (e.id < s.scheduled[m].id)) {
  144. h = m
  145. } else {
  146. l = m + 1
  147. }
  148. }
  149. if l >= ll || s.scheduled[l].id != e.id {
  150. return false
  151. }
  152. copy(s.scheduled[l:ll-1], s.scheduled[l+1:])
  153. s.scheduled = s.scheduled[:ll-1]
  154. return true
  155. }