test-streaming-rows.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. var common = require('../../common');
  2. var connection = common.createConnection();
  3. var assert = require('assert');
  4. common.useTestDb(connection);
  5. var table = 'stream_test';
  6. connection.query([
  7. 'CREATE TEMPORARY TABLE `' + table + '` (',
  8. '`id` int(11) unsigned NOT NULL AUTO_INCREMENT,',
  9. '`title` varchar(255),',
  10. 'PRIMARY KEY (`id`)',
  11. ') ENGINE=InnoDB DEFAULT CHARSET=utf8'
  12. ].join('\n'));
  13. var rowCount = 10;
  14. for (var i = 1; i <= rowCount; i++) {
  15. var row = {
  16. id: i,
  17. title: 'Row #' + i,
  18. };
  19. connection.query('INSERT INTO ' + table + ' SET ?', row);
  20. }
  21. var paused = false;
  22. var query = connection.query('SELECT * FROM ' + table);
  23. var hadEnd = 0;
  24. var rows = [];
  25. var fields = undefined;
  26. query
  27. .on('fields', function(_fields, index) {
  28. assert.equal(index, 0);
  29. assert.ok(!fields);
  30. fields = _fields;
  31. })
  32. .on('result', function(row) {
  33. // Make sure we never receive a row while being paused
  34. assert.equal(paused, false);
  35. paused = true;
  36. connection.pause();
  37. setTimeout(function() {
  38. paused = false;
  39. connection.resume();
  40. rows.push(row);
  41. }, 10);
  42. })
  43. .on('end', function() {
  44. hadEnd = true;
  45. });
  46. connection.end();
  47. process.on('exit', function() {
  48. assert.equal(rows.length, 10);
  49. assert.equal(hadEnd, true);
  50. assert.equal(fields[0].name, 'id');
  51. assert.equal(fields[1].name, 'title');
  52. });