...
1
16
17 package config
18
19 import (
20 "context"
21 "reflect"
22 "testing"
23 )
24
25 func TestConfigurationChannels(t *testing.T) {
26 ctx, cancel := context.WithCancel(context.Background())
27 defer cancel()
28
29 mux := newMux(nil)
30 channelOne := mux.ChannelWithContext(ctx, "one")
31 if channelOne != mux.ChannelWithContext(ctx, "one") {
32 t.Error("Didn't get the same muxuration channel back with the same name")
33 }
34 channelTwo := mux.ChannelWithContext(ctx, "two")
35 if channelOne == channelTwo {
36 t.Error("Got back the same muxuration channel for different names")
37 }
38 }
39
40 type MergeMock struct {
41 source string
42 update interface{}
43 t *testing.T
44 }
45
46 func (m MergeMock) Merge(source string, update interface{}) error {
47 if m.source != source {
48 m.t.Errorf("Expected %s, Got %s", m.source, source)
49 }
50 if !reflect.DeepEqual(m.update, update) {
51 m.t.Errorf("Expected %s, Got %s", m.update, update)
52 }
53 return nil
54 }
55
56 func TestMergeInvoked(t *testing.T) {
57 ctx, cancel := context.WithCancel(context.Background())
58 defer cancel()
59
60 merger := MergeMock{"one", "test", t}
61 mux := newMux(&merger)
62 mux.ChannelWithContext(ctx, "one") <- "test"
63 }
64
65
66 type mergeFunc func(source string, update interface{}) error
67
68 func (f mergeFunc) Merge(source string, update interface{}) error {
69 return f(source, update)
70 }
71
72 func TestSimultaneousMerge(t *testing.T) {
73 ctx, cancel := context.WithCancel(context.Background())
74 defer cancel()
75
76 ch := make(chan bool, 2)
77 mux := newMux(mergeFunc(func(source string, update interface{}) error {
78 switch source {
79 case "one":
80 if update.(string) != "test" {
81 t.Errorf("Expected %s, Got %s", "test", update)
82 }
83 case "two":
84 if update.(string) != "test2" {
85 t.Errorf("Expected %s, Got %s", "test2", update)
86 }
87 default:
88 t.Errorf("Unexpected source, Got %s", update)
89 }
90 ch <- true
91 return nil
92 }))
93 source := mux.ChannelWithContext(ctx, "one")
94 source2 := mux.ChannelWithContext(ctx, "two")
95 source <- "test"
96 source2 <- "test2"
97 <-ch
98 <-ch
99 }
100
View as plain text