Permalink
Browse files

making sure that auto-resend doesn't go into infinite loop

  • Loading branch information...
1 parent 0f5b80b commit cb9ed320dbd35ccb822d8ce6fa1060771315d958 @GBH GBH committed Jan 6, 2011
View
@@ -4,6 +4,7 @@
require 'logger'
require 'json'
+require 'base64'
require 'postageapp/utils'
require 'postageapp/version'
@@ -24,7 +24,9 @@ def self.resend_all
request = initialize_request(filename)
receipt_response = PostageApp::Request.new(:get_message_receipt, :uid => filename).send(true)
- if receipt_response.ok?
+ if receipt_response.fail?
+ return
+ elsif receipt_response.ok?
PostageApp.logger.info "NOT RESENDING FAILED REQUEST [#{filename}]"
File.delete(file_path(filename)) rescue nil
@@ -35,9 +37,11 @@ def self.resend_all
# Not a fail, so we can remove this file, if it was then
# there will be another attempt to resend
File.delete(file_path(filename)) rescue nil if !response.fail?
+ else
+ PostageApp.logger.info "NOT RESENDING FAILED REQUEST [#{filename}], RECEIPT CANNOT BE PROCESSED"
+ File.delete(file_path(filename)) rescue nil
end
end
-
return
end
@@ -54,7 +54,11 @@ def send(skip_failed_requests_processing = false)
PostageApp.logger.info(response)
unless skip_failed_requests_processing
- response.fail?? PostageApp::FailedRequest.store(self) : PostageApp::FailedRequest.resend_all
+ if response.fail?
+ PostageApp::FailedRequest.store(self)
+ elsif response.ok?
+ PostageApp::FailedRequest.resend_all
+ end
end
response
@@ -66,7 +66,7 @@ def test_resend_all
request = PostageApp::Request.new(:get_project_info)
- message_receipt_response = stub(:ok? => false, :not_found? => true)
+ message_receipt_response = stub(:fail? => false, :ok? => false, :not_found? => true)
message_receipt_request = stub(:send => message_receipt_response)
PostageApp::Request.stubs(:new).with{|a,b| a == :get_message_receipt}.returns(message_receipt_request)
@@ -76,4 +76,25 @@ def test_resend_all
assert !File.exists?(file_path)
end
+ def test_resend_all_failure
+ mock_failed_send
+ request = PostageApp::Request.new(:send_message, {
+ :headers => { 'from' => 'sender@test.test',
+ 'subject' => 'Test Message'},
+ :recipients => 'test@test.test',
+ :content => {
+ 'text/plain' => 'text content',
+ 'text/html' => 'html content'
+ }
+ })
+ response = request.send
+ assert response.fail?
+ file_path = File.join(PostageApp::FailedRequest.store_path, request.uid)
+ assert File.exists?(file_path)
+
+ # Forcing to resend. Should quit right away as we can't connect
+ PostageApp::FailedRequest.resend_all
+
+ assert File.exists?(file_path)
+ end
end
View
@@ -35,12 +35,12 @@ def setup
end
end
- def mock_successful_send
+ def mock_successful_send(status = 'ok')
Net::HTTP.any_instance.stubs(:post).returns(Net::HTTPResponse.new(nil, nil, nil))
Net::HTTPResponse.any_instance.stubs(:body).returns({
:response => {
:uid => 'sha1hashuid23456789012345678901234567890',
- :status => 'ok'
+ :status => status
},
:data => {
:message => { :id => 999 }
@@ -15,7 +15,8 @@ def test_mailer_helper_methods
assert_equal ['test@test.test'], request.to
assert_equal ['sender@test.test'], request.from
assert_equal 'Test Message', request.subject
- assert_equal "html content\n\ntext content", request.body
+ assert_match 'html content', request.body
+ assert_match 'text content', request.body
end
end

0 comments on commit cb9ed32

Please sign in to comment.