wait_time_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. Copyright 2015 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package wait
  14. import (
  15. "testing"
  16. "time"
  17. )
  18. func TestWaitTime(t *testing.T) {
  19. wt := NewTimeList()
  20. ch1 := wt.Wait(time.Now())
  21. t1 := time.Now()
  22. wt.Trigger(t1)
  23. select {
  24. case <-ch1:
  25. case <-time.After(10 * time.Millisecond):
  26. t.Fatalf("cannot receive from ch as expected")
  27. }
  28. ch2 := wt.Wait(time.Now())
  29. t2 := time.Now()
  30. wt.Trigger(t1)
  31. select {
  32. case <-ch2:
  33. t.Fatalf("unexpected to receive from ch")
  34. case <-time.After(10 * time.Millisecond):
  35. }
  36. wt.Trigger(t2)
  37. select {
  38. case <-ch2:
  39. case <-time.After(10 * time.Millisecond):
  40. t.Fatalf("cannot receive from ch as expected")
  41. }
  42. }
  43. func TestWaitTestStress(t *testing.T) {
  44. chs := make([]<-chan struct{}, 0)
  45. wt := NewTimeList()
  46. for i := 0; i < 10000; i++ {
  47. chs = append(chs, wt.Wait(time.Now()))
  48. }
  49. wt.Trigger(time.Now())
  50. for _, ch := range chs {
  51. select {
  52. case <-ch:
  53. case <-time.After(10 * time.Millisecond):
  54. t.Fatalf("cannot receive from ch as expected")
  55. }
  56. }
  57. }
  58. func BenchmarkWaitTime(b *testing.B) {
  59. t := time.Now()
  60. wt := NewTimeList()
  61. for i := 0; i < b.N; i++ {
  62. wt.Wait(t)
  63. }
  64. }
  65. func BenchmarkTriggerAnd10KWaitTime(b *testing.B) {
  66. for i := 0; i < b.N; i++ {
  67. t := time.Now()
  68. wt := NewTimeList()
  69. for j := 0; j < 10000; j++ {
  70. wt.Wait(t)
  71. }
  72. wt.Trigger(time.Now())
  73. }
  74. }