-
-
Save matthewd/5605824719c87f160626 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
commit 604fd2cb227d92bed1b738e71feb4ff5360f4491 | |
Author: Matthew Draper <[email protected]> | |
Date: Fri Feb 19 07:50:42 2016 +1030 | |
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. | |
diff --git a/actioncable/test/client_test.rb b/actioncable/test/client_test.rb | |
index d30c381..2662921 100644 | |
--- a/actioncable/test/client_test.rb | |
+++ b/actioncable/test/client_test.rb | |
@@ -54,7 +54,7 @@ def initialize(port) | |
@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 @@ def initialize(port) | |
@pings += 1 | |
else | |
@messages << hash | |
- @has_messages.set | |
+ @has_messages.release | |
end | |
end | |
@@ -92,8 +92,7 @@ def initialize(port) | |
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 @@ def read_message | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment