-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmessagebus_test.go
150 lines (121 loc) · 4.97 KB
/
messagebus_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package foreman
import (
"testing"
"github.com/go-foreman/foreman/testing/mocks/pubsub/transport"
"github.com/go-foreman/foreman/pubsub/message"
"github.com/go-foreman/foreman/pubsub/subscriber"
"github.com/pkg/errors"
"github.com/go-foreman/foreman/runtime/scheme"
"github.com/go-foreman/foreman/testing/log"
messageMock "github.com/go-foreman/foreman/testing/mocks/pubsub/message"
subscriberMock "github.com/go-foreman/foreman/testing/mocks/pubsub/subscriber"
"github.com/stretchr/testify/require"
"github.com/go-foreman/foreman/testing/mocks/pubsub/dispatcher"
"github.com/go-foreman/foreman/testing/mocks/pubsub/endpoint"
"github.com/go-foreman/foreman/testing/mocks/pubsub/message/execution"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)
func TestMessageBusConfigOptions(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
dispatcherMock := dispatcher.NewMockDispatcher(ctrl)
msgExecFactoryMock := execution.NewMockMessageExecutionCtxFactory(ctrl)
routerMock := endpoint.NewMockRouter(ctrl)
componentMock := &aComponent{}
c := &container{}
opts := []ConfigOption{
WithDispatcher(dispatcherMock),
WithMessageExecutionFactory(msgExecFactoryMock),
WithRouter(routerMock),
WithComponents(componentMock),
}
for _, o := range opts {
o(c)
}
assert.Same(t, c.messagesDispatcher, dispatcherMock)
assert.Same(t, c.router, routerMock)
assert.Equal(t, []Component{componentMock}, c.components)
assert.Same(t, c.messageExuctionCtxFactory, msgExecFactoryMock)
}
type aComponent struct {
err error
}
func (a aComponent) Init(b *MessageBus) error {
return a.err
}
func TestMessageBusConstructor(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
testLogger := log.NewNilLogger()
msgMarshallerMock := messageMock.NewMockMarshaller(ctrl)
schemeRegistry := scheme.NewKnownTypesRegistry()
subscriberInstanceMock := subscriberMock.NewMockSubscriber(ctrl)
dispatcherMock := dispatcher.NewMockDispatcher(ctrl)
msgExecFactoryMock := execution.NewMockMessageExecutionCtxFactory(ctrl)
routerMock := endpoint.NewMockRouter(ctrl)
componentMock := &aComponent{}
erroredComponentMock := &aComponent{err: errors.New("component error")}
t.Run("create mbus with all opts", func(t *testing.T) {
opts := []ConfigOption{
WithDispatcher(dispatcherMock),
WithMessageExecutionFactory(msgExecFactoryMock),
WithRouter(routerMock),
}
mBus, err := NewMessageBus(testLogger, msgMarshallerMock, schemeRegistry, WithSubscriber(subscriberInstanceMock), append(opts, WithComponents(componentMock, erroredComponentMock))...)
require.Error(t, err)
assert.Nil(t, mBus)
assert.EqualError(t, err, "component error")
mBus, err = NewMessageBus(testLogger, msgMarshallerMock, schemeRegistry, WithSubscriber(subscriberInstanceMock), append(opts, WithComponents(componentMock))...)
require.NoError(t, err)
require.NotNil(t, mBus)
assert.Same(t, mBus.Logger(), testLogger)
assert.Same(t, mBus.Dispatcher(), dispatcherMock)
assert.Same(t, mBus.Router(), routerMock)
assert.Same(t, mBus.SchemeRegistry(), schemeRegistry)
assert.Same(t, mBus.Marshaller(), msgMarshallerMock)
})
t.Run("create mbus without opts", func(t *testing.T) {
mBus, err := NewMessageBus(testLogger, msgMarshallerMock, schemeRegistry, WithSubscriber(subscriberInstanceMock))
require.NoError(t, err)
require.NotNil(t, mBus)
assert.Same(t, mBus.scheme, mBus.SchemeRegistry())
assert.Same(t, mBus.marshaller, mBus.Marshaller())
assert.Same(t, mBus.messagesDispatcher, mBus.Dispatcher())
assert.Same(t, mBus.router, mBus.Router())
assert.Same(t, mBus.logger, mBus.Logger())
assert.Same(t, mBus.subscriber, mBus.Subscriber())
})
t.Run("create mbus with custom subscriber", func(t *testing.T) {
mBus, err := NewMessageBus(
testLogger,
msgMarshallerMock,
schemeRegistry,
WithSubscriberFactory(func(processor subscriber.Processor, marshaller message.Marshaller) subscriber.Subscriber {
return subscriberInstanceMock
}),
)
require.NoError(t, err)
assert.Same(t, subscriberInstanceMock, mBus.Subscriber())
})
t.Run("create mbus with default subscriber", func(t *testing.T) {
transportMock := transport.NewMockTransport(ctrl)
sConfig := &subscriber.Config{}
opts := []subscriber.Opt{subscriber.WithConfig(sConfig)}
subscriberConfig := DefaultSubscriber(transportMock, opts...)
sOpts := &subscriberOpts{}
sContainer := &subscriberContainer{}
subscriberConfig(sOpts, sContainer)
assert.Same(t, sOpts.transport, transportMock)
assert.Equal(t, sOpts.opts, opts)
mBus, err := NewMessageBus(testLogger, msgMarshallerMock, schemeRegistry, DefaultSubscriber(transportMock))
require.NoError(t, err)
defaultSubscriber := subscriber.NewSubscriber(transportMock, nil, nil)
assert.IsType(t, defaultSubscriber, mBus.Subscriber())
})
t.Run("nil subscriber", func(t *testing.T) {
assert.PanicsWithError(t, "subscriber is nil", func() {
_, _ = NewMessageBus(testLogger, msgMarshallerMock, schemeRegistry, WithSubscriber(nil))
})
})
}