From 604fd2cb227d92bed1b738e71feb4ff5360f4491 Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Fri, 19 Feb 2016 07:50:42 +1030 Subject: [PATCH] Use a semaphore to signal message availability The Event hack was too much of a hack: on actually thinking about it, there's a rather obvious race. --- actioncable/test/client_test.rb | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/actioncable/test/client_test.rb b/actioncable/test/client_test.rb index d30c381131..26629216e5 100644 --- a/actioncable/test/client_test.rb +++ b/actioncable/test/client_test.rb @@ -54,7 +54,7 @@ class ClientTest < ActionCable::TestCase @ws = Faye::WebSocket::Client.new("ws://127.0.0.1:#{port}/") @messages = Queue.new @closed = Concurrent::Event.new - @has_messages = Concurrent::Event.new + @has_messages = Concurrent::Semaphore.new(0) @pings = 0 open = Concurrent::Event.new @@ -79,7 +79,7 @@ class ClientTest < ActionCable::TestCase @pings += 1 else @messages << hash - @has_messages.set + @has_messages.release end end @@ -92,8 +92,7 @@ class ClientTest < ActionCable::TestCase end def read_message - @has_messages.wait(WAIT_WHEN_EXPECTING_EVENT) if @messages.empty? - @has_messages.reset if @messages.size < 2 + @has_messages.try_acquire(1, WAIT_WHEN_EXPECTING_EVENT) msg = @messages.pop(true) raise msg if msg.is_a?(Exception) @@ -104,9 +103,11 @@ class ClientTest < ActionCable::TestCase def read_messages(expected_size = 0) list = [] loop do - @has_messages.wait(list.size < expected_size ? WAIT_WHEN_EXPECTING_EVENT : WAIT_WHEN_NOT_EXPECTING_EVENT) - if @has_messages.set? - list << read_message + if @has_messages.try_acquire(1, list.size < expected_size ? WAIT_WHEN_EXPECTING_EVENT : WAIT_WHEN_NOT_EXPECTING_EVENT) + msg = @messages.pop(true) + raise msg if msg.is_a?(Exception) + + list << msg else break end