From 33d1fafbef8588e431baf81d81458f54bfd673d4 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Mon, 21 Oct 2024 17:08:56 +1300 Subject: [PATCH] Ensure the order of `#close` is consistent. --- lib/protocol/http/body/completable.rb | 10 +++++----- test/protocol/http/body/completable.rb | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/protocol/http/body/completable.rb b/lib/protocol/http/body/completable.rb index 2168cf7..0d83a41 100644 --- a/lib/protocol/http/body/completable.rb +++ b/lib/protocol/http/body/completable.rb @@ -33,12 +33,12 @@ def rewind end def close(error = nil) - super.tap do - if @callback - @callback.call(error) - @callback = nil - end + if @callback + @callback.call(error) + @callback = nil end + + super end end end diff --git a/test/protocol/http/body/completable.rb b/test/protocol/http/body/completable.rb index 092f887..40f2320 100644 --- a/test/protocol/http/body/completable.rb +++ b/test/protocol/http/body/completable.rb @@ -92,4 +92,18 @@ expect(completable.rewind).to be == false end end + + with "#close" do + let(:events) {Array.new} + let(:callback) {Proc.new{events << :close}} + + it "invokes callback once" do + completable1 = subject.new(body, proc{events << :close1}) + completable2 = subject.new(completable1, proc{events << :close2}) + + completable2.close + + expect(events).to be == [:close2, :close1] + end + end end