-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New name & tests for one-at-a-time /setup behavior
`firstPromiseBlocksAndFufills` for "the first promise created blocks others from being created, then fufills all with that first result"
- Loading branch information
John Schulz
committed
Aug 18, 2020
1 parent
409ef85
commit a7d7261
Showing
4 changed files
with
180 additions
and
95 deletions.
There are no files selected for viewing
77 changes: 0 additions & 77 deletions
77
x-pack/plugins/ingest_manager/server/services/retry_setup.test.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
x-pack/plugins/ingest_manager/server/services/setup_utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { firstPromiseBlocksAndFufills } from './setup_utils'; | ||
|
||
async function sleep(ms: number) { | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} | ||
|
||
describe('limitOne', () => { | ||
it('first promise called blocks others', async () => { | ||
const fnA = jest.fn(); | ||
const fnB = jest.fn(); | ||
const fnC = jest.fn(); | ||
const fnD = jest.fn(); | ||
const promises = [ | ||
firstPromiseBlocksAndFufills(fnA), | ||
firstPromiseBlocksAndFufills(fnB), | ||
firstPromiseBlocksAndFufills(fnC), | ||
firstPromiseBlocksAndFufills(fnD), | ||
]; | ||
await Promise.all(promises); | ||
|
||
expect(fnA).toHaveBeenCalledTimes(1); | ||
expect(fnB).toHaveBeenCalledTimes(0); | ||
expect(fnC).toHaveBeenCalledTimes(0); | ||
expect(fnD).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
describe('first promise created, not necessarily first fufilled, sets value for all in queue', () => { | ||
it('succeeds', async () => { | ||
const fnA = jest.fn().mockImplementation(async () => { | ||
await sleep(1000); | ||
return 'called first'; | ||
}); | ||
const fnB = jest.fn().mockImplementation(async () => 'called second'); | ||
const fnC = jest.fn().mockImplementation(async () => 'called third'); | ||
const fnD = jest.fn().mockImplementation(async () => 'called fourth'); | ||
const promises = [ | ||
firstPromiseBlocksAndFufills(fnA), | ||
firstPromiseBlocksAndFufills(fnB), | ||
firstPromiseBlocksAndFufills(fnC), | ||
firstPromiseBlocksAndFufills(fnD), | ||
]; | ||
|
||
expect(fnA).toHaveBeenCalledTimes(1); | ||
expect(fnB).toHaveBeenCalledTimes(0); | ||
expect(fnC).toHaveBeenCalledTimes(0); | ||
expect(fnD).toHaveBeenCalledTimes(0); | ||
await expect(Promise.all(promises)).resolves.toEqual([ | ||
'called first', | ||
'called first', | ||
'called first', | ||
'called first', | ||
]); | ||
}); | ||
|
||
it('throws', async () => { | ||
const expectedError = new Error('error is called first'); | ||
const fnA = jest.fn().mockImplementation(async () => { | ||
await sleep(1000); | ||
throw expectedError; | ||
}); | ||
const fnB = jest.fn().mockImplementation(async () => 'called second'); | ||
const fnC = jest.fn().mockImplementation(async () => 'called third'); | ||
const fnD = jest.fn().mockImplementation(async () => 'called fourth'); | ||
const promises = [ | ||
firstPromiseBlocksAndFufills(fnA), | ||
firstPromiseBlocksAndFufills(fnB), | ||
firstPromiseBlocksAndFufills(fnC), | ||
firstPromiseBlocksAndFufills(fnD), | ||
]; | ||
|
||
await expect(Promise.all(promises)).rejects.toThrow(expectedError); | ||
await expect(Promise.allSettled(promises)).resolves.toEqual([ | ||
{ status: 'rejected', reason: expectedError }, | ||
{ status: 'rejected', reason: expectedError }, | ||
{ status: 'rejected', reason: expectedError }, | ||
{ status: 'rejected', reason: expectedError }, | ||
]); | ||
|
||
expect(fnA).toHaveBeenCalledTimes(1); | ||
expect(fnB).toHaveBeenCalledTimes(0); | ||
expect(fnC).toHaveBeenCalledTimes(0); | ||
expect(fnD).toHaveBeenCalledTimes(0); | ||
}); | ||
}); | ||
|
||
it('does not block other calls after batch is fufilled. can call again for a new result', async () => { | ||
const fnA = jest | ||
.fn() | ||
.mockImplementationOnce(() => 'fnA first') | ||
.mockImplementationOnce(() => 'fnA second') | ||
.mockImplementation(() => 'fnA default/2+'); | ||
const fnB = jest.fn(); | ||
const fnC = jest.fn(); | ||
const fnD = jest.fn(); | ||
let promises = [ | ||
firstPromiseBlocksAndFufills(fnA), | ||
firstPromiseBlocksAndFufills(fnB), | ||
firstPromiseBlocksAndFufills(fnC), | ||
firstPromiseBlocksAndFufills(fnD), | ||
]; | ||
let results = await Promise.all(promises); | ||
|
||
expect(fnA).toHaveBeenCalledTimes(1); | ||
expect(fnB).toHaveBeenCalledTimes(0); | ||
expect(fnC).toHaveBeenCalledTimes(0); | ||
expect(fnD).toHaveBeenCalledTimes(0); | ||
expect(results).toEqual(['fnA first', 'fnA first', 'fnA first', 'fnA first']); | ||
|
||
promises = [ | ||
firstPromiseBlocksAndFufills(fnA), | ||
firstPromiseBlocksAndFufills(fnB), | ||
firstPromiseBlocksAndFufills(fnC), | ||
firstPromiseBlocksAndFufills(fnD), | ||
]; | ||
results = await Promise.all(promises); | ||
expect(fnA).toHaveBeenCalledTimes(2); | ||
expect(fnB).toHaveBeenCalledTimes(0); | ||
expect(fnC).toHaveBeenCalledTimes(0); | ||
expect(fnD).toHaveBeenCalledTimes(0); | ||
expect(results).toEqual(['fnA second', 'fnA second', 'fnA second', 'fnA second']); | ||
|
||
promises = [ | ||
firstPromiseBlocksAndFufills(fnA), | ||
firstPromiseBlocksAndFufills(fnB), | ||
firstPromiseBlocksAndFufills(fnC), | ||
firstPromiseBlocksAndFufills(fnD), | ||
]; | ||
results = await Promise.all(promises); | ||
expect(fnA).toHaveBeenCalledTimes(3); | ||
expect(fnB).toHaveBeenCalledTimes(0); | ||
expect(fnC).toHaveBeenCalledTimes(0); | ||
expect(fnD).toHaveBeenCalledTimes(0); | ||
expect(results).toEqual([ | ||
'fnA default/2+', | ||
'fnA default/2+', | ||
'fnA default/2+', | ||
'fnA default/2+', | ||
]); | ||
|
||
promises = [ | ||
firstPromiseBlocksAndFufills(fnA), | ||
firstPromiseBlocksAndFufills(fnB), | ||
firstPromiseBlocksAndFufills(fnC), | ||
firstPromiseBlocksAndFufills(fnD), | ||
]; | ||
results = await Promise.all(promises); | ||
expect(fnA).toHaveBeenCalledTimes(4); | ||
expect(fnB).toHaveBeenCalledTimes(0); | ||
expect(fnC).toHaveBeenCalledTimes(0); | ||
expect(fnD).toHaveBeenCalledTimes(0); | ||
expect(results).toEqual([ | ||
'fnA default/2+', | ||
'fnA default/2+', | ||
'fnA default/2+', | ||
'fnA default/2+', | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters