Skip to content

Commit 83521ad

Browse files
authored
PHPC-2003: Implement Session::isDirty() (#1293)
1 parent bd757bb commit 83521ad

9 files changed

+77
-0
lines changed

src/MongoDB/Session.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,21 @@ static PHP_METHOD(Session, endSession)
587587
intern->client_session = NULL;
588588
} /* }}} */
589589

590+
/* {{{ proto bool MongoDB\Driver\Session::isDirty()
591+
Returns whether the session is dirty (i.e. was used with a command that
592+
encountered a network error) and will be discarded when returned to the
593+
server session pool. */
594+
static PHP_METHOD(Session, isDirty)
595+
{
596+
php_phongo_session_t* intern = Z_SESSION_OBJ_P(getThis());
597+
598+
SESSION_CHECK_LIVELINESS(intern, "isDirty")
599+
600+
PHONGO_PARSE_PARAMETERS_NONE();
601+
602+
RETVAL_BOOL(mongoc_client_session_get_dirty(intern->client_session));
603+
} /* }}} */
604+
590605
/* {{{ proto void MongoDB\Driver\Session::isInTransaction(void)
591606
Returns whether a multi-document transaction is in progress */
592607
static PHP_METHOD(Session, isInTransaction)
@@ -636,6 +651,7 @@ static zend_function_entry php_phongo_session_me[] = {
636651
PHP_ME(Session, getServer, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
637652
PHP_ME(Session, getTransactionOptions, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
638653
PHP_ME(Session, getTransactionState, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
654+
PHP_ME(Session, isDirty, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
639655
PHP_ME(Session, isInTransaction, ai_Session_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
640656
PHP_ME(Session, startTransaction, ai_Session_startTransaction, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
641657
ZEND_NAMED_ME(__construct, PHP_FN(MongoDB_disabled___construct), ai_Session_void, ZEND_ACC_PRIVATE | ZEND_ACC_FINAL)
@@ -779,6 +795,12 @@ static HashTable* php_phongo_session_get_debug_info(phongo_compat_object_handler
779795
ADD_ASSOC_NULL_EX(&retval, "server");
780796
}
781797

798+
if (intern->client_session) {
799+
ADD_ASSOC_BOOL_EX(&retval, "dirty", mongoc_client_session_get_dirty(intern->client_session));
800+
} else {
801+
ADD_ASSOC_NULL_EX(&retval, "dirty");
802+
}
803+
782804
if (intern->client_session) {
783805
ADD_ASSOC_BOOL_EX(&retval, "inTransaction", mongoc_client_session_in_transaction(intern->client_session));
784806
} else {

tests/session/session-debug-001.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ object(MongoDB\Driver\Session)#%d (%d) {
3939
NULL
4040
["server"]=>
4141
NULL
42+
["dirty"]=>
43+
bool(false)
4244
["inTransaction"]=>
4345
bool(false)
4446
["transactionState"]=>

tests/session/session-debug-002.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ object(MongoDB\Driver\Session)#%d (%d) {
5757
}
5858
["server"]=>
5959
NULL
60+
["dirty"]=>
61+
bool(false)
6062
["inTransaction"]=>
6163
bool(false)
6264
["transactionState"]=>

tests/session/session-debug-003.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ object(MongoDB\Driver\Session)#%d (%d) {
3939
NULL
4040
["server"]=>
4141
NULL
42+
["dirty"]=>
43+
bool(false)
4244
["inTransaction"]=>
4345
bool(false)
4446
["transactionState"]=>

tests/session/session-debug-004.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ object(MongoDB\Driver\Session)#%d (%d) {
3636
NULL
3737
["server"]=>
3838
NULL
39+
["dirty"]=>
40+
NULL
3941
["inTransaction"]=>
4042
NULL
4143
["transactionState"]=>

tests/session/session-debug-005.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ object(MongoDB\Driver\Session)#%d (%d) {
6666
object(MongoDB\Driver\Server)#%d (%d) {
6767
%a
6868
}
69+
["dirty"]=>
70+
bool(false)
6971
["inTransaction"]=>
7072
bool(true)
7173
["transactionState"]=>

tests/session/session-debug-006.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ object(MongoDB\Driver\Session)#%d (%d) {
4747
NULL
4848
["server"]=>
4949
NULL
50+
["dirty"]=>
51+
bool(false)
5052
["inTransaction"]=>
5153
bool(true)
5254
["transactionState"]=>

tests/session/session-debug-007.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ object(MongoDB\Driver\Session)#%d (%d) {
3939
NULL
4040
["server"]=>
4141
NULL
42+
["dirty"]=>
43+
bool(false)
4244
["inTransaction"]=>
4345
bool(false)
4446
["transactionState"]=>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
MongoDB\Driver\Session::isDirty()
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_libmongoc_crypto() ?>
6+
<?php skip_if_no_failcommand_failpoint(); ?>
7+
--FILE--
8+
<?php
9+
require_once __DIR__ . "/../utils/basic.inc";
10+
11+
$manager = create_test_manager();
12+
$server = $manager->selectServer();
13+
$session = $manager->startSession();
14+
15+
printf("New session is dirty: %s\n", $session->isDirty() ? 'yes' : 'no');
16+
17+
$command = new MongoDB\Driver\Command(['ping' => 1]);
18+
$server->executeCommand(DATABASE_NAME, $command, ['session' => $session]);
19+
20+
printf("Session after successful command is dirty: %s\n", $session->isDirty() ? 'yes' : 'no');
21+
22+
configureTargetedFailPoint($server, 'failCommand', [ 'times' => 1 ], [
23+
'failCommands' => ['ping'],
24+
'closeConnection' => true
25+
]);
26+
27+
throws(function() use ($server, $command, $session) {
28+
$server->executeCommand(DATABASE_NAME, $command, ['session' => $session]);
29+
}, MongoDB\Driver\Exception\ConnectionTimeoutException::class);
30+
31+
printf("Session after network error is dirty: %s\n", $session->isDirty() ? 'yes' : 'no');
32+
33+
?>
34+
===DONE===
35+
<?php exit(0); ?>
36+
--EXPECT--
37+
New session is dirty: no
38+
Session after successful command is dirty: no
39+
OK: Got MongoDB\Driver\Exception\ConnectionTimeoutException
40+
Session after network error is dirty: yes
41+
===DONE===

0 commit comments

Comments
 (0)