70 lines
1.5 KiB
Ruby
70 lines
1.5 KiB
Ruby
require "test_helper"
|
|
|
|
class Mailings::SmtpGateTest < ActiveSupport::TestCase
|
|
setup do
|
|
Mailings::SmtpGate.min_gap = 0
|
|
Mailings::SmtpGate.backoff_base = 0
|
|
Mailings::SmtpGate.max_attempts = 4
|
|
Mailings::SmtpGate.reset!
|
|
end
|
|
|
|
teardown do
|
|
Mailings::SmtpGate.min_gap = 0
|
|
Mailings::SmtpGate.backoff_base = 0
|
|
Mailings::SmtpGate.max_attempts = 4
|
|
Mailings::SmtpGate.reset!
|
|
end
|
|
|
|
test "retries EOFError then succeeds" do
|
|
hits = 0
|
|
result = Mailings::SmtpGate.deliver do
|
|
hits += 1
|
|
raise EOFError, "end of file reached" if hits < 3
|
|
|
|
:sent
|
|
end
|
|
|
|
assert_equal :sent, result
|
|
assert_equal 3, hits
|
|
end
|
|
|
|
test "gives up after max attempts" do
|
|
hits = 0
|
|
error = assert_raises(EOFError) do
|
|
Mailings::SmtpGate.deliver do
|
|
hits += 1
|
|
raise EOFError, "end of file reached"
|
|
end
|
|
end
|
|
|
|
assert_equal "end of file reached", error.message
|
|
assert_equal Mailings::SmtpGate.max_attempts, hits
|
|
end
|
|
|
|
test "does not retry permanent SMTP errors" do
|
|
hits = 0
|
|
assert_raises(Net::SMTPAuthenticationError) do
|
|
Mailings::SmtpGate.deliver do
|
|
hits += 1
|
|
raise Net::SMTPAuthenticationError, "535 authentication failed"
|
|
end
|
|
end
|
|
assert_equal 1, hits
|
|
end
|
|
|
|
test "serializes concurrent deliveries" do
|
|
order = Queue.new
|
|
threads = 2.times.map do |i|
|
|
Thread.new do
|
|
Mailings::SmtpGate.deliver do
|
|
order << i
|
|
sleep 0.05
|
|
end
|
|
end
|
|
end
|
|
threads.each(&:join)
|
|
|
|
assert_equal 2, order.size
|
|
end
|
|
end
|