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.

104 lines
3.3 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 TopicPartition = require('../lib/topic-partition');
  10. var Topic = require('../lib/topic');
  11. var t = require('assert');
  12. module.exports = {
  13. 'TopicPartition': {
  14. 'is a function': function() {
  15. t.equal(typeof(TopicPartition), 'function');
  16. },
  17. 'be constructable': function() {
  18. var toppar = new TopicPartition('topic', 1, 0);
  19. t.equal(toppar.topic, 'topic');
  20. t.equal(toppar.offset, 0);
  21. t.equal(toppar.partition, 1);
  22. },
  23. 'be creatable using 0 as the partition': function() {
  24. var toppar = new TopicPartition('topic', 0, 0);
  25. t.equal(toppar.topic, 'topic');
  26. t.equal(toppar.offset, 0);
  27. t.equal(toppar.partition, 0);
  28. },
  29. 'throw if partition is null or undefined': function() {
  30. t.throws(function() {
  31. var tp = new TopicPartition('topic', undefined, 0);
  32. });
  33. t.throws(function() {
  34. var tp = new TopicPartition('topic', null, 0);
  35. });
  36. },
  37. 'sets offset to stored by default': function() {
  38. var toppar = new TopicPartition('topic', 1);
  39. t.equal(toppar.topic, 'topic');
  40. t.equal(toppar.partition, 1);
  41. t.equal(toppar.offset, Topic.OFFSET_STORED);
  42. },
  43. 'sets offset to end if "end" is provided"': function() {
  44. var toppar = new TopicPartition('topic', 1, 'end');
  45. t.equal(toppar.topic, 'topic');
  46. t.equal(toppar.partition, 1);
  47. t.equal(toppar.offset, Topic.OFFSET_END);
  48. },
  49. 'sets offset to end if "latest" is provided"': function() {
  50. var toppar = new TopicPartition('topic', 1, 'latest');
  51. t.equal(toppar.topic, 'topic');
  52. t.equal(toppar.partition, 1);
  53. t.equal(toppar.offset, Topic.OFFSET_END);
  54. },
  55. 'sets offset to beginning if "beginning" is provided"': function() {
  56. var toppar = new TopicPartition('topic', 1, 'beginning');
  57. t.equal(toppar.topic, 'topic');
  58. t.equal(toppar.partition, 1);
  59. t.equal(toppar.offset, Topic.OFFSET_BEGINNING);
  60. },
  61. 'sets offset to start if "beginning" is provided"': function() {
  62. var toppar = new TopicPartition('topic', 1, 'beginning');
  63. t.equal(toppar.topic, 'topic');
  64. t.equal(toppar.partition, 1);
  65. t.equal(toppar.offset, Topic.OFFSET_BEGINNING);
  66. },
  67. 'sets offset to stored if "stored" is provided"': function() {
  68. var toppar = new TopicPartition('topic', 1, 'stored');
  69. t.equal(toppar.topic, 'topic');
  70. t.equal(toppar.partition, 1);
  71. t.equal(toppar.offset, Topic.OFFSET_STORED);
  72. },
  73. 'throws when an invalid special offset is provided"': function() {
  74. t.throws(function() {
  75. var toppar = new TopicPartition('topic', 1, 'fake');
  76. });
  77. }
  78. },
  79. 'TopicPartition.map': {
  80. 'is a function': function() {
  81. t.equal(typeof(TopicPartition.map), 'function');
  82. },
  83. 'converts offsets inside the array': function() {
  84. var result = TopicPartition.map([{ topic: 'topic', partition: 1, offset: 'stored' }]);
  85. var toppar = result[0];
  86. t.equal(toppar.topic, 'topic');
  87. t.equal(toppar.partition, 1);
  88. t.equal(toppar.offset, Topic.OFFSET_STORED);
  89. },
  90. },
  91. };