Permalink
Browse files

a few fixes:

1. rescue any Exception (e.g. TimeoutException) when trying the
   primary mailer
2. if no :retry is passed in to with_settings then use :default.  This
   requires less typing for an app with many call sites
3. fix tests

Note: tests require mocha
  • Loading branch information...
1 parent 4f55700 commit cb665922742d33b2a593a52a4da2f3aac060c9db Jeff Jolma committed Nov 26, 2008
Showing with 39 additions and 15 deletions.
  1. +8 −5 lib/many_mailers.rb
  2. +3 −4 test/abstract_unit.rb
  3. +0 −2 test/fixtures/user_mailer.rb
  4. +28 −4 test/many_mailers_test.rb
View
@@ -11,12 +11,15 @@ def self.included(base)
module ClassMethods
def with_settings(name, options={}, &block)
+ options[:retry] ||= :default
+
self.class_inheritable_accessor :smtp_settings
self.smtp_settings = mail_servers[name]
- rescue_servers = [options[:retry]].flatten.compact
+
+ rescue_servers = Array(options[:retry])
begin
yield self
- rescue => e
+ rescue Exception => e
raise e if rescue_servers.empty?
retry_server = rescue_servers.shift
with_settings(retry_server, :retry => rescue_servers, &block)
@@ -29,11 +32,11 @@ def load_settings!(file_path = "#{RAILS_ROOT}/config/mail_servers.yml")
mail_servers[key.to_sym] = value.to_options!
self.smtp_settings = mail_servers[:default]
end
- rescue
- puts "=> \"#{file_path}\" not found! Using default SMTP settings (if any)."
+ rescue
+ puts "=> \"#{file_path}\" not found! Using default SMTP settings (if any)."
end
end
end
end
-ActionMailer::Base.send :include, Animoto::ManyMailers
+ActionMailer::Base.send :include, Animoto::ManyMailers
View
@@ -7,7 +7,6 @@
require 'action_mailer'
require File.dirname(__FILE__) + '/../lib/many_mailers.rb'
end
-require 'fixtures/user_mailer'
-require 'fixtures/party_mailer'
-require 'test/unit'
-require 'mocha'
+require File.join(File.dirname(__FILE__), 'fixtures/user_mailer')
+require File.join(File.dirname(__FILE__), 'fixtures/party_mailer')
+require 'test/unit'
@@ -1,6 +1,4 @@
class UserMailer < ActionMailer::Base
- self.template_root = "#{RAILS_ROOT}/views"
-
def feedback(message)
from 'Service <service@example.com>'
recipients 'Feedback <feedback@example.com>'
View
@@ -1,6 +1,16 @@
-require 'abstract_unit'
+require File.join(File.dirname(__FILE__), 'abstract_unit')
+require 'mocha'
class ManyMailersTest < Test::Unit::TestCase
+
+ def setup
+ # need to override a possible /config/mail_servers.yml with the test settings
+ ActionMailer::Base.load_settings!(File.join(File.dirname(__FILE__), 'fixtures/config/mail_servers.yml'))
+ ActionMailer::Base.delivery_method = :test
+ ActionMailer::Base.perform_deliveries = true
+ ActionMailer::Base.raise_delivery_errors = true
+ ActionMailer::Base.deliveries = []
+ end
def test_should_load_settings_properly
settings = ActionMailer::Base.mail_servers[:default]
@@ -33,12 +43,26 @@ def test_should_only_change_settings_for_one_class
assert_equal ActionMailer::Base.mail_servers[:default], PartyMailer.smtp_settings, "Changed settings globally!"
end
end
-
+
def test_should_retry_with_failovers
UserMailer.with_settings(:internal, :retry => :default) do |mailer|
begin; mailer.deliver_feedback('Oh, thanks.')
rescue; assert_equal ActionMailer::Base.mail_servers[:default], UserMailer.smtp_settings end
end
end
-
-end
+
+ def test_should_retry_with_default_failovers
+ UserMailer.with_settings(:internal) do |mailer|
+ begin; mailer.deliver_feedback('Oh, thanks.')
+ rescue; assert_equal ActionMailer::Base.mail_servers[:default], UserMailer.smtp_settings end
+ end
+ end
+
+ def test_should_handle_server_timeout
+ UserMailer.expects(:deliver_feedback).times(2).raises(Timeout::Error, 'mail server timed out').then.returns(nil)
+ UserMailer.with_settings(:default, :retry => :internal) do |mailer|
+ mailer.deliver_feedback('test message')
+ assert_equal ActionMailer::Base.mail_servers[:internal], UserMailer.smtp_settings
+ end
+ end
+end

0 comments on commit cb66592

Please sign in to comment.