Skip to content

Commit 328d012

Browse files
committed
Merge branch 'master' into multithreading_updates_2
2 parents 540a559 + 07a3073 commit 328d012

5 files changed

Lines changed: 89 additions & 68 deletions

File tree

spec/cable/connection_spec.cr

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe Cable::Connection do
2525
it "ignores empty messages" do
2626
connect do |connection, socket|
2727
connection.receive("")
28-
sleep 0.1
28+
sleep 100.milliseconds
2929

3030
socket.messages.size.should eq(0)
3131

@@ -42,7 +42,7 @@ describe Cable::Connection do
4242
connection.receive([{command: "subscribe"}].to_json)
4343
end
4444

45-
sleep 0.1
45+
sleep 100.milliseconds
4646

4747
socket.messages.size.should eq(0)
4848

@@ -56,7 +56,7 @@ describe Cable::Connection do
5656
it "accepts" do
5757
connect do |connection, socket|
5858
connection.receive({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
59-
sleep 0.1
59+
sleep 100.milliseconds
6060

6161
socket.messages.should contain({"type" => "confirm_subscription", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
6262

@@ -68,7 +68,7 @@ describe Cable::Connection do
6868
it "accepts without params hash key" do
6969
connect do |connection, socket|
7070
connection.receive({"command" => "subscribe", "identifier" => {channel: "AppearanceChannel"}.to_json}.to_json)
71-
sleep 0.1
71+
sleep 100.milliseconds
7272

7373
socket.messages.should contain({"type" => "confirm_subscription", "identifier" => {channel: "AppearanceChannel"}.to_json}.to_json)
7474

@@ -80,7 +80,7 @@ describe Cable::Connection do
8080
it "accepts with nested hash" do
8181
connect do |connection, socket|
8282
connection.receive({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1", person: {name: "Foo", age: 32, boom: "boom"}}.to_json}.to_json)
83-
sleep 0.1
83+
sleep 100.milliseconds
8484

8585
socket.messages.should contain({"type" => "confirm_subscription", "identifier" => {channel: "ChatChannel", room: "1", person: {name: "Foo", age: 32, boom: "boom"}}.to_json}.to_json)
8686

@@ -92,7 +92,7 @@ describe Cable::Connection do
9292
it "accepts without auth token" do
9393
connect(connection_class: ConnectionNoTokenTest, token: nil) do |connection, socket|
9494
connection.receive({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1", person: {name: "Celso", age: 32, boom: "boom"}}.to_json}.to_json)
95-
sleep 0.1
95+
sleep 100.milliseconds
9696

9797
socket.messages.should contain({"type" => "confirm_subscription", "identifier" => {channel: "ChatChannel", room: "1", person: {name: "Celso", age: 32, boom: "boom"}}.to_json}.to_json)
9898

@@ -104,9 +104,9 @@ describe Cable::Connection do
104104
it "blocks the same connection from subscribing to the same channel multiple times" do
105105
connect do |connection, socket|
106106
connection.receive({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
107-
sleep 0.1
107+
sleep 100.milliseconds
108108
connection.receive({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
109-
sleep 0.1
109+
sleep 100.milliseconds
110110
connection.receive({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
111111

112112
# ensure we only allow subscribing to the same channel once from a connection
@@ -124,7 +124,7 @@ describe Cable::Connection do
124124
connect do |connection, socket|
125125
connection.receive({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
126126
connection.receive({"command" => "unsubscribe", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
127-
sleep 0.1
127+
sleep 100.milliseconds
128128

129129
socket.messages.should contain({"type" => "confirm_subscription", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
130130
socket.messages.should contain({"type" => "confirm_unsubscription", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
@@ -177,7 +177,7 @@ describe Cable::Connection do
177177
connect do |connection, socket|
178178
connection.receive({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
179179
connection.receive({"command" => "message", "identifier" => {channel: "UnknownChannel", room: "1"}.to_json, "data" => {invite_id: "3", action: "invite"}.to_json}.to_json)
180-
sleep 0.1
180+
sleep 100.milliseconds
181181

182182
socket.messages.should contain({"type" => "confirm_subscription", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
183183

@@ -189,9 +189,9 @@ describe Cable::Connection do
189189
it "receives a message and send to Channel#receive" do
190190
connect do |connection, socket|
191191
connection.receive({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
192-
sleep 0.1
192+
sleep 100.milliseconds
193193
connection.receive({"command" => "message", "identifier" => {channel: "ChatChannel", room: "1"}.to_json, "data" => {message: "Hello"}.to_json}.to_json)
194-
sleep 0.1
194+
sleep 100.milliseconds
195195

196196
socket.messages.should contain({"type" => "confirm_subscription", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
197197
socket.messages.should contain({"identifier" => {channel: "ChatChannel", room: "1"}.to_json, "message" => {message: "Hello", current_user: "98"}}.to_json)
@@ -204,9 +204,9 @@ describe Cable::Connection do
204204
it "receives a message with an action key and sends to Channel#Perform" do
205205
connect do |connection, socket|
206206
connection.receive({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
207-
sleep 0.1
207+
sleep 100.milliseconds
208208
connection.receive({"command" => "message", "identifier" => {channel: "ChatChannel", room: "1"}.to_json, "data" => {invite_id: "4", action: "invite"}.to_json}.to_json)
209-
sleep 0.1
209+
sleep 100.milliseconds
210210

211211
socket.messages.should contain({"type" => "confirm_subscription", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
212212
socket.messages.should contain({"identifier" => {channel: "ChatChannel", room: "1"}.to_json, "message" => {"performed" => "invite", "params" => "4"}}.to_json)
@@ -221,7 +221,7 @@ describe Cable::Connection do
221221
it "sends the broadcasted message" do
222222
connect do |connection, socket|
223223
connection.receive({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
224-
sleep 0.1
224+
sleep 100.milliseconds
225225
connection.broadcast_to(ConnectionTest::CHANNELS[connection.connection_identifier][{channel: "ChatChannel", room: "1"}.to_json], {hello: "Broadcast!"}.to_json)
226226

227227
socket.messages.should contain({"type" => "confirm_subscription", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
@@ -236,9 +236,9 @@ describe Cable::Connection do
236236
it "sends the broadcasted message" do
237237
connect do |connection, socket|
238238
connection.receive({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
239-
sleep 0.1
239+
sleep 100.milliseconds
240240
ConnectionTest.broadcast_to("chat_1", {hello: "Broadcast!"}.to_json)
241-
sleep 0.1
241+
sleep 100.milliseconds
242242

243243
socket.messages.should contain({"type" => "confirm_subscription", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
244244
socket.messages.should contain({identifier: {channel: "ChatChannel", room: "1"}.to_json, message: {hello: "Broadcast!"}}.to_json)
@@ -254,9 +254,9 @@ describe Cable::Connection do
254254
it "receives correctly" do
255255
connect do |connection, socket|
256256
connection.receive({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
257-
sleep 0.1
257+
sleep 100.milliseconds
258258
ChatChannel.broadcast_to(channel: "chat_1", message: "<turbo-stream></turbo-stream>")
259-
sleep 0.1
259+
sleep 100.milliseconds
260260

261261
socket.messages.should contain({"type" => "confirm_subscription", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
262262
socket.messages.should contain({"identifier" => {channel: "ChatChannel", room: "1"}.to_json, "message" => "<turbo-stream></turbo-stream>"}.to_json)
@@ -269,10 +269,10 @@ describe Cable::Connection do
269269
it "receives string json correctly" do
270270
connect do |connection, socket|
271271
connection.receive({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
272-
sleep 0.1
272+
sleep 100.milliseconds
273273
json_message = %({"foo": "bar"})
274274
ChatChannel.broadcast_to(channel: "chat_1", message: json_message)
275-
sleep 0.1
275+
sleep 100.milliseconds
276276

277277
socket.messages.should contain({"type" => "confirm_subscription", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
278278
socket.messages.should contain({"identifier" => {channel: "ChatChannel", room: "1"}.to_json, "message" => JSON.parse(%({"foo": "bar"}))}.to_json)
@@ -287,9 +287,9 @@ describe Cable::Connection do
287287
it "receives correctly" do
288288
connect do |connection, socket|
289289
connection.receive({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
290-
sleep 0.1
290+
sleep 100.milliseconds
291291
ChatChannel.broadcast_to(channel: "chat_1", message: {"foo" => "bar"})
292-
sleep 0.1
292+
sleep 100.milliseconds
293293

294294
socket.messages.should contain({"type" => "confirm_subscription", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
295295
socket.messages.should contain({"identifier" => {channel: "ChatChannel", room: "1"}.to_json, "message" => {"foo" => "bar"}}.to_json)
@@ -304,10 +304,10 @@ describe Cable::Connection do
304304
it "receives correctly" do
305305
connect do |connection, socket|
306306
connection.receive({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
307-
sleep 0.1
307+
sleep 100.milliseconds
308308
json_message = JSON.parse(%({"foo": "bar"}))
309309
ChatChannel.broadcast_to(channel: "chat_1", message: json_message)
310-
sleep 0.1
310+
sleep 100.milliseconds
311311

312312
socket.messages.should contain({"type" => "confirm_subscription", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
313313
socket.messages.should contain({"identifier" => {channel: "ChatChannel", room: "1"}.to_json, "message" => {"foo" => "bar"}}.to_json)
@@ -324,9 +324,9 @@ describe Cable::Connection do
324324
it "receives correctly" do
325325
connect do |connection, socket|
326326
connection.receive({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
327-
sleep 0.1
327+
sleep 100.milliseconds
328328
Cable.server.publish(channel: "chat_1", message: "<turbo-stream></turbo-stream>")
329-
sleep 0.1
329+
sleep 100.milliseconds
330330

331331
socket.messages.should contain({"type" => "confirm_subscription", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
332332
socket.messages.should contain({"identifier" => {channel: "ChatChannel", room: "1"}.to_json, "message" => "<turbo-stream></turbo-stream>"}.to_json)
@@ -339,10 +339,10 @@ describe Cable::Connection do
339339
it "receives string json correctly" do
340340
connect do |connection, socket|
341341
connection.receive({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
342-
sleep 0.1
342+
sleep 100.milliseconds
343343
json_message = %({"foo": "bar"})
344344
Cable.server.publish(channel: "chat_1", message: json_message)
345-
sleep 0.1
345+
sleep 100.milliseconds
346346

347347
socket.messages.should contain({"type" => "confirm_subscription", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
348348
socket.messages.should contain({"identifier" => {channel: "ChatChannel", room: "1"}.to_json, "message" => JSON.parse(%({"foo": "bar"}))}.to_json)
@@ -357,9 +357,9 @@ describe Cable::Connection do
357357
it "receives correctly" do
358358
connect do |connection, socket|
359359
connection.receive({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
360-
sleep 0.1
360+
sleep 100.milliseconds
361361
Cable.server.publish(channel: "chat_1", message: %({"foo": "bar"}))
362-
sleep 0.1
362+
sleep 100.milliseconds
363363

364364
socket.messages.should contain({"type" => "confirm_subscription", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
365365
socket.messages.should contain({"identifier" => {channel: "ChatChannel", room: "1"}.to_json, "message" => {"foo" => "bar"}}.to_json)
@@ -376,13 +376,13 @@ describe Cable::Connection do
376376
connect do |connection, socket|
377377
connection.receive({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
378378
connection.receive({"command" => "subscribe", "identifier" => {channel: "RejectionChannel"}.to_json}.to_json)
379-
sleep 0.1
379+
sleep 100.milliseconds
380380
json_message = JSON.parse(%({"foo": "bar"}))
381381
ChatChannel.broadcast_to(channel: "chat_1", message: json_message)
382382

383383
json_message = JSON.parse(%({"foo": "bar"}))
384384
RejectionChannel.broadcast_to(channel: "rejection", message: json_message)
385-
sleep 0.1
385+
sleep 100.milliseconds
386386

387387
# Even after broadcasting to Rejection channel, we can check the socket didn't receive it
388388
socket.messages.size.should eq(3)
@@ -407,14 +407,14 @@ describe Cable::Connection do
407407
connection_2 = ConnectionTest.new(builds_request(token: "101"), socket_2)
408408

409409
connection_1.receive({"command" => "subscribe", "identifier" => {channel: "CallbackTransmitChannel"}.to_json}.to_json)
410-
sleep 0.1
410+
sleep 100.milliseconds
411411
CallbackTransmitChannel.broadcast_to(channel: "callbacks_01", message: "<turbo-stream></turbo-stream>")
412-
sleep 0.1
412+
sleep 100.milliseconds
413413

414414
connection_2.receive({"command" => "subscribe", "identifier" => {channel: "CallbackTransmitChannel"}.to_json}.to_json)
415-
sleep 0.1
415+
sleep 100.milliseconds
416416
CallbackTransmitChannel.broadcast_to(channel: "callbacks_01", message: "<turbo-stream>2nd</turbo-stream>")
417-
sleep 0.1
417+
sleep 100.milliseconds
418418

419419
# since socket_1 was connected first
420420
# 1 x received the subscribe command message
@@ -465,14 +465,14 @@ describe Cable::Connection do
465465
connection_2 = ConnectionTest.new(builds_request(token: "101"), socket_2)
466466

467467
connection_1.receive({"command" => "subscribe", "identifier" => {channel: "CallbackConnectionTransmitChannel"}.to_json}.to_json)
468-
sleep 0.1
468+
sleep 100.milliseconds
469469
CallbackConnectionTransmitChannel.broadcast_to(channel: "callbacks_02", message: "<turbo-stream></turbo-stream>")
470-
sleep 0.1
470+
sleep 100.milliseconds
471471

472472
connection_2.receive({"command" => "subscribe", "identifier" => {channel: "CallbackConnectionTransmitChannel"}.to_json}.to_json)
473-
sleep 0.1
473+
sleep 100.milliseconds
474474
CallbackConnectionTransmitChannel.broadcast_to(channel: "callbacks_02", message: "<turbo-stream>2nd</turbo-stream>")
475-
sleep 0.1
475+
sleep 100.milliseconds
476476

477477
# since socket_1 was connected first
478478
# 1 x received the subscribe command message

spec/cable/handler_spec.cr

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ describe Cable::Handler do
185185

186186
# wait server subscribe to channel
187187
# how can we ensure it was subscribed and avoid this sleep?
188-
sleep 0.2
188+
sleep 200.milliseconds
189189

190190
# App.cable.subscriptions.subscriptions[0].send({message: "test"})
191191
ws2.send({"command" => "message", "identifier" => {channel: "ChatChannel", room: "1"}.to_json, "data" => {message: "test"}.to_json}.to_json)
@@ -213,7 +213,7 @@ describe Cable::Handler do
213213
# before `ws2.close`
214214
if seq == 0
215215
# this is a sleep to avoid publishing before channel hasn't subscribed
216-
sleep 0.2
216+
sleep 200.milliseconds
217217
Cable.server.publish("chat_1", {"message" => "from Ruby!", "current_user" => "1"}.to_json)
218218
end
219219
str.should eq(messages[seq])
@@ -249,7 +249,7 @@ describe Cable::Handler do
249249

250250
# wait server subscribe to channel
251251
# how can we ensure it was subscribed and avoid this sleep?
252-
sleep 0.1
252+
sleep 100.milliseconds
253253

254254
# App.cable.subscriptions.subscriptions[0].perform("invite", {invite_id: "3"});
255255
ws2.send({command: "message", identifier: {channel: "ChatChannel", room: "1"}.to_json, data: {invite_id: "3", action: "invite"}.to_json}.to_json)
@@ -329,7 +329,7 @@ describe Cable::Handler do
329329
# subscribe
330330
ws2.send({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
331331

332-
sleep 0.2
332+
sleep 200.milliseconds
333333

334334
# send message
335335
ws2.send({"command" => "message", "identifier" => {channel: "ChatChannel", room: "1"}.to_json, "data" => {message: "test"}.to_json}.to_json)
@@ -365,7 +365,7 @@ describe Cable::Handler do
365365
# subscribe
366366
ws3.send({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
367367

368-
sleep 0.2
368+
sleep 200.milliseconds
369369

370370
# send message
371371
ws3.send({"command" => "message", "identifier" => {channel: "ChatChannel", room: "1"}.to_json, "data" => {message: "test"}.to_json}.to_json)
@@ -401,7 +401,7 @@ describe Cable::Handler do
401401
# subscribe
402402
ws4.send({"command" => "subscribe", "identifier" => {channel: "ChatChannel", room: "1"}.to_json}.to_json)
403403

404-
sleep 0.2
404+
sleep 200.milliseconds
405405

406406
# send message
407407
ws4.send({"command" => "message", "identifier" => {channel: "ChatChannel", room: "1"}.to_json, "data" => {message: "test"}.to_json}.to_json)
@@ -410,7 +410,7 @@ describe Cable::Handler do
410410

411411
ws4.run
412412

413-
sleep 0.2
413+
sleep 200.milliseconds
414414

415415
# we should have 1 connectoon ws5 open
416416
# but since the server restarted due to volume of errors

src/cable.cr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
require "mutex"
2+
require "set"
3+
require "uuid"
14
require "habitat"
25
require "json"
36
require "./cable/**"

0 commit comments

Comments
 (0)