simclock_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. "testing"
  19. "time"
  20. )
  21. var _ Clock = System{}
  22. var _ Clock = new(Simulated)
  23. func TestSimulatedAfter(t *testing.T) {
  24. var (
  25. timeout = 30 * time.Minute
  26. offset = 99 * time.Hour
  27. adv = 11 * time.Minute
  28. c Simulated
  29. )
  30. c.Run(offset)
  31. end := c.Now().Add(timeout)
  32. ch := c.After(timeout)
  33. for c.Now() < end.Add(-adv) {
  34. c.Run(adv)
  35. select {
  36. case <-ch:
  37. t.Fatal("Timer fired early")
  38. default:
  39. }
  40. }
  41. c.Run(adv)
  42. select {
  43. case stamp := <-ch:
  44. want := AbsTime(0).Add(offset).Add(timeout)
  45. if stamp != want {
  46. t.Errorf("Wrong time sent on timer channel: got %v, want %v", stamp, want)
  47. }
  48. default:
  49. t.Fatal("Timer didn't fire")
  50. }
  51. }
  52. func TestSimulatedAfterFunc(t *testing.T) {
  53. var c Simulated
  54. called1 := false
  55. timer1 := c.AfterFunc(100*time.Millisecond, func() { called1 = true })
  56. if c.ActiveTimers() != 1 {
  57. t.Fatalf("%d active timers, want one", c.ActiveTimers())
  58. }
  59. if fired := timer1.Stop(); !fired {
  60. t.Fatal("Stop returned false even though timer didn't fire")
  61. }
  62. if c.ActiveTimers() != 0 {
  63. t.Fatalf("%d active timers, want zero", c.ActiveTimers())
  64. }
  65. if called1 {
  66. t.Fatal("timer 1 called")
  67. }
  68. if fired := timer1.Stop(); fired {
  69. t.Fatal("Stop returned true after timer was already stopped")
  70. }
  71. called2 := false
  72. timer2 := c.AfterFunc(100*time.Millisecond, func() { called2 = true })
  73. c.Run(50 * time.Millisecond)
  74. if called2 {
  75. t.Fatal("timer 2 called")
  76. }
  77. c.Run(51 * time.Millisecond)
  78. if !called2 {
  79. t.Fatal("timer 2 not called")
  80. }
  81. if fired := timer2.Stop(); fired {
  82. t.Fatal("Stop returned true after timer has fired")
  83. }
  84. }
  85. func TestSimulatedSleep(t *testing.T) {
  86. var (
  87. c Simulated
  88. timeout = 1 * time.Hour
  89. done = make(chan AbsTime, 1)
  90. )
  91. go func() {
  92. c.Sleep(timeout)
  93. done <- c.Now()
  94. }()
  95. c.WaitForTimers(1)
  96. c.Run(2 * timeout)
  97. select {
  98. case stamp := <-done:
  99. want := AbsTime(2 * timeout)
  100. if stamp != want {
  101. t.Errorf("Wrong time after sleep: got %v, want %v", stamp, want)
  102. }
  103. case <-time.After(5 * time.Second):
  104. t.Fatal("Sleep didn't return in time")
  105. }
  106. }
  107. func TestSimulatedTimerReset(t *testing.T) {
  108. var (
  109. c Simulated
  110. timeout = 1 * time.Hour
  111. )
  112. timer := c.NewTimer(timeout)
  113. c.Run(2 * timeout)
  114. select {
  115. case ftime := <-timer.C():
  116. if ftime != AbsTime(timeout) {
  117. t.Fatalf("wrong time %v sent on timer channel, want %v", ftime, AbsTime(timeout))
  118. }
  119. default:
  120. t.Fatal("timer didn't fire")
  121. }
  122. timer.Reset(timeout)
  123. c.Run(2 * timeout)
  124. select {
  125. case ftime := <-timer.C():
  126. if ftime != AbsTime(3*timeout) {
  127. t.Fatalf("wrong time %v sent on timer channel, want %v", ftime, AbsTime(3*timeout))
  128. }
  129. default:
  130. t.Fatal("timer didn't fire again")
  131. }
  132. }
  133. func TestSimulatedTimerStop(t *testing.T) {
  134. var (
  135. c Simulated
  136. timeout = 1 * time.Hour
  137. )
  138. timer := c.NewTimer(timeout)
  139. c.Run(2 * timeout)
  140. if timer.Stop() {
  141. t.Errorf("Stop returned true for fired timer")
  142. }
  143. select {
  144. case <-timer.C():
  145. default:
  146. t.Fatal("timer didn't fire")
  147. }
  148. }