|
1 | 1 | import { AnyItemInterface } from '@standardnotes/models'
|
2 | 2 | import { SyncBackoffService } from './SyncBackoffService'
|
| 3 | +import { InternalEventBusInterface } from '../..' |
| 4 | +import { Uuid } from '@standardnotes/domain-core' |
3 | 5 |
|
4 | 6 | describe('SyncBackoffService', () => {
|
5 |
| - const createService = () => new SyncBackoffService() |
| 7 | + let internalEventBus: InternalEventBusInterface |
| 8 | + |
| 9 | + const createService = () => new SyncBackoffService(internalEventBus) |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + internalEventBus = {} as jest.Mocked<InternalEventBusInterface> |
| 13 | + }) |
6 | 14 |
|
7 | 15 | it('should not be in backoff if no backoff was set', () => {
|
8 | 16 | const service = createService()
|
9 | 17 |
|
10 |
| - expect(service.isItemInBackoff({ uuid: '123' } as jest.Mocked<AnyItemInterface>)).toBe(false) |
| 18 | + expect(service.isItemInBackoff({ uuid: '00000000-0000-0000-0000-000000000000' } as jest.Mocked<AnyItemInterface>)).toBe(false) |
11 | 19 | })
|
12 | 20 |
|
13 | 21 | it('should be in backoff if backoff was set', () => {
|
14 | 22 | const service = createService()
|
15 | 23 |
|
16 |
| - service.backoffItem({ uuid: '123' } as jest.Mocked<AnyItemInterface>) |
| 24 | + service.backoffItem(Uuid.create('00000000-0000-0000-0000-000000000000').getValue()) |
17 | 25 |
|
18 |
| - expect(service.isItemInBackoff({ uuid: '123' } as jest.Mocked<AnyItemInterface>)).toBe(true) |
| 26 | + expect(service.isItemInBackoff({ uuid: '00000000-0000-0000-0000-000000000000' } as jest.Mocked<AnyItemInterface>)).toBe(true) |
19 | 27 | })
|
20 | 28 |
|
21 | 29 | it('should not be in backoff if backoff expired', () => {
|
22 | 30 | const service = createService()
|
23 | 31 |
|
24 | 32 | jest.spyOn(Date, 'now').mockReturnValueOnce(1_000_000)
|
25 | 33 |
|
26 |
| - service.backoffItem({ uuid: '123' } as jest.Mocked<AnyItemInterface>) |
| 34 | + service.backoffItem(Uuid.create('00000000-0000-0000-0000-000000000000').getValue()) |
27 | 35 |
|
28 | 36 | jest.spyOn(Date, 'now').mockReturnValueOnce(2_000_000)
|
29 | 37 |
|
30 |
| - expect(service.isItemInBackoff({ uuid: '123' } as jest.Mocked<AnyItemInterface>)).toBe(false) |
| 38 | + expect(service.isItemInBackoff({ uuid: '00000000-0000-0000-0000-000000000000' } as jest.Mocked<AnyItemInterface>)).toBe(false) |
31 | 39 | })
|
32 | 40 |
|
33 | 41 | it('should double backoff penalty on each backoff', () => {
|
34 | 42 | const service = createService()
|
35 | 43 |
|
36 | 44 | jest.spyOn(Date, 'now').mockReturnValueOnce(1_000_000)
|
37 | 45 |
|
38 |
| - service.backoffItem({ uuid: '123' } as jest.Mocked<AnyItemInterface>) |
| 46 | + service.backoffItem(Uuid.create('00000000-0000-0000-0000-000000000000').getValue()) |
39 | 47 |
|
40 | 48 | jest.spyOn(Date, 'now').mockReturnValueOnce(1_000_000)
|
41 |
| - expect(service.isItemInBackoff({ uuid: '123' } as jest.Mocked<AnyItemInterface>)).toBe(true) |
| 49 | + expect(service.isItemInBackoff({ uuid: '00000000-0000-0000-0000-000000000000' } as jest.Mocked<AnyItemInterface>)).toBe(true) |
42 | 50 |
|
43 | 51 | jest.spyOn(Date, 'now').mockReturnValueOnce(1_000_000)
|
44 |
| - service.backoffItem({ uuid: '123' } as jest.Mocked<AnyItemInterface>) |
| 52 | + service.backoffItem(Uuid.create('00000000-0000-0000-0000-000000000000').getValue()) |
45 | 53 |
|
46 | 54 | jest.spyOn(Date, 'now').mockReturnValueOnce(1_001_000)
|
47 |
| - expect(service.isItemInBackoff({ uuid: '123' } as jest.Mocked<AnyItemInterface>)).toBe(true) |
| 55 | + expect(service.isItemInBackoff({ uuid: '00000000-0000-0000-0000-000000000000' } as jest.Mocked<AnyItemInterface>)).toBe(true) |
48 | 56 |
|
49 | 57 | jest.spyOn(Date, 'now').mockReturnValueOnce(1_000_000)
|
50 |
| - service.backoffItem({ uuid: '123' } as jest.Mocked<AnyItemInterface>) |
| 58 | + service.backoffItem(Uuid.create('00000000-0000-0000-0000-000000000000').getValue()) |
51 | 59 |
|
52 | 60 | jest.spyOn(Date, 'now').mockReturnValueOnce(1_003_000)
|
53 |
| - expect(service.isItemInBackoff({ uuid: '123' } as jest.Mocked<AnyItemInterface>)).toBe(true) |
| 61 | + expect(service.isItemInBackoff({ uuid: '00000000-0000-0000-0000-000000000000' } as jest.Mocked<AnyItemInterface>)).toBe(true) |
| 62 | + }) |
| 63 | + |
| 64 | + it('should remove backoff penalty on successful sync', async () => { |
| 65 | + const service = createService() |
| 66 | + |
| 67 | + jest.spyOn(Date, 'now').mockReturnValueOnce(1_000_000) |
| 68 | + |
| 69 | + service.backoffItem(Uuid.create('00000000-0000-0000-0000-000000000000').getValue()) |
| 70 | + |
| 71 | + jest.spyOn(Date, 'now').mockReturnValueOnce(1_000_000) |
| 72 | + expect(service.isItemInBackoff({ uuid: '00000000-0000-0000-0000-000000000000' } as jest.Mocked<AnyItemInterface>)).toBe(true) |
| 73 | + |
| 74 | + await service.handleEvent({ |
| 75 | + type: 'CompletedIncrementalSync', |
| 76 | + payload: { |
| 77 | + uploadedPayloads: [ |
| 78 | + { |
| 79 | + uuid: '00000000-0000-0000-0000-000000000000', |
| 80 | + }, |
| 81 | + ], |
| 82 | + }, |
| 83 | + }) |
| 84 | + |
| 85 | + expect(service.isItemInBackoff({ uuid: '00000000-0000-0000-0000-000000000000' } as jest.Mocked<AnyItemInterface>)).toBe(false) |
| 86 | + }) |
| 87 | + |
| 88 | + it('should get a smaller subset of item uuids in backoff that have lesser penalty', () => { |
| 89 | + const service = createService() |
| 90 | + |
| 91 | + jest.spyOn(Date, 'now').mockReturnValueOnce(1_000_000) |
| 92 | + |
| 93 | + for (let i = 0; i < 5; i++) { |
| 94 | + service.backoffItem(Uuid.create('00000000-0000-0000-0000-000000000000').getValue()) |
| 95 | + } |
| 96 | + for (let i = 0; i < 4; i++) { |
| 97 | + service.backoffItem(Uuid.create('00000000-0000-0000-0000-000000000001').getValue()) |
| 98 | + } |
| 99 | + for (let i = 0; i < 3; i++) { |
| 100 | + service.backoffItem(Uuid.create('00000000-0000-0000-0000-000000000002').getValue()) |
| 101 | + } |
| 102 | + for (let i = 0; i < 2; i++) { |
| 103 | + service.backoffItem(Uuid.create('00000000-0000-0000-0000-000000000003').getValue()) |
| 104 | + } |
| 105 | + service.backoffItem(Uuid.create('00000000-0000-0000-0000-000000000004').getValue()) |
| 106 | + |
| 107 | + const subset = service.getSmallerSubsetOfItemUuidsInBackoff() |
| 108 | + |
| 109 | + expect(subset.length).toEqual(3) |
| 110 | + |
| 111 | + expect(subset[0].value).toBe('00000000-0000-0000-0000-000000000004') |
| 112 | + expect(subset[1].value).toBe('00000000-0000-0000-0000-000000000003') |
| 113 | + expect(subset[2].value).toBe('00000000-0000-0000-0000-000000000002') |
54 | 114 | })
|
55 | 115 | })
|
0 commit comments