Debian Codegraph @

tests/phpunit/unit/includes/libs/telemetry/TracerStateTest.php

raw symbols SCIP
1 <?php
2 namespace Wikimedia\Tests\Telemetry;
3
4 use MediaWikiUnitTestCase;
5 use Wikimedia\Assert\InvariantException;
6 use Wikimedia\Telemetry\SpanContext;
7 use Wikimedia\Telemetry\SpanInterface;
8 use Wikimedia\Telemetry\TracerState;
9
10 /**
11 * @covers \Wikimedia\Telemetry\TracerState
12 */
13 class TracerStateTest extends MediaWikiUnitTestCase {
14 private TracerState $tracerState;
15
16 protected function setUp(): void {
17 parent::setUp();
18
19 $this->tracerState = new TracerState();
20 }
21
22 public function testShouldMaintainStackOfActiveSpans(): void {
23 $firstSpanContext = new SpanContext(
24 '',
25 str_repeat( 'a', 16 ),
26 null,
27 '',
28 true
29 );
30
31 $secondSpanContext = new SpanContext(
32 '',
33 str_repeat( 'b', 16 ),
34 null,
35 '',
36 true
37 );
38
39 $this->assertNull( $this->tracerState->getActiveSpanContext() );
40
41 $this->tracerState->activateSpan( $firstSpanContext );
42 $this->assertSame( $firstSpanContext, $this->tracerState->getActiveSpanContext() );
43
44 $this->tracerState->activateSpan( $secondSpanContext );
45 $this->assertSame( $secondSpanContext, $this->tracerState->getActiveSpanContext() );
46
47 $this->tracerState->deactivateSpan( $secondSpanContext );
48 $this->assertSame( $firstSpanContext, $this->tracerState->getActiveSpanContext() );
49
50 $this->tracerState->deactivateSpan( $firstSpanContext );
51 $this->assertNull( $this->tracerState->getActiveSpanContext() );
52 }
53
54 public function testShouldRejectDeactivatingSpanWhenNoSpanIsActive(): void {
55 $this->expectException( InvariantException::class );
56 $this->expectExceptionMessage( 'Attempted to deactivate a span which is not the active span.' );
57
58 try {
59 $span = $this->createMock( SpanInterface::class );
60 $this->tracerState->deactivateSpan( new SpanContext( '', '', null, '', false ) );
61 } finally {
62 $this->assertNull( $this->tracerState->getActiveSpanContext() );
63 }
64 }
65
66 public function testShouldRejectDeactivatingSpanWhenItIsNotActive(): void {
67 $this->expectException( InvariantException::class );
68 $this->expectExceptionMessage( 'Attempted to deactivate a span which is not the active span.' );
69
70 $activeSpanContext = new SpanContext(
71 '',
72 str_repeat( 'a', 16 ),
73 null,
74 '',
75 true
76 );
77
78 $this->tracerState->activateSpan( $activeSpanContext );
79
80 try {
81 $otherSpanContext = new SpanContext(
82 '',
83 str_repeat( 'b', 16 ),
84 null,
85 '',
86 true
87 );
88
89 $this->tracerState->deactivateSpan( $otherSpanContext );
90 } finally {
91 $this->assertSame( $activeSpanContext, $this->tracerState->getActiveSpanContext() );
92 }
93 }
94
95 public function testShouldAddAndReturnSpans(): void {
96 $firstSpanContext = new SpanContext(
97 '',
98 str_repeat( 'a', 16 ),
99 null,
100 '',
101 true
102 );
103
104 $secondSpanContext = new SpanContext(
105 '',
106 str_repeat( 'b', 16 ),
107 null,
108 '',
109 true
110 );
111
112 $this->tracerState->addSpanContext( $firstSpanContext );
113 $this->tracerState->addSpanContext( $secondSpanContext );
114
115 $this->assertSame( [ $firstSpanContext, $secondSpanContext ], $this->tracerState->getSpanContexts() );
116
117 $this->tracerState->clearSpanContexts();
118
119 $this->assertSame( [], $this->tracerState->getSpanContexts() );
120 }
121 }