config_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  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 config
  14. import (
  15. "reflect"
  16. "testing"
  17. )
  18. func TestConfigurationChannels(t *testing.T) {
  19. mux := NewMux(nil)
  20. channelOne := mux.Channel("one")
  21. if channelOne != mux.Channel("one") {
  22. t.Error("Didn't get the same muxuration channel back with the same name")
  23. }
  24. channelTwo := mux.Channel("two")
  25. if channelOne == channelTwo {
  26. t.Error("Got back the same muxuration channel for different names")
  27. }
  28. }
  29. type MergeMock struct {
  30. source string
  31. update interface{}
  32. t *testing.T
  33. }
  34. func (m MergeMock) Merge(source string, update interface{}) error {
  35. if m.source != source {
  36. m.t.Errorf("Expected %s, Got %s", m.source, source)
  37. }
  38. if !reflect.DeepEqual(m.update, update) {
  39. m.t.Errorf("Expected %s, Got %s", m.update, update)
  40. }
  41. return nil
  42. }
  43. func TestMergeInvoked(t *testing.T) {
  44. merger := MergeMock{"one", "test", t}
  45. mux := NewMux(&merger)
  46. mux.Channel("one") <- "test"
  47. }
  48. func TestMergeFuncInvoked(t *testing.T) {
  49. ch := make(chan bool)
  50. mux := NewMux(MergeFunc(func(source string, update interface{}) error {
  51. if source != "one" {
  52. t.Errorf("Expected %s, Got %s", "one", source)
  53. }
  54. if update.(string) != "test" {
  55. t.Errorf("Expected %s, Got %s", "test", update)
  56. }
  57. ch <- true
  58. return nil
  59. }))
  60. mux.Channel("one") <- "test"
  61. <-ch
  62. }
  63. func TestSimultaneousMerge(t *testing.T) {
  64. ch := make(chan bool, 2)
  65. mux := NewMux(MergeFunc(func(source string, update interface{}) error {
  66. switch source {
  67. case "one":
  68. if update.(string) != "test" {
  69. t.Errorf("Expected %s, Got %s", "test", update)
  70. }
  71. case "two":
  72. if update.(string) != "test2" {
  73. t.Errorf("Expected %s, Got %s", "test2", update)
  74. }
  75. default:
  76. t.Errorf("Unexpected source, Got %s", update)
  77. }
  78. ch <- true
  79. return nil
  80. }))
  81. source := mux.Channel("one")
  82. source2 := mux.Channel("two")
  83. source <- "test"
  84. source2 <- "test2"
  85. <-ch
  86. <-ch
  87. }
  88. func TestBroadcaster(t *testing.T) {
  89. b := NewBroadcaster()
  90. b.Notify(struct{}{})
  91. ch := make(chan bool, 2)
  92. b.Add(ListenerFunc(func(object interface{}) {
  93. if object != "test" {
  94. t.Errorf("Expected %s, Got %s", "test", object)
  95. }
  96. ch <- true
  97. }))
  98. b.Add(ListenerFunc(func(object interface{}) {
  99. if object != "test" {
  100. t.Errorf("Expected %s, Got %s", "test", object)
  101. }
  102. ch <- true
  103. }))
  104. b.Notify("test")
  105. <-ch
  106. <-ch
  107. }