wait_time_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*
  15. Copyright 2015 CoreOS, Inc.
  16. Licensed under the Apache License, Version 2.0 (the "License");
  17. you may not use this file except in compliance with the License.
  18. You may obtain a copy of the License at
  19. http://www.apache.org/licenses/LICENSE-2.0
  20. Unless required by applicable law or agreed to in writing, software
  21. distributed under the License is distributed on an "AS IS" BASIS,
  22. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. See the License for the specific language governing permissions and
  24. limitations under the License.
  25. */
  26. package wait
  27. import (
  28. "testing"
  29. "time"
  30. )
  31. func TestWaitTime(t *testing.T) {
  32. wt := NewTimeList()
  33. ch1 := wt.Wait(time.Now())
  34. t1 := time.Now()
  35. wt.Trigger(t1)
  36. select {
  37. case <-ch1:
  38. case <-time.After(100 * time.Millisecond):
  39. t.Fatalf("cannot receive from ch as expected")
  40. }
  41. ch2 := wt.Wait(time.Now())
  42. t2 := time.Now()
  43. wt.Trigger(t1)
  44. select {
  45. case <-ch2:
  46. t.Fatalf("unexpected to receive from ch")
  47. case <-time.After(10 * time.Millisecond):
  48. }
  49. wt.Trigger(t2)
  50. select {
  51. case <-ch2:
  52. case <-time.After(10 * time.Millisecond):
  53. t.Fatalf("cannot receive from ch as expected")
  54. }
  55. }
  56. func TestWaitTestStress(t *testing.T) {
  57. chs := make([]<-chan struct{}, 0)
  58. wt := NewTimeList()
  59. for i := 0; i < 10000; i++ {
  60. chs = append(chs, wt.Wait(time.Now()))
  61. // sleep one nanosecond before waiting on the next event
  62. time.Sleep(time.Nanosecond)
  63. }
  64. wt.Trigger(time.Now())
  65. for _, ch := range chs {
  66. select {
  67. case <-ch:
  68. case <-time.After(time.Second):
  69. t.Fatalf("cannot receive from ch as expected")
  70. }
  71. }
  72. }
  73. func BenchmarkWaitTime(b *testing.B) {
  74. t := time.Now()
  75. wt := NewTimeList()
  76. for i := 0; i < b.N; i++ {
  77. wt.Wait(t)
  78. }
  79. }
  80. func BenchmarkTriggerAnd10KWaitTime(b *testing.B) {
  81. for i := 0; i < b.N; i++ {
  82. t := time.Now()
  83. wt := NewTimeList()
  84. for j := 0; j < 10000; j++ {
  85. wt.Wait(t)
  86. }
  87. wt.Trigger(time.Now())
  88. }
  89. }