8000 Merge pull request #178 from clue-labs/unhandled-rejections · friends-of-reactphp/mysql@f9c55f5 · GitHub
[go: up one dir, main page]

Skip to content

Commit f9c55f5

Browse files
authored
Merge pull request #178 from clue-labs/unhandled-rejections
Update close handler to avoid unhandled promise rejections
2 parents a13dbbb + 9c342f3 commit f9c55f5

File tree

6 files changed

+39
-22
lines changed

6 files changed

+39
-22
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"evenement/evenement": "^3.0 || ^2.1 || ^1.1",
99
"react/event-loop": "^1.2",
1010
"react/promise": "^3 || ^2.7",
11-
"react/promise-stream": "^1.4",
11+
"react/promise-stream": "^1.6",
1212
"react/promise-timer": "^1.9",
1313
"react/socket": "^1.12"
1414
},

src/Factory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ public function createConnection(
202202
// either close successful connection or cancel pending connection attempt
203203
$connecting->then(function (SocketConnectionInterface $connection) {
204204
$connection->close();
205+
}, function () {
206+
// ignore to avoid reporting unhandled rejection
205207
});
206208
$connecting->cancel();
207209
});

src/Io/LazyConnection.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ public function close()
220220
if ($this->connecting !== null) {
221221
$this->connecting->then(function (ConnectionInterface $connection) {
222222
$connection->close();
223+
}, function () {
224+
// ignore to avoid reporting unhandled rejection
223225
});
224226
if ($this->connecting !== null) {
225227
$this->connecting->cancel();

tests/FactoryTest.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public function testConnectWithValidAuthWillRunUntilQuit()
279279
$connection->quit()->then(function () {
280280
echo 'closed.';
281281
});
282-
}, 'printf')->then(null, 'printf');
282+
});
283283

284284
Loop::run();
285285
}
@@ -296,7 +296,7 @@ public 8000 function testConnectWithValidAuthAndWithoutDbNameWillRunUntilQuit()
296296
$connection->quit()->then(function () {
297297
echo 'closed.';
298298
});
299-
}, 'printf')->then(null, 'printf');
299+
});
300300

301301
Loop::run();
302302
}
@@ -313,7 +313,7 @@ public function testConnectWithValidAuthWillIgnoreNegativeTimeoutAndRunUntilQuit
313313
$connection->quit()->then(function () {
314314
echo 'closed.';
315315
});
316-
}, 'printf')->then(null, 'printf');
316+
});
317317

318318
Loop::run();
319319
}
@@ -333,8 +333,7 @@ public function testConnectWithValidAuthCanPingAndThenQuit()
333333
echo 'closed.';
334334
});
335335
});
336-
337-
}, 'printf')->then(null, 'printf');
336+
});
338337

339338
Loop::run();
340339
}
@@ -354,14 +353,14 @@ public function testConnectWithValidAuthCanQueuePingAndQuit()
354353
$connection->quit()->then(function () {
355354
echo 'closed.';
356355
});
357-
}, 'printf')->then(null, 'printf');
356+
});
358357

359358
Loop::run();
360359
}
361360

362361
public function testConnectWithValidAuthQuitOnlyOnce()
363362
{
364-
$this->expectOutputString('connected.closed.');
363+
$this->expectOutputString('connected.rejected.closed.');
365364

366365
$factory = new Factory();
367366

@@ -372,9 +371,11 @@ public function testConnectWithValidAuthQuitOnlyOnce()
372371
echo 'closed.';
373372
});
374373
$connection->quit()->then(function () {
375-
echo 'closed.';
374+
echo 'never reached.';
375+
}, function () {
376+
echo 'rejected.';
376377
});
377-
}, 'printf')->then(null, 'printf');
378+
});
378379

379380
Loop::run();
380381
}
@@ -397,7 +398,7 @@ public function testConnectWithValidAuthCanCloseOnlyOnce()
397398

398399
$connection->close();
399400
$connection->close();
400-
}, 'printf')->then(null, 'printf');
401+
});
401402

402403
Loop::run();
403404
}
@@ -425,7 +426,7 @@ public function testConnectWithValidAuthCanCloseAndAbortPing()
425426
echo 'aborted queued (' . $e->getMessage() . ').';
426427
});
427428
$connection->close();
428-
}, 'printf')->then(null, 'printf');
429+
});
429430

430431
Loop::run();
431432
}
@@ -626,7 +627,7 @@ public function testConnectLazyWithInvalidAuthWillRejectPingButWillNotEmitErrorO
626627

627628
public function testConnectLazyWithValidAuthWillPingBeforeQuitButNotAfter()
628629
{
629-
$this->expectOutputString('ping.closed.');
630+
$this->expectOutputString('rejected.ping.closed.');
630631

631632
$factory = new Factory();
632633

@@ -643,6 +644,8 @@ public function testConnectLazyWithValidAuthWillPingBeforeQuitButNotAfter()
643644

644645
$connection->ping()->then(function () {
645646
echo 'never reached';
647+
}, function () {
648+
echo 'rejected.';
646649
});
647650

648651
Loop::run();

tests/Io/LazyConnectionTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ public function testPingWillNotCloseConnectionWhenPendingConnectionFails()
2424
$connection->on('error', $this->expectCallableNever());
2525
$connection->on('close', $this->expectCallableNever());
2626

27-
$connection->ping();
27+
$promise = $connection->ping();
28+
29+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
2830

2931
$deferred->reject(new \RuntimeException());
3032
}
33+
3134
public function testPingWillNotCloseConnectionWhenUnderlyingConnectionCloses()
3235
{
3336
$base = $this->getMockBuilder('React\MySQL\Io\LazyConnection')->setMethods(['ping'])->disableOriginalConstructor()->getMock();
@@ -678,7 +681,10 @@ public function testCloseAfterPingDoesNotEmitConnectionErrorFromAbortedConnectio
678681
$connection->on('error', $this->expectCallableNever());
679682
$connection->on('close', $this->expectCallableOnce());
680683

681-
$connection->ping();
684+
$promise = $connection->ping();
685+
686+
$promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection
687+
682688
$connection->close();
683689
}
684690

tests/ResultQueryTest.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ public function testSelectStaticText()
1717
$this->assertCount(1, $command->resultRows);
1818
$this->assertCount(1, $command->resultRows[0]);
1919
$this->assertSame('foo', reset($command->resultRows[0]));
20-
21-
$this->assertInstanceOf('React\MySQL\Connection', $conn);
2220
});
2321

2422
$connection->quit();
@@ -57,7 +55,7 @@ public function testSelectStaticValueWillBeReturnedAsIs($value)
5755
$this->assertCount(1, $command->resultRows);
5856
$this->assertCount(1, $command->resultRows[0]);
5957
$this->assertSame($expected, reset($command->resultRows[0]));
60-
})->then(null, 'printf');
58+
});
6159

6260
$connection->quit();
6361
Loop::run();
@@ -82,7 +80,7 @@ public function testSelectStaticValueWillBeReturnedAsIsWithNoBackslashEscapesSql
8280
$this->assertCount(1, $command->resultRows);
8381
$this->assertCount(1, $command->resultRows[0]);
8482
$this->assertSame($expected, reset($command->resultRows[0]));
85-
})->then(null, 'printf');
83+
});
8684

8785
$connection->quit();
8886
Loop::run();
@@ -138,7 +136,7 @@ public function testSelectLongStaticTextHasTypeStringWithValidLength()
138136

139137
$connection->query('SELECT ?', [$value])->then(function (QueryResult $command) use ($length) {
140138
$this->assertCount(1, $command->resultFields);
141-
$this->assertEquals($length * 3, $command->resultFields[0]['length']);
139+
$this->assertEquals($length * 4, $command->resultFields[0]['length']);
142140
$this->assertSame(Constants::FIELD_TYPE_VAR_STRING, $command->resultFields[0]['type']);
143141
});
144142

@@ -430,7 +428,7 @@ public function testInvalidSelectShouldFail()
430428

431429
$connection->query('select * from invalid_table')->then(
432430
$this->expectCallableNever(),
433-
function (\Exception $error) {
431+
function (\Exception $error) use ($db) {
434432
$this->assertEquals("Table '$db.invalid_table' doesn't exist", $error->getMessage());
435433
}
436434
);
@@ -446,7 +444,13 @@ public function testInvalidMultiStatementsShouldFailToPreventSqlInjections()
446444
$connection->query('select 1;select 2;')->then(
447445
$this->expectCallableNever(),
448446
function (\Exception $error) {
449-
$this->assertContains("You have an error in your SQL syntax", $error->getMessage());
447+
if (method_exists($this, 'assertStringContainsString')) {
448+
// PHPUnit 9+
449+
$this->assertStringContainsString("You have an error in your SQL syntax", $error->getMessage());
450+
} else {
451+
// legacy PHPUnit < 9
452+
$this->assertContains("You have an error in your SQL syntax", $error->getMessage());
453+
}
450454
}
451455
);
452456

0 commit comments

Comments
 (0)
0