Skip to content

Commit a092a78

Browse files
authored
Merge pull request #49 from brianlmoon/check_server_version_for_timeout
Check server version for timeout unit
2 parents 92a23bd + e9efc05 commit a092a78

File tree

2 files changed

+99
-5
lines changed

2 files changed

+99
-5
lines changed

Net/Gearman/Connection.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ class Net_Gearman_Connection
107107

108108
public $socket;
109109

110+
/**
111+
* Gearmand Server Version
112+
*
113+
* @var string
114+
*/
115+
protected $serverVersion;
116+
110117
public function __construct($host=null, $timeout=250) {
111118
if ($host) {
112119
$this->connect($host, $timeout);
@@ -123,8 +130,8 @@ public function __destruct() {
123130
* Opens the socket to the Gearman Job server. It throws an exception if
124131
* a socket error occurs. Also populates Net_Gearman_Connection::$magic.
125132
*
126-
* @param string $host e.g. 127.0.0.1 or 127.0.0.1:7003
127-
* @param int $timeout Timeout in milliseconds
133+
* @param string $host e.g. 127.0.0.1 or 127.0.0.1:7003
134+
* @param int $timeout Timeout in milliseconds
128135
*
129136
* @return resource A connection to a Gearman server
130137
* @throws Net_Gearman_Exception when it can't connect to server
@@ -203,6 +210,8 @@ public function connect($host, $timeout = 250)
203210

204211
// socket_set_option($this->socket, SOL_TCP, SO_DEBUG, 1); // Debug
205212

213+
$this->setServerVersion($host);
214+
206215
} else {
207216

208217
$errno = @socket_last_error($this->socket);
@@ -259,6 +268,10 @@ public function send($command, array $params = array())
259268
}
260269
}
261270

271+
if ($command === 'can_do_timeout') {
272+
$params = $this->fixTimeout($params);
273+
}
274+
262275
$d = implode("\x00", $data);
263276

264277
$cmd = "\0REQ" . pack("NN",
@@ -548,4 +561,30 @@ public static function subString($str, $start, $length)
548561
return substr($str, $start, $length);
549562
}
550563
}
564+
565+
/**
566+
* Sets the server version.
567+
*
568+
* @param string $host The host
569+
* @param Net_Gearman_Manager $manager Optional manager object
570+
*/
571+
protected function setServerVersion($host, $manager = null)
572+
{
573+
if (empty($manager)) {
574+
$manager = new \Net_Gearman_Manager($host);
575+
}
576+
$this->serverVersion = $manager->version();
577+
unset($manager);
578+
}
579+
580+
protected function fixTimeout($params) {
581+
// In gearmand version 1.1.19 and greater, the timeout is
582+
// expected to be in milliseconds. Before that version, it
583+
// is expected to be in seconds.
584+
// https://github.com/gearman/gearmand/issues/196
585+
if (version_compare('1.1.18', $this->serverVersion)) {
586+
$params['timeout'] *= 1000;
587+
}
588+
return $params;
589+
}
551590
}

tests/Net/Gearman/ConnectionTest.php

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,68 @@
22

33
/**
44
* Net_Gearman_ConnectionTest
5-
* @group functional
65
*/
76
class Net_Gearman_ConnectionTest extends \PHPUnit\Framework\TestCase
87
{
8+
/**
9+
* @group unit
10+
*/
11+
public function testSetServerVersion() {
12+
$mock_manager = new class extends \Net_Gearman_Manager {
13+
public $mock_version;
14+
public function __construct() {
15+
// noop
16+
}
17+
public function version() {
18+
return $this->mock_version;
19+
}
20+
};
21+
22+
$connection = new class extends Net_Gearman_Connection {
23+
public function setServerVersion($host, $manager = null) {
24+
parent::setServerVersion($host, $manager);
25+
return $this->serverVersion;
26+
}
27+
};
28+
29+
$mock_manager->mock_version = '1.1.18';
30+
31+
$result = $connection->setServerVersion('localhost:4730', $mock_manager);
32+
$this->assertEquals('1.1.18', $result);
33+
34+
$mock_manager->mock_version = '1.1.19';
35+
36+
$result = $connection->setServerVersion('localhost:4730', $mock_manager);
37+
$this->assertEquals('1.1.19', $result);
38+
}
39+
40+
/**
41+
* @group unit
42+
*/
43+
public function testFixTimeout() {
44+
$connection = new class extends Net_Gearman_Connection {
45+
public $serverVersion;
46+
public function fixTimeout($params) {
47+
return parent::fixTimeout($params);
48+
}
49+
};
50+
51+
$connection->serverVersion = '1.1.18';
52+
$result = $connection->fixTimeout(['timeout' => 10]);
53+
$this->assertEquals(['timeout' => 10], $result);
54+
55+
$connection->serverVersion = '1.1.19';
56+
$result = $connection->fixTimeout(['timeout' => 10]);
57+
$this->assertEquals(['timeout' => 10000], $result);
58+
59+
$connection->serverVersion = '1.1.19.1';
60+
$result = $connection->fixTimeout(['timeout' => 10]);
61+
$this->assertEquals(['timeout' => 10000], $result);
62+
}
63+
964
/**
1065
* When no server is supplied, it should connect to localhost:4730.
11-
*
66+
* @group functional
1267
* @return void
1368
*/
1469
public function testDefaultConnect()
@@ -25,7 +80,7 @@ public function testDefaultConnect()
2580

2681
/**
2782
* 001-echo_req.phpt
28-
*
83+
* @group functional
2984
* @return void
3085
*/
3186
public function testSend()

0 commit comments

Comments
 (0)