2017-06-20 07:06:12 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\File\Test;
|
|
|
|
|
|
|
|
use Amp\File;
|
2018-07-27 21:00:26 +02:00
|
|
|
use Concurrent\Task;
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
abstract class AsyncHandleTest extends HandleTest
|
|
|
|
{
|
|
|
|
public function testSimultaneousReads(): void
|
|
|
|
{
|
2017-06-20 07:06:12 +02:00
|
|
|
$this->execute(function () {
|
2018-07-27 21:00:26 +02:00
|
|
|
$handle = File\open(__FILE__, "r");
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
$awaitable1 = Task::async([$handle, 'read'], 20);
|
|
|
|
$awaitable2 = Task::async([$handle, 'read'], 20);
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
$expected = \substr(File\get(__FILE__), 0, 20);
|
|
|
|
$this->assertSame($expected, Task::await($awaitable1));
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
$this->expectException(File\PendingOperationError::class);
|
|
|
|
Task::await($awaitable2);
|
2017-06-20 07:06:12 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
public function testSeekWhileReading(): void
|
|
|
|
{
|
2017-06-20 07:06:12 +02:00
|
|
|
$this->execute(function () {
|
2018-07-27 21:00:26 +02:00
|
|
|
$handle = File\open(__FILE__, "r");
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
$awaitable1 = Task::async([$handle, 'read'], 10);
|
|
|
|
$awaitable2 = Task::async([$handle, 'seek'], 0);
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
$expected = \substr(File\get(__FILE__), 0, 10);
|
|
|
|
$this->assertSame($expected, Task::await($awaitable1));
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
$this->expectException(File\PendingOperationError::class);
|
|
|
|
Task::await($awaitable2);
|
2017-06-20 07:06:12 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
public function testReadWhileWriting(): void
|
|
|
|
{
|
2017-06-20 07:06:12 +02:00
|
|
|
$this->execute(function () {
|
2018-07-27 21:00:26 +02:00
|
|
|
$handle = File\open(__FILE__, "r");
|
2017-06-20 07:06:12 +02:00
|
|
|
|
|
|
|
$data = "test";
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
$awaitable1 = Task::async([$handle, 'write'], $data);
|
|
|
|
$awaitable2 = Task::async([$handle, 'read'], 10);
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
Task::await($awaitable1);
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
$this->expectException(File\PendingOperationError::class);
|
|
|
|
Task::await($awaitable2);
|
2017-06-20 07:06:12 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
public function testWriteWhileReading(): void
|
|
|
|
{
|
2017-06-20 07:06:12 +02:00
|
|
|
$this->execute(function () {
|
2018-07-27 21:00:26 +02:00
|
|
|
$handle = File\open(__FILE__, "r");
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
$awaitable1 = Task::async([$handle, 'read'], 10);
|
|
|
|
$awaitable2 = Task::async([$handle, 'write'], "test");
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
$expected = \substr(File\get(__FILE__), 0, 10);
|
|
|
|
$this->assertSame($expected, Task::await($awaitable1));
|
2017-06-20 07:06:12 +02:00
|
|
|
|
2018-07-27 21:00:26 +02:00
|
|
|
$this->expectException(File\PendingOperationError::class);
|
|
|
|
Task::await($awaitable2);
|
2017-06-20 07:06:12 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|