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.

209 lines
5.7 KiB

  1. var makeTest = require('./context')
  2. var Bottleneck = require('./bottleneck')
  3. var assert = require('assert')
  4. describe('Batcher', function () {
  5. var c
  6. afterEach(function () {
  7. return c.limiter.disconnect(false)
  8. })
  9. it('Should batch by time and size', function () {
  10. c = makeTest()
  11. var batcher = new Bottleneck.Batcher({
  12. maxTime: 50,
  13. maxSize: 3
  14. })
  15. var t0 = Date.now()
  16. var batches = []
  17. batcher.on('batch', function (batcher) {
  18. batches.push(batcher)
  19. })
  20. return Promise.all([
  21. batcher.add(1).then((x) => c.limiter.schedule(c.promise, null, Date.now() - t0, 1)),
  22. batcher.add(2).then((x) => c.limiter.schedule(c.promise, null, Date.now() - t0, 2)),
  23. batcher.add(3).then((x) => c.limiter.schedule(c.promise, null, Date.now() - t0, 3)),
  24. batcher.add(4).then((x) => c.limiter.schedule(c.promise, null, Date.now() - t0, 4)),
  25. batcher.add(5).then((x) => c.limiter.schedule(c.promise, null, Date.now() - t0, 5))
  26. ])
  27. .then(function (data) {
  28. c.mustEqual(
  29. data.map((([t, x]) => [Math.floor(t / 50), x])),
  30. [[0, 1], [0, 2], [0, 3], [1, 4], [1, 5]]
  31. )
  32. return c.last()
  33. })
  34. .then(function (results) {
  35. c.checkDuration(50, 20)
  36. c.mustEqual(batches, [[1, 2, 3], [4, 5]])
  37. })
  38. })
  39. it('Should batch by time', function () {
  40. c = makeTest()
  41. var batcher = new Bottleneck.Batcher({
  42. maxTime: 50
  43. })
  44. var t0 = Date.now()
  45. var batches = []
  46. batcher.on('batch', function (batcher) {
  47. batches.push(batcher)
  48. })
  49. return Promise.all([
  50. batcher.add(1).then((x) => c.limiter.schedule(c.promise, null, Date.now() - t0, 1)),
  51. batcher.add(2).then((x) => c.limiter.schedule(c.promise, null, Date.now() - t0, 2))
  52. ])
  53. .then(function (data) {
  54. c.mustEqual(
  55. data.map((([t, x]) => [Math.floor(t / 50), x])),
  56. [[1, 1], [1, 2]]
  57. )
  58. return Promise.all([
  59. batcher.add(3).then((x) => c.limiter.schedule(c.promise, null, Date.now() - t0, 3)),
  60. batcher.add(4).then((x) => c.limiter.schedule(c.promise, null, Date.now() - t0, 4))
  61. ])
  62. })
  63. .then(function (data) {
  64. c.mustEqual(
  65. data.map((([t, x]) => [Math.floor(t / 50), x])),
  66. [[2, 3], [2, 4]]
  67. )
  68. return c.last()
  69. })
  70. .then(function (results) {
  71. c.checkDuration(100)
  72. c.mustEqual(batches, [[1, 2], [3, 4]])
  73. })
  74. })
  75. it('Should batch by size', function () {
  76. c = makeTest()
  77. var batcher = new Bottleneck.Batcher({
  78. maxSize: 2
  79. })
  80. var batches = []
  81. batcher.on('batch', function (batcher) {
  82. batches.push(batcher)
  83. })
  84. return Promise.all([
  85. batcher.add(1).then((x) => c.limiter.schedule(c.promise, null, 1)),
  86. batcher.add(2).then((x) => c.limiter.schedule(c.promise, null, 2))
  87. ])
  88. .then(function () {
  89. return Promise.all([
  90. batcher.add(3).then((x) => c.limiter.schedule(c.promise, null, 3)),
  91. batcher.add(4).then((x) => c.limiter.schedule(c.promise, null, 4))
  92. ])
  93. })
  94. .then(c.last)
  95. .then(function (results) {
  96. c.checkDuration(0)
  97. c.mustEqual(batches, [[1, 2], [3, 4]])
  98. })
  99. })
  100. it('Should stagger flushes', function () {
  101. c = makeTest()
  102. var batcher = new Bottleneck.Batcher({
  103. maxTime: 50,
  104. maxSize: 3
  105. })
  106. var t0 = Date.now()
  107. var batches = []
  108. batcher.on('batch', function (batcher) {
  109. batches.push(batcher)
  110. })
  111. return Promise.all([
  112. batcher.add(1).then((x) => c.limiter.schedule(c.promise, null, Date.now() - t0, 1)),
  113. batcher.add(2).then((x) => c.limiter.schedule(c.promise, null, Date.now() - t0, 2))
  114. ])
  115. .then(function (data) {
  116. c.mustEqual(
  117. data.map((([t, x]) => [Math.floor(t / 50), x])),
  118. [[1, 1], [1, 2]]
  119. )
  120. var promises = []
  121. promises.push(batcher.add(3).then((x) => c.limiter.schedule(c.promise, null, Date.now() - t0, 3)))
  122. return c.wait(10)
  123. .then(function () {
  124. promises.push(batcher.add(4).then((x) => c.limiter.schedule(c.promise, null, Date.now() - t0, 4)))
  125. return Promise.all(promises)
  126. })
  127. })
  128. .then(function (data) {
  129. c.mustEqual(
  130. data.map((([t, x]) => [Math.floor(t / 50), x])),
  131. [[2, 3], [2, 4]]
  132. )
  133. return c.last()
  134. })
  135. .then(function (results) {
  136. c.checkDuration(120, 20)
  137. c.mustEqual(batches, [[1, 2], [3, 4]])
  138. })
  139. })
  140. it('Should force then stagger flushes', function () {
  141. c = makeTest()
  142. var batcher = new Bottleneck.Batcher({
  143. maxTime: 50,
  144. maxSize: 3
  145. })
  146. var t0 = Date.now()
  147. var batches = []
  148. batcher.on('batch', function (batcher) {
  149. batches.push(batcher)
  150. })
  151. var promises = []
  152. promises.push(batcher.add(1).then((x) => c.limiter.schedule(c.promise, null, Date.now() - t0, 1)))
  153. promises.push(batcher.add(2).then((x) => c.limiter.schedule(c.promise, null, Date.now() - t0, 2)))
  154. return c.wait(10)
  155. .then(function () {
  156. promises.push(batcher.add(3).then((x) => c.limiter.schedule(c.promise, null, Date.now() - t0, 3)))
  157. return Promise.all(promises)
  158. })
  159. .then(function (data) {
  160. c.mustEqual(
  161. data.map((([t, x]) => [Math.floor(t / 50), x])),
  162. [[0, 1], [0, 2], [0, 3]]
  163. )
  164. return Promise.all([
  165. batcher.add(4).then((x) => c.limiter.schedule(c.promise, null, Date.now() - t0, 4)),
  166. batcher.add(5).then((x) => c.limiter.schedule(c.promise, null, Date.now() - t0, 5)),
  167. ])
  168. })
  169. .then(function (data) {
  170. c.mustEqual(
  171. data.map((([t, x]) => [Math.floor(t / 50), x])),
  172. [[1, 4], [1, 5]]
  173. )
  174. return c.last()
  175. })
  176. .then(function (results) {
  177. c.checkDuration(85, 25)
  178. c.mustEqual(batches, [[1, 2, 3], [4, 5]])
  179. })
  180. })
  181. })