File: bug00779.phpt

package info (click to toggle)
php-mongo 1.5.7-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,040 kB
  • ctags: 2,802
  • sloc: ansic: 17,632; xml: 2,195; php: 1,630; pascal: 330; makefile: 52; sh: 39
file content (120 lines) | stat: -rw-r--r-- 3,444 bytes parent folder | download | duplicates (2)
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
--TEST--
Test for PHP-779: Non-primary read preferences should set slaveOk bit
--SKIPIF--
<?php if (!MONGO_STREAMS) { echo "skip This test requires streams support"; } ?>
<?php require_once 'tests/utils/replicaset.inc' ?>
--FILE--
<?php
require_once 'tests/utils/server.inc';

$rs = MongoShellServer::getReplicasetInfo();
$mc = new MongoClient($rs['dsn'], array('replicaSet' => $rs['rsname']));

$c = $mc->selectCollection(dbname(), 'bug00779');
$c->drop();
$c->insert(array('x' => 1), array('w' => 'majority'));

function check_slaveOkay($server, $query, $cursor_options)
{
    printf("Bit 2 (SlaveOk) is%s set\n", ($cursor_options['options'] & 1 << 2) ? '' : ' not');
}

$ctx = stream_context_create(array('mongodb' => array('log_query' => 'check_slaveOkay')));

// Connect to secondary in standalone mode
$mc = new MongoClient($rs['hosts'][1], array(), array('context' => $ctx));

$coll = $mc->selectCollection(dbname(), 'bug00779');

echo "Testing primary query with MongoCursor::setReadPreference()\n";
$cursor = $coll->find();
$cursor->setReadPreference(MongoClient::RP_PRIMARY);
try {
    iterator_to_array($cursor);
} catch (MongoCursorException $e) {
    var_dump($e->getMessage(), $e->getCode());
}

echo "\nTesting primary query with MongoCursor::setFlag()\n";
$cursor = $coll->find();
$cursor->setFlag(2, false);
try {
    iterator_to_array($cursor);
} catch (MongoCursorException $e) {
    var_dump($e->getMessage(), $e->getCode());
}

echo "\nTesting primary query\n";
$cursor = $coll->find();
try {
    iterator_to_array($cursor);
} catch (MongoCursorException $e) {
    var_dump($e->getMessage(), $e->getCode());
}

echo "\nTesting primary count\n";
try {
    /* TODO: this will return an error document instead of throwing an
     * exception. Fix the expected output once PHP-781 is resolved.
     */
    $coll->count();
} catch (MongoException $e) {
    var_dump($e->getMessage(), $e->getCode());
}

echo "\n----\n";

echo "\nTesting non-primary query with MongoCursor::setReadPreference()\n";
$cursor = $coll->find();
$cursor->setReadPreference(MongoClient::RP_SECONDARY_PREFERRED);
iterator_to_array($cursor);

echo "\nTesting non-primary query with MongoCursor::setFlag()\n";
$cursor = $coll->find();
$cursor->setFlag(2);
iterator_to_array($cursor);

echo "\nTesting non-primary query with MongoCollection::setReadPreference()\n";
$coll->setReadPreference(MongoClient::RP_SECONDARY_PREFERRED);
$cursor = $coll->find();
iterator_to_array($cursor);

echo "\nTesting non-primary count with MongoCollection::setReadPreference()\n";
$coll->count();

?>
--EXPECTF--

Testing primary query with MongoCursor::setReadPreference()
Bit 2 (SlaveOk) is not set
string(%d) "%s:%d: not master and slaveOk=false"
int(13435)

Testing primary query with MongoCursor::setFlag()
Bit 2 (SlaveOk) is not set
string(%d) "%s:%d: not master and slaveOk=false"
int(13435)

Testing primary query
Bit 2 (SlaveOk) is not set
string(%d) "%s:%d: not master and slaveOk=false"
int(13435)

Testing primary count
Bit 2 (SlaveOk) is not set
string(%d) "Cannot run command count(): not master"
int(20)

----

Testing non-primary query with MongoCursor::setReadPreference()
Bit 2 (SlaveOk) is set

Testing non-primary query with MongoCursor::setFlag()
Bit 2 (SlaveOk) is set

Testing non-primary query with MongoCollection::setReadPreference()
Bit 2 (SlaveOk) is set

Testing non-primary count with MongoCollection::setReadPreference()
Bit 2 (SlaveOk) is set