diff --git a/SftpConnectionProviderTest.php b/SftpConnectionProviderTest.php index 31764c0..453bd9c 100644 --- a/SftpConnectionProviderTest.php +++ b/SftpConnectionProviderTest.php @@ -4,6 +4,7 @@ namespace League\Flysystem\PhpseclibV3; +use League\Flysystem\AdapterTestUtilities\ToxiproxyManagement; use phpseclib3\Net\SFTP; use PHPUnit\Framework\TestCase; @@ -241,6 +242,52 @@ public function providing_an_invalid_password(): void $provider->provideConnection(); } + /** + * @test + */ + public function retries_several_times_until_failure(): void + { + $connectivityChecker = new class implements ConnectivityChecker { + /** @var int */ + public $calls = 0; + + public function isConnected(SFTP $connection): bool + { + ++$this->calls; + + return $connection->isConnected(); + } + }; + + $managesConnectionToxics = ToxiproxyManagement::forServer(); + $managesConnectionToxics->resetPeerOnRequest('sftp', 10); + + $maxTries = 5; + + $provider = SftpConnectionProvider::fromArray( + [ + 'host' => 'localhost', + 'username' => 'bar', + 'privateKey' => __DIR__ . '/../../test_files/sftp/id_rsa', + 'passphrase' => 'secret', + 'port' => 8222, + 'maxTries' => $maxTries, + 'timeout' => 1, + 'connectivityChecker' => $connectivityChecker, + ] + ); + + $this->expectException(UnableToConnectToSftpHost::class); + + try { + $provider->provideConnection(); + } finally { + $managesConnectionToxics->removeAllToxics(); + + self::assertSame($maxTries + 1, $connectivityChecker->calls); + } + } + private function computeFingerPrint(string $publicKey): string { $content = explode(' ', $publicKey, 3);