Skip to content
Closed
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
3 changes: 3 additions & 0 deletions lib/librevox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
module Librevox
class ResponseError < StandardError; end
class ConnectionError < StandardError; end
# A reply arrived that isn't the kind its command expects, so replies and
# commands are out of step. The connection can't be trusted any more.
class ProtocolError < ConnectionError; end

autoload :Client, 'librevox/client'
autoload :CommandSocket, 'librevox/command_socket'
Expand Down
32 changes: 29 additions & 3 deletions lib/librevox/listener/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def send_message(msg)
# every other sender.
@write_lock.acquire do
@connection.send_data(msg)
@reply_promises << promise
@reply_promises << [promise, expected_reply(msg)]
end

reply = promise.wait
Expand Down Expand Up @@ -82,7 +82,7 @@ def execute_app(app, uuid, args = nil, **params)

def receive_message(response)
if response.reply?
@reply_promises.shift&.resolve(response)
resolve_reply(response)
return
end

Expand All @@ -107,7 +107,7 @@ def receive_message(response)
def connection_closed
error = ConnectionError.new("Connection closed")

@reply_promises.each { |p| p.reject(error) }
@reply_promises.each { |p, _| p.reject(error) }
@app_promises.each_value { |p| p.reject(error) }

@reply_promises.clear
Expand All @@ -133,6 +133,32 @@ def session_complete?

private

# FreeSWITCH answers every command on a connection once, in order, and
# its replies carry no request id. So each reply goes to the oldest
# waiting command, and must be the kind that command expects: an
# api/response for `api`, a command/reply for everything else. Anything
# else means commands and replies are out of step, and every waiting
# command is failed rather than handed someone else's reply.
def resolve_reply(response)
promise, expected = @reply_promises.shift
return unless promise
return promise.resolve(response) if response.public_send(expected)

out_of_step(response, promise)
end

def expected_reply(msg)
msg.start_with?("api ") ? :api_response? : :command_reply?
end

def out_of_step(response, promise)
error = ProtocolError.new("reply out of step: got #{response.headers[:content_type]}")
promise.reject(error)
@reply_promises.each { |p, _| p.reject(error) }
@reply_promises.clear
disconnect
end

def on_event(event)
end

Expand Down
2 changes: 1 addition & 1 deletion test/functional/librevox/listener/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_on_event_with_api_does_not_block_handle_response
Sync do
event "SOME_EVENT"
assert_equal "api test", @listener.outgoing_data.shift
command_reply "Reply-Text" => "+OK"
api_response body: "+OK"
end
}.join(1)

Expand Down
3 changes: 2 additions & 1 deletion test/functional/librevox/listener/outbound_apps_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ def test_events_do_not_advance_app

def test_api_responses_do_not_advance_app
assert_execute_app @listener, "foo", "1234"
command_reply "Reply-Text" => "+OK" # the sendmsg's own acknowledgement

api_response body: "Foo"
api_response body: "Foo" # unsolicited: no command waits for it, so it's dropped

assert_send_nothing @listener
end
Expand Down
54 changes: 54 additions & 0 deletions test/functional/librevox/listener/reply_matching_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

require_relative '../../../test_helper'
require_relative '../../../support/listener'

class TestReplyMatching < Minitest::Test
include Librevox::Test::ListenerHelpers

class Listener < Librevox::Listener::Base
attr_reader :disconnected

def disconnect
@disconnected = true
end
end

def setup
@listener = Listener.new(MockConnection.new)
end

def test_an_api_command_gets_its_api_response
Async do
sending = Async { @listener.send_message("api status") }
api_response body: "UP"

assert_equal "UP", sending.wait.content
end
end

def test_other_commands_get_their_command_reply
Async do
sending = Async { @listener.send_message("sendevent CUSTOM") }
command_reply "Reply-Text" => "+OK"

assert_predicate sending.wait, :command_reply?
end
end

def test_a_reply_of_the_wrong_kind_fails_every_waiting_command
Async do
api = Async { @listener.send_message("api status") }
other = Async { @listener.send_message("sendevent CUSTOM") }
command_reply "Reply-Text" => "+OK" # meant for neither: the api call expects an api/response

assert_raises(Librevox::ProtocolError) { api.wait }
assert_raises(Librevox::ProtocolError) { other.wait }
assert @listener.disconnected, "an out-of-step connection is closed"
end
end

def test_the_error_is_a_connection_error
assert_operator Librevox::ProtocolError, :<, Librevox::ConnectionError
end
end
3 changes: 3 additions & 0 deletions test/support/listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def receive_data
nil
end

def close_write
end

def close
end
end
Expand Down