mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
788c908577
I often face the following error when running test/connection/client_socket_test.rb:
```
$ bin/test test/connection/client_socket_test.rb:38
Run options: --seed 44035
# Running:
F
Failure:
ActionCable::Connection::ClientSocketTest#test_delegate_socket_errors_to_on_error_handler
[/app/actioncable/test/connection/client_socket_test.rb:47]:
--- expected
+++ actual
@@ -1 +1 @@
-["foo"]
+["Broken pipe", "Broken pipe", "Broken pipe", "foo"]
```
It can be reproduced easily by applying the following patch:
```
diff --git a/actioncable/test/connection/client_socket_test.rb b/actioncable/test/connection/client_socket_test.rb
index 2051216010..6bb9f13ea7 100644
--- a/actioncable/test/connection/client_socket_test.rb
+++ b/actioncable/test/connection/client_socket_test.rb
@@ -34,7 +34,8 @@ def on_error(message)
@server.config.allowed_request_origins = %w( http://rubyonrails.com )
end
- test "delegate socket errors to on_error handler" do
+ 1000.times do |i|
+ test "delegate socket errors to on_error handler #{i}" do
run_in_eventmachine do
connection = open_connection
@@ -47,6 +48,7 @@ def on_error(message)
assert_equal %w[ foo ], connection.errors
end
end
+ end
test "closes hijacked i/o socket at shutdown" do
run_in_eventmachine do
```
The cause is writing io from different thread at the same time.
`connection.process` sends handshake message from [StreamEventLoop's thread][] whereas
`connection.handle_open` sends welcome message from current thread.
[StreamEventLoop's thread]: 067fc779c4/actioncable/lib/action_cable/connection/stream_event_loop.rb (L75)
92 lines
2.3 KiB
Ruby
92 lines
2.3 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "test_helper"
|
|
require "stubs/test_server"
|
|
|
|
class ActionCable::Connection::ClientSocketTest < ActionCable::TestCase
|
|
class Connection < ActionCable::Connection::Base
|
|
attr_reader :connected, :websocket, :errors
|
|
|
|
def initialize(*)
|
|
super
|
|
@errors = []
|
|
end
|
|
|
|
def connect
|
|
@connected = true
|
|
end
|
|
|
|
def disconnect
|
|
@connected = false
|
|
end
|
|
|
|
def send_async(method, *args)
|
|
send method, *args
|
|
end
|
|
|
|
def on_error(message)
|
|
@errors << message
|
|
end
|
|
end
|
|
|
|
setup do
|
|
@server = TestServer.new
|
|
@server.config.allowed_request_origins = %w( http://rubyonrails.com )
|
|
end
|
|
|
|
test "delegate socket errors to on_error handler" do
|
|
run_in_eventmachine do
|
|
connection = open_connection
|
|
|
|
# Internal hax = :(
|
|
client = connection.websocket.send(:websocket)
|
|
client.instance_variable_get("@stream").expects(:write).raises("foo")
|
|
client.expects(:client_gone).never
|
|
|
|
client.write("boo")
|
|
assert_equal %w[ foo ], connection.errors
|
|
end
|
|
end
|
|
|
|
test "closes hijacked i/o socket at shutdown" do
|
|
run_in_eventmachine do
|
|
connection = open_connection
|
|
|
|
client = connection.websocket.send(:websocket)
|
|
event = Concurrent::Event.new
|
|
client.instance_variable_get("@stream")
|
|
.instance_variable_get("@rack_hijack_io")
|
|
.define_singleton_method(:close) { event.set }
|
|
connection.close
|
|
event.wait
|
|
end
|
|
end
|
|
|
|
private
|
|
def open_connection
|
|
env = Rack::MockRequest.env_for "/test",
|
|
"HTTP_CONNECTION" => "upgrade", "HTTP_UPGRADE" => "websocket",
|
|
"HTTP_HOST" => "localhost", "HTTP_ORIGIN" => "http://rubyonrails.com"
|
|
io, client_io = \
|
|
begin
|
|
Socket.pair(Socket::AF_UNIX, Socket::SOCK_STREAM, 0)
|
|
rescue
|
|
StringIO.new
|
|
end
|
|
env["rack.hijack"] = -> { env["rack.hijack_io"] = io }
|
|
|
|
Connection.new(@server, env).tap do |connection|
|
|
connection.process
|
|
if client_io
|
|
# Make sure server returns handshake response
|
|
Timeout.timeout(1) do
|
|
loop do
|
|
break if client_io.readline == "\r\n"
|
|
end
|
|
end
|
|
end
|
|
connection.send :handle_open
|
|
assert connection.connected
|
|
end
|
|
end
|
|
end
|