Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: Reduce AMQP & PCNTL Tests flakiness #3043

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions tests/Common/IntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ abstract class IntegrationTestCase extends BaseTestCase
public static $database = "test";
private static $createdDatabases = ["test" => true];

protected static $maxRetries = 5;

public static function ddSetUpBeforeClass()
{
parent::ddSetUpBeforeClass();
Expand Down Expand Up @@ -113,4 +115,23 @@ public function assertOneSpan($traces, SpanAssertion $expectedSpan)
{
$this->assertOneExpectedSpan($traces, $expectedSpan);
}

protected function retryTest(callable $testCase, ...$args)
{
$attempts = 0;
while ($attempts < self::$maxRetries) {
try {
$testCase(...$args);
return; // Test passed, exit the loop.
} catch (\Throwable $e) {
echo "Attempt $attempts failed: " . $e->getMessage() . PHP_EOL;
echo "Retrying..." . PHP_EOL;
$attempts++;
if ($attempts >= self::$maxRetries) {
echo "Max retries reached." . PHP_EOL;
throw $e; // Re-throw after max retries.
}
}
}
}
}
159 changes: 81 additions & 78 deletions tests/Integrations/AMQP/V2/AMQPTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -878,92 +878,95 @@ public function testDistributedTracing()
{
// Note: This test is extremely flaky, locally at least. It will eventually pass with some tries...
// Reason: We may parse the traces from dumped data BEFORE the traces are flushed.

self::putEnv('DD_TRACE_DEBUG_PRNG_SEED=42'); // Not necessary, but makes it easier to debug locally

$sendTraces = $this->inCli(
__DIR__ . '/../scripts/send.php',
[
'DD_TRACE_AUTO_FLUSH_ENABLED' => 'true',
'DD_TRACE_GENERATE_ROOT_SPAN' => 'true',
'DD_TRACE_CLI_ENABLED' => 'true',
],
[],
self::$autoloadPath
);

list($receiveTraces, $output) = $this->inCli(
__DIR__ . '/../scripts/receive.php',
[
'DD_TRACE_AUTO_FLUSH_ENABLED' => 'true',
'DD_TRACE_GENERATE_ROOT_SPAN' => 'false',
'DD_TRACE_CLI_ENABLED' => 'true',
],
[],
self::$autoloadPath,
true
);

// Assess that user headers weren't lost
$this->assertSame("", trim(preg_replace("(.*\[ddtrace].*)", "", $output)));

$sendTraces = $sendTraces[0][0]; // There is a root span
// Spans: send.php -> basic_publish -> queue_declare -> connect
$basicPublishSpan = $sendTraces[1];

foreach ($receiveTraces as $receiveTrace) {
// Spans: connect -> queue_declare -> basic_consume & basic_consume_ok -> basic_deliver
if ($receiveTrace[0]["name"] == "amqp.basic.deliver") {
$basicDeliverSpan = $receiveTrace[0];
break;
$this->retryTest(function () {
self::putEnv('DD_TRACE_DEBUG_PRNG_SEED=42'); // Not necessary, but makes it easier to debug locally

$sendTraces = $this->inCli(
__DIR__ . '/../scripts/send.php',
[
'DD_TRACE_AUTO_FLUSH_ENABLED' => 'true',
'DD_TRACE_GENERATE_ROOT_SPAN' => 'true',
'DD_TRACE_CLI_ENABLED' => 'true',
],
[],
self::$autoloadPath
);

list($receiveTraces, $output) = $this->inCli(
__DIR__ . '/../scripts/receive.php',
[
'DD_TRACE_AUTO_FLUSH_ENABLED' => 'true',
'DD_TRACE_GENERATE_ROOT_SPAN' => 'false',
'DD_TRACE_CLI_ENABLED' => 'true',
],
[],
self::$autoloadPath,
true
);

// Assess that user headers weren't lost
$this->assertSame("", trim(preg_replace("(.*\[ddtrace].*)", "", $output)));

$sendTraces = $sendTraces[0][0]; // There is a root span
// Spans: send.php -> basic_publish -> queue_declare -> connect
$basicPublishSpan = $sendTraces[1];

foreach ($receiveTraces as $receiveTrace) {
// Spans: connect -> queue_declare -> basic_consume & basic_consume_ok -> basic_deliver
if ($receiveTrace[0]["name"] == "amqp.basic.deliver") {
$basicDeliverSpan = $receiveTrace[0];
break;
}
}
}

$this->assertSame($basicPublishSpan['trace_id'], $basicDeliverSpan['trace_id']);
$this->assertSame($basicPublishSpan['span_id'], $basicDeliverSpan['parent_id']);
$this->assertSame($basicPublishSpan['trace_id'], $basicDeliverSpan['trace_id']);
$this->assertSame($basicPublishSpan['span_id'], $basicDeliverSpan['parent_id']);
});
}

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

$sendTraces = $this->inCli(
__DIR__ . '/../scripts/send.php',
[
'DD_TRACE_AUTO_FLUSH_ENABLED' => 'true',
'DD_TRACE_GENERATE_ROOT_SPAN' => 'true',
'DD_TRACE_CLI_ENABLED' => 'true',
'DD_DISTRIBUTED_TRACING' => 'false'
],
[],
self::$autoloadPath
);

list($receiveTraces, $output) = $this->inCli(
__DIR__ . '/../scripts/receive.php',
[
'DD_TRACE_AUTO_FLUSH_ENABLED' => 'true',
'DD_TRACE_GENERATE_ROOT_SPAN' => 'false',
'DD_TRACE_CLI_ENABLED' => 'true'
],
[],
self::$autoloadPath,
true
);

// Assess that user headers weren't lost
$this->assertSame("", trim(preg_replace("(.*\[ddtrace].*)", "", $output)));

$sendTraces = $sendTraces[0][0]; // There is a root span
// Spans: send.php -> basic_publish -> queue_declare -> connect
$basicPublishSpan = $sendTraces[1];

$receiveTraces = $receiveTraces[3]; // There isn't a root span
// Spans: connect -> queue_declare -> basic_consume & basic_consume_ok -> basic_deliver
$basicDeliverSpan = $receiveTraces[0];

$this->assertNotSame($basicPublishSpan['trace_id'], $basicDeliverSpan['trace_id']);
$this->assertArrayNotHasKey('parent_id', $basicDeliverSpan);
$this->retryTest(function () {
$sendTraces = $this->inCli(
__DIR__ . '/../scripts/send.php',
[
'DD_TRACE_AUTO_FLUSH_ENABLED' => 'true',
'DD_TRACE_GENERATE_ROOT_SPAN' => 'true',
'DD_TRACE_CLI_ENABLED' => 'true',
'DD_DISTRIBUTED_TRACING' => 'false'
],
[],
self::$autoloadPath
);

list($receiveTraces, $output) = $this->inCli(
__DIR__ . '/../scripts/receive.php',
[
'DD_TRACE_AUTO_FLUSH_ENABLED' => 'true',
'DD_TRACE_GENERATE_ROOT_SPAN' => 'false',
'DD_TRACE_CLI_ENABLED' => 'true'
],
[],
self::$autoloadPath,
true
);

// Assess that user headers weren't lost
$this->assertSame("", trim(preg_replace("(.*\[ddtrace].*)", "", $output)));

$sendTraces = $sendTraces[0][0]; // There is a root span
// Spans: send.php -> basic_publish -> queue_declare -> connect
$basicPublishSpan = $sendTraces[1];

$receiveTraces = $receiveTraces[3]; // There isn't a root span
// Spans: connect -> queue_declare -> basic_consume & basic_consume_ok -> basic_deliver
$basicDeliverSpan = $receiveTraces[0];

$this->assertNotSame($basicPublishSpan['trace_id'], $basicDeliverSpan['trace_id']);
$this->assertArrayNotHasKey('parent_id', $basicDeliverSpan);
});
}

public function testBatchedPublishing()
Expand Down
77 changes: 41 additions & 36 deletions tests/Integrations/PCNTL/PCNTLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ protected function ddSetUp()
}

$this->resetRequestDumper();
\usleep(500000); // 500ms
parent::ddSetUp();
}

Expand All @@ -24,49 +25,53 @@ protected function ddSetUp()
*/
public function testDoesNoHangAtShutdownWhenDisabled($scriptPath)
{
if ($scriptPath === (__DIR__ . '/scripts/long-running-manual-flush.php')) {
$this->markTestSkipped('manual tracing cannot be done when the tracer is disabled because the "DDTrace\\*" classes are not available.');
return;
}

$start = \microtime(true);
$this->executeCli(
$scriptPath,
[
'DD_TRACE_CLI_ENABLED' => 'false',
'DD_TRACE_SHUTDOWN_TIMEOUT' => 5000,
]
);
$end = \microtime(true);
$this->assertLessThan(self::$acceptable_test_execution_time, $end - $start);
$this->retryTest(function ($scriptPath) {
if ($scriptPath === (__DIR__ . '/scripts/long-running-manual-flush.php')) {
$this->markTestSkipped('manual tracing cannot be done when the tracer is disabled because the "DDTrace\\*" classes are not available.');
return;
}

$start = \microtime(true);
$this->executeCli(
$scriptPath,
[
'DD_TRACE_CLI_ENABLED' => 'false',
'DD_TRACE_SHUTDOWN_TIMEOUT' => 5000,
]
);
$end = \microtime(true);
$this->assertLessThan(self::$acceptable_test_execution_time, $end - $start);
}, $scriptPath);
}

/**
* @dataProvider dataProviderAllScripts
*/
public function testDoesNoHangAtShutdownWhenEnabled($scriptPath)
{
if (extension_loaded('xdebug')) {
$this->markTestSkipped('xdebug is enabled, which causes the tracer to slow down dramatically.');
}

$start = \microtime(true);
$this->executeCli(
$scriptPath,
[
'DD_TRACE_CLI_ENABLED' => 'true',
'DD_TRACE_SHUTDOWN_TIMEOUT' => 5000,
],
[],
'',
false,
true
);
$end = \microtime(true);
$this->assertLessThan(self::$acceptable_test_execution_time, $end - $start);
if (\dd_trace_env_config("DD_TRACE_SIDECAR_TRACE_SENDER")) {
\dd_trace_synchronous_flush();
}
$this->retryTest(function ($scriptPath) {
if (extension_loaded('xdebug')) {
$this->markTestSkipped('xdebug is enabled, which causes the tracer to slow down dramatically.');
}

$start = \microtime(true);
$this->executeCli(
$scriptPath,
[
'DD_TRACE_CLI_ENABLED' => 'true',
'DD_TRACE_SHUTDOWN_TIMEOUT' => 5000,
],
[],
'',
false,
true
);
$end = \microtime(true);
$this->assertLessThan(self::$acceptable_test_execution_time, $end - $start);
if (\dd_trace_env_config("DD_TRACE_SIDECAR_TRACE_SENDER")) {
\dd_trace_synchronous_flush();
}
}, $scriptPath);
}

public function dataProviderAllScripts()
Expand Down
Loading