wait_test.go 1.5 KB

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