test-exception-safety.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // This test covers all event / callback interfaces offered by node-mysql and
  2. // throws an exception in them. Exception safety means that each of those
  3. // exceptions can be caught by an 'uncaughtException' / Domain handler without
  4. // the connection instance ending up in a bad state where it doesn't work
  5. // properly or doesn't execute the next sequence anymore.
  6. var common = require('../../common');
  7. var connection = common.createConnection();
  8. var assert = require('assert');
  9. var errors = [];
  10. process.on('uncaughtException', function(err) {
  11. console.log(err.stack);
  12. errors.push(err);
  13. });
  14. // Normal callback
  15. connection.connect(function(err) {
  16. throw err || new Error('1');
  17. });
  18. // Normal callback (same code path as connect, but in here should that
  19. // implementation detail change at some point).
  20. connection.query('SELECT 1', function(err) {
  21. throw err || new Error('2');
  22. });
  23. // Row streaming events
  24. connection.query('SELECT 1')
  25. .on('fields', function() {
  26. throw new Error('3');
  27. })
  28. .on('result', function() {
  29. throw new Error('4');
  30. });
  31. // Normal callback with error
  32. connection.query('INVALID SQL', function(err) {
  33. assert.equal(err.code, 'ER_PARSE_ERROR');
  34. throw new Error('5');
  35. });
  36. // Row streaming 'result' event triggered by Ok Packet (special code path)
  37. connection.query('USE ' + common.testDatabase)
  38. .on('result', function() {
  39. throw new Error('6');
  40. });
  41. // Normal callback (same code path as connect, but in here should that
  42. // implementation detail change at some point).
  43. connection.end(function(err) {
  44. throw err || new Error('7');
  45. });
  46. process.on('exit', function() {
  47. process.removeAllListeners();
  48. var expectedErrors = 7;
  49. for (var i = 0; i < expectedErrors - 1; i++) {
  50. var error = errors[i];
  51. assert.equal(error.message, String(i + 1));
  52. assert.equal(error.code, undefined);
  53. }
  54. });