File: mongoclient-construct-002.phpt

package info (click to toggle)
php-mongo 1.4.5-1~bpo70%2B1
  • links: PTS
  • area: main
  • in suites: wheezy-backports
  • size: 9,828 kB
  • sloc: ansic: 13,275; xml: 1,702; php: 1,625; pascal: 255; makefile: 106; sh: 27
file content (163 lines) | stat: -rw-r--r-- 3,428 bytes parent folder | download
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
--TEST--
MongoClient::__construct() during failover (2)
--SKIPIF--
<?php if (!MONGO_STREAMS) { echo "skip This test requires streams support"; } ?>
<?php require_once "tests/utils/replicaset-failover.inc" ?>
--FILE--
<?php
require_once "tests/utils/server.inc";

$server = new MongoShellServer;
$rs = $server->getReplicasetConfig();

function log_query($server, $query, $cursor_options) {
    var_dump($server, $query, $cursor_options);
}
$ctx = stream_context_create(
    array(
        "mongodb" => array(
            "log_query" => "log_query",
        )
    )
);
$mc = new MongoClient($rs["dsn"], array("replicaSet" => $rs["rsname"]), array("context" => $ctx));

$coll = $mc->selectCollection("ctorfailover", "test1");
$data = array("x" => "The world is not enough");
$coll->insert($data);
$id = $data["_id"];

echo "About to kill master\n";
$server->killMaster();
echo "Master killed\n";


$t = time();
try {
    echo "Attempting insert\n";
    $coll->insert($data);
    echo "failed, somehow managed to insert when no primary was found\n";
} catch(Exception $e) {
    var_dump(get_class($e), $e->getMessage(), $e->getCode());
}

echo "Doing secondary read\n";
$data = $coll->setReadPreference(MongoClient::RP_SECONDARY); 
$coll->findOne(array("_id" => $id));

echo "Doing primary read, should fail since we don't have primary\n";
try {
    $coll->setReadPreference(MongoClient::RP_PRIMARY); 
    $coll->findOne(array("_id" => $id));
    echo "Did a primary read without a primary?!\n";
} catch(Exception $e) {
    var_dump(get_class($e), $e->getMessage(), $e->getCode());
}
// Since the cleanup is part of this test it can take a while.. this section 
// should definitily not take more then 3secs
// The only reason we have this here though is to verify we aren't wasting 
// time in attempting to reconnect to master and blocking
var_dump(time()-$t > 3);
?>
--CLEAN--
<?php require_once "tests/utils/fix-master.inc"; ?>
--EXPECTF--
array(5) {
  ["hash"]=>
  string(%d) "%s:%d;REPLICASET;.;%d"
  ["type"]=>
  int(2)
  ["max_bson_size"]=>
  int(16777216)
  ["max_message_size"]=>
  int(%d)
  ["request_id"]=>
  int(%d)
}
array(1) {
  ["getlasterror"]=>
  int(1)
}
array(5) {
  ["request_id"]=>
  int(4)
  ["skip"]=>
  int(0)
  ["limit"]=>
  int(-1)
  ["options"]=>
  int(0)
  ["cursor_id"]=>
  int(0)
}
About to kill master
Master killed
Attempting insert
array(5) {
  ["hash"]=>
  string(%d) "%s:%d;REPLICASET;.;%d"
  ["type"]=>
  int(2)
  ["max_bson_size"]=>
  int(16777216)
  ["max_message_size"]=>
  int(%d)
  ["request_id"]=>
  int(%d)
}
array(1) {
  ["getlasterror"]=>
  int(1)
}
array(5) {
  ["request_id"]=>
  int(6)
  ["skip"]=>
  int(0)
  ["limit"]=>
  int(-1)
  ["options"]=>
  int(0)
  ["cursor_id"]=>
  int(0)
}
string(20) "MongoCursorException"
string(%d) "%s:%d: Remote server has closed the connection"
int(32)
Doing secondary read
array(5) {
  ["hash"]=>
  string(%d) "%s:%d;REPLICASET;.;%d"
  ["type"]=>
  int(4)
  ["max_bson_size"]=>
  int(16777216)
  ["max_message_size"]=>
  int(%d)
  ["request_id"]=>
  int(%d)
}
array(1) {
  ["_id"]=>
  object(MongoId)#%d (1) {
    ["$id"]=>
    string(24) "%s"
  }
}
array(5) {
  ["request_id"]=>
  int(7)
  ["skip"]=>
  int(0)
  ["limit"]=>
  int(-1)
  ["options"]=>
  int(4)
  ["cursor_id"]=>
  int(0)
}
Doing primary read, should fail since we don't have primary
string(24) "MongoConnectionException"
string(26) "No candidate servers found"
int(71)
bool(false)