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

Await pending writes before closing

This commit is contained in:
Aaron Piotrowski 2022-11-20 10:39:47 -06:00
parent d68ac6dc4f
commit 91505b49e7
No known key found for this signature in database
GPG Key ID: 5B456E6AABA44A63
2 changed files with 27 additions and 1 deletions

View File

@ -70,7 +70,16 @@ abstract class QueuedWritesFile implements File
public function end(): void
{
$this->writable = false;
$this->close();
if ($this->queue->isEmpty()) {
$this->close();
return;
}
$future = $this->queue->top()->finally($this->close(...));
$this->queue->push($future);
$future->await();
}
/**

View File

@ -4,6 +4,7 @@ namespace Amp\File\Test;
use Amp\ByteStream\ClosedException;
use Amp\File;
use function Amp\async;
abstract class FileTest extends FilesystemTest
{
@ -204,6 +205,22 @@ abstract class FileTest extends FilesystemTest
$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
*/