Skip to content

Commit dcee94b

Browse files
authored
Merge pull request #199 from appkit-framework/channel-state-checks
Throw exceptions for operations on closed or failed channel
2 parents 1542bd0 + 829d408 commit dcee94b

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

src/Channel.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,20 @@ public function __construct(private Connection $connection, private Client $clie
109109
$this->bodyBuffer = new Buffer();
110110
}
111111

112-
public function getClient(): Connection
112+
protected function getClient(): Connection
113113
{
114+
if ($this->state === ChannelState::Error) {
115+
throw new ChannelException('Channel in error state.');
116+
}
117+
118+
if ($this->state === ChannelState::Closing) {
119+
throw new ChannelException('Channel is closing');
120+
}
121+
122+
if ($this->state === ChannelState::Closed) {
123+
throw new ChannelException('Channel is closed');
124+
}
125+
114126
return $this->connection;
115127
}
116128

test/ChannelTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,4 +265,18 @@ public function testChannelClosingMidMessageHandling(): void
265265

266266
self::assertFalse($c->isConnected());
267267
}
268+
269+
public function testAttemptingToOperateOnANoLongerConnectedConnectionThrows(): void
270+
{
271+
self::expectException(ChannelException::class);
272+
self::expectExceptionMessage('Channel is closed');
273+
274+
$c = $this->helper->createClient();
275+
276+
$ch = $c->connect()->channel();
277+
self::assertTrue($c->isConnected());
278+
$c->disconnect();
279+
self::assertFalse($c->isConnected());
280+
$ch->publish('hi', [], '', 'test_queue');
281+
}
268282
}

0 commit comments

Comments
 (0)