File: session-002.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 (173 lines) | stat: -rw-r--r-- 5,904 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
--TEST--
MongoDB\Driver\Session spec test: $clusterTime in commands
--SKIPIF--
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
<?php skip_if_not_libmongoc_crypto(); ?>
<?php skip_if_not_replica_set_or_mongos_with_replica_set(); ?>
<?php skip_if_server_version('<', '3.6'); ?>
<?php skip_if_not_clean(); ?>
--FILE--
<?php
require_once __DIR__ . "/../utils/basic.inc";

class Test implements MongoDB\Driver\Monitoring\CommandSubscriber
{
    private $lastSeenClusterTime;

    public function aggregate()
    {
        $this->lastSeenClusterTime = null;

        MongoDB\Driver\Monitoring\addSubscriber($this);

        $manager = new MongoDB\Driver\Manager(URI);
        $session = $manager->startSession();

        $command = new MongoDB\Driver\Command([
            'aggregate' => COLLECTION_NAME,
            'pipeline' => [],
            'cursor' => new stdClass(),
        ]);
        $manager->executeReadWriteCommand(DATABASE_NAME, $command, ['session' => $session]);
        $manager->executeReadWriteCommand(DATABASE_NAME, $command, ['session' => $session]);

        printf("Session reports last seen \$clusterTime: %s\n", ($session->getClusterTime() == $this->lastSeenClusterTime) ? 'yes' : 'no');

        MongoDB\Driver\Monitoring\removeSubscriber($this);
    }

    public function find()
    {
        $this->lastSeenClusterTime = null;

        MongoDB\Driver\Monitoring\addSubscriber($this);

        $manager = new MongoDB\Driver\Manager(URI);
        $session = $manager->startSession();

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

        printf("Session reports last seen \$clusterTime: %s\n", ($session->getClusterTime() == $this->lastSeenClusterTime) ? 'yes' : 'no');

        MongoDB\Driver\Monitoring\removeSubscriber($this);
    }

    public function insert()
    {
        $this->lastSeenClusterTime = null;

        MongoDB\Driver\Monitoring\addSubscriber($this);

        $manager = new MongoDB\Driver\Manager(URI);
        $session = $manager->startSession();

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

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

        printf("Session reports last seen \$clusterTime: %s\n", ($session->getClusterTime() == $this->lastSeenClusterTime) ? 'yes' : 'no');

        MongoDB\Driver\Monitoring\removeSubscriber($this);
    }

    public function ping()
    {
        $this->lastSeenClusterTime = null;

        MongoDB\Driver\Monitoring\addSubscriber($this);

        $manager = new MongoDB\Driver\Manager(URI);
        $session = $manager->startSession();

        $command = new MongoDB\Driver\Command(['ping' => 1]);
        $manager->executeCommand(DATABASE_NAME, $command, ['session' => $session]);
        $manager->executeCommand(DATABASE_NAME, $command, ['session' => $session]);

        printf("Session reports last seen \$clusterTime: %s\n", ($session->getClusterTime() == $this->lastSeenClusterTime) ? 'yes' : 'no');

        MongoDB\Driver\Monitoring\removeSubscriber($this);
    }

    public function commandStarted(MongoDB\Driver\Monitoring\CommandStartedEvent $event)
    {
        $command = $event->getCommand();
        $hasClusterTime = isset($command->{'$clusterTime'});

        printf("%s command includes \$clusterTime: %s\n", $event->getCommandName(), $hasClusterTime ? 'yes' : 'no');

        if ($hasClusterTime && $this->lastSeenClusterTime !== null) {
            printf("%s command uses last seen \$clusterTime: %s\n", $event->getCommandName(), ($command->{'$clusterTime'} == $this->lastSeenClusterTime) ? 'yes' : 'no');
        }
    }

    public function commandSucceeded(MongoDB\Driver\Monitoring\CommandSucceededEvent $event)
    {
        $reply = $event->getReply();
        $hasClusterTime = isset($reply->{'$clusterTime'});

        printf("%s command reply includes \$clusterTime: %s\n", $event->getCommandName(), $hasClusterTime ? 'yes' : 'no');

        if ($hasClusterTime) {
            $this->lastSeenClusterTime = $reply->{'$clusterTime'};
        }
    }

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

echo "\nTesting aggregate command\n";
(new Test)->aggregate();

echo "\nTesting find command\n";
(new Test)->find();

echo "\nTesting insert command\n";
(new Test)->insert();

echo "\nTesting ping command\n";
(new Test)->ping();

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
Testing aggregate command
aggregate command includes $clusterTime: yes
aggregate command reply includes $clusterTime: yes
aggregate command includes $clusterTime: yes
aggregate command uses last seen $clusterTime: yes
aggregate command reply includes $clusterTime: yes
Session reports last seen $clusterTime: yes

Testing find command
find command includes $clusterTime: yes
find command reply includes $clusterTime: yes
find command includes $clusterTime: yes
find command uses last seen $clusterTime: yes
find command reply includes $clusterTime: yes
Session reports last seen $clusterTime: yes

Testing insert command
insert command includes $clusterTime: yes
insert command reply includes $clusterTime: yes
insert command includes $clusterTime: yes
insert command uses last seen $clusterTime: yes
insert command reply includes $clusterTime: yes
Session reports last seen $clusterTime: yes

Testing ping command
ping command includes $clusterTime: yes
ping command reply includes $clusterTime: yes
ping command includes $clusterTime: yes
ping command uses last seen $clusterTime: yes
ping command reply includes $clusterTime: yes
Session reports last seen $clusterTime: yes
===DONE===