Skip to content

Commit

Permalink
Handle multi fetch failure gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyates committed Jun 10, 2024
1 parent 39cbf3b commit f71cfb5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/imap/backup/flag_refresher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ def run

def refresh_block(uids)
uids_and_flags = folder.fetch_multi(uids, ["FLAGS"])
if !uids_and_flags
Logger.logger.debug(
"[#{folder.name}] failed to fetch flags for #{uids} - " \
"cannot refresh flags"
)
return
end
uids_and_flags.each do |uid_and_flags|
uid = uid_and_flags[:uid]
flags = uid_and_flags[:flags]
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 @@ -6,7 +6,7 @@ module Imap::Backup
# @private
MINOR = 0
# @private
REVISION = 1
REVISION = 2
# @private
PRE = nil
# The application version
Expand Down
33 changes: 33 additions & 0 deletions spec/unit/flag_refresher_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "imap/backup/account/folder"
require "imap/backup/flag_refresher"
require "imap/backup/serializer"

module Imap::Backup
RSpec.describe FlagRefresher do
subject { described_class.new(folder, serializer) }

let(:serializer) { instance_double(Serializer, uids: [1, 2]) }
let(:folder) { instance_double(Account::Folder, uids: [2], name: "my_folder") }

it "refreshes the flags" do
response = [{uid: 1, flags: [:Draft]}, {uid: 2, flags: [:Seen]}]
allow(folder).to receive(:fetch_multi).with([1, 2], ["FLAGS"]) { response }

expect(serializer).to receive(:update).with(1, flags: [:Draft])
expect(serializer).to receive(:update).with(2, flags: [:Seen])

subject.run
end

context "when the fetch fails" do
it "logs a warning" do
allow(folder).to receive(:fetch_multi).with([1, 2], ["FLAGS"]).and_return(nil)
expect(Logger.logger).
to receive(:debug).
with("[#{folder.name}] failed to fetch flags for [1, 2] - cannot refresh flags")

subject.run
end
end
end
end

0 comments on commit f71cfb5

Please sign in to comment.