-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathchannels_spec.rb
146 lines (117 loc) · 4.53 KB
/
channels_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
# encoding: utf-8
require 'spec_helper'
describe Ably::Rest::Channels do
let(:client) { instance_double('Ably::Rest::Client', logger: double('logger').as_null_object) }
let(:channel_name) { 'unique'.encode(Encoding::UTF_8) }
let(:options) do
{ params: { 'bizarre' => 'value' } }
end
subject { Ably::Rest::Channels.new(client) }
describe '#get' do
context "when channel doesn't exist" do
shared_examples 'creates a channel' do
it 'creates a channel (RSN3a)' do
expect(Ably::Rest::Channel).to receive(:new).with(client, channel_name, options)
subject.get(channel_name, options)
end
end
describe 'hash' do
let(:channel_options) { options }
it { expect(channel_options).to be_a(Hash) }
include_examples 'creates a channel'
end
describe 'ChannelOptions object' do
let(:channel_options) { Ably::Models::ChannelOptions.new(options) }
it { expect(channel_options).to be_a(Ably::Models::ChannelOptions) }
include_examples 'creates a channel'
end
end
context 'when an existing channel exists' do
shared_examples 'reuse a channel object if it exists' do
it 'will reuse a channel object if it exists (RSN3a)' do
channel = subject.get(channel_name, channel_options)
expect(channel).to be_a(Ably::Rest::Channel)
expect(subject.get(channel_name, channel_options).object_id).to eql(channel.object_id)
end
end
describe 'hash' do
let(:channel_options) { options }
it { expect(channel_options).to be_a(Hash) }
include_examples 'reuse a channel object if it exists'
end
describe 'ChannelOptions object' do
let(:channel_options) { Ably::Models::ChannelOptions.new(options) }
it { expect(channel_options).to be_a(Ably::Models::ChannelOptions) }
include_examples 'reuse a channel object if it exists'
end
context 'with new channel_options modes' do
shared_examples 'update channel with provided options :modes' do
it 'will update channel with provided options modes (RSN3c)' do
channel = subject.get(channel_name, channel_options)
expect(channel.options.modes).to eq(modes)
subject.get(channel_name, channel_options)
expect(channel.options.modes).to eq(modes)
end
end
let(:modes) { %i[subscribe] }
let(:new_options) { { modes: modes } }
describe 'hash' do
let(:channel_options) { new_options }
it { expect(channel_options).to be_a(Hash) }
include_examples 'update channel with provided options :modes'
end
describe 'ChannelOptions object' do
let(:channel_options) { Ably::Models::ChannelOptions.new(new_options) }
it { expect(channel_options).to be_a(Ably::Models::ChannelOptions) }
include_examples 'update channel with provided options :modes'
end
end
end
end
it '[] creates a channel' do
expect(Ably::Rest::Channel).to receive(:new).with(client, channel_name, options)
subject.get(channel_name, options)
end
context '#fetch' do
it 'retrieves a channel if it exists' do
channel = subject.get(channel_name, options)
expect(subject.fetch(channel_name)).to eql(channel)
end
it 'calls the block if channel is missing' do
block_called = false
subject.fetch(channel_name) { block_called = true }
expect(block_called).to eql(true)
end
end
context 'destroying channels' do
it '#release releases the channel resoures' do
released_channel = subject.get(channel_name, options)
subject.release(channel_name)
expect(subject.get(channel_name, options).object_id).to_not eql(released_channel.object_id)
end
end
context 'is Enumerable' do
let(:channel_count) { 5 }
let(:mock_channel) { instance_double('Ably::Rest::Channel') }
before do
allow(Ably::Rest::Channel).to receive(:new).and_return(mock_channel)
channel_count.times { |index| subject.get("channel-#{index}") }
end
it 'allows enumeration' do
expect(subject.map.count).to eql(channel_count)
end
context '#each' do
it 'returns an enumerator' do
expect(subject.each).to be_a(Enumerator)
end
it 'yields each channel' do
subject.each do |channel|
expect(channel).to eql(mock_channel)
end
end
end
it 'provides #length' do
expect(subject.length).to eql(channel_count)
end
end
end