mirror of
https://github.com/danog/file.git
synced 2024-12-02 09:17:57 +01:00
Await pending writes before closing
This commit is contained in:
parent
d68ac6dc4f
commit
91505b49e7
@ -70,7 +70,16 @@ abstract class QueuedWritesFile implements File
|
|||||||
public function end(): void
|
public function end(): void
|
||||||
{
|
{
|
||||||
$this->writable = false;
|
$this->writable = false;
|
||||||
|
|
||||||
|
if ($this->queue->isEmpty()) {
|
||||||
$this->close();
|
$this->close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$future = $this->queue->top()->finally($this->close(...));
|
||||||
|
$this->queue->push($future);
|
||||||
|
|
||||||
|
$future->await();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -4,6 +4,7 @@ namespace Amp\File\Test;
|
|||||||
|
|
||||||
use Amp\ByteStream\ClosedException;
|
use Amp\ByteStream\ClosedException;
|
||||||
use Amp\File;
|
use Amp\File;
|
||||||
|
use function Amp\async;
|
||||||
|
|
||||||
abstract class FileTest extends FilesystemTest
|
abstract class FileTest extends FilesystemTest
|
||||||
{
|
{
|
||||||
@ -204,6 +205,22 @@ abstract class FileTest extends FilesystemTest
|
|||||||
$handle->read();
|
$handle->read();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testCloseWithPendingWrites(): void
|
||||||
|
{
|
||||||
|
$path = Fixture::path() . "/write";
|
||||||
|
|
||||||
|
$handle = $this->driver->openFile($path, "c+");
|
||||||
|
|
||||||
|
$data = "data";
|
||||||
|
$writeFuture = async($handle->write(...), $data);
|
||||||
|
$closeFuture = async($handle->close(...));
|
||||||
|
|
||||||
|
$writeFuture->await();
|
||||||
|
$closeFuture->await();
|
||||||
|
|
||||||
|
$this->assertSame($data, $this->driver->read($path));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @depends testWrite
|
* @depends testWrite
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user