|
| 1 | +--TEST-- |
| 2 | +Causal consistency: first read or write in session updates operationTime (even on error) |
| 3 | +--SKIPIF-- |
| 4 | +<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?> |
| 5 | +<?php NEEDS('REPLICASET'); CLEANUP(REPLICASET); ?> |
| 6 | +--FILE-- |
| 7 | +<?php |
| 8 | +require_once __DIR__ . "/../utils/basic.inc"; |
| 9 | + |
| 10 | +class Test implements MongoDB\Driver\Monitoring\CommandSubscriber |
| 11 | +{ |
| 12 | + private $lastSeenOperationTime; |
| 13 | + |
| 14 | + public function executeBulkWrite() |
| 15 | + { |
| 16 | + $this->lastSeenOperationTime = null; |
| 17 | + |
| 18 | + MongoDB\Driver\Monitoring\addSubscriber($this); |
| 19 | + |
| 20 | + $manager = new MongoDB\Driver\Manager(REPLICASET); |
| 21 | + $session = $manager->startSession(); |
| 22 | + |
| 23 | + $bulk = new MongoDB\Driver\BulkWrite; |
| 24 | + $bulk->insert(['_id' => 1]); |
| 25 | + $bulk->insert(['_id' => 1]); |
| 26 | + |
| 27 | + throws(function() use ($manager, $bulk, $session) { |
| 28 | + $manager->executeBulkWrite(NS, $bulk, ['session' => $session]); |
| 29 | + }, 'MongoDB\Driver\Exception\BulkWriteException'); |
| 30 | + |
| 31 | + printf("Session reports last seen operationTime: %s\n", ($session->getOperationTime() == $this->lastSeenOperationTime) ? 'yes' : 'no'); |
| 32 | + |
| 33 | + MongoDB\Driver\Monitoring\removeSubscriber($this); |
| 34 | + } |
| 35 | + |
| 36 | + public function executeCommand() |
| 37 | + { |
| 38 | + $this->lastSeenOperationTime = null; |
| 39 | + |
| 40 | + MongoDB\Driver\Monitoring\addSubscriber($this); |
| 41 | + |
| 42 | + $manager = new MongoDB\Driver\Manager(REPLICASET); |
| 43 | + $session = $manager->startSession(); |
| 44 | + |
| 45 | + $command = new MongoDB\Driver\Command([ |
| 46 | + 'aggregate' => COLLECTION_NAME, |
| 47 | + 'pipeline' => [ |
| 48 | + ['$unsupportedOperator' => 1], |
| 49 | + ], |
| 50 | + 'cursor' => new stdClass, |
| 51 | + ]); |
| 52 | + |
| 53 | + throws(function() use ($manager, $command, $session) { |
| 54 | + $manager->executeCommand(DATABASE_NAME, $command, ['session' => $session]); |
| 55 | + }, 'MongoDB\Driver\Exception\RuntimeException'); |
| 56 | + |
| 57 | + /* We cannot access the server reply if an exception is thrown for a |
| 58 | + * failed command (see: PHPC-1076). For the time being, just assert that |
| 59 | + * the operationTime is not null. */ |
| 60 | + printf("Session has non-null operationTime: %s\n", ($session->getOperationTime() !== null ? 'yes' : 'no')); |
| 61 | + |
| 62 | + MongoDB\Driver\Monitoring\removeSubscriber($this); |
| 63 | + } |
| 64 | + |
| 65 | + public function executeQuery() |
| 66 | + { |
| 67 | + $this->lastSeenOperationTime = null; |
| 68 | + |
| 69 | + MongoDB\Driver\Monitoring\addSubscriber($this); |
| 70 | + |
| 71 | + $manager = new MongoDB\Driver\Manager(REPLICASET); |
| 72 | + $session = $manager->startSession(); |
| 73 | + |
| 74 | + $query = new MongoDB\Driver\Query(['$unsupportedOperator' => 1]); |
| 75 | + |
| 76 | + throws(function() use ($manager, $query, $session) { |
| 77 | + $manager->executeQuery(NS, $query, ['session' => $session]); |
| 78 | + }, 'MongoDB\Driver\Exception\RuntimeException'); |
| 79 | + |
| 80 | + /* We cannot access the server reply if an exception is thrown for a |
| 81 | + * failed command (see: PHPC-1076). For the time being, just assert that |
| 82 | + * the operationTime is not null. */ |
| 83 | + printf("Session has non-null operationTime: %s\n", ($session->getOperationTime() !== null ? 'yes' : 'no')); |
| 84 | + |
| 85 | + MongoDB\Driver\Monitoring\removeSubscriber($this); |
| 86 | + } |
| 87 | + |
| 88 | + public function commandStarted(MongoDB\Driver\Monitoring\CommandStartedEvent $event) |
| 89 | + { |
| 90 | + } |
| 91 | + |
| 92 | + public function commandSucceeded(MongoDB\Driver\Monitoring\CommandSucceededEvent $event) |
| 93 | + { |
| 94 | + $reply = $event->getReply(); |
| 95 | + $hasOperationTime = isset($reply->operationTime); |
| 96 | + |
| 97 | + printf("%s command reply includes operationTime: %s\n", $event->getCommandName(), $hasOperationTime ? 'yes' : 'no'); |
| 98 | + |
| 99 | + if ($hasOperationTime) { |
| 100 | + $this->lastSeenOperationTime = $reply->operationTime; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public function commandFailed(MongoDB\Driver\Monitoring\CommandFailedEvent $event) |
| 105 | + { |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +echo "Testing executeBulkWrite()\n"; |
| 110 | +(new Test)->executeBulkWrite(); |
| 111 | + |
| 112 | +echo "\nTesting executeCommand()\n"; |
| 113 | +(new Test)->executeCommand(); |
| 114 | + |
| 115 | +echo "\nTesting executeQuery()\n"; |
| 116 | +(new Test)->executeQuery(); |
| 117 | + |
| 118 | +?> |
| 119 | +===DONE=== |
| 120 | +<?php exit(0); ?> |
| 121 | +--EXPECT-- |
| 122 | +Testing executeBulkWrite() |
| 123 | +insert command reply includes operationTime: yes |
| 124 | +OK: Got MongoDB\Driver\Exception\BulkWriteException |
| 125 | +Session reports last seen operationTime: yes |
| 126 | + |
| 127 | +Testing executeCommand() |
| 128 | +OK: Got MongoDB\Driver\Exception\RuntimeException |
| 129 | +Session has non-null operationTime: yes |
| 130 | + |
| 131 | +Testing executeQuery() |
| 132 | +OK: Got MongoDB\Driver\Exception\RuntimeException |
| 133 | +Session has non-null operationTime: yes |
| 134 | +===DONE=== |
0 commit comments