Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Runtime/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ public function __construct(StreamFactory $streamFactory)
public function attach(StreamContainerInterface $streamContainer, Closure $onSelect, string $identity): void
{
if (array_key_exists($identity, $this->containers)) {
throw new RunnerException("Stream container with identity {$identity} already attached");
// On repeated identity, check if actually readable (detach if not)
if ($this->containers[$identity]->stream->isReadable()) {
throw new RunnerException("Stream container with identity {$identity} already attached");
}
$this->detach($identity);
}
$stream = $streamContainer->getStream();
$this->streamCollection->attach($stream, $identity);
Expand Down
34 changes: 34 additions & 0 deletions tests/suites/runtime/RunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,36 @@ public function testHandling(): void
$runner->detach($streamContainer->getIdentity());
}

public function testIdentityConflictResolvable(): void
{
$this->expectStreamFactory();
$factory = new StreamFactory();
$this->expectStreamFactoryCreateStreamCollection();
$this->expectStreamCollection();
$runner = new Runner($factory);

$this->expectStreamFactoryCreateStream();
$this->expectStreamFactoryCreateStreamFromResource();
$this->expectStream();
$this->expectStreamGetMetadata();
$this->expectContext();
$streamContainer = new MockStreamContainer($factory);

$this->expectStreamCollectionAttach();
$runner->attach($streamContainer, function () {
// ignore
}, $streamContainer->getIdentity());

$this->expectStreamIsReadable()->setReturn(function () {
return false;
});
$this->expectStreamCollectionDetach();
$this->expectStreamCollectionAttach();
$runner->attach($streamContainer, function () {
// ignore
}, $streamContainer->getIdentity());
}

public function testIdentityConflict(): void
{
$this->expectStreamFactory();
Expand All @@ -81,6 +111,10 @@ public function testIdentityConflict(): void
$runner->attach($streamContainer, function () {
// ignore
}, $streamContainer->getIdentity());

$this->expectStreamIsReadable()->setReturn(function () {
return true;
});
$this->expectException(RunnerException::class);
$this->expectExceptionMessage('Stream container with identity mock-stream-container already attached');
$runner->attach($streamContainer, function () {
Expand Down