Skip to content

Commit

Permalink
Close connection when receiving permanent error
Browse files Browse the repository at this point in the history
  • Loading branch information
bforma committed Sep 12, 2024
1 parent a6f7469 commit 2c9fd14
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 28 deletions.
13 changes: 1 addition & 12 deletions lib/s2/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,8 @@ class Connection

on S2::Messages::ReceptionStatus do |message|
if message_sent?(message.subject_message_id)
case message.status
when S2::Messages::ReceptionStatusValues::Ok
# no-op
else
@logger.error(
"Received ReceptionStatus with status #{message.status} for " \
"unknown message ID #{message.subject_message_id}",
)
end

delete_sent_message(message)
close if message.status == S2::Messages::ReceptionStatusValues::PermanentError
else
@logger.error("Received ReceptionStatus for unknown message ID #{message.subject_message_id}")
end
Expand Down Expand Up @@ -71,8 +62,6 @@ def reply(message, status:)
subject_message_id: message.message_id,
)
end

@ws.close if status == S2::Messages::ReceptionStatusValues::PermanentError
end

def message_sent?(message_id)
Expand Down
31 changes: 15 additions & 16 deletions spec/lib/s2/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,6 @@
end

context "when the message is a ReceptionStatus" do
it "logs an error if the status is not OK" do
allow(SecureRandom).to receive(:uuid).and_return("123")

logger = Logger.new(nil)
connection = described_class.new(ws, logger:)

allow(logger).to receive(:error)

connection.send_message(S2::Messages::Handshake, { role: S2::Messages::EnergyManagementRole::Cem })
reception_status = build(:s2_reception_status, :invalid_content, subject_message_id: "123")
connection.receive_message(reception_status.to_json)

expect(logger).to have_received(:error)
.with(/Received ReceptionStatus with status INVALID_CONTENT for unknown message ID 123/)
end

it "logs an error when the original message has not been sent" do
logger = Logger.new(nil)
connection = described_class.new(ws, logger:)
Expand Down Expand Up @@ -85,6 +69,21 @@
connection.receive_message(reception_status.to_json)
end.to change { connection.sent_messages.size }.from(1).to(0)
end

it "closes the connection when the status is PermanentError" do
allow(SecureRandom).to receive(:uuid).and_return("123")

logger = Logger.new(nil)
connection = described_class.new(ws, logger:)
allow(connection).to receive(:close)

connection.send_message(S2::Messages::Handshake, { role: S2::Messages::EnergyManagementRole::Cem })
reception_status = build(:s2_reception_status, :permanent_error, subject_message_id: "123")

connection.receive_message(reception_status.to_json)

expect(connection).to have_received(:close)
end
end
end

Expand Down

0 comments on commit 2c9fd14

Please sign in to comment.