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

79 lines
2.0 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;
abstract class AsyncFileTest extends FileTest
{
2019-08-23 20:59:26 +02:00
public function testSimultaneousReads(): \Generator
{
2019-08-23 20:23:33 +02:00
$this->expectException(PendingOperationError::class);
/** @var \Amp\File\File $handle */
2019-08-23 20:59:26 +02:00
$handle = yield File\open(__FILE__, "r");
2019-08-23 20:59:26 +02:00
$promise1 = $handle->read();
$promise2 = $handle->read();
2019-08-23 20:59:26 +02:00
$expected = \substr(yield File\get(__FILE__), 0, 20);
$this->assertSame($expected, yield $promise1);
2019-08-23 20:59:26 +02:00
yield $promise2;
}
2019-08-23 20:59:26 +02:00
public function testSeekWhileReading(): \Generator
{
2019-08-23 20:23:33 +02:00
$this->expectException(PendingOperationError::class);
/** @var \Amp\File\File $handle */
2019-08-23 20:59:26 +02:00
$handle = yield File\open(__FILE__, "r");
2019-08-23 20:59:26 +02:00
$promise1 = $handle->read(10);
$promise2 = $handle->seek(0);
2019-08-23 20:59:26 +02:00
$expected = \substr(yield File\get(__FILE__), 0, 10);
$this->assertSame($expected, yield $promise1);
2019-08-23 20:59:26 +02:00
yield $promise2;
}
2019-08-23 20:59:26 +02:00
public function testReadWhileWriting(): \Generator
{
2019-08-23 20:23:33 +02:00
$this->expectException(PendingOperationError::class);
2020-02-21 20:12:48 +01:00
$path = Fixture::path() . "/temp";
/** @var \Amp\File\File $handle */
2020-02-21 20:12:48 +01:00
$handle = yield File\open($path, "c+");
2019-08-23 20:59:26 +02:00
$data = "test";
2019-08-23 20:59:26 +02:00
$promise1 = $handle->write($data);
$promise2 = $handle->read(10);
2019-08-23 20:59:26 +02:00
$this->assertSame(\strlen($data), yield $promise1);
2020-02-21 20:12:48 +01:00
yield $promise2; // Should throw.
}
2019-08-23 20:59:26 +02:00
public function testWriteWhileReading(): \Generator
{
2019-08-23 20:23:33 +02:00
$this->expectException(PendingOperationError::class);
2020-02-21 20:12:48 +01:00
$path = Fixture::path() . "/temp";
/** @var \Amp\File\File $handle */
2020-02-21 20:12:48 +01:00
$handle = yield File\open($path, "c+");
2019-08-23 20:59:26 +02:00
$promise1 = $handle->read(10);
$promise2 = $handle->write("test");
2019-08-23 20:59:26 +02:00
$expected = \substr(yield File\get(__FILE__), 0, 10);
$this->assertSame($expected, yield $promise1);
2020-02-21 20:12:48 +01:00
yield $promise2; // Should throw.
}
}