Skip to content

Commit dad5d51

Browse files
committed
PHPC-107: Throw ExecutionTimeoutException for maxTimeMs
1 parent 713dc5a commit dad5d51

File tree

7 files changed

+168
-1
lines changed

7 files changed

+168
-1
lines changed

config.m4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ if test "$PHONGO" != "no"; then
194194
src/MongoDB/AuthenticationException.c \
195195
src/MongoDB/SSLConnectionException.c \
196196
src/MongoDB/DuplicateKeyException.c \
197+
src/MongoDB/ExecutionTimeoutException.c \
197198
src/MongoDB/WriteException.c \
198199
";
199200

php_phongo.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ zend_class_entry* phongo_exception_from_phongo_domain(php_phongo_error_domain_t
9191
zend_class_entry* phongo_exception_from_mongoc_domain(uint32_t /* mongoc_error_domain_t */ domain, uint32_t /* mongoc_error_code_t */ code)
9292
{
9393
switch(code) {
94-
case 11000: /* Duplicate key */
94+
case 50: /* ExceededTimeLimit */
95+
return php_phongo_executiontimeoutexception_ce;
96+
case 11000: /* DuplicateKey */
9597
return php_phongo_duplicatekeyexception_ce;
9698
case MONGOC_ERROR_CLIENT_AUTHENTICATE:
9799
return php_phongo_authenticationexception_ce;
@@ -1619,6 +1621,7 @@ PHP_MINIT_FUNCTION(phongo)
16191621
PHP_MINIT(SSLConnectionException)(INIT_FUNC_ARGS_PASSTHRU);
16201622
PHP_MINIT(WriteException)(INIT_FUNC_ARGS_PASSTHRU);
16211623
PHP_MINIT(DuplicateKeyException)(INIT_FUNC_ARGS_PASSTHRU);
1624+
PHP_MINIT(ExecutionTimeoutException)(INIT_FUNC_ARGS_PASSTHRU);
16221625

16231626
PHP_MINIT(Type)(INIT_FUNC_ARGS_PASSTHRU);
16241627
PHP_MINIT(Serializable)(INIT_FUNC_ARGS_PASSTHRU);

php_phongo_classes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ extern PHONGO_API zend_class_entry *php_phongo_connectionexception_ce;
206206
extern PHONGO_API zend_class_entry *php_phongo_authenticationexception_ce;
207207
extern PHONGO_API zend_class_entry *php_phongo_sslconnectionexception_ce;
208208
extern PHONGO_API zend_class_entry *php_phongo_duplicatekeyexception_ce;
209+
extern PHONGO_API zend_class_entry *php_phongo_executiontimeoutexception_ce;
209210
extern PHONGO_API zend_class_entry *php_phongo_writeexception_ce;
210211

211212
extern PHONGO_API zend_class_entry *php_phongo_type_ce;
@@ -245,6 +246,7 @@ PHP_MINIT_FUNCTION(ConnectionException);
245246
PHP_MINIT_FUNCTION(AuthenticationException);
246247
PHP_MINIT_FUNCTION(SSLConnectionException);
247248
PHP_MINIT_FUNCTION(DuplicateKeyException);
249+
PHP_MINIT_FUNCTION(ExecutionTimeoutException);
248250
PHP_MINIT_FUNCTION(WriteException);
249251

250252
PHP_MINIT_FUNCTION(Type);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
+---------------------------------------------------------------------------+
3+
| PHP Driver for MongoDB |
4+
+---------------------------------------------------------------------------+
5+
| Copyright 2013-2014 MongoDB, Inc. |
6+
| |
7+
| Licensed under the Apache License, Version 2.0 (the "License"); |
8+
| you may not use this file except in compliance with the License. |
9+
| You may obtain a copy of the License at |
10+
| |
11+
| http://www.apache.org/licenses/LICENSE-2.0 |
12+
| |
13+
| Unless required by applicable law or agreed to in writing, software |
14+
| distributed under the License is distributed on an "AS IS" BASIS, |
15+
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16+
| See the License for the specific language governing permissions and |
17+
| limitations under the License. |
18+
+---------------------------------------------------------------------------+
19+
| Copyright (c) 2014, MongoDB, Inc. |
20+
+---------------------------------------------------------------------------+
21+
*/
22+
23+
#ifdef HAVE_CONFIG_H
24+
# include "config.h"
25+
#endif
26+
27+
/* External libs */
28+
#include <bson.h>
29+
#include <mongoc.h>
30+
31+
/* PHP Core stuff */
32+
#include <php.h>
33+
#include <php_ini.h>
34+
#include <ext/standard/info.h>
35+
#include <Zend/zend_interfaces.h>
36+
#include <ext/spl/spl_iterators.h>
37+
/* Our Compatability header */
38+
#include "php_compat_53.h"
39+
40+
/* Our stuffz */
41+
#include "php_phongo.h"
42+
#include "php_bson.h"
43+
#include <ext/spl/spl_exceptions.h>
44+
45+
46+
PHONGO_API zend_class_entry *php_phongo_executiontimeoutexception_ce;
47+
48+
/* {{{ MongoDB\Driver\ExecutionTimeoutException */
49+
50+
static zend_function_entry php_phongo_executiontimeoutexception_me[] = {
51+
PHP_FE_END
52+
};
53+
54+
/* }}} */
55+
56+
57+
/* {{{ PHP_MINIT_FUNCTION */
58+
PHP_MINIT_FUNCTION(ExecutionTimeoutException)
59+
{
60+
(void)type;
61+
(void)module_number;
62+
zend_class_entry ce;
63+
64+
INIT_NS_CLASS_ENTRY(ce, "MongoDB\\Driver", "ExecutionTimeoutException", php_phongo_executiontimeoutexception_me);
65+
php_phongo_executiontimeoutexception_ce = zend_register_internal_class_ex(&ce, php_phongo_runtimeexception_ce, NULL TSRMLS_CC);
66+
php_phongo_executiontimeoutexception_ce->ce_flags |= ZEND_ACC_FINAL_CLASS;
67+
68+
return SUCCESS;
69+
}
70+
/* }}} */
71+
72+
73+
74+
/*
75+
* Local variables:
76+
* tab-width: 4
77+
* c-basic-offset: 4
78+
* End:
79+
* vim600: noet sw=4 ts=4 fdm=marker
80+
* vim<600: noet sw=4 ts=4
81+
*/
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
ExecutionTimeoutException: exceeding $maxTimeMS (queries)
3+
--SKIPIF--
4+
<?php require "tests/utils/basic-skipif.inc" ?>
5+
--FILE--
6+
<?php
7+
require_once "tests/utils/basic.inc";
8+
9+
$manager = new MongoDB\Driver\Manager(MONGODB_URI);
10+
11+
$query = new MongoDB\Driver\Query(array("company" => "Smith, Carter and Buckridge"), array(
12+
'projection' => array('_id' => 0, 'username' => 1),
13+
'sort' => array('phoneNumber' => 1),
14+
'modifiers' => array(
15+
'$maxTimeMS' => 1,
16+
),
17+
));
18+
19+
failMaxTimeMS($manager);
20+
throws(function() use ($manager, $query) {
21+
$result = $manager->executeQuery(NS, $query);
22+
}, "MongoDB\Driver\ExecutionTimeoutException");
23+
24+
?>
25+
===DONE===
26+
<?php exit(0); ?>
27+
--EXPECT--
28+
OK: Got MongoDB\Driver\ExecutionTimeoutException
29+
===DONE===
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
ExecutionTimeoutException: exceeding maxTimeMS (commands)
3+
--SKIPIF--
4+
<?php require "tests/utils/basic-skipif.inc" ?>
5+
--FILE--
6+
<?php
7+
require_once "tests/utils/basic.inc";
8+
9+
$manager = new MongoDB\Driver\Manager(MONGODB_URI);
10+
11+
$cmd = array(
12+
"count" => "collection",
13+
"query" => array("a" => 1),
14+
"maxTimeMS" => 100,
15+
);
16+
$command = new MongoDB\Driver\Command($cmd);
17+
18+
failMaxTimeMS($manager);
19+
throws(function() use ($manager, $command) {
20+
$result = $manager->executeCommand(DATABASE_NAME, $command);
21+
}, "MongoDB\Driver\ExecutionTimeoutException");
22+
23+
?>
24+
===DONE===
25+
<?php exit(0); ?>
26+
--EXPECT--
27+
OK: Got MongoDB\Driver\ExecutionTimeoutException
28+
===DONE===

tests/utils/tools.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,26 @@ function def($arr) {
175175
}
176176
}
177177

178+
function configureFailPoint(MongoDB\Driver\Manager $manager, $failPoint, $mode, $data = array()) {
179+
180+
$doc = array(
181+
"configureFailPoint" => $failPoint,
182+
"mode" => $mode,
183+
);
184+
if ($data) {
185+
$doc["data"] = $data;
186+
}
187+
188+
$cmd = new MongoDB\Driver\Command($doc);
189+
$result = $manager->executeCommand("admin", $cmd);
190+
if (empty($result->toArray()["ok"])) {
191+
var_dump($result);
192+
throw new RuntimeException("Failpoint failed");
193+
}
194+
return true;
195+
}
196+
197+
function failMaxTimeMS(MongoDB\Driver\Manager $manager) {
198+
return configureFailPoint($manager, "maxTimeAlwaysTimeOut", array("times" => 1));
199+
}
200+

0 commit comments

Comments
 (0)