Skip to content

Commit 4abf4a7

Browse files
committed
Harden test against non-deterministic return order
1 parent 7ecb58c commit 4abf4a7

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

tests/server/server-getTags-002.phpt

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,29 @@ $manager = new MongoDB\Driver\Manager(URI);
1212
$command = new MongoDB\Driver\Command(['ping' => 1]);
1313
$manager->executeCommand(DATABASE_NAME, $command);
1414

15-
foreach ($manager->getServers() as $server) {
16-
$tags = $server->getTags();
17-
echo "dc: ", array_key_exists('dc', $tags) ? $tags['dc'] : 'not set', "\n";
18-
echo "ordinal: ", array_key_exists('ordinal', $tags) ? $tags['ordinal'] : 'not set', "\n";
15+
function assertSomeServerHasTags(array $servers, array $expectedTags) {
16+
foreach ($servers as $server) {
17+
/* Using a non-strict comparison guards against tags being returned in
18+
* a different order than expected. */
19+
if ($expectedTags == $server->getTags()) {
20+
printf("Found server with tags: %s\n", json_encode($expectedTags));
21+
return;
22+
}
23+
}
24+
25+
printf("No server has tags: %s\n", json_encode($expectedTags));
1926
}
2027

28+
$servers = $manager->getServers();
29+
assertSomeServerHasTags($servers, ['dc' => 'ny', 'ordinal' => 'one']);
30+
assertSomeServerHasTags($servers, ['dc' => 'pa', 'ordinal' => 'two']);
31+
assertSomeServerHasTags($servers, []);
32+
2133
?>
2234
===DONE===
2335
<?php exit(0); ?>
2436
--EXPECT--
25-
dc: ny
26-
ordinal: one
27-
dc: pa
28-
ordinal: two
29-
dc: not set
30-
ordinal: not set
37+
Found server with tags: {"dc":"ny","ordinal":"one"}
38+
Found server with tags: {"dc":"pa","ordinal":"two"}
39+
Found server with tags: []
3140
===DONE===

0 commit comments

Comments
 (0)