You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.9 KiB

  1. /*
  2. * node-rdkafka - Node.js wrapper for RdKafka C/C++ library
  3. *
  4. * Copyright (c) 2016 Blizzard Entertainment
  5. *
  6. * This software may be modified and distributed under the terms
  7. * of the MIT license. See the LICENSE.txt file for details.
  8. */
  9. var Producer = require('../lib/producer');
  10. var t = require('assert');
  11. // var Mock = require('./mock');
  12. var client;
  13. var defaultConfig = {
  14. 'client.id': 'kafka-mocha',
  15. 'metadata.broker.list': 'localhost:9092',
  16. 'socket.timeout.ms': 250
  17. };
  18. var topicConfig = {};
  19. var server;
  20. module.exports = {
  21. 'Producer client': {
  22. 'beforeEach': function() {
  23. client = new Producer(defaultConfig, topicConfig);
  24. },
  25. 'afterEach': function() {
  26. client = null;
  27. },
  28. 'is an object': function() {
  29. t.equal(typeof(client), 'object');
  30. },
  31. 'requires configuration': function() {
  32. t.throws(function() {
  33. return new Producer();
  34. });
  35. },
  36. 'has necessary methods from superclass': function() {
  37. var methods = ['connect', 'disconnect', 'getMetadata'];
  38. methods.forEach(function(m) {
  39. t.equal(typeof(client[m]), 'function', 'Client is missing ' + m + ' method');
  40. });
  41. },
  42. 'has "_disconnect" override': function() {
  43. t.equal(typeof(client._disconnect), 'function', 'Producer is missing base _disconnect method');
  44. },
  45. 'does not modify config and clones it': function () {
  46. t.deepStrictEqual(defaultConfig, {
  47. 'client.id': 'kafka-mocha',
  48. 'metadata.broker.list': 'localhost:9092',
  49. 'socket.timeout.ms': 250
  50. });
  51. t.deepStrictEqual(client.globalConfig, {
  52. 'client.id': 'kafka-mocha',
  53. 'metadata.broker.list': 'localhost:9092',
  54. 'socket.timeout.ms': 250
  55. });
  56. t.notEqual(defaultConfig, client.globalConfig);
  57. },
  58. 'does not modify topic config and clones it': function () {
  59. t.deepStrictEqual(topicConfig, {});
  60. t.deepStrictEqual(client.topicConfig, {});
  61. t.notEqual(topicConfig, client.topicConfig);
  62. },
  63. 'disconnect method': {
  64. 'calls flush before it runs': function(next) {
  65. var providedTimeout = 1;
  66. client.flush = function(timeout, cb) {
  67. t.equal(providedTimeout, timeout, 'Timeouts do not match');
  68. t.equal(typeof(cb), 'function');
  69. setImmediate(cb);
  70. };
  71. client._disconnect = function(cb) {
  72. setImmediate(cb);
  73. };
  74. client.disconnect(providedTimeout, next);
  75. },
  76. 'provides a default timeout when none is provided': function(next) {
  77. client.flush = function(timeout, cb) {
  78. t.notEqual(timeout, undefined);
  79. t.notEqual(timeout, null);
  80. t.notEqual(timeout, 0);
  81. t.equal(typeof(cb), 'function');
  82. setImmediate(cb);
  83. };
  84. client._disconnect = function(cb) {
  85. setImmediate(cb);
  86. };
  87. client.disconnect(next);
  88. }
  89. }
  90. },
  91. };