@@ -148,21 +148,33 @@ function get_server_storage_engine($uri)
148
148
}
149
149
150
150
/**
151
- * Returns the version of the primary server.
151
+ * Helper to return the version of a specific server.
152
152
*
153
- * @param string $uri Connection string
153
+ * @param MongoDB\Driver\Server $server
154
154
* @return string
155
155
* @throws RuntimeException
156
156
*/
157
- function get_server_version ( $ uri )
157
+ function get_server_version_from_server ( MongoDB \ Driver \ Server $ server )
158
158
{
159
- $ server = get_primary_server ($ uri );
160
159
$ command = new Command (['buildInfo ' => 1 ]);
161
160
$ cursor = $ server ->executeCommand ('admin ' , $ command );
162
161
163
162
return current ($ cursor ->toArray ())->version ;
164
163
}
165
164
165
+ /**
166
+ * Returns the version of the primary server.
167
+ *
168
+ * @param string $uri Connection string
169
+ * @return string
170
+ * @throws RuntimeException
171
+ */
172
+ function get_server_version ($ uri )
173
+ {
174
+ $ server = get_primary_server ($ uri );
175
+ return get_server_version_from_server ($ server );
176
+ }
177
+
166
178
/**
167
179
* Returns the value of a URI option, or null if it's not found.
168
180
*
@@ -441,11 +453,19 @@ function loadFixtures(\MongoDB\Driver\Manager $manager, $dbname = DATABASE_NAME,
441
453
}
442
454
}
443
455
444
- function START ($ id , array $ options = array ()) {
445
- /* starting/stopping servers only works using the Vagrant setup */
446
- PREDICTABLE ();
447
-
448
- $ options += array ("name " => "mongod " , "id " => $ id );
456
+ function createTemporaryMongoInstance (array $ options = [])
457
+ {
458
+ $ id = 'mo_ ' . COLLECTION_NAME ;
459
+ $ options += [
460
+ "name " => "mongod " ,
461
+ "id " => $ id ,
462
+ 'procParams ' => [
463
+ 'dbpath ' => "/tmp/phongo/ {$ id }" ,
464
+ 'logpath ' => "/tmp/phongo/ {$ id }.log " ,
465
+ 'ipv6 ' => true ,
466
+ 'setParameter ' => [ 'enableTestCommands ' => 1 ],
467
+ ],
468
+ ];
449
469
$ opts = array (
450
470
"http " => array (
451
471
"timeout " => 60 ,
@@ -457,24 +477,24 @@ function START($id, array $options = array()) {
457
477
),
458
478
);
459
479
$ ctx = stream_context_create ($ opts );
460
- $ json = file_get_contents (getMOUri () . "/servers/ $ id " , false , $ ctx );
480
+ $ json = file_get_contents (MONGO_ORCHESTRATION_URI . "/servers/ $ id " , false , $ ctx );
461
481
$ result = json_decode ($ json , true );
462
482
463
483
/* Failed -- or was already started */
464
484
if (!isset ($ result ["mongodb_uri " ])) {
465
- DELETE ($ id );
466
- define ( $ id , false );
485
+ destroyTemporaryMongoInstance ($ id );
486
+ throw new Exception ( " Could not start temporary server instance \n" );
467
487
} else {
468
- define ($ id , $ result ["mongodb_uri " ]);
469
- $ FILENAME = sys_get_temp_dir () . "/PHONGO-SERVERS.json " ;
470
-
471
- $ json = file_get_contents ($ FILENAME );
472
- $ config = json_decode ($ json , true );
473
- $ config [$ id ] = constant ($ id );
474
- file_put_contents ($ FILENAME , json_encode ($ config , JSON_PRETTY_PRINT ));
488
+ return $ result ['mongodb_uri ' ];
475
489
}
476
490
}
477
- function DELETE ($ id ) {
491
+
492
+ function destroyTemporaryMongoInstance ($ id = NULL )
493
+ {
494
+ if ($ id == NULL ) {
495
+ $ id = 'mo_ ' . COLLECTION_NAME ;
496
+ }
497
+
478
498
$ opts = array (
479
499
"http " => array (
480
500
"timeout " => 60 ,
@@ -484,14 +504,9 @@ function DELETE($id) {
484
504
),
485
505
);
486
506
$ ctx = stream_context_create ($ opts );
487
- $ json = file_get_contents (getMOUri () . "/servers/ $ id " , false , $ ctx );
488
- $ FILENAME = sys_get_temp_dir () . "/PHONGO-SERVERS.json " ;
489
-
490
- $ json = file_get_contents ($ FILENAME );
491
- $ config = json_decode ($ json , true );
492
- unset($ config [$ id ]);
493
- file_put_contents ($ FILENAME , json_encode ($ config , JSON_PRETTY_PRINT ));
507
+ $ json = file_get_contents (MONGO_ORCHESTRATION_URI . "/servers/ $ id " , false , $ ctx );
494
508
}
509
+
495
510
function severityToString ($ type ) {
496
511
switch ($ type ) {
497
512
case E_WARNING :
@@ -629,41 +644,23 @@ function def($arr) {
629
644
}
630
645
}
631
646
632
- function configureFailPoint (MongoDB \Driver \Manager $ manager , $ failPoint , $ mode , $ data = array ()) {
633
-
647
+ function configureFailPoint (MongoDB \Driver \Manager $ manager , $ failPoint , $ mode , array $ data = [])
648
+ {
634
649
$ doc = array (
635
- " configureFailPoint " => $ failPoint ,
636
- " mode " => $ mode ,
650
+ ' configureFailPoint ' => $ failPoint ,
651
+ ' mode ' => $ mode ,
637
652
);
638
653
if ($ data ) {
639
- $ doc [" data " ] = $ data ;
654
+ $ doc [' data ' ] = $ data ;
640
655
}
641
656
642
657
$ cmd = new MongoDB \Driver \Command ($ doc );
643
- $ result = $ manager ->executeCommand ("admin " , $ cmd );
644
- $ arr = current ($ result ->toArray ());
645
- if (empty ($ arr ->ok )) {
646
- var_dump ($ result );
647
- throw new RuntimeException ("Failpoint failed " );
648
- }
649
- return true ;
658
+ $ manager ->executeCommand ('admin ' , $ cmd );
650
659
}
651
660
652
- function failMaxTimeMS (MongoDB \Driver \Manager $ manager ) {
653
- return configureFailPoint ($ manager , "maxTimeAlwaysTimeOut " , array ("times " => 1 ));
654
- }
655
-
656
- function getMOUri () {
657
- if (!($ HOST = getenv ("MONGODB_ORCHESTRATION_HOST " ))) {
658
- $ HOST = "192.168.112.10 " ;
659
- }
660
-
661
- if (!($ PORT = getenv ("MONGODB_ORCHESTRATION_PORT " ))) {
662
- $ PORT = "8889 " ;
663
- }
664
- $ MO = "http:// $ HOST : $ PORT /v1 " ;
665
-
666
- return $ MO ;
661
+ function failMaxTimeMS (MongoDB \Driver \Manager $ manager )
662
+ {
663
+ configureFailPoint ($ manager , 'maxTimeAlwaysTimeOut ' , [ 'times ' => 1 ]);
667
664
}
668
665
669
666
function getMOPresetBase () {
@@ -695,6 +692,26 @@ function fromJSON($var) {
695
692
696
693
/* Note: this fail point may terminate the mongod process, so you may want to
697
694
* use this in conjunction with a throwaway server. */
698
- function failGetMore (MongoDB \Driver \Manager $ manager ) {
699
- return configureFailPoint ($ manager , "failReceivedGetmore " , "alwaysOn " );
695
+ function failGetMore (MongoDB \Driver \Manager $ manager )
696
+ {
697
+ /* We need to do version detection here */
698
+ $ primary = $ manager ->selectServer (new ReadPreference ('primary ' ));
699
+ $ version = get_server_version_from_server ($ primary );
700
+
701
+ if (version_compare ($ version , "3.2 " , "< " )) {
702
+ configureFailPoint ($ manager , 'failReceivedGetmore ' , 'alwaysOn ' );
703
+ return ;
704
+ }
705
+
706
+ if (version_compare ($ version , "4.0 " , ">= " )) {
707
+ /* We use 237 here, as that's the same original code that MongoD would
708
+ * throw if a cursor had already gone by the time we call getMore. This
709
+ * allows us to make things consistent with the getMore OP behaviour
710
+ * from previous mongod versions. An errorCode is required here for the
711
+ * failPoint to work. */
712
+ configureFailPoint ($ manager , 'failCommand ' , 'alwaysOn ' , [ 'errorCode ' => 237 , 'failCommands ' => ['getMore ' ] ]);
713
+ return ;
714
+ }
715
+
716
+ throw new Exception ("Trying to configure a getMore fail point for a server version ( $ version) that doesn't support it " );
700
717
}
0 commit comments