diff --git a/spec/nice_http/nice_http_spec.rb b/spec/nice_http/nice_http_spec.rb index 4ff43bb..071b584 100644 --- a/spec/nice_http/nice_http_spec.rb +++ b/spec/nice_http/nice_http_spec.rb @@ -598,6 +598,44 @@ http = klass.new expect { http.close; http.close }.not_to raise_error end + + it "close handles nil @http and marks connection as closed" do + klass.host = "https://www.example.com" + http = klass.new + klass.active = 1 + http.instance_variable_set(:@http, nil) + http.instance_variable_set(:@closed, false) + + expect { http.close }.not_to raise_error + expect(http.instance_variable_get(:@closed)).to eq true + expect(klass.active).to eq 0 + end + + it "close handles finish exceptions without raising" do + klass.host = "https://www.example.com" + http = klass.new + klass.active = 1 + broken_http = double("broken_http") + allow(broken_http).to receive(:finish).and_raise(StandardError.new("boom")) + http.instance_variable_set(:@http, broken_http) + http.instance_variable_set(:@closed, false) + + expect { http.close }.not_to raise_error + expect(klass.active).to eq 0 + end + + it "close on already-closed connection does not try to finish again" do + klass.host = "https://www.example.com" + http = klass.new + klass.active = 1 + already_closed_http = double("already_closed_http") + allow(already_closed_http).to receive(:finish).and_raise("should not be called") + http.instance_variable_set(:@http, already_closed_http) + http.instance_variable_set(:@closed, true) + + expect { http.close }.not_to raise_error + expect(klass.active).to eq 0 + end end describe "inherited" do diff --git a/spec/nice_http/stats_spec.rb b/spec/nice_http/stats_spec.rb index 9e16e2a..e5e36b8 100644 --- a/spec/nice_http/stats_spec.rb +++ b/spec/nice_http/stats_spec.rb @@ -276,5 +276,22 @@ expect(klass.stats[:specific][:solo_name][:solo_state][:num]).to eq 1 expect(klass.stats[:specific][:solo_name][:time_elapsed][:total]).to be >= 1 end + + it "uses current thread name as item when item is nil" do + klass.reset! + original_name = Thread.current.name + Thread.current.name = "spec-thread-name" + started = Time.now - 2 + finished = Time.now + + klass.add_stats(:thread_case, :ok, started, finished) + + expect(klass.stats[:specific][:thread_case][:time_elapsed][:item_maximum]).to eq "spec-thread-name" + expect(klass.stats[:specific][:thread_case][:time_elapsed][:item_minimum]).to eq "spec-thread-name" + expect(klass.stats[:specific][:thread_case][:ok][:time_elapsed][:item_maximum]).to eq "spec-thread-name" + expect(klass.stats[:specific][:thread_case][:ok][:time_elapsed][:item_minimum]).to eq "spec-thread-name" + ensure + Thread.current.name = original_name + end end end