Skip to content

Commit 97f3c6f

Browse files
committed
Testing - Limit 2 API calls per second
1 parent ac9dc63 commit 97f3c6f

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

tests/TestResource.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ class TestResource extends \PHPUnit_Framework_TestCase
1414
*/
1515
public static $shopify;
1616

17+
public static $microtimeOfLastAPICall;
18+
1719
/**
18-
* Initial setup for the tests to run
20+
* @inheritDoc
1921
*/
2022
public static function setUpBeforeClass()
2123
{
@@ -28,8 +30,34 @@ public static function setUpBeforeClass()
2830
self::$shopify = ShopifySDK::config($config);
2931
}
3032

33+
/**
34+
* @inheritDoc
35+
*/
3136
public static function tearDownAfterClass()
3237
{
3338
self::$shopify = null;
3439
}
40+
41+
/**
42+
* @inheritDoc
43+
*/
44+
public function setUp()
45+
{
46+
if(static::$microtimeOfLastAPICall == null) {
47+
//Maintain 2 seconds per call
48+
usleep(500000);
49+
} else {
50+
$now = microtime(true);
51+
$timeSinceLastCall = $now - static::$microtimeOfLastAPICall;
52+
//Ensure 2 API calls per second
53+
if($timeSinceLastCall < .5) {
54+
$timeToWait = .5 - $timeSinceLastCall;
55+
//convert time to microseconds
56+
$microSecondsToWait = $timeToWait * 1000000;
57+
//Wait to maintain the API call difference of .5 seconds
58+
usleep($microSecondsToWait);
59+
}
60+
}
61+
static::$microtimeOfLastAPICall = microtime(true);
62+
}
3563
}

0 commit comments

Comments
 (0)