Skip to content

Commit a85ba4f

Browse files
committed
fix: to low test coverage
1 parent 2075cfd commit a85ba4f

4 files changed

Lines changed: 379 additions & 0 deletions

File tree

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Flow\Bridge\PHPUnit\Telemetry\Tests\Unit;
6+
7+
use Flow\Bridge\PHPUnit\Telemetry\{Configuration,
8+
CurlTransportConfig,
9+
ErrorLogHandlerConfig,
10+
GrpcTransportConfig,
11+
NullErrorHandlerConfig,
12+
SerializerType,
13+
StreamErrorHandlerConfig,
14+
StreamTransportConfig,
15+
SyslogErrorHandlerConfig,
16+
TelemetryFactory,
17+
UdpSyslogErrorHandlerConfig};
18+
use Flow\Bridge\PHPUnit\Telemetry\Tests\Mother\ConfigurationMother;
19+
use Flow\Telemetry\ErrorHandler\{ErrorLogMessageType, SyslogFacility, SyslogSeverity};
20+
use Flow\Telemetry\Telemetry;
21+
use PHPUnit\Framework\TestCase;
22+
23+
final class TelemetryFactoryTest extends TestCase
24+
{
25+
public function test_create_returns_telemetry_with_default_curl_transport() : void
26+
{
27+
$telemetry = TelemetryFactory::create(ConfigurationMother::default());
28+
29+
self::assertInstanceOf(Telemetry::class, $telemetry);
30+
$telemetry->shutdown();
31+
}
32+
33+
public function test_create_with_disabled_metrics_routes_metrics_to_void() : void
34+
{
35+
$telemetry = TelemetryFactory::create(ConfigurationMother::withDisabledMetrics());
36+
37+
self::assertInstanceOf(Telemetry::class, $telemetry);
38+
$telemetry->shutdown();
39+
}
40+
41+
public function test_create_with_disabled_traces_routes_spans_to_void() : void
42+
{
43+
$telemetry = TelemetryFactory::create(ConfigurationMother::withDisabledTraces());
44+
45+
self::assertInstanceOf(Telemetry::class, $telemetry);
46+
$telemetry->shutdown();
47+
}
48+
49+
public function test_create_with_error_log_handler() : void
50+
{
51+
$config = $this->configWithErrorHandler(new ErrorLogHandlerConfig(
52+
messageType: ErrorLogMessageType::Sapi,
53+
expandNewlines: true,
54+
messagePrefix: '[test]',
55+
));
56+
57+
$telemetry = TelemetryFactory::create($config);
58+
59+
self::assertInstanceOf(Telemetry::class, $telemetry);
60+
$telemetry->shutdown();
61+
}
62+
63+
public function test_create_with_full_curl_options_covers_optional_branches() : void
64+
{
65+
$config = new Configuration(
66+
serviceName: 'phpunit',
67+
transport: new CurlTransportConfig(
68+
endpoint: 'http://localhost:4318',
69+
headers: ['Authorization' => 'Bearer token', 'X-Custom' => 'value'],
70+
timeoutMs: 250,
71+
connectTimeoutMs: 250,
72+
shutdownTimeoutMs: 5000,
73+
compression: true,
74+
followRedirects: true,
75+
maxRedirects: 5,
76+
proxy: 'http://proxy:8080',
77+
sslVerifyPeer: false,
78+
sslVerifyHost: false,
79+
sslCertPath: '/tmp/cert.pem',
80+
sslKeyPath: '/tmp/key.pem',
81+
caInfoPath: '/tmp/ca.pem',
82+
serializer: SerializerType::PROTOBUF,
83+
),
84+
emitTraces: true,
85+
emitMetrics: true,
86+
emitTestSpans: true,
87+
emitTestCaseSpans: true,
88+
batchSize: 256,
89+
errorHandler: ConfigurationMother::defaultErrorHandler(),
90+
);
91+
92+
$telemetry = TelemetryFactory::create($config);
93+
94+
self::assertInstanceOf(Telemetry::class, $telemetry);
95+
$telemetry->shutdown();
96+
}
97+
98+
public function test_create_with_grpc_transport() : void
99+
{
100+
if (!\extension_loaded('grpc')) {
101+
self::markTestSkipped('grpc extension is required');
102+
}
103+
104+
$config = new Configuration(
105+
serviceName: 'phpunit',
106+
transport: new GrpcTransportConfig(
107+
endpoint: 'localhost:4317',
108+
headers: ['api-key' => 'x'],
109+
insecure: true,
110+
timeoutMs: 250,
111+
shutdownTimeoutMs: 5000,
112+
),
113+
emitTraces: true,
114+
emitMetrics: true,
115+
emitTestSpans: true,
116+
emitTestCaseSpans: true,
117+
batchSize: Configuration::DEFAULT_BATCH_SIZE,
118+
errorHandler: ConfigurationMother::defaultErrorHandler(),
119+
);
120+
121+
$telemetry = TelemetryFactory::create($config);
122+
123+
self::assertInstanceOf(Telemetry::class, $telemetry);
124+
$telemetry->shutdown();
125+
}
126+
127+
public function test_create_with_null_error_handler() : void
128+
{
129+
$config = $this->configWithErrorHandler(new NullErrorHandlerConfig());
130+
131+
$telemetry = TelemetryFactory::create($config);
132+
133+
self::assertInstanceOf(Telemetry::class, $telemetry);
134+
$telemetry->shutdown();
135+
}
136+
137+
public function test_create_with_stream_error_handler() : void
138+
{
139+
$config = $this->configWithErrorHandler(new StreamErrorHandlerConfig(
140+
destination: 'php://memory',
141+
filePermissions: 0644,
142+
createDirectories: true,
143+
messagePrefix: '[test]',
144+
));
145+
146+
$telemetry = TelemetryFactory::create($config);
147+
148+
self::assertInstanceOf(Telemetry::class, $telemetry);
149+
$telemetry->shutdown();
150+
}
151+
152+
public function test_create_with_stream_transport() : void
153+
{
154+
$config = new Configuration(
155+
serviceName: 'phpunit',
156+
transport: new StreamTransportConfig(
157+
destination: 'php://memory',
158+
filePermissions: 0644,
159+
createDirectories: true,
160+
),
161+
emitTraces: true,
162+
emitMetrics: true,
163+
emitTestSpans: true,
164+
emitTestCaseSpans: true,
165+
batchSize: Configuration::DEFAULT_BATCH_SIZE,
166+
errorHandler: ConfigurationMother::defaultErrorHandler(),
167+
);
168+
169+
$telemetry = TelemetryFactory::create($config);
170+
171+
self::assertInstanceOf(Telemetry::class, $telemetry);
172+
$telemetry->shutdown();
173+
}
174+
175+
public function test_create_with_syslog_error_handler() : void
176+
{
177+
$config = $this->configWithErrorHandler(new SyslogErrorHandlerConfig(
178+
ident: 'flow-test',
179+
facility: SyslogFacility::Local0,
180+
logOpts: \LOG_PID,
181+
severity: SyslogSeverity::Warning,
182+
));
183+
184+
$telemetry = TelemetryFactory::create($config);
185+
186+
self::assertInstanceOf(Telemetry::class, $telemetry);
187+
$telemetry->shutdown();
188+
}
189+
190+
public function test_create_with_udp_syslog_error_handler() : void
191+
{
192+
$config = $this->configWithErrorHandler(new UdpSyslogErrorHandlerConfig(
193+
host: '127.0.0.1',
194+
port: 514,
195+
ident: 'flow-test',
196+
facility: SyslogFacility::User,
197+
severity: SyslogSeverity::Info,
198+
));
199+
200+
$telemetry = TelemetryFactory::create($config);
201+
202+
self::assertInstanceOf(Telemetry::class, $telemetry);
203+
$telemetry->shutdown();
204+
}
205+
206+
private function configWithErrorHandler(
207+
ErrorLogHandlerConfig|NullErrorHandlerConfig|StreamErrorHandlerConfig|SyslogErrorHandlerConfig|UdpSyslogErrorHandlerConfig $errorHandler,
208+
) : Configuration {
209+
return new Configuration(
210+
serviceName: 'phpunit',
211+
transport: ConfigurationMother::defaultTransport(),
212+
emitTraces: true,
213+
emitMetrics: true,
214+
emitTestSpans: true,
215+
emitTestCaseSpans: true,
216+
batchSize: Configuration::DEFAULT_BATCH_SIZE,
217+
errorHandler: $errorHandler,
218+
);
219+
}
220+
}

src/bridge/telemetry/otlp/tests/Flow/Bridge/Telemetry/OTLP/Tests/Unit/Transport/CurlTransportOptionsTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ public function test_default_options() : void
2828
self::assertFalse($options->compression());
2929
}
3030

31+
public function test_default_shutdown_timeout() : void
32+
{
33+
$options = new CurlTransportOptions();
34+
35+
self::assertSame(CurlTransportOptions::DEFAULT_SHUTDOWN_TIMEOUT_MS, $options->shutdownTimeoutMs());
36+
}
37+
3138
public function test_dsl_function_creates_options() : void
3239
{
3340
$options = otlp_curl_options();
@@ -206,6 +213,21 @@ public function test_with_proxy() : void
206213
self::assertSame('http://proxy:8080', $options->proxy());
207214
}
208215

216+
public function test_with_shutdown_timeout() : void
217+
{
218+
$options = (new CurlTransportOptions())->withShutdownTimeout(7500);
219+
220+
self::assertSame(7500, $options->shutdownTimeoutMs());
221+
}
222+
223+
public function test_with_shutdown_timeout_rejects_negative_value() : void
224+
{
225+
$this->expectException(\InvalidArgumentException::class);
226+
$this->expectExceptionMessage('Shutdown timeout must be non-negative');
227+
228+
(new CurlTransportOptions())->withShutdownTimeout(-1);
229+
}
230+
209231
public function test_with_ssl_certificate() : void
210232
{
211233
$options = (new CurlTransportOptions())->withSslCertificate('/path/to/cert.pem');

src/bridge/telemetry/otlp/tests/Flow/Bridge/Telemetry/OTLP/Tests/Unit/Transport/CurlTransportTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,70 @@ public function test_shutdown_surfaces_failover_shutdown_exception_when_no_defer
281281
$transport->shutdown();
282282
}
283283

284+
public function test_shutdown_with_zero_timeout_forwards_pending_to_failover() : void
285+
{
286+
if (!\extension_loaded('curl')) {
287+
self::markTestSkipped('ext-curl is required');
288+
}
289+
290+
$failover = new RecordingTransport();
291+
$batch = Signals::traces($this->createSpans());
292+
293+
$transport = new CurlTransport(
294+
'http://127.0.0.1:1',
295+
new JsonSerializer(),
296+
(new CurlTransportOptions())
297+
->withConnectTimeout(60_000)
298+
->withTimeout(60_000)
299+
->withShutdownTimeout(0),
300+
$failover,
301+
);
302+
303+
$transport->send($batch);
304+
305+
try {
306+
$transport->shutdown();
307+
self::addToAssertionCount(1);
308+
} catch (FailoverTransportException $e) {
309+
self::assertGreaterThanOrEqual(1, \count($e->failures));
310+
// Either the still-pending path forwarded the batch, or the normal failover drain did.
311+
self::assertContains($batch, $failover->sent);
312+
}
313+
314+
self::assertSame(1, $failover->shutdownCalls);
315+
}
316+
317+
public function test_shutdown_with_zero_timeout_marks_pending_as_failed_in_legacy_mode() : void
318+
{
319+
if (!\extension_loaded('curl')) {
320+
self::markTestSkipped('ext-curl is required');
321+
}
322+
323+
$transport = new CurlTransport(
324+
'http://127.0.0.1:1',
325+
new JsonSerializer(),
326+
(new CurlTransportOptions())
327+
->withConnectTimeout(60_000)
328+
->withTimeout(60_000)
329+
->withShutdownTimeout(0),
330+
);
331+
332+
$transport->send(Signals::traces($this->createSpans()));
333+
334+
try {
335+
$transport->shutdown();
336+
337+
// If everything completed before shutdown_timeout=0 fired, that's also valid (race condition).
338+
self::addToAssertionCount(1);
339+
} catch (TransportException $e) {
340+
// Either the shutdown-deadline-reached path OR the normal connection-refused path.
341+
self::assertTrue(
342+
\str_contains($e->getMessage(), 'shutdown_timeout=0ms expired')
343+
|| \str_contains($e->getMessage(), 'curl error'),
344+
);
345+
}
346+
}
347+
284348
/**
285349
* @return array<Span>
286350
*/

0 commit comments

Comments
 (0)