Skip to content

Commit

Permalink
Retry on client errors
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyates committed Apr 23, 2023
1 parent 6cd82d8 commit bf58df7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
10 changes: 8 additions & 2 deletions lib/imap/backup/account/folder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Account::Folder
BODY_ATTRIBUTE = "BODY[]".freeze
UID_FETCH_RETRY_CLASSES = [EOFError, Errno::ECONNRESET, IOError].freeze
APPEND_RETRY_CLASSES = [Net::IMAP::BadResponseError].freeze
CREATE_RETRY_CLASSES = [Net::IMAP::BadResponseError].freeze
EXAMINE_RETRY_CLASSES = [Net::IMAP::BadResponseError].freeze
PERMITTED_FLAGS = %i(Answered Draft Flagged Seen).freeze

attr_reader :connection
Expand All @@ -33,7 +35,9 @@ def folder
end

def exist?
examine
retry_on_error(errors: EXAMINE_RETRY_CLASSES) do
examine
end
true
rescue FolderNotFound
false
Expand All @@ -42,7 +46,9 @@ def exist?
def create
return if exist?

client.create(utf7_encoded_name)
retry_on_error(errors: CREATE_RETRY_CLASSES) do
client.create(utf7_encoded_name)
end
end

def uid_validity
Expand Down
2 changes: 1 addition & 1 deletion lib/imap/backup/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Imap; end

module Imap::Backup
MAJOR = 9
MINOR = 2
MINOR = 3
REVISION = 0
PRE = nil
VERSION = [MAJOR, MINOR, REVISION, PRE].compact.map(&:to_s).join(".")
Expand Down
42 changes: 37 additions & 5 deletions spec/unit/imap/backup/account/folder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ module Imap::Backup
let(:responses) { [] }
let(:append_response) { nil }
let(:uids) { [5678, 123] }
let(:bad_response_error_response) do
response_text = Net::IMAP::ResponseText.new(42, "BOOM")
Net::IMAP::TaggedResponse.new("BAD", "name", response_text, "BOOM")
end

before { allow(client).to receive(:uid_search).with(["ALL"]) { uids } }

Expand Down Expand Up @@ -169,6 +173,22 @@ module Imap::Backup
expect(subject.exist?).to be_falsey
end
end

context "when the examine fails with a BadResponseError" do
before do
outcomes = [
-> { raise Net::IMAP::BadResponseError, bad_response_error_response },
-> {}
]
allow(client).to receive(:examine) { outcomes.shift.call }
end

it "retries" do
subject.create

expect(client).to have_received(:examine).twice
end
end
end

describe "#create" do
Expand Down Expand Up @@ -197,6 +217,22 @@ module Imap::Backup

subject.create
end

context "when the create fails with a BadResponseError" do
before do
outcomes = [
-> { raise Net::IMAP::BadResponseError, bad_response_error_response },
-> {}
]
allow(client).to receive(:create) { outcomes.shift.call }
end

it "retries" do
subject.create

expect(client).to have_received(:create).twice
end
end
end
end

Expand Down Expand Up @@ -278,11 +314,7 @@ module Imap::Backup
context "when the append fails with a BadResponseError" do
before do
outcomes = [
-> do
response_text = Net::IMAP::ResponseText.new(42, "BOOM")
response = Net::IMAP::TaggedResponse.new("BAD", "name", response_text, "BOOM")
raise Net::IMAP::BadResponseError, response
end,
-> { raise Net::IMAP::BadResponseError, bad_response_error_response },
-> { append_response }
]
allow(client).to receive(:append) { outcomes.shift.call }
Expand Down

0 comments on commit bf58df7

Please sign in to comment.