Skip to content

Commit f812fca

Browse files
committed
Retry Distributed AMQP Tests
1 parent 2e00fde commit f812fca

File tree

3 files changed

+99
-79
lines changed

3 files changed

+99
-79
lines changed

tests/Common/IntegrationTestCase.php

+18
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ abstract class IntegrationTestCase extends BaseTestCase
1919
public static $database = "test";
2020
private static $createdDatabases = ["test" => true];
2121

22+
protected static $maxRetries = 3;
23+
2224
public static function ddSetUpBeforeClass()
2325
{
2426
parent::ddSetUpBeforeClass();
@@ -113,4 +115,20 @@ public function assertOneSpan($traces, SpanAssertion $expectedSpan)
113115
{
114116
$this->assertOneExpectedSpan($traces, $expectedSpan);
115117
}
118+
119+
protected function retryTest(callable $testCase, ...$args)
120+
{
121+
$attempts = 0;
122+
while ($attempts < self::$maxRetries) {
123+
try {
124+
$testCase(...$args);
125+
return; // Test passed, exit the loop.
126+
} catch (\Throwable $e) {
127+
$attempts++;
128+
if ($attempts >= self::$maxRetries) {
129+
throw $e; // Re-throw after max retries.
130+
}
131+
}
132+
}
133+
}
116134
}

tests/Integrations/AMQP/V2/AMQPTest.php

+81-78
Original file line numberDiff line numberDiff line change
@@ -878,92 +878,95 @@ public function testDistributedTracing()
878878
{
879879
// Note: This test is extremely flaky, locally at least. It will eventually pass with some tries...
880880
// Reason: We may parse the traces from dumped data BEFORE the traces are flushed.
881-
882-
self::putEnv('DD_TRACE_DEBUG_PRNG_SEED=42'); // Not necessary, but makes it easier to debug locally
883-
884-
$sendTraces = $this->inCli(
885-
__DIR__ . '/../scripts/send.php',
886-
[
887-
'DD_TRACE_AUTO_FLUSH_ENABLED' => 'true',
888-
'DD_TRACE_GENERATE_ROOT_SPAN' => 'true',
889-
'DD_TRACE_CLI_ENABLED' => 'true',
890-
],
891-
[],
892-
self::$autoloadPath
893-
);
894-
895-
list($receiveTraces, $output) = $this->inCli(
896-
__DIR__ . '/../scripts/receive.php',
897-
[
898-
'DD_TRACE_AUTO_FLUSH_ENABLED' => 'true',
899-
'DD_TRACE_GENERATE_ROOT_SPAN' => 'false',
900-
'DD_TRACE_CLI_ENABLED' => 'true',
901-
],
902-
[],
903-
self::$autoloadPath,
904-
true
905-
);
906-
907-
// Assess that user headers weren't lost
908-
$this->assertSame("", trim(preg_replace("(.*\[ddtrace].*)", "", $output)));
909-
910-
$sendTraces = $sendTraces[0][0]; // There is a root span
911-
// Spans: send.php -> basic_publish -> queue_declare -> connect
912-
$basicPublishSpan = $sendTraces[1];
913-
914-
foreach ($receiveTraces as $receiveTrace) {
915-
// Spans: connect -> queue_declare -> basic_consume & basic_consume_ok -> basic_deliver
916-
if ($receiveTrace[0]["name"] == "amqp.basic.deliver") {
917-
$basicDeliverSpan = $receiveTrace[0];
918-
break;
881+
$this->retryTest(function () {
882+
self::putEnv('DD_TRACE_DEBUG_PRNG_SEED=42'); // Not necessary, but makes it easier to debug locally
883+
884+
$sendTraces = $this->inCli(
885+
__DIR__ . '/../scripts/send.php',
886+
[
887+
'DD_TRACE_AUTO_FLUSH_ENABLED' => 'true',
888+
'DD_TRACE_GENERATE_ROOT_SPAN' => 'true',
889+
'DD_TRACE_CLI_ENABLED' => 'true',
890+
],
891+
[],
892+
self::$autoloadPath
893+
);
894+
895+
list($receiveTraces, $output) = $this->inCli(
896+
__DIR__ . '/../scripts/receive.php',
897+
[
898+
'DD_TRACE_AUTO_FLUSH_ENABLED' => 'true',
899+
'DD_TRACE_GENERATE_ROOT_SPAN' => 'false',
900+
'DD_TRACE_CLI_ENABLED' => 'true',
901+
],
902+
[],
903+
self::$autoloadPath,
904+
true
905+
);
906+
907+
// Assess that user headers weren't lost
908+
$this->assertSame("", trim(preg_replace("(.*\[ddtrace].*)", "", $output)));
909+
910+
$sendTraces = $sendTraces[0][0]; // There is a root span
911+
// Spans: send.php -> basic_publish -> queue_declare -> connect
912+
$basicPublishSpan = $sendTraces[1];
913+
914+
foreach ($receiveTraces as $receiveTrace) {
915+
// Spans: connect -> queue_declare -> basic_consume & basic_consume_ok -> basic_deliver
916+
if ($receiveTrace[0]["name"] == "amqp.basic.deliver") {
917+
$basicDeliverSpan = $receiveTrace[0];
918+
break;
919+
}
919920
}
920-
}
921921

922-
$this->assertSame($basicPublishSpan['trace_id'], $basicDeliverSpan['trace_id']);
923-
$this->assertSame($basicPublishSpan['span_id'], $basicDeliverSpan['parent_id']);
922+
$this->assertSame($basicPublishSpan['trace_id'], $basicDeliverSpan['trace_id']);
923+
$this->assertSame($basicPublishSpan['span_id'], $basicDeliverSpan['parent_id']);
924+
});
924925
}
925926

926927
public function testDistributedTracingIsNotPropagatedIfDisabled()
927928
{
928929
self::putEnv('DD_TRACE_DEBUG_PRNG_SEED=42'); // Not necessary, but makes it easier to debug locally
929930

930-
$sendTraces = $this->inCli(
931-
__DIR__ . '/../scripts/send.php',
932-
[
933-
'DD_TRACE_AUTO_FLUSH_ENABLED' => 'true',
934-
'DD_TRACE_GENERATE_ROOT_SPAN' => 'true',
935-
'DD_TRACE_CLI_ENABLED' => 'true',
936-
'DD_DISTRIBUTED_TRACING' => 'false'
937-
],
938-
[],
939-
self::$autoloadPath
940-
);
941-
942-
list($receiveTraces, $output) = $this->inCli(
943-
__DIR__ . '/../scripts/receive.php',
944-
[
945-
'DD_TRACE_AUTO_FLUSH_ENABLED' => 'true',
946-
'DD_TRACE_GENERATE_ROOT_SPAN' => 'false',
947-
'DD_TRACE_CLI_ENABLED' => 'true'
948-
],
949-
[],
950-
self::$autoloadPath,
951-
true
952-
);
953-
954-
// Assess that user headers weren't lost
955-
$this->assertSame("", trim(preg_replace("(.*\[ddtrace].*)", "", $output)));
956-
957-
$sendTraces = $sendTraces[0][0]; // There is a root span
958-
// Spans: send.php -> basic_publish -> queue_declare -> connect
959-
$basicPublishSpan = $sendTraces[1];
960-
961-
$receiveTraces = $receiveTraces[3]; // There isn't a root span
962-
// Spans: connect -> queue_declare -> basic_consume & basic_consume_ok -> basic_deliver
963-
$basicDeliverSpan = $receiveTraces[0];
964-
965-
$this->assertNotSame($basicPublishSpan['trace_id'], $basicDeliverSpan['trace_id']);
966-
$this->assertArrayNotHasKey('parent_id', $basicDeliverSpan);
931+
$this->retryTest(function () {
932+
$sendTraces = $this->inCli(
933+
__DIR__ . '/../scripts/send.php',
934+
[
935+
'DD_TRACE_AUTO_FLUSH_ENABLED' => 'true',
936+
'DD_TRACE_GENERATE_ROOT_SPAN' => 'true',
937+
'DD_TRACE_CLI_ENABLED' => 'true',
938+
'DD_DISTRIBUTED_TRACING' => 'false'
939+
],
940+
[],
941+
self::$autoloadPath
942+
);
943+
944+
list($receiveTraces, $output) = $this->inCli(
945+
__DIR__ . '/../scripts/receive.php',
946+
[
947+
'DD_TRACE_AUTO_FLUSH_ENABLED' => 'true',
948+
'DD_TRACE_GENERATE_ROOT_SPAN' => 'false',
949+
'DD_TRACE_CLI_ENABLED' => 'true'
950+
],
951+
[],
952+
self::$autoloadPath,
953+
true
954+
);
955+
956+
// Assess that user headers weren't lost
957+
$this->assertSame("", trim(preg_replace("(.*\[ddtrace].*)", "", $output)));
958+
959+
$sendTraces = $sendTraces[0][0]; // There is a root span
960+
// Spans: send.php -> basic_publish -> queue_declare -> connect
961+
$basicPublishSpan = $sendTraces[1];
962+
963+
$receiveTraces = $receiveTraces[3]; // There isn't a root span
964+
// Spans: connect -> queue_declare -> basic_consume & basic_consume_ok -> basic_deliver
965+
$basicDeliverSpan = $receiveTraces[0];
966+
967+
$this->assertNotSame($basicPublishSpan['trace_id'], $basicDeliverSpan['trace_id']);
968+
$this->assertArrayNotHasKey('parent_id', $basicDeliverSpan);
969+
});
967970
}
968971

969972
public function testBatchedPublishing()

tests/Integrations/PCNTL/PCNTLTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
final class PCNTLTest extends IntegrationTestCase
99
{
1010
private static $acceptable_test_execution_time = 2;
11-
const MAX_RETRIES = 3;
1211

1312
protected function ddSetUp()
1413
{

0 commit comments

Comments
 (0)