Skip to content

Commit

Permalink
simplify specs
Browse files Browse the repository at this point in the history
  • Loading branch information
dpep committed Sep 28, 2023
1 parent 1b92c4f commit 2e1f198
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 58 deletions.
47 changes: 22 additions & 25 deletions spec/faraday_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,76 +7,73 @@
let(:uri) { URI("http://example.com") }

describe ".connect" do
subject(:response) { faraday.get.body }
subject do
response rescue Faraday::ConnectionFailed
NetworkResiliency.statsd
end

let(:response) { faraday.get.body }

it "logs connection" do
expect(NetworkResiliency.statsd).to receive(:distribution).with(
is_expected.to have_received(:distribution).with(
/connect/,
Numeric,
tags: include(adapter: "http"),
)

response
end

it "logs duration" do
expect(NetworkResiliency.statsd).to receive(:distribution) do |_, duration, _|
is_expected.to have_received(:distribution) do |_, duration, _|
expect(duration).to be > 0
end

response
end

it "tags the destination host" do
expect(NetworkResiliency.statsd).to receive(:distribution).with(
is_expected.to have_received(:distribution).with(
String,
Numeric,
tags: include(destination: uri.host),
)

response
end

it "completes request" do
expect(NetworkResiliency.statsd).to receive(:distribution)
is_expected.to have_received(:distribution)

expect(response).to eq "OK"
end

context "when server connection times out" do
let(:uri) { URI("http://timeout.com") }

it "logs timeouts" do
expect(NetworkResiliency.statsd).to receive(:distribution).with(
it "raises an error" do
expect { response }.to raise_error(Faraday::ConnectionFailed)
end

it "logs timeout" do
is_expected.to have_received(:distribution).with(
String,
Numeric,
tags: include(error: Net::OpenTimeout),
)

expect {
faraday.get
}.to raise_error(Faraday::ConnectionFailed)
end
end

context "when NetworkResiliency is disabled" do
before { NetworkResiliency.enabled = false }

it "does not call datadog" do
expect(NetworkResiliency.statsd).not_to receive(:distribution)

faraday.get
is_expected.not_to have_received(:distribution)
end

context "when server connection times out" do
let(:uri) { URI("http://timeout.com") }

it "does not log timeouts" do
expect(NetworkResiliency.statsd).not_to receive(:distribution)
it "raises an error" do
expect { response }.to raise_error(Faraday::ConnectionFailed)
end

expect {
faraday.get
}.to raise_error(Faraday::ConnectionFailed)
it "does not log timeout" do
is_expected.not_to have_received(:distribution)
end
end
end
Expand Down
53 changes: 24 additions & 29 deletions spec/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,79 +19,76 @@
end

describe ".connect" do
subject do
http.connect rescue Net::OpenTimeout

NetworkResiliency.statsd
end

before do
described_class.patch(http)
end

it "logs connection" do
expect(NetworkResiliency.statsd).to receive(:distribution).with(
is_expected.to have_received(:distribution).with(
/connect/,
Numeric,
anything,
)

http.connect
end

it "logs duration" do
expect(NetworkResiliency.statsd).to receive(:distribution) do |_, duration, _|
is_expected.to have_received(:distribution) do |_, duration, _|
expect(duration).to be > 0
end

http.connect
end

it "tags the destination host" do
expect(NetworkResiliency.statsd).to receive(:distribution).with(
is_expected.to have_received(:distribution).with(
String,
Numeric,
tags: include(destination: uri.host),
)

http.connect
end

it "completes request" do
expect(NetworkResiliency.statsd).to receive(:distribution)

res = http.get "/"
expect(res.body).to eq "OK"
expect(NetworkResiliency.statsd).to have_received(:distribution)
end

context "when server connection times out" do
let(:uri) { URI("http://timeout.com") }

it "logs timeouts" do
expect(NetworkResiliency.statsd).to receive(:distribution).with(
it "raises an error" do
expect { http.connect }.to raise_error(Net::OpenTimeout)
end

it "logs timeout" do
is_expected.to have_received(:distribution).with(
String,
Numeric,
tags: include(error: Net::OpenTimeout),
)

expect {
http.connect
}.to raise_error(Net::OpenTimeout)
end
end

context "when NetworkResiliency is disabled" do
before { NetworkResiliency.enabled = false }
before { NetworkResiliency.disable! }

it "does not call datadog" do
expect(NetworkResiliency.statsd).not_to receive(:distribution)

http.connect
is_expected.not_to have_received(:distribution)
end

context "when server connection times out" do
let(:uri) { URI("http://timeout.com") }

it "does not log timeouts" do
expect(NetworkResiliency.statsd).not_to receive(:distribution)
it "raises an error" do
expect { http.connect }.to raise_error(Net::OpenTimeout)
end

expect {
http.connect
}.to raise_error(Net::OpenTimeout)
it "does not log timeout" do
is_expected.not_to have_received(:distribution)
end
end
end
Expand All @@ -100,9 +97,7 @@
let(:http) { Net::HTTP.new("127.0.0.1") }

it "does not call datadog" do
expect(NetworkResiliency.statsd).not_to receive(:distribution)

http.connect
is_expected.not_to have_received(:distribution)
end
end
end
Expand Down
4 changes: 0 additions & 4 deletions spec/network_resiliency_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ def expect_enabled
let(:duration) { 10 }
let(:host) { "example.com" }

before do
allow(NetworkResiliency.statsd).to receive(:distribution)
end

it "calls Datadog" do
is_expected.to have_received(:distribution)
end
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
NetworkResiliency.reset

NetworkResiliency.statsd = instance_double(Datadog::Statsd)
allow(NetworkResiliency.statsd).to receive(:distribution)
end
end

Expand Down

0 comments on commit 2e1f198

Please sign in to comment.