File: mongoclient-construct-002.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 (80 lines) | stat: -rw-r--r-- 2,505 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
--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_server_type($server) {
    printf("Server type: %s (%d)\n", $server["type"] == 2 ? "PRIMARY" : ($server["type"] == 4 ? "SECONDARY" : "UNKNOWN"), $server["type"]);
}

$ctx = stream_context_create(array("mongodb" => array(
    "log_query" => "log_server_type",
    "log_cmd_insert" => "log_server_type",
)));

$mc = new MongoClient($rs["dsn"], array("replicaSet" => $rs["rsname"]), array("context" => $ctx));

$coll = $mc->selectCollection("ctorfailover", "test2");
$coll->drop();
$data = array("x" => "The world is not enough");
$coll->insert($data, array('w' => 'majority'));
$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--
Server type: PRIMARY (2)
Server type: PRIMARY (2)
About to kill master
Master killed
Attempting insert
Server type: PRIMARY (2)
string(20) "MongoCursorException"
string(%d) "%s:%d: Remote server has closed the connection"
int(32)
Doing secondary read
Server type: SECONDARY (4)
Doing primary read, should fail since we don't have primary
string(24) "MongoConnectionException"
string(26) "No candidate servers found"
int(71)
bool(false)