Skip to content

Commit

Permalink
Support Unicorn in Promenade::Raindrops::Middleware (#69)
Browse files Browse the repository at this point in the history
* Support Unicorn in Promenade::Raindrops::Middleware

* Use raindrops middleware only when either pitchfork or unicorn is loaded
  • Loading branch information
coord-e authored Sep 25, 2024
1 parent 6442f72 commit 0b7254b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/promenade/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Railtie < ::Rails::Railtie
Rails.application.config.middleware.insert 0,
Promenade::Client::Rack::HTTPRequestQueueTimeCollector

if defined?(::Raindrops)
if defined?(::Raindrops) && (defined?(::Pitchfork) || defined?(::Unicorn))
require "promenade/raindrops/middleware"
Rails.application.config.middleware.use Promenade::Raindrops::Middleware
end
Expand Down
9 changes: 8 additions & 1 deletion lib/promenade/raindrops/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ def call(env)
private

def tcp_listener_names
::Pitchfork.listener_names
if defined?(::Pitchfork)
::Pitchfork.listener_names
elsif defined?(::Unicorn)
::Unicorn.listener_names
else
raise StandardError,
"Promenade::Raindrops::Middleware expects either ::Pitchfork or ::Unicorn to be defined"
end
end

def instrument
Expand Down
47 changes: 38 additions & 9 deletions spec/promenade/raindrops/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,48 @@
RSpec.describe Promenade::Raindrops::Middleware do
let(:app) { double(:app, call: nil) }
let(:listener_address) { "127.0.0.1:#{ENV.fetch('PORT', 3000)}" }
let(:pitchfork) { class_double("Pitchfork").as_stubbed_const }

before do
allow(pitchfork).to receive(:listener_names).and_return([listener_address])
shared_examples "middleware" do
it "is add it's instrumentaion to the rack.after_reply" do
stats = class_spy("Promenade::Raindrops::Stats").as_stubbed_const

after_reply = []
described_class.new(app).call({ "rack.after_reply" => after_reply })
after_reply.each(&:call)

expect(stats).to have_received(:instrument).with(listener_address: listener_address)
end
end

it "is add it's instrumentaion to the rack.after_reply" do
stats = class_spy("Promenade::Raindrops::Stats").as_stubbed_const
context "when Pitchfork is defined" do
let(:pitchfork) { class_double("Pitchfork").as_stubbed_const }

before do
allow(pitchfork).to receive(:listener_names).and_return([listener_address])
end

it_behaves_like "middleware"
end

context "when Unicorn is defined" do
let(:unicorn) { class_double("Unicorn").as_stubbed_const }

before do
allow(unicorn).to receive(:listener_names).and_return([listener_address])
end

it_behaves_like "middleware"
end

after_reply = []
described_class.new(app).call({ "rack.after_reply" => after_reply })
after_reply.each(&:call)
context "when neither Pitchfork nor Unicorn is defined" do
it "raises an error" do
stats = class_spy("Promenade::Raindrops::Stats").as_stubbed_const

expect(stats).to have_received(:instrument).with(listener_address: listener_address)
expect do
after_reply = []
described_class.new(app).call({ "rack.after_reply" => after_reply })
after_reply.each(&:call)
end.to raise_error "Promenade::Raindrops::Middleware expects either ::Pitchfork or ::Unicorn to be defined"
end
end
end

0 comments on commit 0b7254b

Please sign in to comment.