Skip to content

Commit

Permalink
Merge pull request #55 from rdohms/DragonBe-issue-53/rate-limit-divis…
Browse files Browse the repository at this point in the history
…ion-by-zero

Fixing issue #53: preventing a division by zero error
  • Loading branch information
rdohms authored Dec 26, 2017
2 parents cc8e649 + 4b267e4 commit 6abb76a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ php:
- 5.6
- 7.0

matrix:
include:
- php: 5.3
dist: precise
exclude:
- php: 5.3
dist: trusty

before_script:
- composer selfupdate
- composer install --prefer-source --dev
Expand Down
7 changes: 5 additions & 2 deletions src/DMS/Service/Meetup/Plugin/RateLimitPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ public function onBeforeSend()
*/
protected function slowdownRequests()
{
$microsecondsPerRequestRemaining = $this->rateLimitReset / $this->rateLimitRemaining * 1000000;
usleep($microsecondsPerRequestRemaining);
$sleepInMicroseconds = ((int) $this->rateLimitRemaining === 0)
? $this->rateLimitReset * 1000000
: $this->rateLimitReset / $this->rateLimitRemaining * 1000000;

usleep((int) $sleepInMicroseconds);
}
}
17 changes: 17 additions & 0 deletions tests/DMS/Service/Meetup/Plugin/RateLimitPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,21 @@ protected function setupProxyPluginWithValues($limit, $remaining, $reset, $facto

return $plugin;
}

/**
* Test to ensure a division by zero will never occur when validating
* the rate limit of the API call when the rate limit remaining has a
* value of 0
*
* @group issue-53
* @see https://github.com/rdohms/meetup-api-client/issues/53
* @covers \DMS\Service\Meetup\Plugin\RateLimitPlugin::slowdownRequests
*/
public function testExhaustedRateLimitIsHandledWithoutDivisionByZeroError()
{
$plugin = $this->setupProxyPluginWithValues(10, 0, 1, 0.5);
$plugin->onBeforeSend();

$this->assertGreaterThan(0, $plugin->slowdowns);
}
}

0 comments on commit 6abb76a

Please sign in to comment.