forked from ably/ably-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_spec.rb
70 lines (59 loc) · 2.18 KB
/
connection_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
require 'spec_helper'
require 'shared/protocol_msgbus_behaviour'
describe Ably::Realtime::Connection do
let(:client) { instance_double('Ably::Realtime::Client', logger: double('logger').as_null_object, recover: nil, endpoint: double('endpoint', host: 'realtime.ably.io')) }
subject do
Ably::Realtime::Connection.new(client, {}).tap do |connection|
connection.__incoming_protocol_msgbus__.unsubscribe
connection.__outgoing_protocol_msgbus__.unsubscribe
end
end
before do
expect(EventMachine).to receive(:next_tick) # non_blocking_loop_while for delivery of messages async
subject.__incoming_protocol_msgbus__.off
subject.__outgoing_protocol_msgbus__.off
end
describe 'callbacks' do
specify 'are supported for valid STATE events' do
state = nil
subject.on(:initialized) { state = :ready }
expect { subject.emit(:initialized) }.to change { state }.to(:ready)
end
specify 'fail with unacceptable STATE event names' do
expect { subject.on(:invalid) }.to raise_error KeyError
expect { subject.emit(:invalid) }.to raise_error KeyError
expect { subject.off(:invalid) }.to raise_error KeyError
end
end
it_behaves_like 'an incoming protocol message bus'
it_behaves_like 'an outgoing protocol message bus'
describe 'connection resume callbacks', api_private: true do
let(:callbacks) { [] }
describe '#trigger_resumed' do
it 'executes the callbacks' do
subject.on_resume { callbacks << true }
subject.trigger_resumed
expect(callbacks.count).to eql(1)
end
end
describe '#on_resume' do
it 'registers a callback' do
subject.on_resume { callbacks << true }
subject.trigger_resumed
expect(callbacks.count).to eql(1)
end
end
describe '#off_resume' do
it 'registers a callback' do
subject.on_resume { callbacks << true }
additional_proc = lambda { raise 'This should not be called' }
subject.off_resume(&additional_proc)
subject.trigger_resumed
expect(callbacks.count).to eql(1)
end
end
end
after(:all) do
sleep 1 # let realtime library shut down any open clients
end
end