Skip to content
This repository has been archived by the owner on May 17, 2018. It is now read-only.

Commit

Permalink
Force reset_connection! after apns errors
Browse files Browse the repository at this point in the history
  • Loading branch information
anderscarling committed Mar 30, 2012
1 parent d05cd5d commit 482b4d0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
10 changes: 4 additions & 6 deletions lib/apn_client/delivery.rb
Expand Up @@ -86,11 +86,9 @@ def process!
if has_next_message?
readable, writable = connection.availability(self.poll_timeout)

if readable
read_error
end

if writable and has_next_message?
if readable and read_error
reset_connection!
elsif writable
write_message
next_message!
end
Expand Down Expand Up @@ -123,11 +121,11 @@ def read_error
if message_id
invoke_callback(:on_apns_error, error_code, message_id)
rewind_messages!(message_id)
return error_code
end
end

def write_message
next_message.message_id ||= connection.next_message_id
connection.write(next_message.to_apns)
invoke_callback(:on_write, next_message)
end
Expand Down
22 changes: 15 additions & 7 deletions spec/apn_client/delivery_spec.rb
Expand Up @@ -279,50 +279,58 @@
@delivery.process!
end

it "should not do anything fancy if #read_apns_error returns error code that's not in the messages array" do
it "should reset_socket if #read_apns_error returns error code that's not in the messages array" do
while an_id = rand(10000)
break unless @messages.map(&:message_id).include?(an_id)
end
@connection.expects(:read_apns_error).twice.\
@connection.expects(:read_apns_error).times(3).\
returns(nil).then.\
returns([8,1,an_id])
@connection.expects(:availability).with(@poll_timeout).times(6).\
returns([8,1,an_id]).then.\
returns(nil)
@connection.expects(:availability).with(@poll_timeout).times(7).\
returns([false, false]).then.\
returns([true, false]).then.\
returns([false, true]).then.\
returns([false, false]).then.\
returns([false, true]).then.\
returns([true, true]).then.\
returns([true, true])

write_seq = sequence('writes')
@connection.expects(:write).with(@messages[0].to_apns).\
in_sequence(write_seq)
@connection.expects(:write).with(@messages[1].to_apns).\
in_sequence(write_seq)
@delivery.expects(:reset_connection!).\
in_sequence(write_seq)
@connection.expects(:write).with(@messages[2].to_apns).\
in_sequence(write_seq)

@delivery.process!
end

it "should resend messages sent after if #read_apns_error returns error code for message id in messages array" do
@connection.expects(:read_apns_error).twice.\
@connection.expects(:read_apns_error).times(3).\
returns(nil).then.\
returns([8,7,@messages[0].message_id])
@connection.expects(:availability).with(@poll_timeout).times(7).\
returns([8,7,@messages[0].message_id]).then.\
returns(nil)
@connection.expects(:availability).with(@poll_timeout).times(8).\
returns([false, false]).then.\
returns([true, false]).then.\
returns([false, true]).then.\
returns([false, false]).then.\
returns([false, true]).then.\
returns([true, true]).then.\
returns([true, true]).then.\
returns([nil, true])

write_seq = sequence('writes')
@connection.expects(:write).with(@messages[0].to_apns).\
in_sequence(write_seq)
@connection.expects(:write).with(@messages[1].to_apns).\
in_sequence(write_seq)
@delivery.expects(:reset_connection!).\
in_sequence(write_seq)
@connection.expects(:write).with(@messages[1].to_apns).\
in_sequence(write_seq)
@connection.expects(:write).with(@messages[2].to_apns).\
Expand Down

0 comments on commit 482b4d0

Please sign in to comment.