-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
346 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Tests\Unit\SDK\Metrics\Stream; | ||
|
||
use OpenTelemetry\Context\Context; | ||
use OpenTelemetry\Context\ContextStorageInterface; | ||
use OpenTelemetry\SDK\Common\Attribute\Attributes; | ||
use OpenTelemetry\SDK\Metrics\Stream\MultiStreamWriter; | ||
use OpenTelemetry\SDK\Metrics\Stream\StreamWriter; | ||
use OpenTelemetry\SDK\Metrics\Stream\WritableMetricStreamInterface; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class StreamWriterTest extends TestCase | ||
{ | ||
/** | ||
* @covers \OpenTelemetry\SDK\Metrics\Stream\StreamWriter | ||
*/ | ||
public function test_stream_writer(): void | ||
{ | ||
$stream = $this->createMock(WritableMetricStreamInterface::class); | ||
$stream->expects($this->once())->method('record')->with(5, Attributes::create(['foo' => 1]), $this->anything(), 3); | ||
|
||
$w = new StreamWriter(null, Attributes::factory(), $stream); | ||
$w->record(5, ['foo' => 1], null, 3); | ||
} | ||
|
||
/** | ||
* @covers \OpenTelemetry\SDK\Metrics\Stream\MultiStreamWriter | ||
*/ | ||
public function test_multi_stream_writer(): void | ||
{ | ||
$streams = []; | ||
for ($i = 0; $i < 3; $i++) { | ||
$stream = $this->createMock(WritableMetricStreamInterface::class); | ||
$stream->expects($this->once())->method('record')->with(5, Attributes::create(['foo' => 1]), $this->anything(), 3); | ||
|
||
$streams[] = $stream; | ||
} | ||
|
||
$w = new MultiStreamWriter(null, Attributes::factory(), $streams); | ||
$w->record(5, ['foo' => 1], null, 3); | ||
} | ||
|
||
/** | ||
* @covers \OpenTelemetry\SDK\Metrics\Stream\StreamWriter | ||
*/ | ||
public function test_stream_writer_provides_current_context_on_context_null(): void | ||
{ | ||
$context = Context::getRoot()->with(Context::createKey('-'), 5); | ||
$contextStorage = $this->createMock(ContextStorageInterface::class); | ||
$contextStorage->method('current')->willReturn($context); | ||
|
||
$stream = $this->createMock(WritableMetricStreamInterface::class); | ||
$stream->expects($this->once())->method('record')->with(5, Attributes::create(['foo' => 1]), $context, 3); | ||
|
||
$w = new StreamWriter($contextStorage, Attributes::factory(), $stream); | ||
$w->record(5, ['foo' => 1], null, 3); | ||
} | ||
|
||
/** | ||
* @covers \OpenTelemetry\SDK\Metrics\Stream\StreamWriter | ||
*/ | ||
public function test_stream_writer_context_provides_context_on_context_provided(): void | ||
{ | ||
$context = Context::getRoot()->with(Context::createKey('-'), 5); | ||
$contextStorage = $this->createMock(ContextStorageInterface::class); | ||
|
||
$stream = $this->createMock(WritableMetricStreamInterface::class); | ||
$stream->expects($this->once())->method('record')->with(5, Attributes::create(['foo' => 1]), $context, 3); | ||
|
||
$w = new StreamWriter($contextStorage, Attributes::factory(), $stream); | ||
$w->record(5, ['foo' => 1], $context, 3); | ||
} | ||
|
||
/** | ||
* @covers \OpenTelemetry\SDK\Metrics\Stream\StreamWriter | ||
*/ | ||
public function test_stream_writer_context_provides_root_context_on_context_false(): void | ||
{ | ||
$contextStorage = $this->createMock(ContextStorageInterface::class); | ||
|
||
$stream = $this->createMock(WritableMetricStreamInterface::class); | ||
$stream->expects($this->once())->method('record')->with(5, Attributes::create(['foo' => 1]), Context::getRoot(), 3); | ||
|
||
$w = new StreamWriter($contextStorage, Attributes::factory(), $stream); | ||
$w->record(5, ['foo' => 1], false, 3); | ||
} | ||
|
||
/** | ||
* @covers \OpenTelemetry\SDK\Metrics\Stream\MultiStreamWriter | ||
*/ | ||
public function test_multi_stream_writer_provides_current_context_on_context_null(): void | ||
{ | ||
$context = Context::getRoot()->with(Context::createKey('-'), 5); | ||
$contextStorage = $this->createMock(ContextStorageInterface::class); | ||
$contextStorage->method('current')->willReturn($context); | ||
|
||
$stream = $this->createMock(WritableMetricStreamInterface::class); | ||
$stream->expects($this->once())->method('record')->with(5, Attributes::create(['foo' => 1]), $context, 3); | ||
|
||
$w = new MultiStreamWriter($contextStorage, Attributes::factory(), [$stream]); | ||
$w->record(5, ['foo' => 1], null, 3); | ||
} | ||
|
||
/** | ||
* @covers \OpenTelemetry\SDK\Metrics\Stream\MultiStreamWriter | ||
*/ | ||
public function test_multi_stream_writer_context_provides_context_on_context_provided(): void | ||
{ | ||
$context = Context::getRoot()->with(Context::createKey('-'), 5); | ||
$contextStorage = $this->createMock(ContextStorageInterface::class); | ||
|
||
$stream = $this->createMock(WritableMetricStreamInterface::class); | ||
$stream->expects($this->once())->method('record')->with(5, Attributes::create(['foo' => 1]), $context, 3); | ||
|
||
$w = new MultiStreamWriter($contextStorage, Attributes::factory(), [$stream]); | ||
$w->record(5, ['foo' => 1], $context, 3); | ||
} | ||
|
||
/** | ||
* @covers \OpenTelemetry\SDK\Metrics\Stream\MultiStreamWriter | ||
*/ | ||
public function test_multi_stream_writer_context_provides_root_context_on_context_false(): void | ||
{ | ||
$contextStorage = $this->createMock(ContextStorageInterface::class); | ||
|
||
$stream = $this->createMock(WritableMetricStreamInterface::class); | ||
$stream->expects($this->once())->method('record')->with(5, Attributes::create(['foo' => 1]), Context::getRoot(), 3); | ||
|
||
$w = new MultiStreamWriter($contextStorage, Attributes::factory(), [$stream]); | ||
$w->record(5, ['foo' => 1], false, 3); | ||
} | ||
} |