1
1
/* eslint-disable no-await-in-loop */
2
- /* eslint-disable prettier/prettier */
3
2
import fetch from 'node-fetch'
4
3
4
+ /* eslint-disable prettier/prettier */
5
5
import { relayerUrl , velcroUrl } from '../../../test/config'
6
6
import { produceMemoryStore } from '../../../test/helpers'
7
7
import { mockUiManager } from '../../../test/helpers/ui'
8
8
import { waitForFnToBeCalledAndExecuted } from '../../../test/recurringTimeout'
9
9
import { ACCOUNT_STATE_PENDING_INTERVAL } from '../../consts/intervals'
10
+ import { RPCProviders } from '../../interfaces/provider'
10
11
import { SubmittedAccountOp } from '../../libs/accountOp/submittedAccountOp'
11
12
import { KeystoreSigner } from '../../libs/keystoreSigner/keystoreSigner'
13
+ import wait from '../../utils/wait'
12
14
import { MainController } from '../main/main'
13
15
14
16
// Public API key, shared by Socket, for testing purposes only
@@ -77,6 +79,12 @@ const submittedAccountOp = {
77
79
}
78
80
} as SubmittedAccountOp
79
81
82
+ function filterProviders ( providers : RPCProviders , chainIdsToFilter : string [ ] = [ ] ) : RPCProviders {
83
+ return Object . fromEntries (
84
+ Object . entries ( providers ) . filter ( ( [ key ] ) => chainIdsToFilter . includes ( key ) )
85
+ ) as RPCProviders
86
+ }
87
+
80
88
const prepareTest = async ( ) => {
81
89
const storage = produceMemoryStore ( )
82
90
await storage . set ( 'accounts' , accounts )
@@ -96,10 +104,16 @@ const prepareTest = async () => {
96
104
velcroUrl
97
105
} )
98
106
mainCtrl . portfolio . updateSelectedAccount = jest . fn ( ) . mockResolvedValue ( undefined )
99
- mainCtrl . updateSelectedAccountPortfolio = jest . fn ( ) . mockResolvedValue ( undefined )
107
+ mainCtrl . updateSelectedAccountPortfolio = jest . fn ( ) . mockImplementation ( async ( ) => {
108
+ await wait ( 500 )
109
+ } )
100
110
mainCtrl . domains . reverseLookup = jest . fn ( ) . mockResolvedValue ( undefined )
101
- mainCtrl . accounts . updateAccountStates = jest . fn ( ) . mockResolvedValue ( undefined )
102
- mainCtrl . accounts . updateAccountState = jest . fn ( ) . mockResolvedValue ( undefined )
111
+ mainCtrl . accounts . updateAccountStates = jest . fn ( ) . mockImplementation ( async ( ) => {
112
+ await wait ( 500 )
113
+ } )
114
+ mainCtrl . accounts . updateAccountState = jest . fn ( ) . mockImplementation ( async ( ) => {
115
+ await wait ( 500 )
116
+ } )
103
117
mainCtrl . updateAccountsOpsStatuses = jest . fn ( ) . mockResolvedValue ( { newestOpTimestamp : 0 } )
104
118
105
119
return { mainCtrl }
@@ -121,6 +135,14 @@ const waitForContinuousUpdatesCtrlReady = async (mainCtrl: MainController) => {
121
135
}
122
136
}
123
137
138
+ const waitForAccountStatesInitialLoad = async ( mainCtrl : MainController ) => {
139
+ await jest . advanceTimersByTimeAsync ( 0 )
140
+
141
+ while ( mainCtrl . accounts . accountStatesInitialLoadPromise ) {
142
+ await jest . advanceTimersByTimeAsync ( 20 )
143
+ }
144
+ }
145
+
124
146
describe ( 'ContinuousUpdatesController intervals' , ( ) => {
125
147
beforeEach ( ( ) => {
126
148
jest . useFakeTimers ( )
@@ -135,29 +157,46 @@ describe('ContinuousUpdatesController intervals', () => {
135
157
test ( 'should run updatePortfolioInterval' , async ( ) => {
136
158
const { mainCtrl } = await prepareTest ( )
137
159
await waitForContinuousUpdatesCtrlReady ( mainCtrl )
160
+ await waitForAccountStatesInitialLoad ( mainCtrl )
161
+ const providersForTesting = [ '1' , '137' ]
162
+ const mockedProviders = filterProviders ( mainCtrl . providers . providers , providersForTesting )
163
+ // ensure all providers are working
164
+ mockedProviders [ 1 ] . isWorking = true
165
+ mockedProviders [ 137 ] . isWorking = true
166
+ mainCtrl . providers . providers = mockedProviders
167
+
138
168
jest . spyOn ( mainCtrl . continuousUpdates . updatePortfolioInterval , 'restart' )
139
- const updatePortfolioSpy = jest . spyOn ( mainCtrl . continuousUpdates , 'updatePortfolio' )
140
169
mainCtrl . ui . addView ( { id : '1' , type : 'popup' , currentRoute : 'dashboard' , isReady : true } )
141
170
await jest . advanceTimersByTimeAsync ( 0 )
142
171
expect ( mainCtrl . continuousUpdates . updatePortfolioInterval . restart ) . toHaveBeenCalled ( )
143
172
const updateSelectedAccountPortfolioSpy = jest . spyOn ( mainCtrl , 'updateSelectedAccountPortfolio' )
173
+ const initialFnExecutionsCount =
174
+ mainCtrl . continuousUpdates . updatePortfolioInterval . fnExecutionsCount
144
175
await waitForFnToBeCalledAndExecuted ( mainCtrl . continuousUpdates . updatePortfolioInterval )
145
- expect ( updatePortfolioSpy ) . toHaveBeenCalledTimes ( 1 )
176
+ expect ( mainCtrl . continuousUpdates . updatePortfolioInterval . fnExecutionsCount ) . toBe (
177
+ initialFnExecutionsCount + 1
178
+ )
146
179
const updateSelectedAccountCalledTimes = updateSelectedAccountPortfolioSpy . mock . calls . length
147
180
await mainCtrl . activity . addAccountOp ( submittedAccountOp )
148
181
await jest . advanceTimersByTimeAsync ( 0 )
149
182
await waitForFnToBeCalledAndExecuted ( mainCtrl . continuousUpdates . updatePortfolioInterval )
150
- expect ( updatePortfolioSpy ) . toHaveBeenCalledTimes ( 2 )
183
+ expect ( mainCtrl . continuousUpdates . updatePortfolioInterval . fnExecutionsCount ) . toBe (
184
+ initialFnExecutionsCount + 2
185
+ )
151
186
expect ( updateSelectedAccountPortfolioSpy ) . toHaveBeenCalledTimes (
152
187
updateSelectedAccountCalledTimes
153
188
) // tests the branching in the updatePortfolio func
154
189
mainCtrl . ui . removeView ( '1' )
155
190
await jest . advanceTimersByTimeAsync ( 0 )
156
191
expect ( mainCtrl . continuousUpdates . updatePortfolioInterval . restart ) . toHaveBeenCalledTimes ( 2 )
157
192
await waitForFnToBeCalledAndExecuted ( mainCtrl . continuousUpdates . updatePortfolioInterval )
158
- expect ( updatePortfolioSpy ) . toHaveBeenCalledTimes ( 3 )
193
+ expect ( mainCtrl . continuousUpdates . updatePortfolioInterval . fnExecutionsCount ) . toBe (
194
+ initialFnExecutionsCount + 3
195
+ )
159
196
await waitForFnToBeCalledAndExecuted ( mainCtrl . continuousUpdates . updatePortfolioInterval )
160
- expect ( updatePortfolioSpy ) . toHaveBeenCalledTimes ( 4 )
197
+ expect ( mainCtrl . continuousUpdates . updatePortfolioInterval . fnExecutionsCount ) . toBe (
198
+ initialFnExecutionsCount + 4
199
+ )
161
200
} )
162
201
163
202
test ( 'should run accountsOpsStatusesInterval' , async ( ) => {
@@ -166,18 +205,22 @@ describe('ContinuousUpdatesController intervals', () => {
166
205
167
206
jest . spyOn ( mainCtrl . continuousUpdates . accountsOpsStatusesInterval , 'start' )
168
207
jest . spyOn ( mainCtrl . continuousUpdates . accountsOpsStatusesInterval , 'stop' )
169
- const updateAccountsOpsStatuses = jest . spyOn (
170
- mainCtrl . continuousUpdates ,
171
- 'updateAccountsOpsStatuses'
172
- )
173
208
174
209
await mainCtrl . activity . addAccountOp ( submittedAccountOp )
175
210
await jest . advanceTimersByTimeAsync ( 0 )
211
+
212
+ const initialFnExecutionsCount =
213
+ mainCtrl . continuousUpdates . accountsOpsStatusesInterval . fnExecutionsCount
214
+
176
215
expect ( mainCtrl . continuousUpdates . accountsOpsStatusesInterval . start ) . toHaveBeenCalled ( )
177
216
await waitForFnToBeCalledAndExecuted ( mainCtrl . continuousUpdates . accountsOpsStatusesInterval )
178
- expect ( updateAccountsOpsStatuses ) . toHaveBeenCalledTimes ( 1 )
217
+ expect ( mainCtrl . continuousUpdates . accountsOpsStatusesInterval . fnExecutionsCount ) . toBe (
218
+ initialFnExecutionsCount + 1
219
+ )
179
220
await waitForFnToBeCalledAndExecuted ( mainCtrl . continuousUpdates . accountsOpsStatusesInterval )
180
- expect ( updateAccountsOpsStatuses ) . toHaveBeenCalledTimes ( 2 )
221
+ expect ( mainCtrl . continuousUpdates . accountsOpsStatusesInterval . fnExecutionsCount ) . toBe (
222
+ initialFnExecutionsCount + 2
223
+ )
181
224
jest . spyOn ( mainCtrl . activity , 'broadcastedButNotConfirmed' , 'get' ) . mockReturnValue ( [ ] )
182
225
// @ts -ignore
183
226
mainCtrl . activity . emitUpdate ( )
@@ -191,21 +234,21 @@ describe('ContinuousUpdatesController intervals', () => {
191
234
jest . spyOn ( mainCtrl . continuousUpdates . accountStateLatestInterval , 'restart' )
192
235
jest . spyOn ( mainCtrl . continuousUpdates . accountStatePendingInterval , 'start' )
193
236
jest . spyOn ( mainCtrl . continuousUpdates . accountStatePendingInterval , 'stop' )
194
- const updateAccountStateLatestMock = jest . spyOn (
195
- mainCtrl . continuousUpdates ,
196
- 'updateAccountStateLatest'
197
- )
198
- const updateAccountStatePendingMock = jest . spyOn (
199
- mainCtrl . continuousUpdates ,
200
- 'updateAccountStatePending'
201
- )
202
237
203
238
await waitForContinuousUpdatesCtrlReady ( mainCtrl )
204
239
240
+ const initialAccountStateLatestFnExecutionsCount =
241
+ mainCtrl . continuousUpdates . accountStateLatestInterval . fnExecutionsCount
242
+
243
+ const initialAccountStatePendingFnExecutionsCount =
244
+ mainCtrl . continuousUpdates . accountStateLatestInterval . fnExecutionsCount
245
+
205
246
expect ( mainCtrl . continuousUpdates . accountStateLatestInterval . running ) . toBe ( true )
206
247
expect ( mainCtrl . continuousUpdates . accountStatePendingInterval . running ) . toBe ( false )
207
248
await waitForFnToBeCalledAndExecuted ( mainCtrl . continuousUpdates . accountStateLatestInterval )
208
- expect ( updateAccountStateLatestMock ) . toHaveBeenCalledTimes ( 1 )
249
+ expect ( mainCtrl . continuousUpdates . accountStateLatestInterval . fnExecutionsCount ) . toBe (
250
+ initialAccountStateLatestFnExecutionsCount + 1
251
+ )
209
252
mainCtrl . statuses . signAndBroadcastAccountOp = 'SUCCESS'
210
253
// @ts -ignore
211
254
mainCtrl . emitUpdate ( )
@@ -217,36 +260,76 @@ describe('ContinuousUpdatesController intervals', () => {
217
260
expect ( mainCtrl . continuousUpdates . accountStatePendingInterval . currentTimeout ) . toBe (
218
261
ACCOUNT_STATE_PENDING_INTERVAL / 2
219
262
)
263
+
220
264
await waitForFnToBeCalledAndExecuted ( mainCtrl . continuousUpdates . accountStatePendingInterval )
221
- expect ( updateAccountStatePendingMock ) . toHaveBeenCalledTimes ( 1 )
265
+ expect ( mainCtrl . continuousUpdates . accountStatePendingInterval . fnExecutionsCount ) . toBe (
266
+ initialAccountStatePendingFnExecutionsCount + 1
267
+ )
222
268
expect ( mainCtrl . continuousUpdates . accountStateLatestInterval . restart ) . toHaveBeenCalledTimes ( 2 )
223
269
expect ( mainCtrl . continuousUpdates . accountStatePendingInterval . stop ) . toHaveBeenCalledTimes ( 1 )
224
270
} )
225
271
226
272
test ( 'should run fastAccountStateReFetchTimeout' , async ( ) => {
227
273
const { mainCtrl } = await prepareTest ( )
228
274
await waitForContinuousUpdatesCtrlReady ( mainCtrl )
275
+ await waitForAccountStatesInitialLoad ( mainCtrl )
229
276
277
+ const providersForTesting = [ '1' , '137' ]
278
+ const mockedProviders = filterProviders ( mainCtrl . providers . providers , providersForTesting )
279
+ // ensure there is at least one provider that is not working
280
+ mockedProviders [ 1 ] . isWorking = false
281
+ mockedProviders [ 137 ] . isWorking = true
282
+ mainCtrl . providers . providers = mockedProviders
230
283
jest . spyOn ( mainCtrl . continuousUpdates . fastAccountStateReFetchTimeout , 'start' )
231
- jest . spyOn ( mainCtrl . continuousUpdates , 'updateAccountStateLatest' )
232
- jest . spyOn ( mainCtrl . continuousUpdates , 'updateAccountStatePending' )
233
-
234
- const fastAccountStateReFetchMock = jest . spyOn (
235
- mainCtrl . continuousUpdates ,
236
- 'fastAccountStateReFetch'
237
- )
284
+ mainCtrl . continuousUpdates . accountStateLatestInterval . start = jest
285
+ . fn ( )
286
+ . mockResolvedValue ( undefined )
287
+ mainCtrl . continuousUpdates . accountStateLatestInterval . restart = jest
288
+ . fn ( )
289
+ . mockResolvedValue ( undefined )
290
+ mainCtrl . continuousUpdates . accountStatePendingInterval . start = jest
291
+ . fn ( )
292
+ . mockResolvedValue ( undefined )
293
+ mainCtrl . continuousUpdates . accountStatePendingInterval . restart = jest
294
+ . fn ( )
295
+ . mockResolvedValue ( undefined )
238
296
297
+ // ensure there is at least one provider that is not working
298
+ mainCtrl . providers . providers [ 1 ] . isWorking = false
299
+ mainCtrl . providers . providers [ 137 ] . isWorking = true
239
300
mainCtrl . ui . addView ( { id : '1' , type : 'popup' , currentRoute : 'dashboard' , isReady : true } )
240
- await jest . advanceTimersByTimeAsync ( 0 )
301
+ const initialFnExecutionsCount =
302
+ mainCtrl . continuousUpdates . fastAccountStateReFetchTimeout . fnExecutionsCount
241
303
expect ( mainCtrl . continuousUpdates . fastAccountStateReFetchTimeout . start ) . toHaveBeenCalledTimes ( 1 )
242
- expect ( fastAccountStateReFetchMock ) . toHaveBeenCalledTimes ( 0 )
243
- // ensure there is at least one provider that is not working
244
- if ( Object . values ( mainCtrl . providers . providers ) . some ( ( p ) => ! p . isWorking ) ) {
245
- mainCtrl . providers . providers [ 1 ] . isWorking = false
246
- }
304
+ expect ( mainCtrl . continuousUpdates . fastAccountStateReFetchTimeout . fnExecutionsCount ) . toBe (
305
+ initialFnExecutionsCount
306
+ )
307
+ // @ts -ignore
308
+ mainCtrl . providers . emitUpdate ( )
309
+ // @ts -ignore
310
+ mainCtrl . providers . emitUpdate ( )
311
+ // @ts -ignore
312
+ mainCtrl . providers . emitUpdate ( )
313
+
247
314
await waitForFnToBeCalledAndExecuted ( mainCtrl . continuousUpdates . fastAccountStateReFetchTimeout )
248
- expect ( fastAccountStateReFetchMock ) . toHaveBeenCalledTimes ( 1 )
315
+ // @ts -ignore
316
+ mainCtrl . providers . emitUpdate ( )
317
+ // @ts -ignore
318
+ mainCtrl . providers . emitUpdate ( )
319
+
320
+ expect ( mainCtrl . continuousUpdates . fastAccountStateReFetchTimeout . fnExecutionsCount ) . toBe (
321
+ initialFnExecutionsCount + 1
322
+ )
323
+ // @ts -ignore
324
+ mainCtrl . providers . emitUpdate ( )
325
+ // @ts -ignore
326
+ mainCtrl . providers . emitUpdate ( )
327
+ // @ts -ignore
328
+ mainCtrl . providers . emitUpdate ( )
329
+
249
330
await waitForFnToBeCalledAndExecuted ( mainCtrl . continuousUpdates . fastAccountStateReFetchTimeout )
250
- expect ( fastAccountStateReFetchMock ) . toHaveBeenCalledTimes ( 2 )
331
+ expect ( mainCtrl . continuousUpdates . fastAccountStateReFetchTimeout . fnExecutionsCount ) . toBe (
332
+ initialFnExecutionsCount + 2
333
+ )
251
334
} )
252
335
} )
0 commit comments