-
Notifications
You must be signed in to change notification settings - Fork 3
/
instant.test.ts
189 lines (167 loc) · 5.77 KB
/
instant.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { describe, it, expect, beforeEach, jest } from '@jest/globals'
import {
createDependencyTree,
walkDependencyTree,
concurrentifyAction
} from './instant'
describe('createDependencyTree', () => {
it('should handle a single package with no dependencies', () => {
const allPackages = {
package1: { metadata: { id: 'package1', dependencies: [] } }
}
const chosenPackageIds = ['package1']
const expectedTree = { package1: {} }
expect(createDependencyTree(allPackages, chosenPackageIds)).toEqual(
expectedTree
)
})
it('should handle multiple packages with dependencies', () => {
const allPackages = {
package1: { metadata: { id: 'package1', dependencies: ['package2'] } },
package2: { metadata: { id: 'package2', dependencies: [] } }
}
const chosenPackageIds = ['package1']
const expectedTree = { package1: { package2: {} } }
expect(createDependencyTree(allPackages, chosenPackageIds)).toEqual(
expectedTree
)
})
it('should handle complex dependency trees', () => {
const allPackages = {
package1: {
metadata: { id: 'package1', dependencies: ['package2', 'package3'] }
},
package2: { metadata: { id: 'package2', dependencies: ['package3'] } },
package3: { metadata: { id: 'package3', dependencies: [] } }
}
const chosenPackageIds = ['package1']
const expectedTree = {
package1: { package2: { package3: {} }, package3: {} }
}
expect(createDependencyTree(allPackages, chosenPackageIds)).toEqual(
expectedTree
)
})
it('should throw an error for circular dependencies', () => {
const allPackages = {
package1: { metadata: { id: 'package1', dependencies: ['package2'] } },
package2: { metadata: { id: 'package2', dependencies: ['package1'] } }
}
const chosenPackageIds = ['package1']
expect(() => createDependencyTree(allPackages, chosenPackageIds)).toThrow(
'Circular dependency detected: package1 has already been visited.'
)
})
it('should handle invalid or missing package IDs gracefully', () => {
const allPackages = {
package1: { metadata: { id: 'package1', dependencies: [] } }
}
const chosenPackageIds = ['nonExistentPackage']
expect(() => createDependencyTree(allPackages, chosenPackageIds)).toThrow(
'Invalid package ID: nonExistentPackage'
)
})
})
describe('walkDependencyTree', () => {
const mockAction = jest.fn()
beforeEach(() => {
mockAction.mockClear()
})
it('should call action on a single node tree in pre-order', async () => {
const tree = { package1: {} }
await walkDependencyTree(tree, 'pre', mockAction)
expect(mockAction).toHaveBeenCalledTimes(1)
expect(mockAction).toHaveBeenCalledWith('package1')
})
it('should call action on a single node tree in post-order', async () => {
const tree = { package1: {} }
await walkDependencyTree(tree, 'post', mockAction)
expect(mockAction).toHaveBeenCalledTimes(1)
expect(mockAction).toHaveBeenCalledWith('package1')
})
it('should walk a complex tree in pre-order and call action correctly', async () => {
const tree = {
package1: {
package2: {},
package3: {
package4: {}
}
}
}
await walkDependencyTree(tree, 'pre', mockAction)
expect(mockAction.mock.calls).toEqual([
['package1'],
['package2'],
['package3'],
['package4']
])
})
it('should walk a complex tree in post-order and call action correctly', async () => {
const tree = {
package1: {
package2: {},
package3: {
package4: {}
}
}
}
await walkDependencyTree(tree, 'post', mockAction)
expect(mockAction.mock.calls).toEqual([
['package2'],
['package4'],
['package3'],
['package1']
])
})
it('should handle an empty tree', async () => {
const tree = {}
await walkDependencyTree(tree, 'pre', mockAction)
expect(mockAction).not.toHaveBeenCalled()
})
})
describe('concurrentifyAction', () => {
it('executes actions concurrently up to the specified limit', async () => {
const action = jest
.fn()
.mockImplementation(
(id) => new Promise((resolve) => setTimeout(resolve, 100))
)
const concurrentAction = concurrentifyAction(action, 2)
const startTime = Date.now()
await Promise.all([
concurrentAction('1'),
concurrentAction('2'),
concurrentAction('3') // This should wait until one of the first two completes
])
const endTime = Date.now()
expect(action).toHaveBeenCalledTimes(3)
// Check if the total time taken is in the expected range considering concurrency limit
expect(endTime - startTime).toBeGreaterThanOrEqual(200) // At least two batches of 100ms each
})
it('does not execute the same action for a given ID more than once', async () => {
const action = jest.fn().mockResolvedValue(undefined)
const concurrentAction = concurrentifyAction(action, 2)
await Promise.all([
concurrentAction('1'),
concurrentAction('1') // This should not cause a second execution
])
expect(action).toHaveBeenCalledTimes(1)
})
it('queues actions correctly when exceeding the concurrency limit', async () => {
let activeCount = 0
const action = jest.fn().mockImplementation(async (id) => {
activeCount++
expect(activeCount).toBeLessThanOrEqual(2) // Ensure no more than 2 active at a time
await new Promise((resolve) => setTimeout(resolve, 50))
activeCount--
})
const concurrentAction = concurrentifyAction(action, 2)
await Promise.all([
concurrentAction('1'),
concurrentAction('2'),
concurrentAction('3'),
concurrentAction('4') // These should be queued
])
expect(action).toHaveBeenCalledTimes(4)
})
})