1
0
mirror of https://github.com/danog/file.git synced 2024-11-30 04:19:39 +01:00
file/test/AsyncHandleTest.php

75 lines
2.1 KiB
PHP
Raw Permalink Normal View History

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