File: bug1274-003.phpt

package info (click to toggle)
php-mongodb 1.15.0%2B1.11.1%2B1.9.2%2B1.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 63,984 kB
  • sloc: ansic: 328,429; xml: 10,797; php: 4,235; sh: 179; python: 47; pascal: 36; makefile: 3
file content (97 lines) | stat: -rw-r--r-- 2,913 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
--TEST--
PHPC-1274: Child process should not reset parent client multiple times
--SKIPIF--
<?php if (!function_exists('pcntl_fork')) { die('skip pcntl_fork() not available'); } ?>
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php skip_if_not_live(); ?>
<?php skip_if_not_clean(); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

class CommandLogger implements MongoDB\Driver\Monitoring\CommandSubscriber
{
    private $pid;

    public function __construct()
    {
        $this->pid = getmypid();
    }

    public function commandStarted(MongoDB\Driver\Monitoring\CommandStartedEvent $event)
    {
        $command = $event->getCommand();
        $commandName = $event->getCommandName();
        $process = $this->pid === getmypid() ? 'Parent' : 'Child';

        if ($commandName === 'find' || $commandName === 'getMore') {
            printf("%s executes %s with batchSize: %d\n", $process, $commandName, $command->batchSize);
            return;
        }

        printf("%s executes %s\n", $process, $commandName);
    }

    public function commandSucceeded(MongoDB\Driver\Monitoring\CommandSucceededEvent $event)
    {
    }

    public function commandFailed(MongoDB\Driver\Monitoring\CommandFailedEvent $event)
    {
    }
}

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

$bulk = new MongoDB\Driver\BulkWrite();
$bulk->insert(['x' => 1]);
$bulk->insert(['x' => 2]);
$bulk->insert(['x' => 3]);
$manager->executeBulkWrite(NS, $bulk);

MongoDB\Driver\Monitoring\addSubscriber(new CommandLogger);

$query = new MongoDB\Driver\Query([], ['batchSize' => 2]);
$cursor = $manager->executeQuery(NS, $query);

$childPid = pcntl_fork();

if ($childPid === 0) {
    /* By having the child execute a query with the parent's client, unsetting
     * the parent's cursor, and then iterating the child's cursor, we test that
     * the parent's client is reset only once and prior to execution of the
     * child's find and getMore commands. If the client were reset multiple
     * times (i.e. between find and getMore), mongoc_cursor_next() would report
     * an error for advancing the cursor after resetting its client. */
    $childCursor = $manager->executeQuery(NS, $query);

    // Unsetting the parent's cursor should not reset its client a second time
    unset($cursor);

    printf("Child fully iterated cursor for %d documents\n", iterator_count($childCursor));
    echo "Child exits\n";
    exit;
}

if ($childPid > 0) {
    $waitPid = pcntl_waitpid($childPid, $status);

    if ($waitPid === $childPid) {
        echo "Parent waited for child to exit\n";
    }

    unset($cursor);
}

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
Parent executes find with batchSize: 2
Child executes find with batchSize: 2
Child executes getMore with batchSize: 2
Child fully iterated cursor for 3 documents
Child exits
Parent waited for child to exit
Parent executes killCursors
===DONE===