test-send-and-receive-large-packets.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. var common = require('../../common');
  2. var connection = common.createConnection();
  3. var assert = require('assert');
  4. connection.connect(function(err) {
  5. if (err) throw err;
  6. getMaxAllowedPacket();
  7. });
  8. var oldMaxAllowedPacket;
  9. function getMaxAllowedPacket() {
  10. connection.query('SHOW VARIABLES WHERE Variable_name = ?', ['max_allowed_packet'], function(err, rows) {
  11. if (err) throw err;
  12. oldMaxAllowedPacket = Number(rows[0].Value);
  13. increaseMaxAllowedPacketIfNeeded();
  14. });
  15. }
  16. function increaseMaxAllowedPacketIfNeeded() {
  17. // Our test generates a SQL query a few bytes larger than 16 MB, but lets
  18. // leave a little margin:
  19. var minMaxAllowedPacket = 20 * 1024 * 1024;
  20. var newMaxAllowedPacket = (oldMaxAllowedPacket < minMaxAllowedPacket)
  21. ? minMaxAllowedPacket
  22. : oldMaxAllowedPacket;
  23. connection.query('SET GLOBAL max_allowed_packet = ?', [newMaxAllowedPacket], function(err, rows) {
  24. if (err) throw err;
  25. // We need to re-connect for this change to take effect, bah
  26. connection.end();
  27. connection = common.createConnection();
  28. // We need to wait for the re-connect to happen before starting the actual
  29. // test. That's because our buffer to hex shim in 0.4.x takes ~12 sec on
  30. // TravisCI, causing a MySQL connection timeout otherwise.
  31. connection.connect(function(err) {
  32. if (err) throw err;
  33. triggerLargeQueryAndResponsePackets();
  34. });
  35. });
  36. }
  37. var rows = [];
  38. var length = Math.pow(256, 3) / 2; // Half, because of hex encoding
  39. var buffer = new Buffer(length);
  40. var sql = 'SELECT ? as bigField';
  41. function triggerLargeQueryAndResponsePackets() {
  42. connection.query(sql, [buffer], function(err, _rows) {
  43. if (err) throw err;
  44. rows = _rows;
  45. resetMaxAllowedPacket();
  46. });
  47. }
  48. function resetMaxAllowedPacket() {
  49. connection.query('SET GLOBAL max_allowed_packet = ?', [oldMaxAllowedPacket], function(err, rows) {
  50. if (err) {
  51. err.message = 'Could not reset max_allowed_packet size, please check your server settings: ' + err.message;
  52. throw err;
  53. }
  54. });
  55. connection.end();
  56. }
  57. process.on('exit', function() {
  58. assert.equal(rows.length, 1);
  59. assert.equal(rows[0].bigField.length, length);
  60. });