From 1a7c268dab202b5ab0b57c8b343d2f84c27d12b9 Mon Sep 17 00:00:00 2001 From: Fabien MEYNARD Date: Tue, 24 Sep 2013 16:43:55 +0200 Subject: [PATCH 1/2] Convert timeout option to connectTimeoutMS for driver >= 1.4.0 --- lib/Doctrine/MongoDB/Connection.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/Doctrine/MongoDB/Connection.php b/lib/Doctrine/MongoDB/Connection.php index 0034cc5d..cb98d796 100644 --- a/lib/Doctrine/MongoDB/Connection.php +++ b/lib/Doctrine/MongoDB/Connection.php @@ -276,6 +276,11 @@ public function initialize() $server = $this->server ?: 'mongodb://localhost:27017'; $options = $this->options; + if (version_compare(phpversion('mongo'), '1.4.0', '>=') && isset($options['timeout'])) { + $options['connectTimeoutMS'] = $options['timeout']; + unset($options['timeout']); + } + $this->mongoClient = $this->retry(function() use ($server, $options) { return version_compare(phpversion('mongo'), '1.3.0', '<') ? new \Mongo($server, $options) From 23356a7a8cb3ae06b8d30b446bde8a9ca3b3b568 Mon Sep 17 00:00:00 2001 From: Jeremy Mikola Date: Mon, 21 Apr 2014 16:17:09 -0400 Subject: [PATCH 2/2] Convert MongoClient timeout/wTimeout options for driver 1.4.0+ --- lib/Doctrine/MongoDB/Connection.php | 54 +++++++++++++++++-- .../Doctrine/MongoDB/Tests/ConnectionTest.php | 16 ++++++ 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/lib/Doctrine/MongoDB/Connection.php b/lib/Doctrine/MongoDB/Connection.php index cb98d796..6fa570ab 100644 --- a/lib/Doctrine/MongoDB/Connection.php +++ b/lib/Doctrine/MongoDB/Connection.php @@ -276,10 +276,8 @@ public function initialize() $server = $this->server ?: 'mongodb://localhost:27017'; $options = $this->options; - if (version_compare(phpversion('mongo'), '1.4.0', '>=') && isset($options['timeout'])) { - $options['connectTimeoutMS'] = $options['timeout']; - unset($options['timeout']); - } + $options = isset($options['timeout']) ? $this->convertConnectTimeout($options) : $options; + $options = isset($options['wTimeout']) ? $this->convertWriteTimeout($options) : $options; $this->mongoClient = $this->retry(function() use ($server, $options) { return version_compare(phpversion('mongo'), '1.3.0', '<') @@ -447,4 +445,52 @@ protected function retry(\Closure $retry) } } } + + /** + * Converts "timeout" MongoClient constructor option to "connectTimeoutMS" + * for driver versions 1.4.0+. + * + * Note: MongoClient actually allows case-insensitive option names, but + * we'll only process the canonical version here. + * + * @param array $options + * @return array + */ + protected function convertConnectTimeout(array $options) + { + if (version_compare(phpversion('mongo'), '1.4.0', '<')) { + return $options; + } + + if (isset($options['timeout']) && ! isset($options['connectTimeoutMS'])) { + $options['connectTimeoutMS'] = $options['timeout']; + unset($options['timeout']); + } + + return $options; + } + + /** + * Converts "timeout" MongoClient constructor option to "connectTimeoutMS" + * for driver versions 1.4.0+. + * + * Note: MongoClient actually allows case-insensitive option names, but + * we'll only process the canonical version here. + * + * @param array $options + * @return array + */ + protected function convertWriteTimeout(array $options) + { + if (version_compare(phpversion('mongo'), '1.4.0', '<')) { + return $options; + } + + if (isset($options['wTimeout']) && ! isset($options['wTimeoutMS'])) { + $options['wTimeoutMS'] = $options['wTimeout']; + unset($options['wTimeout']); + } + + return $options; + } } diff --git a/tests/Doctrine/MongoDB/Tests/ConnectionTest.php b/tests/Doctrine/MongoDB/Tests/ConnectionTest.php index 6f95e6a7..161abbc6 100644 --- a/tests/Doctrine/MongoDB/Tests/ConnectionTest.php +++ b/tests/Doctrine/MongoDB/Tests/ConnectionTest.php @@ -206,6 +206,22 @@ public function testToString() $this->assertEquals('Test', (string) $conn); } + public function testConnectTimeoutOptionIsConverted() + { + if (version_compare(phpversion('mongo'), '1.4.0', '<')) { + $this->markTestSkipped('This test is not applicable to driver versions < 1.4.0'); + } + + /* Since initialize() creates MongoClient directly, we cannot examine + * the options passed to its constructor. + * + * Note: we do not test "wTimeout" conversion, since the driver does not + * raise a deprecation notice for its usage (see: PHP-1079). + */ + $conn = new Connection(null, array('timeout' => 10000)); + $conn->initialize(); + } + private function getTestConnection($mongo) { return new Connection($mongo);