Skip to content

PHPC-676: Do not allow BulkWrite objects to be executed multiple times #462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions php_phongo.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,15 +489,9 @@ bool phongo_execute_write(zval *manager, const char *namespace, php_phongo_bulkw

client = Z_MANAGER_OBJ_P(manager)->client;

/* Since BulkWrite objects can currently be executed multiple times, ensure
* that the database and collection name are freed before we overwrite them.
* This may be removed once PHPC-676 is implemented. */
if (bulk_write->database) {
efree(bulk_write->database);
}

if (bulk_write->collection) {
efree(bulk_write->collection);
if (bulk_write->executed) {
phongo_throw_exception(PHONGO_ERROR_WRITE_FAILED TSRMLS_CC, "BulkWrite objects may only be executed once and this instance has already been executed");
return false;
}

if (!phongo_split_namespace(namespace, &bulk_write->database, &bulk_write->collection)) {
Expand Down
27 changes: 27 additions & 0 deletions tests/bulk/bulkwrite_error-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--TEST--
MongoDB\Driver\BulkWrite cannot be executed multiple times
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; CLEANUP(STANDALONE); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

$manager = new MongoDB\Driver\Manager(STANDALONE);

$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['x' => 1]);
$result = $manager->executeBulkWrite(NS, $bulk);
printf("Inserted %d document(s)\n", $result->getInsertedCount());

echo throws(function() use ($manager, $bulk) {
$result = $manager->executeBulkWrite(NS, $bulk);
}, 'MongoDB\Driver\Exception\BulkWriteException'), "\n";

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
Inserted 1 document(s)
OK: Got MongoDB\Driver\Exception\BulkWriteException
BulkWrite objects may only be executed once and this instance has already been executed
===DONE===
85 changes: 0 additions & 85 deletions tests/bulk/write-0003.phpt

This file was deleted.

11 changes: 6 additions & 5 deletions tests/manager/manager-executeBulkWrite_error-007.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ MongoDB\Driver\Manager::executeBulkWrite() should not issue warning before excep
<?php
require_once __DIR__ . "/../utils/basic.inc";

$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['x' => 1]);

// Invalid host cannot be resolved
$manager = new MongoDB\Driver\Manager('mongodb://invalid.host:27017', ['serverSelectionTimeoutMS' => 1]);

echo throws(function() use ($manager, $bulk) {
echo throws(function() use ($manager) {
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['x' => 1]);
$manager->executeBulkWrite(NS, $bulk);
}, 'MongoDB\Driver\Exception\ConnectionTimeoutException'), "\n";

// Valid host refuses connection
$manager = new MongoDB\Driver\Manager('mongodb://localhost:54321', ['serverSelectionTimeoutMS' => 1]);

echo throws(function() use ($manager, $bulk) {
echo throws(function() use ($manager) {
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(['x' => 1]);
$manager->executeBulkWrite(NS, $bulk);
}, 'MongoDB\Driver\Exception\ConnectionTimeoutException'), "\n";

Expand Down
5 changes: 5 additions & 0 deletions tests/replicaset/manager-selectserver-001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ $bulk->insert(array('_id' => 3, 'x' => 4, 'y' => 5));
throws(function() use($server2, $bulk) {
$server2->executeBulkWrite(NS, $bulk);
}, "MongoDB\Driver\Exception\BulkWriteException");

$bulk = new \MongoDB\Driver\BulkWrite();
$bulk->insert(array('_id' => 1, 'x' => 2, 'y' => 3));
$bulk->insert(array('_id' => 2, 'x' => 3, 'y' => 4));
$bulk->insert(array('_id' => 3, 'x' => 4, 'y' => 5));
$result = $server2->executeBulkWrite("local.example", $bulk);
var_dump($result->getInsertedCount());
?>
Expand Down