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:
parent
d68ac6dc4f
commit
91505b49e7
@ -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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user