forked from ably/ably-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_emitter_spec.rb
421 lines (350 loc) · 12.2 KB
/
event_emitter_spec.rb
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
require 'spec_helper'
describe Ably::Modules::EventEmitter do
let(:options) { {} }
let(:klass) do
callback_opts = options
Class.new do
include Ably::Modules::EventEmitter
configure_event_emitter callback_opts
def logger
@logger ||= Ably::Models::NilLogger.new
end
end
end
let(:obj) { double('example') }
let(:msg) { double('message') }
subject { klass.new }
context '#emit event fan out' do
it 'should emit an event for any number of subscribers' do
2.times do
subject.on(:message) { |msg| obj.received_message msg }
end
expect(obj).to receive(:received_message).with(msg).twice
subject.emit :message, msg
end
it 'sends only messages to matching event names' do
subject.on(:valid) { |msg| obj.received_message msg }
expect(obj).to receive(:received_message).with(msg).once
subject.emit :valid, msg
subject.emit :ignored, msg
subject.emit 'valid', msg
end
context 'with coercion', :api_private do
let(:options) do
{ coerce_into: lambda { |event| String(event) } }
end
it 'calls the provided proc to coerce the event name' do
subject.on('valid') { |msg| obj.received_message msg }
expect(obj).to receive(:received_message).with(msg).once
subject.emit :valid, msg
end
end
context 'without coercion', :api_private do
it 'only matches event names on type matches' do
subject.on('valid') { |msg| obj.received_message msg }
expect(obj).to_not receive(:received_message).with(msg)
subject.emit :valid, msg
end
end
context '#on subscribe to multiple events' do
it 'with the same block' do
subject.on(:click, :hover) { |msg| obj.received_message msg }
expect(obj).to receive(:received_message).with(msg).twice
subject.emit :click, msg
subject.emit :hover, msg
end
end
context 'event callback changes within the callback block' do
context 'when new event callbacks are added' do
before do
2.times do
subject.on(:message) do |msg|
obj.received_message msg
subject.on(:message) do |message|
obj.received_message_from_new_callbacks message
end
end
end
allow(obj).to receive(:received_message)
end
it 'is unaffected and processes the prior event callbacks once (#RTE6b)' do
expect(obj).to receive(:received_message).with(msg).twice
expect(obj).to_not receive(:received_message_from_new_callbacks).with(msg)
subject.emit :message, msg
end
it 'adds them for the next emitted event (#RTE6b)' do
expect(obj).to receive(:received_message_from_new_callbacks).with(msg).twice
# New callbacks are added in this emit
subject.emit :message, msg
# New callbacks are now called with second event emitted
subject.emit :message, msg
end
end
context 'when callbacks are removed' do
before do
2.times do
subject.once(:message) do |msg|
obj.received_message msg
subject.off
end
end
end
it 'is unaffected and processes the prior event callbacks once (#RTE6b)' do
expect(obj).to receive(:received_message).with(msg).twice
subject.emit :message, msg
end
it 'removes them for the next emitted event (#RTE6b)' do
expect(obj).to receive(:received_message).with(msg).twice
# Callbacks are removed in this emit
subject.emit :message, msg
# No callbacks should exist now
subject.emit :message, msg
end
end
end
end
context '#on (#RTE3)' do
context 'with event specified' do
it 'calls the block every time an event is emitted only' do
block_called = 0
subject.on('event') { block_called += 1 }
3.times { subject.emit 'event', 'data' }
expect(block_called).to eql(3)
end
it 'catches exceptions in the provided block, logs the error and continues' do
expect(subject.logger).to receive(:error) do |*args, &block|
expect(args.concat([block ? block.call : nil]).join(',')).to match(/Intentional exception/)
end
subject.on(:event) { raise 'Intentional exception' }
subject.emit :event
end
end
context 'with no event specified' do
it 'calls the block every time an event is emitted only' do
block_called = 0
subject.on { block_called += 1 }
3.times { subject.emit 'event', 'data' }
expect(block_called).to eql(3)
end
it 'catches exceptions in the provided block, logs the error and continues' do
expect(subject.logger).to receive(:error) do |*args, &block|
expect(args.concat([block ? block.call : nil]).join(',')).to match(/Intentional exception/)
end
subject.on { raise 'Intentional exception' }
subject.emit :event
end
end
end
context '#unsafe_on', api_private: true do
it 'calls the block every time an event is emitted only' do
block_called = 0
subject.unsafe_on('event') { block_called += 1 }
3.times { subject.emit 'event', 'data' }
expect(block_called).to eql(3)
end
it 'does not catch exceptions in provided blocks' do
subject.unsafe_on(:event) { raise 'Intentional exception' }
expect { subject.emit :event }.to raise_error(/Intentional exception/)
end
end
context '#once (#RTE4)' do
context 'with event specified' do
it 'calls the block the first time an event is emitted only' do
block_called = 0
subject.once('event') { block_called += 1 }
3.times { subject.emit 'event', 'data' }
expect(block_called).to eql(1)
end
it 'does not remove other blocks after it is called' do
block_called = 0
subject.once('event') { block_called += 1 }
subject.on('event') { block_called += 1 }
3.times { subject.emit 'event', 'data' }
expect(block_called).to eql(4)
end
it 'catches exceptions in the provided block, logs the error and continues' do
expect(subject.logger).to receive(:error) do |*args, &block|
expect(args.concat([block ? block.call : nil]).join(',')).to match(/Intentional exception/)
end
subject.once(:event) { raise 'Intentional exception' }
subject.emit :event
end
end
context 'with no event specified' do
it 'calls the block the first time an event is emitted only' do
block_called = 0
subject.once { block_called += 1 }
3.times { subject.emit 'event', 'data' }
expect(block_called).to eql(1)
end
it 'does not remove other blocks after it is called' do
block_called = 0
subject.once { block_called += 1 }
subject.on { block_called += 1 }
3.times { subject.emit 'event', 'data' }
expect(block_called).to eql(4)
end
it 'catches exceptions in the provided block, logs the error and continues' do
expect(subject.logger).to receive(:error) do |*args, &block|
expect(args.concat([block ? block.call : nil]).join(',')).to match(/Intentional exception/)
end
subject.once { raise 'Intentional exception' }
subject.emit :event
end
end
end
context '#unsafe_once' do
it 'calls the block the first time an event is emitted only' do
block_called = 0
subject.unsafe_once('event') { block_called += 1 }
3.times { subject.emit 'event', 'data' }
expect(block_called).to eql(1)
end
it 'does not catch exceptions in provided blocks' do
subject.unsafe_once(:event) { raise 'Intentional exception' }
expect { subject.emit :event }.to raise_error(/Intentional exception/)
end
end
context '#off' do
let(:callback) { lambda { |msg| obj.received_message msg } }
context 'with event specified in on handler' do
before do
subject.on(:message, &callback)
end
after do
subject.emit :message, msg
end
context 'with event names as arguments' do
it 'deletes matching callbacks when a block is provided' do
expect(obj).to_not receive(:received_message).with(msg)
subject.off(:message, &callback)
end
it 'deletes all matching callbacks when a block is not provided' do
expect(obj).to_not receive(:received_message).with(msg)
subject.off(:message)
end
it 'continues if the block does not exist' do
expect(obj).to receive(:received_message).with(msg)
subject.off(:message) { true }
end
end
context 'without any event names' do
it 'deletes all matching callbacks' do
expect(obj).to_not receive(:received_message).with(msg)
subject.off(&callback)
end
it 'deletes all callbacks if not block given' do
expect(obj).to_not receive(:received_message).with(msg)
subject.off
end
end
end
context 'when on callback is configured for all events' do
before do
subject.on(&callback)
end
after do
subject.emit :message, msg
end
context 'with event names as arguments' do
it 'does not remove the all events callback when a block is provided' do
expect(obj).to receive(:received_message).with(msg)
subject.off(:message, &callback)
end
it 'does not remove the all events callback when a block is not provided' do
expect(obj).to receive(:received_message).with(msg)
subject.off(:message)
end
it 'does not remove the all events callback when the block does not match' do
expect(obj).to receive(:received_message).with(msg)
subject.off(:message) { true }
end
end
context 'without any event names' do
it 'deletes all matching callbacks' do
expect(obj).to_not receive(:received_message).with(msg)
subject.off(&callback)
end
it 'deletes all callbacks if not block given' do
expect(obj).to_not receive(:received_message).with(msg)
subject.off
end
end
end
context 'with unsafe_on subscribers' do
before do
subject.unsafe_on(&callback)
end
after do
subject.emit :message, msg
end
it 'does not deregister them' do
expect(obj).to receive(:received_message).with(msg)
subject.off
end
end
context 'with unsafe_once subscribers' do
before do
subject.unsafe_once(&callback)
end
after do
subject.emit :message, msg
end
it 'does not deregister them' do
expect(obj).to receive(:received_message).with(msg)
subject.off
end
end
end
context '#unsafe_off' do
let(:callback) { lambda { |msg| obj.received_message msg } }
context 'with unsafe_on subscribers' do
before do
subject.unsafe_on(&callback)
end
after do
subject.emit :message, msg
end
it 'deregisters them' do
expect(obj).to_not receive(:received_message).with(msg)
subject.unsafe_off
end
end
context 'with unsafe_once subscribers' do
before do
subject.unsafe_once(&callback)
end
after do
subject.emit :message, msg
end
it 'deregister them' do
expect(obj).to_not receive(:received_message).with(msg)
subject.unsafe_off
end
end
context 'with on subscribers' do
before do
subject.on(&callback)
end
after do
subject.emit :message, msg
end
it 'does not deregister them' do
expect(obj).to receive(:received_message).with(msg)
subject.unsafe_off
end
end
context 'with once subscribers' do
before do
subject.once(&callback)
end
after do
subject.emit :message, msg
end
it 'does not deregister them' do
expect(obj).to receive(:received_message).with(msg)
subject.unsafe_off
end
end
end
end