2022-12-17 22:52:21 +01:00
|
|
|
<?php declare(strict_types=1);
|
2017-06-20 07:06:12 +02:00
|
|
|
|
|
|
|
namespace Amp\File\Test;
|
|
|
|
|
2021-11-21 20:12:23 +01:00
|
|
|
use Amp\CancelledException;
|
2021-12-23 00:23:01 +01:00
|
|
|
use Amp\DeferredCancellation;
|
2017-06-20 07:06:12 +02:00
|
|
|
use Amp\File;
|
2019-08-23 20:23:33 +02:00
|
|
|
use Amp\File\PendingOperationError;
|
2021-12-04 15:23:37 +01:00
|
|
|
use function Amp\async;
|
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
|
|
|
{
|
2020-10-08 16:23:44 +02:00
|
|
|
public function testSimultaneousReads()
|
2018-10-27 17:57:31 +02:00
|
|
|
{
|
2019-08-23 20:23:33 +02:00
|
|
|
$this->expectException(PendingOperationError::class);
|
|
|
|
|
2021-10-16 04:53:50 +02:00
|
|
|
$handle = $this->driver->openFile(__FILE__, "r");
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2023-03-02 22:50:39 +01:00
|
|
|
$future1 = async(fn () => $handle->read(length: 20));
|
|
|
|
$future2 = async(fn () => $handle->read(length: 20));
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2020-10-08 16:23:44 +02:00
|
|
|
$expected = \substr(File\read(__FILE__), 0, 20);
|
2023-03-02 22:50:39 +01:00
|
|
|
$this->assertSame($expected, $future1->await());
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2023-03-02 22:50:39 +01:00
|
|
|
$future2->await();
|
2017-06-20 07:06:12 +02:00
|
|
|
}
|
|
|
|
|
2020-10-08 16:23:44 +02:00
|
|
|
public function testSeekWhileReading()
|
2018-10-27 17:57:31 +02:00
|
|
|
{
|
2019-08-23 20:23:33 +02:00
|
|
|
$this->expectException(PendingOperationError::class);
|
|
|
|
|
2021-10-16 04:53:50 +02:00
|
|
|
$handle = $this->driver->openFile(__FILE__, "r");
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2023-03-02 22:50:39 +01:00
|
|
|
$future1 = async(fn () => $handle->read(length: 10));
|
|
|
|
$future2 = async(fn () => $handle->read(length: 0));
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2020-10-08 16:23:44 +02:00
|
|
|
$expected = \substr(File\read(__FILE__), 0, 10);
|
2023-03-02 22:50:39 +01:00
|
|
|
$this->assertSame($expected, $future1->await());
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2023-03-02 22:50:39 +01:00
|
|
|
$future2->await();
|
2017-06-20 07:06:12 +02:00
|
|
|
}
|
|
|
|
|
2020-10-08 16:23:44 +02:00
|
|
|
public function testReadWhileWriting()
|
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";
|
|
|
|
|
2021-10-16 04:53:50 +02:00
|
|
|
$handle = $this->driver->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
|
|
|
|
2023-03-02 22:50:39 +01:00
|
|
|
$future1 = async(fn () => $handle->write($data));
|
|
|
|
$future2 = async(fn () => $handle->read(length: 10));
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2023-03-02 22:50:39 +01:00
|
|
|
$this->assertNull($future1->await());
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2023-03-02 22:50:39 +01:00
|
|
|
$future2->await();
|
2017-06-20 07:06:12 +02:00
|
|
|
}
|
|
|
|
|
2020-10-08 16:23:44 +02:00
|
|
|
public function testWriteWhileReading()
|
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";
|
|
|
|
|
2021-10-16 04:53:50 +02:00
|
|
|
$handle = $this->driver->openFile($path, "c+");
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2023-03-02 22:50:39 +01:00
|
|
|
$future1 = async(fn () => $handle->read(length: 10));
|
|
|
|
$future2 = async(fn () => $handle->write("test"));
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2023-03-02 22:50:39 +01:00
|
|
|
$this->assertNull($future1->await());
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2023-03-02 22:50:39 +01:00
|
|
|
$future2->await();
|
2017-06-20 07:06:12 +02:00
|
|
|
}
|
2021-11-21 20:12:23 +01:00
|
|
|
|
|
|
|
public function testCancelReadThenReadAgain()
|
|
|
|
{
|
|
|
|
$path = Fixture::path() . "/temp";
|
|
|
|
|
|
|
|
$handle = $this->driver->openFile($path, "c+");
|
|
|
|
|
2021-12-23 00:23:01 +01:00
|
|
|
$deferredCancellation = new DeferredCancellation();
|
|
|
|
$deferredCancellation->cancel();
|
2021-11-21 20:12:23 +01:00
|
|
|
|
2021-12-23 00:23:01 +01:00
|
|
|
$handle->write("test");
|
2021-11-21 20:12:23 +01:00
|
|
|
$handle->seek(0);
|
|
|
|
|
|
|
|
try {
|
2021-12-23 00:23:01 +01:00
|
|
|
$handle->read(cancellation: $deferredCancellation->getCancellation(), length: 2);
|
2021-11-21 20:12:23 +01:00
|
|
|
$handle->seek(0); // If the read succeeds (e.g.: ParallelFile), we need to seek back to 0.
|
|
|
|
} catch (CancelledException) {
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertSame("test", $handle->read());
|
|
|
|
}
|
2017-06-20 07:06:12 +02:00
|
|
|
}
|