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.

241 lines
6.1 KiB

  1. var makeTest = require('./context')
  2. var Bottleneck = require('./bottleneck')
  3. var assert = require('assert')
  4. describe('Group', function () {
  5. var c
  6. afterEach(function () {
  7. return c.limiter.disconnect(false)
  8. })
  9. it('Should create limiters', function (done) {
  10. c = makeTest()
  11. var group = new Bottleneck.Group({
  12. maxConcurrent: 1, minTime: 100
  13. })
  14. var results = []
  15. var job = function (...result) {
  16. results.push(result)
  17. return new Promise(function (resolve, reject) {
  18. setTimeout(function () {
  19. return resolve()
  20. }, 50)
  21. })
  22. }
  23. group.key('A').schedule(job, 1, 2)
  24. group.key('A').schedule(job, 3)
  25. group.key('A').schedule(job, 4)
  26. setTimeout(function () {
  27. group.key('B').schedule(job, 5)
  28. }, 20)
  29. setTimeout(function () {
  30. group.key('C').schedule(job, 6)
  31. group.key('C').schedule(job, 7)
  32. }, 40)
  33. group.key('A').submit(function (cb) {
  34. c.mustEqual(results, [[1,2], [5], [6], [3], [7], [4]])
  35. cb()
  36. done()
  37. }, null)
  38. })
  39. it('Should set up the limiter IDs (default)', function () {
  40. c = makeTest()
  41. var group = new Bottleneck.Group({
  42. maxConcurrent: 1, minTime: 100
  43. })
  44. c.mustEqual(group.key('A').id, 'group-key-A')
  45. c.mustEqual(group.key('B').id, 'group-key-B')
  46. c.mustEqual(group.key('XYZ').id, 'group-key-XYZ')
  47. var ids = group.keys().map(function (key) {
  48. var limiter = group.key(key)
  49. c.mustEqual(limiter._store.timeout, group.timeout)
  50. return limiter.id
  51. })
  52. c.mustEqual(ids.sort(), ['group-key-A', 'group-key-B', 'group-key-XYZ'])
  53. })
  54. it('Should set up the limiter IDs (custom)', function () {
  55. c = makeTest()
  56. var group = new Bottleneck.Group({
  57. maxConcurrent: 1, minTime: 100,
  58. id: 'custom-id'
  59. })
  60. c.mustEqual(group.key('A').id, 'custom-id-A')
  61. c.mustEqual(group.key('B').id, 'custom-id-B')
  62. c.mustEqual(group.key('XYZ').id, 'custom-id-XYZ')
  63. var ids = group.keys().map(function (key) {
  64. var limiter = group.key(key)
  65. c.mustEqual(limiter._store.timeout, group.timeout)
  66. return limiter.id
  67. })
  68. c.mustEqual(ids.sort(), ['custom-id-A', 'custom-id-B', 'custom-id-XYZ'])
  69. })
  70. it('Should pass new limiter to \'created\' event', function () {
  71. c = makeTest()
  72. var group = new Bottleneck.Group({
  73. maxConcurrent: 1, minTime: 100
  74. })
  75. var keys = []
  76. var ids = []
  77. var promises = []
  78. group.on('created', function (created, key) {
  79. keys.push(key)
  80. promises.push(
  81. created.updateSettings({ id: key })
  82. .then(function (limiter) {
  83. ids.push(limiter.id)
  84. })
  85. )
  86. })
  87. group.key('A')
  88. group.key('B')
  89. group.key('A')
  90. group.key('B')
  91. group.key('B')
  92. group.key('BB')
  93. group.key('C')
  94. group.key('A')
  95. return Promise.all(promises)
  96. .then(function () {
  97. c.mustEqual(keys, ids)
  98. return c.limiter.ready()
  99. })
  100. })
  101. it('Should pass error on failure', function (done) {
  102. var failureMessage = 'SOMETHING BLEW UP!!'
  103. c = makeTest()
  104. var group = new Bottleneck.Group({
  105. maxConcurrent: 1, minTime: 100
  106. })
  107. c.mustEqual(Object.keys(group.limiters), [])
  108. var results = []
  109. var job = function (...result) {
  110. results.push(result)
  111. return new Promise(function (resolve, reject) {
  112. setTimeout(function () {
  113. return resolve()
  114. }, 50)
  115. })
  116. }
  117. group.key('A').schedule(job, 1, 2)
  118. group.key('A').schedule(job, 3)
  119. group.key('A').schedule(job, 4)
  120. group.key('B').schedule(() => Promise.reject(new Error(failureMessage)))
  121. .catch(function (err) {
  122. results.push(['CAUGHT', err.message])
  123. })
  124. setTimeout(function () {
  125. group.key('C').schedule(job, 6)
  126. group.key('C').schedule(job, 7)
  127. }, 40)
  128. group.key('A').submit(function (cb) {
  129. c.mustEqual(results, [[1,2], ['CAUGHT', failureMessage], [6], [3], [7], [4]])
  130. cb()
  131. done()
  132. }, null)
  133. })
  134. it('Should update its timeout', function () {
  135. c = makeTest()
  136. var group1 = new Bottleneck.Group({
  137. maxConcurrent: 1, minTime: 100
  138. })
  139. var group2 = new Bottleneck.Group({
  140. maxConcurrent: 1, minTime: 100, timeout: 5000
  141. })
  142. c.mustEqual(group1.timeout, 300000)
  143. c.mustEqual(group2.timeout, 5000)
  144. var p1 = group1.updateSettings({ timeout: 123 })
  145. var p2 = group2.updateSettings({ timeout: 456 })
  146. return Promise.all([p1, p2])
  147. .then(function () {
  148. c.mustEqual(group1.timeout, 123)
  149. c.mustEqual(group2.timeout, 456)
  150. })
  151. })
  152. it('Should update its limiter options', function () {
  153. c = makeTest()
  154. var group = new Bottleneck.Group({
  155. maxConcurrent: 1, minTime: 100
  156. })
  157. var limiter1 = group.key('AAA')
  158. c.mustEqual(limiter1._store.storeOptions.minTime, 100)
  159. group.updateSettings({ minTime: 200 })
  160. c.mustEqual(limiter1._store.storeOptions.minTime, 100)
  161. var limiter2 = group.key('BBB')
  162. c.mustEqual(limiter2._store.storeOptions.minTime, 200)
  163. })
  164. it('Should support keys() and limiters()', function () {
  165. c = makeTest()
  166. var group1 = new Bottleneck.Group({
  167. maxConcurrent: 1
  168. })
  169. var KEY_A = "AAA"
  170. var KEY_B = "BBB"
  171. group1.key(KEY_A).submit(c.job, null, 1)
  172. group1.key(KEY_B).submit(c.job, null, 2)
  173. var keys = group1.keys()
  174. var limiters = group1.limiters()
  175. c.mustEqual(keys, [KEY_A, KEY_B])
  176. c.mustEqual(limiters.length, 2)
  177. limiters.forEach(function (limiter, i) {
  178. c.mustEqual(limiter.key, keys[i])
  179. assert(limiter.limiter instanceof Bottleneck)
  180. })
  181. })
  182. it('Should call autocleanup', function () {
  183. var KEY = 'test-key'
  184. var group = new Bottleneck.Group({
  185. maxConcurrent: 1
  186. })
  187. group.updateSettings({ timeout: 50 })
  188. c = makeTest({ id: 'something', timeout: group.timeout })
  189. group.instances[KEY] = c.limiter
  190. return group.key(KEY).schedule(function () {
  191. return Promise.resolve()
  192. })
  193. .then(function () {
  194. assert(group.instances[KEY] != null)
  195. return new Promise(function (resolve, reject) {
  196. setTimeout(resolve, 100)
  197. })
  198. })
  199. .then(function () {
  200. assert(group.instances[KEY] == null)
  201. })
  202. })
  203. })