wait_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. Copyright 2014 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. "fmt"
  16. "testing"
  17. )
  18. func TestWait(t *testing.T) {
  19. const eid = 1
  20. wt := New()
  21. ch := wt.Register(eid)
  22. wt.Trigger(eid, "foo")
  23. v := <-ch
  24. if g, w := fmt.Sprintf("%v (%T)", v, v), "foo (string)"; g != w {
  25. t.Errorf("<-ch = %v, want %v", g, w)
  26. }
  27. if g := <-ch; g != nil {
  28. t.Errorf("unexpected non-nil value: %v (%T)", g, g)
  29. }
  30. }
  31. func TestRegisterDupSuppression(t *testing.T) {
  32. const eid = 1
  33. wt := New()
  34. ch1 := wt.Register(eid)
  35. ch2 := wt.Register(eid)
  36. wt.Trigger(eid, "foo")
  37. <-ch1
  38. g := <-ch2
  39. if g != nil {
  40. t.Errorf("unexpected non-nil value: %v (%T)", g, g)
  41. }
  42. }
  43. func TestTriggerDupSuppression(t *testing.T) {
  44. const eid = 1
  45. wt := New()
  46. ch := wt.Register(eid)
  47. wt.Trigger(eid, "foo")
  48. wt.Trigger(eid, "bar")
  49. v := <-ch
  50. if g, w := fmt.Sprintf("%v (%T)", v, v), "foo (string)"; g != w {
  51. t.Errorf("<-ch = %v, want %v", g, w)
  52. }
  53. if g := <-ch; g != nil {
  54. t.Errorf("unexpected non-nil value: %v (%T)", g, g)
  55. }
  56. }