| 1 |
<?php |
| 2 |
namespace Wikimedia\Tests\Telemetry; |
| 3 |
|
| 4 |
use MediaWikiUnitTestCase; |
| 5 |
use Wikimedia\Assert\PreconditionException; |
| 6 |
use Wikimedia\Telemetry\Clock; |
| 7 |
use Wikimedia\Telemetry\Span; |
| 8 |
use Wikimedia\Telemetry\SpanContext; |
| 9 |
use Wikimedia\Telemetry\TracerState; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class SpanTest extends MediaWikiUnitTestCase { |
| 15 |
private Clock $clock; |
| 16 |
private TracerState $tracerState; |
| 17 |
|
| 18 |
protected function setUp(): void { |
| 19 |
parent::setUp(); |
| 20 |
|
| 21 |
$this->clock = $this->createMock( Clock::class ); |
| 22 |
$this->tracerState = $this->createMock( TracerState::class ); |
| 23 |
} |
| 24 |
|
| 25 |
private function createSpan(): Span { |
| 26 |
$context = new SpanContext( |
| 27 |
str_repeat( 'a', 64 ), |
| 28 |
str_repeat( 'b', 16 ), |
| 29 |
null, |
| 30 |
'test', |
| 31 |
true |
| 32 |
); |
| 33 |
return new Span( |
| 34 |
$this->clock, |
| 35 |
$this->tracerState, |
| 36 |
$context |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
public function testDuplicateSpanStartShouldError(): void { |
| 41 |
$this->expectException( PreconditionException::class ); |
| 42 |
$this->expectExceptionMessage( 'Cannot start a span more than once' ); |
| 43 |
|
| 44 |
$span = $this->createSpan(); |
| 45 |
|
| 46 |
$span->start(); |
| 47 |
$span->start(); |
| 48 |
} |
| 49 |
|
| 50 |
public function testEndingUnstartedSpanShouldError(): void { |
| 51 |
$this->expectException( PreconditionException::class ); |
| 52 |
$this->expectExceptionMessage( 'Cannot end a span that has not been started' ); |
| 53 |
|
| 54 |
$span = $this->createSpan(); |
| 55 |
|
| 56 |
$span->end(); |
| 57 |
} |
| 58 |
|
| 59 |
public function testShouldExposeDataViaContext(): void { |
| 60 |
$traceId = str_repeat( 'c', 64 ); |
| 61 |
$spanId = str_repeat( 'd', 16 ); |
| 62 |
$context = new SpanContext( |
| 63 |
$traceId, |
| 64 |
$spanId, |
| 65 |
null, |
| 66 |
'test', |
| 67 |
true |
| 68 |
); |
| 69 |
|
| 70 |
$span = new Span( |
| 71 |
$this->clock, |
| 72 |
$this->tracerState, |
| 73 |
$context |
| 74 |
); |
| 75 |
$span->start(); |
| 76 |
|
| 77 |
$this->assertSame( $traceId, $span->getContext()->getTraceId() ); |
| 78 |
$this->assertSame( $spanId, $span->getContext()->getSpanId() ); |
| 79 |
$this->assertTrue( $span->getContext()->isSampled() ); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function testShouldNotDeactivateSpanInSharedStateByGoingOutOfScopeIfItWasNeverActive( |
| 86 |
?SpanContext $activeSpanContext |
| 87 |
): void { |
| 88 |
$span = $this->createSpan(); |
| 89 |
|
| 90 |
$this->tracerState->expects( $this->never() ) |
| 91 |
->method( 'activateSpan' ); |
| 92 |
|
| 93 |
$this->tracerState->method( 'getActiveSpanContext' ) |
| 94 |
->willReturn( $activeSpanContext ); |
| 95 |
|
| 96 |
$this->tracerState->expects( $this->never() ) |
| 97 |
->method( 'deactivateSpan' ); |
| 98 |
|
| 99 |
$span->start(); |
| 100 |
|
| 101 |
$span = null; |
| 102 |
} |
| 103 |
|
| 104 |
public static function provideInactiveSpanTestCases(): iterable { |
| 105 |
yield 'no active span' => [ null ]; |
| 106 |
yield 'different active span' => [ new SpanContext( '', 'bbb', null, '', false ) ]; |
| 107 |
} |
| 108 |
|
| 109 |
public function testShouldActivateAndDeactivateSpanInSharedStateByGoingOutOfScope(): void { |
| 110 |
$span = $this->createSpan(); |
| 111 |
|
| 112 |
$this->tracerState->expects( $this->once() ) |
| 113 |
->method( 'activateSpan' ) |
| 114 |
->with( $span->getContext() ); |
| 115 |
|
| 116 |
$this->tracerState->method( 'getActiveSpanContext' ) |
| 117 |
->willReturn( $span->getContext() ); |
| 118 |
|
| 119 |
$this->tracerState->expects( $this->once() ) |
| 120 |
->method( 'deactivateSpan' ) |
| 121 |
->with( $span->getContext() ); |
| 122 |
|
| 123 |
$span->start(); |
| 124 |
$span->activate(); |
| 125 |
|
| 126 |
$span = null; |
| 127 |
} |
| 128 |
|
| 129 |
public function testShouldDeactivateSpanInSharedStateExplicitly(): void { |
| 130 |
$span = $this->createSpan(); |
| 131 |
|
| 132 |
$this->tracerState->expects( $this->once() ) |
| 133 |
->method( 'deactivateSpan' ) |
| 134 |
->with( $span->getContext() ); |
| 135 |
|
| 136 |
$span->start(); |
| 137 |
$span->deactivate(); |
| 138 |
} |
| 139 |
|
| 140 |
public function testShouldAddSpanToSharedStateOnceWhenEndedExplicitly(): void { |
| 141 |
$span = $this->createSpan(); |
| 142 |
|
| 143 |
$this->tracerState->expects( $this->once() ) |
| 144 |
->method( 'addSpanContext' ) |
| 145 |
->with( $span->getContext() ); |
| 146 |
|
| 147 |
$span->start(); |
| 148 |
|
| 149 |
$span->end(); |
| 150 |
$span->end(); |
| 151 |
} |
| 152 |
|
| 153 |
public function testShouldAddSpanToSharedStateOnceWhenEndedByGoingOutOfScope(): void { |
| 154 |
$span = $this->createSpan(); |
| 155 |
|
| 156 |
$this->tracerState->expects( $this->once() ) |
| 157 |
->method( 'addSpanContext' ) |
| 158 |
->with( $span->getContext() ); |
| 159 |
|
| 160 |
$span->start(); |
| 161 |
|
| 162 |
$span = null; |
| 163 |
} |
| 164 |
} |