Add request timeout.

This commit is contained in:
sc0Vu 2018-02-21 10:48:45 +08:00
parent d89e7d1884
commit 6f18ea8431
3 changed files with 27 additions and 6 deletions

View File

@ -73,7 +73,9 @@ class HttpRequestManager extends RequestManager implements IRequestManager
'headers' => [ 'headers' => [
'content-type' => 'application/json' 'content-type' => 'application/json'
], ],
'body' => $payload 'body' => $payload,
'timeout' => $this->timeout,
'connect_timeout' => $this->timeout
]); ]);
$json = json_decode($res->getBody()); $json = json_decode($res->getBody());

View File

@ -20,16 +20,24 @@ class RequestManager
*/ */
protected $host; protected $host;
/**
* timeout
*
* @var float
*/
protected $timeout;
/** /**
* construct * construct
* *
* @param string $host * @param string $host
* @param float $timeout
* @return void * @return void
*/ */
public function __construct($host) public function __construct($host, $timeout=1)
{ {
$this->host = $host; $this->host = $host;
$this->timeout = (float) $timeout;
} }
/** /**
@ -74,4 +82,14 @@ class RequestManager
{ {
return $this->host; return $this->host;
} }
/**
* getTimeout
*
* @return float
*/
public function getTimeout()
{
return $this->timeout;
}
} }

View File

@ -14,12 +14,13 @@ class RequestManagerTest extends TestCase
*/ */
public function testSetHost() public function testSetHost()
{ {
$requestManager = new RequestManager('http://localhost:8545'); $requestManager = new RequestManager('http://localhost:8545', 0.1);
$this->assertEquals($requestManager->host, 'http://localhost:8545'); $this->assertEquals($requestManager->host, 'http://localhost:8545');
$this->assertEquals($requestManager->timeout, 0.1);
$requestManager->host = $this->testRinkebyHost; $requestManager->host = $this->testRinkebyHost;
$requestManager->timeout = 1;
$this->assertEquals($requestManager->host, 'http://localhost:8545'); $this->assertEquals($requestManager->host, 'http://localhost:8545');
$this->assertEquals($requestManager->timeout, 0.1);
} }
} }