abstract_store.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. 'use strict'
  2. require('should')
  3. module.exports = function abstractStoreTest (build) {
  4. var store
  5. beforeEach(function (done) {
  6. build(function (err, _store) {
  7. store = _store
  8. done(err)
  9. })
  10. })
  11. afterEach(function (done) {
  12. store.close(done)
  13. })
  14. it('should put and stream in-flight packets', function (done) {
  15. var packet = {
  16. topic: 'hello',
  17. payload: 'world',
  18. qos: 1,
  19. messageId: 42
  20. }
  21. store.put(packet, function () {
  22. store
  23. .createStream()
  24. .on('data', function (data) {
  25. data.should.eql(packet)
  26. done()
  27. })
  28. })
  29. })
  30. it('should support destroying the stream', function (done) {
  31. var packet = {
  32. topic: 'hello',
  33. payload: 'world',
  34. qos: 1,
  35. messageId: 42
  36. }
  37. store.put(packet, function () {
  38. var stream = store.createStream()
  39. stream.on('close', done)
  40. stream.destroy()
  41. })
  42. })
  43. it('should add and del in-flight packets', function (done) {
  44. var packet = {
  45. topic: 'hello',
  46. payload: 'world',
  47. qos: 1,
  48. messageId: 42
  49. }
  50. store.put(packet, function () {
  51. store.del(packet, function () {
  52. store
  53. .createStream()
  54. .on('data', function () {
  55. done(new Error('this should never happen'))
  56. })
  57. .on('end', done)
  58. })
  59. })
  60. })
  61. it('should replace a packet when doing put with the same messageId', function (done) {
  62. var packet1 = {
  63. cmd: 'publish', // added
  64. topic: 'hello',
  65. payload: 'world',
  66. qos: 2,
  67. messageId: 42
  68. }
  69. var packet2 = {
  70. cmd: 'pubrel', // added
  71. qos: 2,
  72. messageId: 42
  73. }
  74. store.put(packet1, function () {
  75. store.put(packet2, function () {
  76. store
  77. .createStream()
  78. .on('data', function (data) {
  79. data.should.eql(packet2)
  80. done()
  81. })
  82. })
  83. })
  84. })
  85. it('should return the original packet on del', function (done) {
  86. var packet = {
  87. topic: 'hello',
  88. payload: 'world',
  89. qos: 1,
  90. messageId: 42
  91. }
  92. store.put(packet, function () {
  93. store.del({ messageId: 42 }, function (err, deleted) {
  94. if (err) {
  95. throw err
  96. }
  97. deleted.should.eql(packet)
  98. done()
  99. })
  100. })
  101. })
  102. it('should get a packet with the same messageId', function (done) {
  103. var packet = {
  104. topic: 'hello',
  105. payload: 'world',
  106. qos: 1,
  107. messageId: 42
  108. }
  109. store.put(packet, function () {
  110. store.get({ messageId: 42 }, function (err, fromDb) {
  111. if (err) {
  112. throw err
  113. }
  114. fromDb.should.eql(packet)
  115. done()
  116. })
  117. })
  118. })
  119. }