| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- import { queueJob, nextTick, queuePostFlushCb } from '../src/scheduler'
- describe('scheduler', () => {
- it('nextTick', async () => {
- const calls: string[] = []
- const dummyThen = Promise.resolve().then()
- const job1 = () => {
- calls.push('job1')
- }
- const job2 = () => {
- calls.push('job2')
- }
- nextTick(job1)
- job2()
- expect(calls.length).toBe(1)
- await dummyThen
- // job1 will be pushed in nextTick
- expect(calls.length).toBe(2)
- expect(calls).toMatchObject(['job2', 'job1'])
- })
- describe('queueJob', () => {
- it('basic usage', async () => {
- const calls: string[] = []
- const job1 = () => {
- calls.push('job1')
- }
- const job2 = () => {
- calls.push('job2')
- }
- queueJob(job1)
- queueJob(job2)
- expect(calls).toEqual([])
- await nextTick()
- expect(calls).toEqual(['job1', 'job2'])
- })
- it('should dedupe queued jobs', async () => {
- const calls: string[] = []
- const job1 = () => {
- calls.push('job1')
- }
- const job2 = () => {
- calls.push('job2')
- }
- queueJob(job1)
- queueJob(job2)
- queueJob(job1)
- queueJob(job2)
- expect(calls).toEqual([])
- await nextTick()
- expect(calls).toEqual(['job1', 'job2'])
- })
- it('queueJob while flushing', async () => {
- const calls: string[] = []
- const job1 = () => {
- calls.push('job1')
- // job2 will be excuted after job1 at the same tick
- queueJob(job2)
- }
- const job2 = () => {
- calls.push('job2')
- }
- queueJob(job1)
- await nextTick()
- expect(calls).toEqual(['job1', 'job2'])
- })
- })
- describe('queuePostFlushCb', () => {
- it('basic usage', async () => {
- const calls: string[] = []
- const cb1 = () => {
- calls.push('cb1')
- }
- const cb2 = () => {
- calls.push('cb2')
- }
- const cb3 = () => {
- calls.push('cb3')
- }
- queuePostFlushCb([cb1, cb2])
- queuePostFlushCb(cb3)
- expect(calls).toEqual([])
- await nextTick()
- expect(calls).toEqual(['cb1', 'cb2', 'cb3'])
- })
- it('should dedupe queued postFlushCb', async () => {
- const calls: string[] = []
- const cb1 = () => {
- calls.push('cb1')
- }
- const cb2 = () => {
- calls.push('cb2')
- }
- const cb3 = () => {
- calls.push('cb3')
- }
- queuePostFlushCb([cb1, cb2])
- queuePostFlushCb(cb3)
- queuePostFlushCb([cb1, cb3])
- queuePostFlushCb(cb2)
- expect(calls).toEqual([])
- await nextTick()
- expect(calls).toEqual(['cb1', 'cb2', 'cb3'])
- })
- it('queuePostFlushCb while flushing', async () => {
- const calls: string[] = []
- const cb1 = () => {
- calls.push('cb1')
- // cb2 will be excuted after cb1 at the same tick
- queuePostFlushCb(cb2)
- }
- const cb2 = () => {
- calls.push('cb2')
- }
- queuePostFlushCb(cb1)
- await nextTick()
- expect(calls).toEqual(['cb1', 'cb2'])
- })
- })
- describe('queueJob w/ queuePostFlushCb', () => {
- it('queueJob inside postFlushCb', async () => {
- const calls: string[] = []
- const job1 = () => {
- calls.push('job1')
- }
- const cb1 = () => {
- // queueJob in postFlushCb
- calls.push('cb1')
- queueJob(job1)
- }
- queuePostFlushCb(cb1)
- await nextTick()
- expect(calls).toEqual(['cb1', 'job1'])
- })
- it('queueJob & postFlushCb inside postFlushCb', async () => {
- const calls: string[] = []
- const job1 = () => {
- calls.push('job1')
- }
- const cb1 = () => {
- calls.push('cb1')
- queuePostFlushCb(cb2)
- // job1 will executed before cb2
- // Job has higher priority than postFlushCb
- queueJob(job1)
- }
- const cb2 = () => {
- calls.push('cb2')
- }
- queuePostFlushCb(cb1)
- await nextTick()
- expect(calls).toEqual(['cb1', 'job1', 'cb2'])
- })
- it('postFlushCb inside queueJob', async () => {
- const calls: string[] = []
- const job1 = () => {
- calls.push('job1')
- // postFlushCb in queueJob
- queuePostFlushCb(cb1)
- }
- const cb1 = () => {
- calls.push('cb1')
- }
- queueJob(job1)
- await nextTick()
- expect(calls).toEqual(['job1', 'cb1'])
- })
- it('queueJob & postFlushCb inside queueJob', async () => {
- const calls: string[] = []
- const job1 = () => {
- calls.push('job1')
- // cb1 will executed after job2
- // Job has higher priority than postFlushCb
- queuePostFlushCb(cb1)
- queueJob(job2)
- }
- const job2 = () => {
- calls.push('job2')
- }
- const cb1 = () => {
- calls.push('cb1')
- }
- queueJob(job1)
- await nextTick()
- expect(calls).toEqual(['job1', 'job2', 'cb1'])
- })
- it('nested queueJob w/ postFlushCb', async () => {
- const calls: string[] = []
- const job1 = () => {
- calls.push('job1')
- queuePostFlushCb(cb1)
- queueJob(job2)
- }
- const job2 = () => {
- calls.push('job2')
- queuePostFlushCb(cb2)
- }
- const cb1 = () => {
- calls.push('cb1')
- }
- const cb2 = () => {
- calls.push('cb2')
- }
- queueJob(job1)
- await nextTick()
- expect(calls).toEqual(['job1', 'job2', 'cb1', 'cb2'])
- })
- })
- })
|