1
0
mirror of https://github.com/danog/file.git synced 2024-11-26 20:04:51 +01:00
file/test/AsyncFileTest.php
2020-02-21 13:12:48 -06:00

79 lines
2.0 KiB
PHP

<?php
namespace Amp\File\Test;
use Amp\File;
use Amp\File\PendingOperationError;
abstract class AsyncFileTest extends FileTest
{
public function testSimultaneousReads(): \Generator
{
$this->expectException(PendingOperationError::class);
/** @var \Amp\File\File $handle */
$handle = yield File\open(__FILE__, "r");
$promise1 = $handle->read();
$promise2 = $handle->read();
$expected = \substr(yield File\get(__FILE__), 0, 20);
$this->assertSame($expected, yield $promise1);
yield $promise2;
}
public function testSeekWhileReading(): \Generator
{
$this->expectException(PendingOperationError::class);
/** @var \Amp\File\File $handle */
$handle = yield File\open(__FILE__, "r");
$promise1 = $handle->read(10);
$promise2 = $handle->seek(0);
$expected = \substr(yield File\get(__FILE__), 0, 10);
$this->assertSame($expected, yield $promise1);
yield $promise2;
}
public function testReadWhileWriting(): \Generator
{
$this->expectException(PendingOperationError::class);
$path = Fixture::path() . "/temp";
/** @var \Amp\File\File $handle */
$handle = yield File\open($path, "c+");
$data = "test";
$promise1 = $handle->write($data);
$promise2 = $handle->read(10);
$this->assertSame(\strlen($data), yield $promise1);
yield $promise2; // Should throw.
}
public function testWriteWhileReading(): \Generator
{
$this->expectException(PendingOperationError::class);
$path = Fixture::path() . "/temp";
/** @var \Amp\File\File $handle */
$handle = yield File\open($path, "c+");
$promise1 = $handle->read(10);
$promise2 = $handle->write("test");
$expected = \substr(yield File\get(__FILE__), 0, 10);
$this->assertSame($expected, yield $promise1);
yield $promise2; // Should throw.
}
}