-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move to use rate_throttle_client Gem
Instead of maintaining a single purpose rate throttle client wrapper, I ported the code over to a gem `rate_throttle_client` that has it's own set of tests and metrics generating code. This will make it easier to maintain and other libraries can use the code now as well. I'm now also testing configuring the rate throttling logic.
- Loading branch information
Showing
9 changed files
with
129 additions
and
335 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
require 'platform-api' | ||
|
||
describe 'Platform API config' do | ||
describe 'rate limiting' do | ||
before(:each) do | ||
WebMock.enable! | ||
|
||
rate_throttle = PlatformAPI.rate_throttle | ||
@original_rate_throttle = rate_throttle.dup | ||
|
||
# No junk in test dots | ||
rate_throttle.log = ->(*_) {} | ||
|
||
# Don't sleep in tests | ||
def rate_throttle.sleep(value); end | ||
end | ||
|
||
after(:each) do | ||
WebMock.disable! | ||
|
||
PlatformAPI.rate_throttle = @original_rate_throttle | ||
end | ||
|
||
it "works even if first request is rate limited" do | ||
stub_request(:get, "https://api.heroku.com/apps") | ||
.to_return([ | ||
{status: 429}, | ||
{status: 200} | ||
]) | ||
|
||
client.app.list | ||
|
||
expect(WebMock).to have_requested(:get, "https://api.heroku.com/apps").twice | ||
end | ||
|
||
it "allows the rate throttling class to be modified" do | ||
stub_request(:get, "https://api.heroku.com/apps") | ||
.to_return([ | ||
{status: 429}, | ||
{status: 429}, | ||
{status: 200} | ||
]) | ||
|
||
@retry_count = 0 | ||
PlatformAPI.rate_throttle.log = ->(*_) { @retry_count += 1 } | ||
client.app.list | ||
|
||
expect(WebMock).to have_requested(:get, "https://api.heroku.com/apps").times(3) | ||
expect(@retry_count).to eq(2) | ||
end | ||
|
||
it "allows rate throttling logic to be changed" do | ||
stub_request(:get, "https://api.heroku.com/apps") | ||
.to_return([ | ||
{status: 429} | ||
]) | ||
|
||
@times_called = 0 | ||
PlatformAPI.rate_throttle = ->(&block) { @times_called += 1; block.call } | ||
|
||
client.app.list | ||
|
||
expect(WebMock).to have_requested(:get, "https://api.heroku.com/apps").once | ||
expect(@times_called).to eq(1) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.