Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Fix deadlocking when sending an action in a response #41

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/ruby_ami/stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def run

def post_init
@state = :started
fire_event Connected.new
async.fire_event Connected.new
login @username, @password if @username && @password
end

Expand Down Expand Up @@ -93,7 +93,7 @@ def message_received(message)
action << message
complete_causal_action_for_event message if action.complete?
else
fire_event message
async.fire_event message
end
when Response, Error
action = sent_action_for_response message
Expand Down
33 changes: 33 additions & 0 deletions spec/ruby_ami/stream_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ def mocked_server(times = nil, fake_client = nil, &block)
rescue Timeout::Error
end

def mocked_server2(times = nil, fake_client = nil, handle_event = lambda { |m, stream| client.message_received m, stream }, &block)
mock_target = MockServer.new
mock_target.should_receive(:receive_data).send(*(times ? [:exactly, times] : [:at_least, 1]), &block)
s = ServerMock.new '127.0.0.1', server_port, mock_target
@stream = Stream.new '127.0.0.1', server_port, username, password, handle_event
fake_client.call if fake_client.respond_to? :call
Timeout.timeout 60 do
Celluloid::Actor.join s
Celluloid::Actor.join @stream
end
rescue Timeout::Error
end

before { @sequence = 1 }

describe "after connection" do
Expand Down Expand Up @@ -204,6 +217,26 @@ def mocked_server(times = nil, fake_client = nil, &block)
response.should == Response.new('ActionID' => RubyAMI.new_uuid, 'Message' => 'Thanks for all the fish.')
end

it 'should not deadlock when sending an action after receiving a response' do
response = nil

handle_event = lambda do |m, stream|
client.message_received m, stream
response = stream.send_action 'Command', 'Command' => 'RECORD FILE evil' if m.is_a? RubyAMI::Stream::Connected
end

mocked_server2(1, lambda {}, handle_event) do |val, server|
server.send_data <<-EVENT
Response: Success
ActionID: #{RubyAMI.new_uuid}
Message: Recording started

EVENT
end

response.should == Response.new('ActionID' => RubyAMI.new_uuid, 'Message' => 'Recording started')
end

describe 'when it is an error' do
describe 'when there is no error handler' do
it 'should be raised by #send_action, but not kill the stream' do
Expand Down