Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
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.

142 lines
3.9 KiB

  1. var makeTest = require('./context')
  2. var Bottleneck = require('./bottleneck')
  3. var assert = require('assert')
  4. describe('Promises', function () {
  5. var c
  6. afterEach(function () {
  7. return c.limiter.disconnect(false)
  8. })
  9. it('Should support promises', function () {
  10. c = makeTest({maxConcurrent: 1, minTime: 100})
  11. c.limiter.submit(c.job, null, 1, 9, c.noErrVal(1, 9))
  12. c.limiter.submit(c.job, null, 2, c.noErrVal(2))
  13. c.limiter.submit(c.job, null, 3, c.noErrVal(3))
  14. c.pNoErrVal(c.limiter.schedule(c.promise, null, 4, 5), 4, 5)
  15. return c.last()
  16. .then(function (results) {
  17. c.checkResultsOrder([[1,9], [2], [3], [4,5]])
  18. c.checkDuration(300)
  19. })
  20. })
  21. it('Should pass error on failure', function () {
  22. var failureMessage = 'failed'
  23. c = makeTest({maxConcurrent: 1, minTime: 100})
  24. return c.limiter.schedule(c.promise, new Error(failureMessage))
  25. .catch(function (err) {
  26. c.mustEqual(err.message, failureMessage)
  27. })
  28. })
  29. it('Should allow non-Promise returns', function () {
  30. c = makeTest()
  31. var str = 'This is a string'
  32. return c.limiter.schedule(() => str)
  33. .then(function (x) {
  34. c.mustEqual(x, str)
  35. })
  36. })
  37. it('Should get rejected when rejectOnDrop is true', function () {
  38. c = makeTest({
  39. maxConcurrent: 1,
  40. minTime: 0,
  41. highWater: 1,
  42. strategy: Bottleneck.strategy.OVERFLOW,
  43. rejectOnDrop: true
  44. })
  45. var dropped = 0
  46. var caught = 0
  47. var p1
  48. var p2
  49. c.limiter.on('dropped', function () {
  50. dropped++
  51. })
  52. p1 = c.pNoErrVal(c.limiter.schedule({id: 1}, c.slowPromise, 50, null, 1), 1)
  53. p2 = c.pNoErrVal(c.limiter.schedule({id: 2}, c.slowPromise, 50, null, 2), 2)
  54. return c.limiter.schedule({id: 3}, c.slowPromise, 50, null, 3)
  55. .catch(function (err) {
  56. c.mustEqual(err.message, 'This job has been dropped by Bottleneck')
  57. assert(err instanceof Bottleneck.BottleneckError)
  58. caught++
  59. return Promise.all([p1, p2])
  60. })
  61. .then(c.last)
  62. .then(function (results) {
  63. c.checkResultsOrder([[1], [2]])
  64. c.checkDuration(100)
  65. c.mustEqual(dropped, 1)
  66. c.mustEqual(caught, 1)
  67. })
  68. })
  69. describe('Wrap', function () {
  70. it('Should wrap', function () {
  71. c = makeTest({maxConcurrent: 1, minTime: 100})
  72. c.limiter.submit(c.job, null, 1, c.noErrVal(1))
  73. c.limiter.submit(c.job, null, 2, c.noErrVal(2))
  74. c.limiter.submit(c.job, null, 3, c.noErrVal(3))
  75. var wrapped = c.limiter.wrap(c.promise)
  76. c.pNoErrVal(wrapped(null, 4), 4)
  77. return c.last()
  78. .then(function (results) {
  79. c.checkResultsOrder([[1], [2], [3], [4]])
  80. c.checkDuration(300)
  81. })
  82. })
  83. it('Should pass errors back', function () {
  84. var failureMessage = 'BLEW UP!!!'
  85. c = makeTest({maxConcurrent: 1, minTime: 100})
  86. var wrapped = c.limiter.wrap(c.promise)
  87. c.pNoErrVal(wrapped(null, 1), 1)
  88. c.pNoErrVal(wrapped(null, 2), 2)
  89. return wrapped(new Error(failureMessage), 3)
  90. .catch(function (err) {
  91. c.mustEqual(err.message, failureMessage)
  92. return c.last()
  93. })
  94. .then(function (results) {
  95. c.checkResultsOrder([[1], [2], [3]])
  96. c.checkDuration(200)
  97. })
  98. })
  99. it('Should allow passing options', function () {
  100. var failureMessage = 'BLEW UP!!!'
  101. c = makeTest({maxConcurrent: 1, minTime: 50})
  102. var wrapped = c.limiter.wrap(c.promise)
  103. c.pNoErrVal(wrapped(null, 1), 1)
  104. c.pNoErrVal(wrapped(null, 2), 2)
  105. c.pNoErrVal(wrapped(null, 3), 3)
  106. c.pNoErrVal(wrapped(null, 4), 4)
  107. c.pNoErrVal(wrapped.withOptions({ priority: 1 }, null, 5), 5)
  108. return wrapped.withOptions({ priority: 1 }, new Error(failureMessage), 6)
  109. .catch(function (err) {
  110. c.mustEqual(err.message, failureMessage)
  111. return c.last()
  112. })
  113. .then(function (results) {
  114. c.checkResultsOrder([[1], [2], [5], [6], [3], [4]])
  115. c.checkDuration(250)
  116. })
  117. })
  118. })
  119. })