Skip to content

Commit

Permalink
handle empty responses when polling
Browse files Browse the repository at this point in the history
that can be result of some interrupted connection

like https://travis-ci.org/lowjoel/message_bus-client/jobs/266253959
  • Loading branch information
mikz committed Aug 19, 2017
1 parent c3f31c1 commit d2e7f38
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/message_bus_client/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ def initialize(base_url)

def diagnostics; end

def start
def start(**options)
return unless @state == INITIALISED || stopped?

@connection = Excon.new(server_endpoint, persistent: true)
@connection = Excon.new(server_endpoint, persistent: true, **options)

@runner = Thread.new(&method(:runner))
@runner.name = "MessageBusClient (#{@client_id})"
Expand Down
2 changes: 1 addition & 1 deletion lib/message_bus_client/message_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def handle_chunk(chunk, _remaining_bytes, _total_bytes)
end

def handle_response(body)
handle_messages(JSON.parse(body))
handle_messages(JSON.parse(body)) unless body.empty?
end

def try_consume_message
Expand Down
10 changes: 10 additions & 0 deletions spec/message_bus_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ def write_message(message, user = 'message_bus_client')
sleep(1)
end
end

it 'handles empty message' do
expect(subject).to receive(:handle_response).with('').and_call_original.at_least(:once)

Excon.stub({}, body: '', status: 200)

subject.start(mock: true, persistent: false)

sleep(0.1)
end
end

it 'allows pausing messages' do
Expand Down
13 changes: 13 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,17 @@
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed

config.after(:each) do
Thread.list.each do |thread|
if thread.name&.match('MessageBusClient')
thread.kill
thread.join
end
end
end

config.after(:each) do
Excon.stubs.clear
end
end

0 comments on commit d2e7f38

Please sign in to comment.