Skip to content

Commit 63abf62

Browse files
committed
Minor changes to integration specs. Ensure client.stop! is called from within client thread.
1 parent 92e80b5 commit 63abf62

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

spec/integration/integration_spec.rb

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
Slack.configure do |slack|
2020
slack.logger = logger
2121
end
22-
23-
@queue = QueueWithTimeout.new
2422
end
2523

2624
after do
@@ -29,15 +27,17 @@
2927

3028
let(:client) { Slack::RealTime::Client.new(token: ENV['SLACK_API_TOKEN']) }
3129

32-
let(:queue) { @queue }
30+
let!(:queue) { @queue = QueueWithTimeout.new }
3331

3432
def start
3533
# starts the client and pushes an item on a queue when connected
3634
client.start_async do |driver|
3735
driver.on :open do |data|
3836
logger.debug "connection.on :open, data=#{data}"
39-
queue.push nil
37+
queue.push :opened
4038
end
39+
40+
yield driver if block_given?
4141
end
4242
end
4343

@@ -49,16 +49,16 @@ def start
4949
client.on :close do
5050
logger.info 'Disconnecting ...'
5151
# pushes another item to the queue when disconnected
52-
queue.push nil if @queue
52+
queue.push :closed
5353
end
5454
end
5555

56-
def start_server
57-
dt = rand(2..6)
56+
def start_server(&block)
57+
dt = rand(10..20)
5858
logger.debug "#start_server, waiting #{dt} second(s)"
5959
sleep dt # prevent Slack 429 rate limit errors
6060
# start server and wait for on :open
61-
@server = start
61+
@server = start(&block)
6262
logger.debug "started #{@server}"
6363
queue.pop_with_timeout(5)
6464
end
@@ -72,22 +72,12 @@ def wait_for_server
7272
@queue = nil
7373
end
7474

75-
def stop_server
76-
logger.debug '#stop_server'
77-
client.stop!
78-
logger.debug '#stop_server, stopped'
79-
end
80-
8175
after do
82-
wait_for_server
76+
wait_for_server # wait for :closed to be pushed on queue
8377
@server.join if @server.is_a?(::Thread)
8478
end
8579

8680
context 'client connected' do
87-
before do
88-
start_server
89-
end
90-
9181
let(:channel) { "@#{client.self.id}" }
9282

9383
it 'responds to message' do
@@ -105,13 +95,17 @@ def stop_server
10595
client.stop!
10696
end
10797

98+
start_server
99+
108100
logger.debug "chat_postMessage, channel=#{channel}, message=#{message}"
109101
client.web_client.chat_postMessage channel: channel, text: message
110102
end
111103

112104
it 'sends message' do
113-
client.message(channel: channel, text: 'Hello world!')
114-
client.stop!
105+
start_server do
106+
client.message(channel: channel, text: 'Hello world!')
107+
client.stop!
108+
end
115109
end
116110
end
117111

@@ -127,19 +121,23 @@ def stop_server
127121

128122
context 'with websocket_ping set' do
129123
before do
130-
client.websocket_ping = 2
124+
client.websocket_ping = 1
131125
end
126+
132127
it 'sends pings' do
133128
@reply_to = nil
134129
client.on :pong do |data|
135130
@reply_to = data.reply_to
136-
queue.push nil
131+
queue.push :pong
137132
client.stop!
138133
end
134+
139135
start_server
136+
140137
queue.pop_with_timeout(5)
141138
expect(@reply_to).to be 1
142139
end
140+
143141
it 'rebuilds the websocket connection when dropped' do
144142
@reply_to = nil
145143
client.on :pong do |data|
@@ -148,11 +146,13 @@ def stop_server
148146
client.instance_variable_get(:@socket).close
149147
else
150148
expect(@reply_to).to be 2
151-
queue.push nil
149+
queue.push :pong
152150
client.stop!
153151
end
154152
end
153+
155154
start_server
155+
156156
queue.pop_with_timeout(10)
157157
queue.pop_with_timeout(10)
158158
end
@@ -162,16 +162,21 @@ def stop_server
162162
before do
163163
client.websocket_ping = 0
164164
end
165+
165166
it 'does not send pings' do
166167
@reply_to = nil
168+
167169
client.on :pong do |data|
168170
@reply_to = data.reply_to
169171
end
172+
170173
client.on :hello do
171174
client.stop!
172175
end
176+
173177
start_server
174178
wait_for_server
179+
175180
expect(@reply_to).to be nil
176181
end
177182
end

0 commit comments

Comments
 (0)