2017-06-20 07:06:12 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\File\Test;
|
|
|
|
|
|
|
|
use Amp\File;
|
2019-08-23 20:23:33 +02:00
|
|
|
use Amp\File\PendingOperationError;
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2019-08-23 21:47:03 +02:00
|
|
|
abstract class AsyncFileTest extends FileTest
|
2018-10-27 17:57:31 +02:00
|
|
|
{
|
2019-08-23 20:59:26 +02:00
|
|
|
public function testSimultaneousReads(): \Generator
|
2018-10-27 17:57:31 +02:00
|
|
|
{
|
2019-08-23 20:23:33 +02:00
|
|
|
$this->expectException(PendingOperationError::class);
|
|
|
|
|
2020-06-30 21:45:09 +02:00
|
|
|
/** @var File\File $handle */
|
|
|
|
$handle = yield File\openFile(__FILE__, "r");
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2019-08-23 20:59:26 +02:00
|
|
|
$promise1 = $handle->read();
|
|
|
|
$promise2 = $handle->read();
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2020-06-30 21:45:09 +02:00
|
|
|
$expected = \substr(yield File\read(__FILE__), 0, 20);
|
2019-08-23 20:59:26 +02:00
|
|
|
$this->assertSame($expected, yield $promise1);
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2019-08-23 20:59:26 +02:00
|
|
|
yield $promise2;
|
2017-06-20 07:06:12 +02:00
|
|
|
}
|
|
|
|
|
2019-08-23 20:59:26 +02:00
|
|
|
public function testSeekWhileReading(): \Generator
|
2018-10-27 17:57:31 +02:00
|
|
|
{
|
2019-08-23 20:23:33 +02:00
|
|
|
$this->expectException(PendingOperationError::class);
|
|
|
|
|
2020-06-30 21:45:09 +02:00
|
|
|
/** @var File\File $handle */
|
|
|
|
$handle = yield File\openFile(__FILE__, "r");
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2019-08-23 20:59:26 +02:00
|
|
|
$promise1 = $handle->read(10);
|
|
|
|
$promise2 = $handle->seek(0);
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2020-06-30 21:45:09 +02:00
|
|
|
$expected = \substr(yield File\read(__FILE__), 0, 10);
|
2019-08-23 20:59:26 +02:00
|
|
|
$this->assertSame($expected, yield $promise1);
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2019-08-23 20:59:26 +02:00
|
|
|
yield $promise2;
|
2017-06-20 07:06:12 +02:00
|
|
|
}
|
|
|
|
|
2019-08-23 20:59:26 +02:00
|
|
|
public function testReadWhileWriting(): \Generator
|
2018-10-27 17:57:31 +02:00
|
|
|
{
|
2019-08-23 20:23:33 +02:00
|
|
|
$this->expectException(PendingOperationError::class);
|
|
|
|
|
2020-02-21 20:12:48 +01:00
|
|
|
$path = Fixture::path() . "/temp";
|
|
|
|
|
2020-06-30 21:45:09 +02:00
|
|
|
/** @var File\File $handle */
|
|
|
|
$handle = yield File\openFile($path, "c+");
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2019-08-23 20:59:26 +02:00
|
|
|
$data = "test";
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2019-08-23 20:59:26 +02:00
|
|
|
$promise1 = $handle->write($data);
|
|
|
|
$promise2 = $handle->read(10);
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2019-08-23 20:59:26 +02:00
|
|
|
$this->assertSame(\strlen($data), yield $promise1);
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2020-02-21 20:12:48 +01:00
|
|
|
yield $promise2; // Should throw.
|
2017-06-20 07:06:12 +02:00
|
|
|
}
|
|
|
|
|
2019-08-23 20:59:26 +02:00
|
|
|
public function testWriteWhileReading(): \Generator
|
2018-10-27 17:57:31 +02:00
|
|
|
{
|
2019-08-23 20:23:33 +02:00
|
|
|
$this->expectException(PendingOperationError::class);
|
|
|
|
|
2020-02-21 20:12:48 +01:00
|
|
|
$path = Fixture::path() . "/temp";
|
|
|
|
|
2020-06-30 21:45:09 +02:00
|
|
|
/** @var File\File $handle */
|
|
|
|
$handle = yield File\openFile($path, "c+");
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2019-08-23 20:59:26 +02:00
|
|
|
$promise1 = $handle->read(10);
|
|
|
|
$promise2 = $handle->write("test");
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2020-06-30 21:45:09 +02:00
|
|
|
$expected = \substr(yield File\read(__FILE__), 0, 10);
|
2019-08-23 20:59:26 +02:00
|
|
|
$this->assertSame($expected, yield $promise1);
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2020-02-21 20:12:48 +01:00
|
|
|
yield $promise2; // Should throw.
|
2017-06-20 07:06:12 +02:00
|
|
|
}
|
|
|
|
}
|