scheduler.spec.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import { queueJob, nextTick, queuePostFlushCb } from '../src/scheduler'
  2. describe('scheduler', () => {
  3. it('nextTick', async () => {
  4. const calls: string[] = []
  5. const dummyThen = Promise.resolve().then()
  6. const job1 = () => {
  7. calls.push('job1')
  8. }
  9. const job2 = () => {
  10. calls.push('job2')
  11. }
  12. nextTick(job1)
  13. job2()
  14. expect(calls.length).toBe(1)
  15. await dummyThen
  16. // job1 will be pushed in nextTick
  17. expect(calls.length).toBe(2)
  18. expect(calls).toMatchObject(['job2', 'job1'])
  19. })
  20. describe('queueJob', () => {
  21. it('basic usage', async () => {
  22. const calls: string[] = []
  23. const job1 = () => {
  24. calls.push('job1')
  25. }
  26. const job2 = () => {
  27. calls.push('job2')
  28. }
  29. queueJob(job1)
  30. queueJob(job2)
  31. expect(calls).toEqual([])
  32. await nextTick()
  33. expect(calls).toEqual(['job1', 'job2'])
  34. })
  35. it('should dedupe queued jobs', async () => {
  36. const calls: string[] = []
  37. const job1 = () => {
  38. calls.push('job1')
  39. }
  40. const job2 = () => {
  41. calls.push('job2')
  42. }
  43. queueJob(job1)
  44. queueJob(job2)
  45. queueJob(job1)
  46. queueJob(job2)
  47. expect(calls).toEqual([])
  48. await nextTick()
  49. expect(calls).toEqual(['job1', 'job2'])
  50. })
  51. it('queueJob while flushing', async () => {
  52. const calls: string[] = []
  53. const job1 = () => {
  54. calls.push('job1')
  55. // job2 will be excuted after job1 at the same tick
  56. queueJob(job2)
  57. }
  58. const job2 = () => {
  59. calls.push('job2')
  60. }
  61. queueJob(job1)
  62. await nextTick()
  63. expect(calls).toEqual(['job1', 'job2'])
  64. })
  65. })
  66. describe('queuePostFlushCb', () => {
  67. it('basic usage', async () => {
  68. const calls: string[] = []
  69. const cb1 = () => {
  70. calls.push('cb1')
  71. }
  72. const cb2 = () => {
  73. calls.push('cb2')
  74. }
  75. const cb3 = () => {
  76. calls.push('cb3')
  77. }
  78. queuePostFlushCb([cb1, cb2])
  79. queuePostFlushCb(cb3)
  80. expect(calls).toEqual([])
  81. await nextTick()
  82. expect(calls).toEqual(['cb1', 'cb2', 'cb3'])
  83. })
  84. it('should dedupe queued postFlushCb', async () => {
  85. const calls: string[] = []
  86. const cb1 = () => {
  87. calls.push('cb1')
  88. }
  89. const cb2 = () => {
  90. calls.push('cb2')
  91. }
  92. const cb3 = () => {
  93. calls.push('cb3')
  94. }
  95. queuePostFlushCb([cb1, cb2])
  96. queuePostFlushCb(cb3)
  97. queuePostFlushCb([cb1, cb3])
  98. queuePostFlushCb(cb2)
  99. expect(calls).toEqual([])
  100. await nextTick()
  101. expect(calls).toEqual(['cb1', 'cb2', 'cb3'])
  102. })
  103. it('queuePostFlushCb while flushing', async () => {
  104. const calls: string[] = []
  105. const cb1 = () => {
  106. calls.push('cb1')
  107. // cb2 will be excuted after cb1 at the same tick
  108. queuePostFlushCb(cb2)
  109. }
  110. const cb2 = () => {
  111. calls.push('cb2')
  112. }
  113. queuePostFlushCb(cb1)
  114. await nextTick()
  115. expect(calls).toEqual(['cb1', 'cb2'])
  116. })
  117. })
  118. describe('queueJob w/ queuePostFlushCb', () => {
  119. it('queueJob inside postFlushCb', async () => {
  120. const calls: string[] = []
  121. const job1 = () => {
  122. calls.push('job1')
  123. }
  124. const cb1 = () => {
  125. // queueJob in postFlushCb
  126. calls.push('cb1')
  127. queueJob(job1)
  128. }
  129. queuePostFlushCb(cb1)
  130. await nextTick()
  131. expect(calls).toEqual(['cb1', 'job1'])
  132. })
  133. it('queueJob & postFlushCb inside postFlushCb', async () => {
  134. const calls: string[] = []
  135. const job1 = () => {
  136. calls.push('job1')
  137. }
  138. const cb1 = () => {
  139. calls.push('cb1')
  140. queuePostFlushCb(cb2)
  141. // job1 will executed before cb2
  142. // Job has higher priority than postFlushCb
  143. queueJob(job1)
  144. }
  145. const cb2 = () => {
  146. calls.push('cb2')
  147. }
  148. queuePostFlushCb(cb1)
  149. await nextTick()
  150. expect(calls).toEqual(['cb1', 'job1', 'cb2'])
  151. })
  152. it('postFlushCb inside queueJob', async () => {
  153. const calls: string[] = []
  154. const job1 = () => {
  155. calls.push('job1')
  156. // postFlushCb in queueJob
  157. queuePostFlushCb(cb1)
  158. }
  159. const cb1 = () => {
  160. calls.push('cb1')
  161. }
  162. queueJob(job1)
  163. await nextTick()
  164. expect(calls).toEqual(['job1', 'cb1'])
  165. })
  166. it('queueJob & postFlushCb inside queueJob', async () => {
  167. const calls: string[] = []
  168. const job1 = () => {
  169. calls.push('job1')
  170. // cb1 will executed after job2
  171. // Job has higher priority than postFlushCb
  172. queuePostFlushCb(cb1)
  173. queueJob(job2)
  174. }
  175. const job2 = () => {
  176. calls.push('job2')
  177. }
  178. const cb1 = () => {
  179. calls.push('cb1')
  180. }
  181. queueJob(job1)
  182. await nextTick()
  183. expect(calls).toEqual(['job1', 'job2', 'cb1'])
  184. })
  185. it('nested queueJob w/ postFlushCb', async () => {
  186. const calls: string[] = []
  187. const job1 = () => {
  188. calls.push('job1')
  189. queuePostFlushCb(cb1)
  190. queueJob(job2)
  191. }
  192. const job2 = () => {
  193. calls.push('job2')
  194. queuePostFlushCb(cb2)
  195. }
  196. const cb1 = () => {
  197. calls.push('cb1')
  198. }
  199. const cb2 = () => {
  200. calls.push('cb2')
  201. }
  202. queueJob(job1)
  203. await nextTick()
  204. expect(calls).toEqual(['job1', 'job2', 'cb1', 'cb2'])
  205. })
  206. })
  207. })