spec/routemaster/api_client_spec.rb in routemaster-drain-3.6.8 vs spec/routemaster/api_client_spec.rb in routemaster-drain-3.7.0

- old
+ new

@@ -107,14 +107,42 @@ expect(subject.dummy).to be_truthy end end end + shared_examples 'a circuit breaker wrapped request' do + let(:fetcher) { described_class.new(retry_attempts: 0) } + let(:url) { 'https://circuit.example.com/circuit/132' } + let(:method) { :get } + + before do + allow(ENV).to receive(:fetch).and_call_original + allow(ENV) + .to receive(:fetch) + .with('ROUTEMASTER_ENABLE_API_CLIENT_CIRCUIT', 'NO').and_return('YES') + allow(ENV) + .to receive(:fetch) + .with('ROUTEMASTER_CIRCUIT_BREAKER_ERROR_THRESHOLD', 50).and_return('1') + allow(ENV) + .to receive(:fetch) + .with('ROUTEMASTER_CIRCUIT_BREAKER_VOLUME_THRESHOLD', 50).and_return('1') + + stub_request(method, url).to_timeout + end + + it 'trips after the second request' do + 5.times { expect { subject }.to raise_error StandardError } + expect(a_request(method, url)).to have_been_made.at_most_times(2) + end + end + describe '#get' do subject { fetcher.get(url, headers: headers) } + it_behaves_like 'a GET requester' it_behaves_like 'a wrappable response' + it_behaves_like 'a circuit breaker wrapped request' end describe '#fget' do subject { fetcher.fget(url, headers: headers) } it_behaves_like 'a GET requester' @@ -181,10 +209,13 @@ subject expect(@post_req).to have_been_requested end it_behaves_like 'a wrappable response' + it_behaves_like 'a circuit breaker wrapped request' do + let(:method) { :post } + end end describe '#put' do let(:body) { { 'one' => 1, 'two' => 2 }.to_json } subject { fetcher.put(url, body: body, headers: headers) } @@ -204,10 +235,13 @@ subject expect(@put_req).to have_been_requested end it_behaves_like 'a wrappable response' + it_behaves_like 'a circuit breaker wrapped request' do + let(:method) { :put } + end end context 'when request times out' do subject do begin @@ -280,9 +314,12 @@ subject expect(@patch_req).to have_been_requested end it_behaves_like 'a wrappable response' + it_behaves_like 'a circuit breaker wrapped request' do + let(:method) { :patch } + end end context 'when request times out' do subject do begin