wait_time_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2015 The etcd Authors
  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. package wait
  15. import (
  16. "testing"
  17. "time"
  18. )
  19. func TestWaitTime(t *testing.T) {
  20. wt := NewTimeList()
  21. ch1 := wt.Wait(1)
  22. wt.Trigger(2)
  23. select {
  24. case <-ch1:
  25. default:
  26. t.Fatalf("cannot receive from ch as expected")
  27. }
  28. ch2 := wt.Wait(4)
  29. wt.Trigger(3)
  30. select {
  31. case <-ch2:
  32. t.Fatalf("unexpected to receive from ch2")
  33. default:
  34. }
  35. wt.Trigger(4)
  36. select {
  37. case <-ch2:
  38. default:
  39. t.Fatalf("cannot receive from ch2 as expected")
  40. }
  41. select {
  42. // wait on a triggered deadline
  43. case <-wt.Wait(4):
  44. default:
  45. t.Fatalf("unexpected blocking when wait on triggered deadline")
  46. }
  47. }
  48. func TestWaitTestStress(t *testing.T) {
  49. chs := make([]<-chan struct{}, 0)
  50. wt := NewTimeList()
  51. for i := 0; i < 10000; i++ {
  52. chs = append(chs, wt.Wait(uint64(i)))
  53. }
  54. wt.Trigger(10000 + 1)
  55. for _, ch := range chs {
  56. select {
  57. case <-ch:
  58. case <-time.After(time.Second):
  59. t.Fatalf("cannot receive from ch as expected")
  60. }
  61. }
  62. }
  63. func BenchmarkWaitTime(b *testing.B) {
  64. wt := NewTimeList()
  65. for i := 0; i < b.N; i++ {
  66. wt.Wait(1)
  67. }
  68. }
  69. func BenchmarkTriggerAnd10KWaitTime(b *testing.B) {
  70. for i := 0; i < b.N; i++ {
  71. wt := NewTimeList()
  72. for j := 0; j < 10000; j++ {
  73. wt.Wait(uint64(j))
  74. }
  75. wt.Trigger(10000 + 1)
  76. }
  77. }