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