2017-06-20 07:06:12 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\File\Test;
|
|
|
|
|
|
|
|
use Amp\File;
|
|
|
|
|
2018-10-27 17:57:31 +02:00
|
|
|
abstract class AsyncHandleTest extends HandleTest
|
|
|
|
{
|
2017-06-20 07:06:12 +02:00
|
|
|
/**
|
|
|
|
* @expectedException \Amp\File\PendingOperationError
|
|
|
|
*/
|
2018-10-27 17:57:31 +02:00
|
|
|
public function testSimultaneousReads()
|
|
|
|
{
|
2017-06-20 07:06:12 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
/** @var \Amp\File\Handle $handle */
|
|
|
|
$handle = yield File\open(__FILE__, "r");
|
|
|
|
|
|
|
|
$promise1 = $handle->read();
|
|
|
|
$promise2 = $handle->read();
|
|
|
|
|
|
|
|
$expected = \substr(yield File\get(__FILE__), 0, 20);
|
|
|
|
$this->assertSame($expected, yield $promise1);
|
|
|
|
|
|
|
|
yield $promise2;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Amp\File\PendingOperationError
|
|
|
|
*/
|
2018-10-27 17:57:31 +02:00
|
|
|
public function testSeekWhileReading()
|
|
|
|
{
|
2017-06-20 07:06:12 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
/** @var \Amp\File\Handle $handle */
|
|
|
|
$handle = yield File\open(__FILE__, "r");
|
|
|
|
|
|
|
|
$promise1 = $handle->read(10);
|
|
|
|
$promise2 = $handle->seek(0);
|
|
|
|
|
|
|
|
$expected = \substr(yield File\get(__FILE__), 0, 10);
|
|
|
|
$this->assertSame($expected, yield $promise1);
|
|
|
|
|
|
|
|
yield $promise2;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Amp\File\PendingOperationError
|
|
|
|
*/
|
2018-10-27 17:57:31 +02:00
|
|
|
public function testReadWhileWriting()
|
|
|
|
{
|
2017-06-20 07:06:12 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
/** @var \Amp\File\Handle $handle */
|
|
|
|
$handle = yield File\open(__FILE__, "r");
|
|
|
|
|
|
|
|
$data = "test";
|
|
|
|
|
|
|
|
$promise1 = $handle->write($data);
|
|
|
|
$promise2 = $handle->read(10);
|
|
|
|
|
|
|
|
$this->assertSame(\strlen($data), yield $promise1);
|
|
|
|
|
|
|
|
yield $promise2;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Amp\File\PendingOperationError
|
|
|
|
*/
|
2018-10-27 17:57:31 +02:00
|
|
|
public function testWriteWhileReading()
|
|
|
|
{
|
2017-06-20 07:06:12 +02:00
|
|
|
$this->execute(function () {
|
|
|
|
/** @var \Amp\File\Handle $handle */
|
|
|
|
$handle = yield File\open(__FILE__, "r");
|
|
|
|
|
|
|
|
$promise1 = $handle->read(10);
|
|
|
|
$promise2 = $handle->write("test");
|
|
|
|
|
|
|
|
$expected = \substr(yield File\get(__FILE__), 0, 10);
|
|
|
|
$this->assertSame($expected, yield $promise1);
|
|
|
|
|
|
|
|
yield $promise2;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|