diff --git a/src/Internal/PostgresHandleConnection.php b/src/Internal/PostgresHandleConnection.php index 9a6cc57..edbe9e6 100644 --- a/src/Internal/PostgresHandleConnection.php +++ b/src/Internal/PostgresHandleConnection.php @@ -82,8 +82,10 @@ private function awaitPending(): void */ private function reserve(): void { + $this->awaitPending(); \assert($this->busy === null); - $this->busy = new DeferredFuture; + + $this->busy = new DeferredFuture(); } /** diff --git a/test/AbstractConnectionTest.php b/test/AbstractConnectionTest.php index b321835..67dd6aa 100644 --- a/test/AbstractConnectionTest.php +++ b/test/AbstractConnectionTest.php @@ -142,4 +142,21 @@ public function testTransactionsCallbacksOnDestruct(): void unset($transaction); delay(0.1); // Destructor is async, so give control to the loop to invoke callbacks. } + + /** + * Ensure prior transaction has fully completed before attempting to begin another transaction. + * @see https://github.com/amphp/postgres/pull/73 + */ + public function testSequentialTransactionsWaitForPreviousRelease(): void + { + $connection = $this->executor; + + $transaction = $connection->beginTransaction(); + $transaction->onClose($this->createCallback(1)); + $transaction->commit(); + + $transaction = $connection->beginTransaction(); + $transaction->onClose($this->createCallback(1)); + $transaction->commit(); + } }