1
0
mirror of https://github.com/danog/file.git synced 2024-12-03 09:47:54 +01:00
file/test/AsyncFileTest.php

76 lines
1.8 KiB
PHP
Raw Normal View History

<?php
namespace Amp\File\Test;
use Amp\File;
2019-08-23 20:23:33 +02:00
use Amp\File\PendingOperationError;
2020-10-08 16:23:44 +02:00
use function Amp\async;
use function Amp\await;
abstract class AsyncFileTest extends FileTest
{
2020-10-08 16:23:44 +02:00
public function testSimultaneousReads()
{
2019-08-23 20:23:33 +02:00
$this->expectException(PendingOperationError::class);
2020-10-08 16:23:44 +02:00
$handle = File\openFile(__FILE__, "r");
2020-10-08 16:23:44 +02:00
$promise1 = async(fn() => $handle->read(20));
$promise2 = async(fn() => $handle->read(20));
2020-10-08 16:23:44 +02:00
$expected = \substr(File\read(__FILE__), 0, 20);
$this->assertSame($expected, await($promise1));
2020-10-08 16:23:44 +02:00
await($promise2);
}
2020-10-08 16:23:44 +02:00
public function testSeekWhileReading()
{
2019-08-23 20:23:33 +02:00
$this->expectException(PendingOperationError::class);
2020-10-08 16:23:44 +02:00
$handle = File\openFile(__FILE__, "r");
2020-10-08 16:23:44 +02:00
$promise1 = async(fn() => $handle->read(10));
$promise2 = async(fn() => $handle->read(0));
2020-10-08 16:23:44 +02:00
$expected = \substr(File\read(__FILE__), 0, 10);
$this->assertSame($expected, await($promise1));
2020-10-08 16:23:44 +02:00
await($promise2);
}
2020-10-08 16:23:44 +02:00
public function testReadWhileWriting()
{
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-10-08 16:23:44 +02:00
$handle = File\openFile($path, "c+");
2019-08-23 20:59:26 +02:00
$data = "test";
2020-10-08 16:23:44 +02:00
$promise1 = async(fn() => $handle->write($data));
$promise2 = async(fn() => $handle->read(10));
2020-10-08 16:23:44 +02:00
$this->assertNull(await($promise1));
2020-10-08 16:23:44 +02:00
await($promise2);
}
2020-10-08 16:23:44 +02:00
public function testWriteWhileReading()
{
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-10-08 16:23:44 +02:00
$handle = File\openFile($path, "c+");
2020-10-08 16:23:44 +02:00
$promise1 = async(fn() => $handle->read(10));
$promise2 = async(fn() => $handle->write("test"));
2020-10-08 16:23:44 +02:00
$this->assertNull(await($promise1));
2020-10-08 16:23:44 +02:00
await($promise2);
}
}