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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::EventStore::Store, feature_category: :shared do
let(:event_klass) { stub_const('TestEvent', Class.new(Gitlab::EventStore::Event)) }
let(:event) { event_klass.new(data: data) }
let(:another_event_klass) { stub_const('TestAnotherEvent', Class.new(Gitlab::EventStore::Event)) }
let(:worker) do
stub_const('EventSubscriber', Class.new).tap do |klass|
klass.class_eval do
include Gitlab::EventStore::Subscriber
def handle_event(event)
event.data
end
end
end
end
let(:another_worker) do
stub_const('AnotherEventSubscriber', Class.new).tap do |klass|
klass.class_eval do
include Gitlab::EventStore::Subscriber
end
end
end
let(:unrelated_worker) do
stub_const('UnrelatedEventSubscriber', Class.new).tap do |klass|
klass.class_eval do
include Gitlab::EventStore::Subscriber
end
end
end
before do
event_klass.class_eval do
def schema
{
'required' => %w[name id],
'type' => 'object',
'properties' => {
'name' => { 'type' => 'string' },
'id' => { 'type' => 'integer' }
}
}
end
end
end
describe '#subscribe' do
it 'subscribes a worker to an event' do
store = described_class.new do |s|
s.subscribe worker, to: event_klass
end
subscriptions = store.subscriptions[event_klass]
expect(subscriptions.map(&:worker)).to contain_exactly(worker)
end
it 'subscribes multiple workers to an event' do
store = described_class.new do |s|
s.subscribe worker, to: event_klass
s.subscribe another_worker, to: event_klass
end
subscriptions = store.subscriptions[event_klass]
expect(subscriptions.map(&:worker)).to contain_exactly(worker, another_worker)
end
it 'subscribes a worker to multiple events is separate calls' do
store = described_class.new do |s|
s.subscribe worker, to: event_klass
s.subscribe worker, to: another_event_klass
end
subscriptions = store.subscriptions[event_klass]
expect(subscriptions.map(&:worker)).to contain_exactly(worker)
subscriptions = store.subscriptions[another_event_klass]
expect(subscriptions.map(&:worker)).to contain_exactly(worker)
end
it 'subscribes a worker to multiple events in a single call' do
store = described_class.new do |s|
s.subscribe worker, to: [event_klass, another_event_klass]
end
subscriptions = store.subscriptions[event_klass]
expect(subscriptions.map(&:worker)).to contain_exactly(worker)
subscriptions = store.subscriptions[another_event_klass]
expect(subscriptions.map(&:worker)).to contain_exactly(worker)
end
it 'subscribes a worker to an event with condition' do
store = described_class.new do |s|
s.subscribe worker, to: event_klass, if: ->(event) { event.data[:name] == 'Alice' }
end
subscriptions = store.subscriptions[event_klass]
expect(subscriptions.size).to eq(1)
subscription = subscriptions.first
expect(subscription).to be_an_instance_of(Gitlab::EventStore::Subscription)
expect(subscription.worker).to eq(worker)
expect(subscription.condition.call(double(data: { name: 'Bob' }))).to eq(false)
expect(subscription.condition.call(double(data: { name: 'Alice' }))).to eq(true)
end
it 'refuses the subscription if the target is not an Event object' do
expect do
described_class.new do |s|
s.subscribe worker, to: Integer
end
end.to raise_error(
Gitlab::EventStore::Error,
/Event being subscribed to is not a subclass of Gitlab::EventStore::Event/)
end
it 'refuses the subscription if the subscriber is not a worker' do
expect do
described_class.new do |s|
s.subscribe double, to: event_klass
end
end.to raise_error(
Gitlab::EventStore::Error,
/Subscriber is not an ApplicationWorker/)
end
end
describe '#publish' do
let(:data) { { name: 'Bob', id: 123 } }
let(:serialized_data) { data.deep_stringify_keys }
context 'when event has a subscribed worker' do
let(:store) do
described_class.new do |store|
store.subscribe worker, to: event_klass
store.subscribe another_worker, to: another_event_klass
end
end
it 'dispatches the event to the subscribed worker' do
expect(worker).to receive(:perform_async).with('TestEvent', serialized_data)
expect(another_worker).not_to receive(:perform_async)
store.publish(event)
end
it 'does not raise any Sidekiq warning' do
logger = double(:logger, info: nil)
allow(Sidekiq).to receive(:logger).and_return(logger)
expect(logger).not_to receive(:warn).with(/do not serialize to JSON safely/)
expect(worker).to receive(:perform_async).with('TestEvent', serialized_data).and_call_original
store.publish(event)
end
context 'when other workers subscribe to the same event' do
let(:store) do
described_class.new do |store|
store.subscribe worker, to: event_klass
store.subscribe another_worker, to: event_klass
store.subscribe unrelated_worker, to: another_event_klass
end
end
it 'dispatches the event to each subscribed worker' do
expect(worker).to receive(:perform_async).with('TestEvent', serialized_data)
expect(another_worker).to receive(:perform_async).with('TestEvent', serialized_data)
expect(unrelated_worker).not_to receive(:perform_async)
store.publish(event)
end
end
context 'when an error is raised' do
before do
allow(worker).to receive(:perform_async).and_raise(NoMethodError, 'the error message')
end
it 'is rescued and tracked' do
expect(Gitlab::ErrorTracking)
.to receive(:track_and_raise_for_dev_exception)
.with(kind_of(NoMethodError), event_class: event.class.name, event_data: event.data)
.and_call_original
expect { store.publish(event) }.to raise_error(NoMethodError, 'the error message')
end
end
it 'raises and tracks an error when event is published inside a database transaction' do
expect(Gitlab::ErrorTracking)
.to receive(:track_and_raise_for_dev_exception)
.at_least(:once)
.and_call_original
expect do
ApplicationRecord.transaction do
store.publish(event)
end
end.to raise_error(Sidekiq::Worker::EnqueueFromTransactionError)
end
it 'refuses publishing if the target is not an Event object' do
expect { store.publish(double(:event)) }
.to raise_error(
Gitlab::EventStore::Error,
/Event being published is not an instance of Gitlab::EventStore::Event/)
end
end
context 'when event has subscribed workers with condition' do
let(:store) do
described_class.new do |s|
s.subscribe worker, to: event_klass, if: -> (event) { event.data[:name] == 'Bob' }
s.subscribe another_worker, to: event_klass, if: -> (event) { event.data[:name] == 'Alice' }
end
end
it 'dispatches the event to the workers satisfying the condition' do
expect(worker).to receive(:perform_async).with('TestEvent', serialized_data)
expect(another_worker).not_to receive(:perform_async)
store.publish(event)
end
end
context 'when subscription has delayed dispatching of event' do
let(:store) do
described_class.new do |s|
s.subscribe worker, to: event_klass, delay: 1.minute
end
end
it 'dispatches the event to the worker after some time' do
expect(worker).to receive(:perform_in).with(1.minute, 'TestEvent', serialized_data)
store.publish(event)
end
end
context 'when the event does not have any subscribers' do
let(:store) do
described_class.new do |s|
s.subscribe unrelated_worker, to: another_event_klass
end
end
it 'returns successfully' do
expect { store.publish(event) }.not_to raise_error
end
it 'does not dispatch the event to another subscription' do
expect(unrelated_worker).not_to receive(:perform_async)
store.publish(event)
end
end
end
describe 'subscriber' do
let(:data) { { name: 'Bob', id: 123 } }
let(:event_name) { event.class.name }
let(:worker_instance) { worker.new }
subject { worker_instance.perform(event_name, data) }
it 'is a Sidekiq worker' do
expect(worker_instance).to be_a(ApplicationWorker)
end
it 'handles the event' do
expect(worker_instance).to receive(:handle_event).with(instance_of(event.class))
expect_any_instance_of(event.class) do |event|
expect(event).to receive(:data).and_return(data)
end
subject
end
context 'when the event name does not exist' do
let(:event_name) { 'UnknownClass' }
it 'raises an error' do
expect { subject }.to raise_error(Gitlab::EventStore::InvalidEvent)
end
end
context 'when the worker does not define handle_event method' do
let(:worker_instance) { another_worker.new }
it 'raises an error' do
expect { subject }.to raise_error(NotImplementedError)
end
end
end
end
|