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.

208 lines
5.7 KiB

  1. var makeTest = require('./context')
  2. var Bottleneck = require('./bottleneck')
  3. var assert = require('assert')
  4. describe('Stop', function () {
  5. var c
  6. afterEach(function () {
  7. return c.limiter.disconnect(false)
  8. })
  9. it('Should stop and drop the queue', function (done) {
  10. c = makeTest({
  11. maxConcurrent: 2,
  12. minTime: 100,
  13. trackDoneStatus: true
  14. })
  15. var submitFailed = false
  16. var queuedDropped = false
  17. var scheduledDropped = false
  18. var dropped = 0
  19. c.limiter.on('dropped', function () {
  20. dropped++
  21. })
  22. c.pNoErrVal(c.limiter.schedule({id: '0'}, c.promise, null, 0), 0)
  23. c.pNoErrVal(c.limiter.schedule({id: '1'}, c.slowPromise, 100, null, 1), 1)
  24. c.limiter.schedule({id: '2'}, c.promise, null, 2)
  25. .catch(function (err) {
  26. c.mustEqual(err.message, 'Dropped!')
  27. scheduledDropped = true
  28. })
  29. c.limiter.schedule({id: '3'}, c.promise, null, 3)
  30. .catch(function (err) {
  31. c.mustEqual(err.message, 'Dropped!')
  32. queuedDropped = true
  33. })
  34. setTimeout(function () {
  35. var counts = c.limiter.counts()
  36. c.mustEqual(counts.RECEIVED, 0)
  37. c.mustEqual(counts.QUEUED, 1)
  38. c.mustEqual(counts.RUNNING, 1)
  39. c.mustEqual(counts.EXECUTING, 1)
  40. c.mustEqual(counts.DONE, 1)
  41. c.limiter.stop({
  42. enqueueErrorMessage: 'Stopped!',
  43. dropErrorMessage: 'Dropped!'
  44. })
  45. .then(function () {
  46. counts = c.limiter.counts()
  47. c.mustEqual(submitFailed, true)
  48. c.mustEqual(scheduledDropped, true)
  49. c.mustEqual(queuedDropped, true)
  50. c.mustEqual(dropped, 2)
  51. c.mustEqual(counts.RECEIVED, 0)
  52. c.mustEqual(counts.QUEUED, 0)
  53. c.mustEqual(counts.RUNNING, 0)
  54. c.mustEqual(counts.EXECUTING, 0)
  55. c.mustEqual(counts.DONE, 2)
  56. c.checkResultsOrder([[0], [1]])
  57. done()
  58. })
  59. c.limiter.schedule(() => Promise.resolve(true))
  60. .catch(function (err) {
  61. c.mustEqual(err.message, 'Stopped!')
  62. submitFailed = true
  63. })
  64. }, 125)
  65. })
  66. it('Should stop and let the queue finish', function (done) {
  67. c = makeTest({
  68. maxConcurrent: 1,
  69. minTime: 100,
  70. trackDoneStatus: true
  71. })
  72. var submitFailed = false
  73. var dropped = 0
  74. c.limiter.on('dropped', function () {
  75. dropped++
  76. })
  77. c.pNoErrVal(c.limiter.schedule({id: '1'}, c.promise, null, 1), 1)
  78. c.pNoErrVal(c.limiter.schedule({id: '2'}, c.promise, null, 2), 2)
  79. c.pNoErrVal(c.limiter.schedule({id: '3'}, c.slowPromise, 100, null, 3), 3)
  80. setTimeout(function () {
  81. var counts = c.limiter.counts()
  82. c.mustEqual(counts.RECEIVED, 0)
  83. c.mustEqual(counts.QUEUED, 1)
  84. c.mustEqual(counts.RUNNING, 1)
  85. c.mustEqual(counts.EXECUTING, 0)
  86. c.mustEqual(counts.DONE, 1)
  87. c.limiter.stop({
  88. enqueueErrorMessage: 'Stopped!',
  89. dropWaitingJobs: false
  90. })
  91. .then(function () {
  92. counts = c.limiter.counts()
  93. c.mustEqual(submitFailed, true)
  94. c.mustEqual(dropped, 0)
  95. c.mustEqual(counts.RECEIVED, 0)
  96. c.mustEqual(counts.QUEUED, 0)
  97. c.mustEqual(counts.RUNNING, 0)
  98. c.mustEqual(counts.EXECUTING, 0)
  99. c.mustEqual(counts.DONE, 4)
  100. c.checkResultsOrder([[1], [2], [3]])
  101. done()
  102. })
  103. c.limiter.schedule(() => Promise.resolve(true))
  104. .catch(function (err) {
  105. c.mustEqual(err.message, 'Stopped!')
  106. submitFailed = true
  107. })
  108. }, 75)
  109. })
  110. it('Should still resolve when rejectOnDrop is false', function (done) {
  111. c = makeTest({
  112. maxConcurrent: 1,
  113. minTime: 100,
  114. rejectOnDrop: false
  115. })
  116. c.pNoErrVal(c.limiter.schedule({id: '1'}, c.promise, null, 1), 1)
  117. c.pNoErrVal(c.limiter.schedule({id: '2'}, c.promise, null, 2), 2)
  118. c.pNoErrVal(c.limiter.schedule({id: '3'}, c.slowPromise, 100, null, 3), 3)
  119. c.limiter.stop()
  120. .then(function () {
  121. return c.limiter.stop()
  122. })
  123. .then(function () {
  124. done(new Error("Should not be here"))
  125. })
  126. .catch(function (err) {
  127. c.mustEqual(err.message, "stop() has already been called")
  128. done()
  129. })
  130. })
  131. it('Should not allow calling stop() twice when dropWaitingJobs=true', function (done) {
  132. c = makeTest({
  133. maxConcurrent: 1,
  134. minTime: 100
  135. })
  136. var failed = 0
  137. var handler = function (err) {
  138. c.mustEqual(err.message, "This limiter has been stopped.")
  139. failed++
  140. }
  141. c.pNoErrVal(c.limiter.schedule({id: '1'}, c.promise, null, 1), 1).catch(handler)
  142. c.pNoErrVal(c.limiter.schedule({id: '2'}, c.promise, null, 2), 2).catch(handler)
  143. c.pNoErrVal(c.limiter.schedule({id: '3'}, c.slowPromise, 100, null, 3), 3).catch(handler)
  144. c.limiter.stop({ dropWaitingJobs: true })
  145. .then(function () {
  146. return c.limiter.stop({ dropWaitingJobs: true })
  147. })
  148. .then(function () {
  149. done(new Error("Should not be here"))
  150. })
  151. .catch(function (err) {
  152. c.mustEqual(err.message, "stop() has already been called")
  153. c.mustEqual(failed, 3)
  154. done()
  155. })
  156. })
  157. it('Should not allow calling stop() twice when dropWaitingJobs=false', function (done) {
  158. c = makeTest({
  159. maxConcurrent: 1,
  160. minTime: 100
  161. })
  162. c.pNoErrVal(c.limiter.schedule({id: '1'}, c.promise, null, 1), 1)
  163. c.pNoErrVal(c.limiter.schedule({id: '2'}, c.promise, null, 2), 2)
  164. c.pNoErrVal(c.limiter.schedule({id: '3'}, c.slowPromise, 100, null, 3), 3)
  165. c.limiter.stop({ dropWaitingJobs: false })
  166. .then(function () {
  167. return c.limiter.stop({ dropWaitingJobs: false })
  168. })
  169. .then(function () {
  170. done(new Error("Should not be here"))
  171. })
  172. .catch(function (err) {
  173. c.mustEqual(err.message, "stop() has already been called")
  174. done()
  175. })
  176. })
  177. })