Skip to content

Commit 5ad804a

Browse files
committed
PHPC-1290: Add tests for server selection in sharded transactions
1 parent 969b0f8 commit 5ad804a

14 files changed

+835
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::executeBulkWrite() pins transaction to server
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_mongos_with_replica_set(); ?>
6+
<?php skip_if_server_version('<', '4.1.6'); ?>
7+
<?php skip_if_not_clean(); ?>
8+
--FILE--
9+
<?php
10+
require_once __DIR__ . "/../utils/basic.inc";
11+
12+
$manager = new MongoDB\Driver\Manager(URI);
13+
14+
/* Create collections as that can't be (automatically) done in a transaction */
15+
$manager->executeCommand(
16+
DATABASE_NAME,
17+
new \MongoDB\Driver\Command([ 'create' => COLLECTION_NAME ]),
18+
[ 'writeConcern' => new \MongoDB\Driver\WriteConcern( \MongoDB\Driver\WriteConcern::MAJORITY ) ]
19+
);
20+
21+
$session = $manager->startSession();
22+
var_dump($session->getServer() instanceof \MongoDB\Driver\Server);
23+
24+
$session->startTransaction();
25+
var_dump($session->getServer() instanceof \MongoDB\Driver\Server);
26+
27+
$bulk = new MongoDB\Driver\BulkWrite();
28+
$bulk->insert(['x' => 1]);
29+
$manager->executeBulkWrite(NS, $bulk, ['session' => $session]);
30+
31+
$pinnedServer = $session->getServer();
32+
var_dump($pinnedServer instanceof \MongoDB\Driver\Server);
33+
34+
$bulk = new MongoDB\Driver\BulkWrite();
35+
$bulk->insert(['x' => 1]);
36+
$manager->executeBulkWrite(NS, $bulk, ['session' => $session]);
37+
38+
$session->commitTransaction();
39+
40+
var_dump($session->getServer() == $pinnedServer);
41+
42+
$bulk = new MongoDB\Driver\BulkWrite();
43+
$bulk->insert(['x' => 1]);
44+
$manager->executeBulkWrite(NS, $bulk, ['session' => $session]);
45+
46+
var_dump($session->getServer() instanceof \MongoDB\Driver\Server);
47+
48+
?>
49+
===DONE===
50+
<?php exit(0); ?>
51+
--EXPECT--
52+
bool(false)
53+
bool(false)
54+
bool(true)
55+
bool(true)
56+
bool(false)
57+
===DONE===
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::executeCommand() pins transaction to server
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_mongos_with_replica_set(); ?>
6+
<?php skip_if_server_version('<', '4.1.6'); ?>
7+
<?php skip_if_not_clean(); ?>
8+
--FILE--
9+
<?php
10+
require_once __DIR__ . "/../utils/basic.inc";
11+
12+
$manager = new MongoDB\Driver\Manager(URI);
13+
14+
/* Create collections as that can't be (automatically) done in a transaction */
15+
$manager->executeCommand(
16+
DATABASE_NAME,
17+
new \MongoDB\Driver\Command([ 'create' => COLLECTION_NAME ]),
18+
[ 'writeConcern' => new \MongoDB\Driver\WriteConcern( \MongoDB\Driver\WriteConcern::MAJORITY ) ]
19+
);
20+
21+
$session = $manager->startSession();
22+
var_dump($session->getServer() instanceof \MongoDB\Driver\Server);
23+
24+
$session->startTransaction();
25+
var_dump($session->getServer() instanceof \MongoDB\Driver\Server);
26+
27+
$command = new MongoDB\Driver\Command([
28+
'aggregate' => COLLECTION_NAME,
29+
'pipeline' => [['$group' => ['_id' => 1]]],
30+
'cursor' => (object) []
31+
]);
32+
$manager->executeCommand(DATABASE_NAME, $command, ['session' => $session]);
33+
34+
$pinnedServer = $session->getServer();
35+
var_dump($pinnedServer instanceof \MongoDB\Driver\Server);
36+
37+
$bulk = new MongoDB\Driver\BulkWrite();
38+
$bulk->insert(['x' => 1]);
39+
$manager->executeBulkWrite(NS, $bulk, ['session' => $session]);
40+
41+
$session->commitTransaction();
42+
43+
var_dump($session->getServer() == $pinnedServer);
44+
45+
$bulk = new MongoDB\Driver\BulkWrite();
46+
$bulk->insert(['x' => 1]);
47+
$manager->executeBulkWrite(NS, $bulk, ['session' => $session]);
48+
49+
var_dump($session->getServer() instanceof \MongoDB\Driver\Server);
50+
51+
?>
52+
===DONE===
53+
<?php exit(0); ?>
54+
--EXPECT--
55+
bool(false)
56+
bool(false)
57+
bool(true)
58+
bool(true)
59+
bool(false)
60+
===DONE===
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::executeQuery() pins transaction to server
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_mongos_with_replica_set(); ?>
6+
<?php skip_if_server_version('<', '4.1.6'); ?>
7+
<?php skip_if_not_clean(); ?>
8+
--FILE--
9+
<?php
10+
require_once __DIR__ . "/../utils/basic.inc";
11+
12+
$manager = new MongoDB\Driver\Manager(URI);
13+
14+
/* Create collections as that can't be (automatically) done in a transaction */
15+
$manager->executeReadWriteCommand(
16+
DATABASE_NAME,
17+
new \MongoDB\Driver\Command([ 'create' => COLLECTION_NAME ]),
18+
[ 'writeConcern' => new \MongoDB\Driver\WriteConcern( \MongoDB\Driver\WriteConcern::MAJORITY ) ]
19+
);
20+
21+
$session = $manager->startSession();
22+
var_dump($session->getServer() instanceof \MongoDB\Driver\Server);
23+
24+
$session->startTransaction();
25+
var_dump($session->getServer() instanceof \MongoDB\Driver\Server);
26+
27+
$query = new MongoDB\Driver\Query([]);
28+
$manager->executeQuery(NS, $query, ['session' => $session]);
29+
30+
$pinnedServer = $session->getServer();
31+
var_dump($pinnedServer instanceof \MongoDB\Driver\Server);
32+
33+
$bulk = new MongoDB\Driver\BulkWrite();
34+
$bulk->insert(['x' => 1]);
35+
$manager->executeBulkWrite(NS, $bulk, ['session' => $session]);
36+
37+
$session->commitTransaction();
38+
39+
var_dump($session->getServer() == $pinnedServer);
40+
41+
$bulk = new MongoDB\Driver\BulkWrite();
42+
$bulk->insert(['x' => 1]);
43+
$manager->executeBulkWrite(NS, $bulk, ['session' => $session]);
44+
45+
var_dump($session->getServer() instanceof \MongoDB\Driver\Server);
46+
47+
?>
48+
===DONE===
49+
<?php exit(0); ?>
50+
--EXPECT--
51+
bool(false)
52+
bool(false)
53+
bool(true)
54+
bool(true)
55+
bool(false)
56+
===DONE===
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::executeReadCommand() pins transaction to server
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_mongos_with_replica_set(); ?>
6+
<?php skip_if_server_version('<', '4.1.6'); ?>
7+
<?php skip_if_not_clean(); ?>
8+
--FILE--
9+
<?php
10+
require_once __DIR__ . "/../utils/basic.inc";
11+
12+
$manager = new MongoDB\Driver\Manager(URI);
13+
14+
/* Create collections as that can't be (automatically) done in a transaction */
15+
$manager->executeReadCommand(
16+
DATABASE_NAME,
17+
new \MongoDB\Driver\Command([ 'create' => COLLECTION_NAME ]),
18+
[ 'writeConcern' => new \MongoDB\Driver\WriteConcern( \MongoDB\Driver\WriteConcern::MAJORITY ) ]
19+
);
20+
21+
$session = $manager->startSession();
22+
var_dump($session->getServer() instanceof \MongoDB\Driver\Server);
23+
24+
$session->startTransaction();
25+
var_dump($session->getServer() instanceof \MongoDB\Driver\Server);
26+
27+
$command = new MongoDB\Driver\Command([
28+
'aggregate' => COLLECTION_NAME,
29+
'pipeline' => [['$group' => ['_id' => 1]]],
30+
'cursor' => (object) []
31+
]);
32+
$manager->executeReadCommand(DATABASE_NAME, $command, ['session' => $session]);
33+
34+
$pinnedServer = $session->getServer();
35+
var_dump($pinnedServer instanceof \MongoDB\Driver\Server);
36+
37+
$bulk = new MongoDB\Driver\BulkWrite();
38+
$bulk->insert(['x' => 1]);
39+
$manager->executeBulkWrite(NS, $bulk, ['session' => $session]);
40+
41+
$session->commitTransaction();
42+
43+
var_dump($session->getServer() == $pinnedServer);
44+
45+
$bulk = new MongoDB\Driver\BulkWrite();
46+
$bulk->insert(['x' => 1]);
47+
$manager->executeBulkWrite(NS, $bulk, ['session' => $session]);
48+
49+
var_dump($session->getServer() instanceof \MongoDB\Driver\Server);
50+
51+
?>
52+
===DONE===
53+
<?php exit(0); ?>
54+
--EXPECT--
55+
bool(false)
56+
bool(false)
57+
bool(true)
58+
bool(true)
59+
bool(false)
60+
===DONE===
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::executeReadWriteCommand() pins transaction to server
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_mongos_with_replica_set(); ?>
6+
<?php skip_if_server_version('<', '4.1.6'); ?>
7+
<?php skip_if_not_clean(); ?>
8+
--FILE--
9+
<?php
10+
require_once __DIR__ . "/../utils/basic.inc";
11+
12+
$manager = new MongoDB\Driver\Manager(URI);
13+
14+
/* Create collections as that can't be (automatically) done in a transaction */
15+
$manager->executeReadWriteCommand(
16+
DATABASE_NAME,
17+
new \MongoDB\Driver\Command([ 'create' => COLLECTION_NAME ]),
18+
[ 'writeConcern' => new \MongoDB\Driver\WriteConcern( \MongoDB\Driver\WriteConcern::MAJORITY ) ]
19+
);
20+
21+
$session = $manager->startSession();
22+
var_dump($session->getServer() instanceof \MongoDB\Driver\Server);
23+
24+
$session->startTransaction();
25+
var_dump($session->getServer() instanceof \MongoDB\Driver\Server);
26+
27+
$command = new MongoDB\Driver\Command([
28+
'aggregate' => COLLECTION_NAME,
29+
'pipeline' => [['$group' => ['_id' => 1]]],
30+
'cursor' => (object) []
31+
]);
32+
$manager->executeReadWriteCommand(DATABASE_NAME, $command, ['session' => $session]);
33+
34+
$pinnedServer = $session->getServer();
35+
var_dump($pinnedServer instanceof \MongoDB\Driver\Server);
36+
37+
$bulk = new MongoDB\Driver\BulkWrite();
38+
$bulk->insert(['x' => 1]);
39+
$manager->executeBulkWrite(NS, $bulk, ['session' => $session]);
40+
41+
$session->commitTransaction();
42+
43+
var_dump($session->getServer() == $pinnedServer);
44+
45+
$bulk = new MongoDB\Driver\BulkWrite();
46+
$bulk->insert(['x' => 1]);
47+
$manager->executeBulkWrite(NS, $bulk, ['session' => $session]);
48+
49+
var_dump($session->getServer() instanceof \MongoDB\Driver\Server);
50+
51+
?>
52+
===DONE===
53+
<?php exit(0); ?>
54+
--EXPECT--
55+
bool(false)
56+
bool(false)
57+
bool(true)
58+
bool(true)
59+
bool(false)
60+
===DONE===
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::executeWriteCommand() pins transaction to server
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_mongos_with_replica_set(); ?>
6+
<?php skip_if_server_version('<', '4.1.6'); ?>
7+
<?php skip_if_not_clean(); ?>
8+
--FILE--
9+
<?php
10+
require_once __DIR__ . "/../utils/basic.inc";
11+
12+
$manager = new MongoDB\Driver\Manager(URI);
13+
14+
/* Create collections as that can't be (automatically) done in a transaction */
15+
$manager->executeReadWriteCommand(
16+
DATABASE_NAME,
17+
new \MongoDB\Driver\Command([ 'create' => COLLECTION_NAME ]),
18+
[ 'writeConcern' => new \MongoDB\Driver\WriteConcern( \MongoDB\Driver\WriteConcern::MAJORITY ) ]
19+
);
20+
21+
$session = $manager->startSession();
22+
var_dump($session->getServer() instanceof \MongoDB\Driver\Server);
23+
24+
$session->startTransaction();
25+
var_dump($session->getServer() instanceof \MongoDB\Driver\Server);
26+
27+
$command = new MongoDB\Driver\Command([
28+
'findAndModify' => COLLECTION_NAME,
29+
'query' => ['_id' => 'foo'],
30+
'upsert' => true,
31+
'new' => true,
32+
'update' => ['x' => 1]
33+
]);
34+
$manager->executeWriteCommand(DATABASE_NAME, $command, ['session' => $session]);
35+
36+
$pinnedServer = $session->getServer();
37+
var_dump($pinnedServer instanceof \MongoDB\Driver\Server);
38+
39+
$bulk = new MongoDB\Driver\BulkWrite();
40+
$bulk->insert(['x' => 1]);
41+
$manager->executeBulkWrite(NS, $bulk, ['session' => $session]);
42+
43+
$session->commitTransaction();
44+
45+
var_dump($session->getServer() == $pinnedServer);
46+
47+
$bulk = new MongoDB\Driver\BulkWrite();
48+
$bulk->insert(['x' => 1]);
49+
$manager->executeBulkWrite(NS, $bulk, ['session' => $session]);
50+
51+
var_dump($session->getServer() instanceof \MongoDB\Driver\Server);
52+
53+
?>
54+
===DONE===
55+
<?php exit(0); ?>
56+
--EXPECT--
57+
bool(false)
58+
bool(false)
59+
bool(true)
60+
bool(true)
61+
bool(false)
62+
===DONE===

0 commit comments

Comments
 (0)