mysql2-sudden-exit-without-error.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * Test case when mysql2 driver strangely exits when one tries to send query
  3. * to dead connection.
  4. */
  5. const Bluebird = require('bluebird');
  6. const toxiproxy = require('toxiproxy-node-client');
  7. const toxicli = new toxiproxy.Toxiproxy('http://localhost:8474');
  8. const rp = require('request-promise-native');
  9. // drops old toxicproxy and creates new
  10. async function recreateProxy(serviceName, listenPort, proxyToPort) {
  11. try {
  12. await rp.delete({
  13. url: `${toxicli.host}/proxies/${serviceName}`
  14. });
  15. } catch(err) {
  16. // there was no proxy by that name... its ok
  17. }
  18. const proxy = await toxicli.createProxy({
  19. name: serviceName,
  20. listen: `0.0.0.0:${listenPort}`,
  21. upstream: `${serviceName}:${proxyToPort}`
  22. });
  23. }
  24. async function insanelyParanoidQuery(con) {
  25. console.log('sending query');
  26. const res = await new Promise((resolve,reject) => {
  27. try {
  28. con.query('select 1', [], function(err, rows, fields) {
  29. if (err) {
  30. reject(err);
  31. } else {
  32. resolve(rows);
  33. }
  34. });
  35. } catch(err) {
  36. console.log('Huh synchronous exception?! (shouldnt be possible)');
  37. reject(err);
  38. }
  39. });
  40. console.log(res);
  41. }
  42. async function main() {
  43. // create proxy from localhost:23306 -> mysqldocker:3306
  44. await recreateProxy('mysql', 23306, 3306);
  45. // ------------- setup mysql2 db driver connection
  46. const mysql2 = require('mysql2'); // with mysql this works...
  47. const mysql2Con = await mysql2.createConnection({
  48. host: 'localhost',
  49. user: 'root',
  50. password: 'mysqlrootpassword',
  51. port: 23306
  52. });
  53. mysql2Con.on('error', function(err) {
  54. console.log("I'm dead", err);
  55. })
  56. console.log('Going to cut connections');
  57. // cut connection during recreate
  58. await recreateProxy('mysql', 23306, 3306);
  59. console.log('Proxy recreated... start waiting');
  60. // wait forever
  61. while(true) {
  62. await Bluebird.delay(1000);
  63. try {
  64. await insanelyParanoidQuery(mysql2Con);
  65. } catch (err) {
  66. console.log('query failed:', err);
  67. }
  68. await recreateProxy('mysql', 23306, 3306);
  69. }
  70. }
  71. main()
  72. .then(() => console.log('Process stopped normally'))
  73. .catch(err => {
  74. console.log('Process stopped to failure', err);
  75. })
  76. console.log('Waiting for eventloop to stop...');
  77. process.on('uncaughtException', function (err) {
  78. console.log('uncaughtException', err);
  79. });
  80. process.on('exit', function () {
  81. console.log('Did someone call process.exit() or wat? exitCode:', process.exitCode);
  82. });