mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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.
This commit is contained in:
parent
9671df3ff7
commit
604fd2cb22
1 changed files with 8 additions and 7 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue