Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Ekstazi/dispatcher-hangs-up
#17 fix infinite loop in dispatcher and enh error handling
- Loading branch information
Showing
8 changed files
with
289 additions
and
44 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
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,73 @@ | ||
<?php | ||
|
||
namespace Amp\Ssh\Tests\Channel; | ||
|
||
use Amp\Loop; | ||
use Amp\Ssh\Authentication\UsernamePassword; | ||
use Amp\Ssh\Channel\ChannelException; | ||
use Amp\Ssh\Channel\Session; | ||
use function Amp\Ssh\connect; | ||
use Amp\Ssh\SshResource; | ||
use Amp\Ssh\Tests\NetworkHelper; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class SessionTest extends TestCase { | ||
protected function getSsh() { | ||
return connect('127.0.0.1:2222', new UsernamePassword('root', 'root')); | ||
} | ||
|
||
/** | ||
* if connection closed by server then fail dispatcher and all opened request emitters. | ||
*/ | ||
public function testRequestEmitterFailedAfterDisconnect() { | ||
$this->expectException(ChannelException::class); | ||
Loop::run(function () { | ||
$connection = yield $this->getSsh(); | ||
/** @var Session $session */ | ||
$session = $connection->createSession(); | ||
yield $session->open(); | ||
NetworkHelper::disconnect($connection); | ||
$iterator = $session->getRequestEmitter()->iterate(); | ||
yield $iterator->advance(); | ||
}); | ||
} | ||
|
||
/** | ||
* if connection closed by server then fail dispatcher and close all opened data emitters. | ||
*/ | ||
public function testDataEmitterClosedAfterDisconnect() { | ||
Loop::run(function () { | ||
$connection = yield $this->getSsh(); | ||
/** @var Session $session */ | ||
$session = $connection->createSession(); | ||
yield $session->open(); | ||
NetworkHelper::disconnect($connection); | ||
|
||
$iterator = $session->getDataEmitter()->iterate(); | ||
$hasNext = yield $iterator->advance(); | ||
$this->assertFalse($hasNext); | ||
|
||
$iterator = $session->getDataExtendedEmitter()->iterate(); | ||
$hasNext = yield $iterator->advance(); | ||
$this->assertFalse($hasNext); | ||
}); | ||
} | ||
|
||
/** | ||
* if dispatcher closed then all opened channels from client must stop. | ||
*/ | ||
public function testSessionClosedAfterConnectionClose() { | ||
Loop::run(function () { | ||
/** @var SshResource $connection */ | ||
$connection = yield $this->getSsh(); | ||
$session = $connection->createSession(); | ||
yield $session->open(); | ||
$connection->close(); | ||
|
||
$iterator = $session->getRequestEmitter()->iterate(); | ||
|
||
$hasNext = yield $iterator->advance(); | ||
$this->assertFalse($hasNext); | ||
}); | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
namespace Amp\Ssh\Tests; | ||
|
||
use Amp\Ssh\SshResource; | ||
|
||
class NetworkHelper { | ||
/** | ||
* Simulate disconnect from server. | ||
* @param SshResource $sshResource | ||
*/ | ||
public static function disconnect(SshResource $sshResource) { | ||
$reflection = new \ReflectionObject($sshResource); | ||
$property = $reflection->getProperty('handler'); | ||
$property->setAccessible(true); | ||
$handler = $property->getValue($sshResource); | ||
$handler->close(); | ||
} | ||
} |
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
Oops, something went wrong.