-
-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to wait for leaders when creating topics
- Loading branch information
Showing
5 changed files
with
156 additions
and
6 deletions.
There are no files selected for viewing
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
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
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
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,45 @@ | ||
const sleep = require('./sleep') | ||
const { KafkaJSTimeout } = require('../errors') | ||
|
||
module.exports = ( | ||
fn, | ||
{ delay = 50, maxWait = 10000, timeoutMessage = 'Timeout', ignoreTimeout = false } = {} | ||
) => { | ||
let timeoutId | ||
let totalWait = 0 | ||
let fulfilled = false | ||
|
||
const checkCondition = async (resolve, reject) => { | ||
totalWait += delay | ||
await sleep(delay) | ||
|
||
try { | ||
const result = await fn(totalWait) | ||
if (result) { | ||
fulfilled = true | ||
clearTimeout(timeoutId) | ||
return resolve(result) | ||
} | ||
|
||
checkCondition(resolve, reject) | ||
} catch (e) { | ||
fulfilled = true | ||
clearTimeout(timeoutId) | ||
reject(e) | ||
} | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
checkCondition(resolve, reject) | ||
|
||
if (ignoreTimeout) { | ||
return | ||
} | ||
|
||
timeoutId = setTimeout(() => { | ||
if (!fulfilled) { | ||
return reject(new KafkaJSTimeout(timeoutMessage)) | ||
} | ||
}, maxWait) | ||
}) | ||
} |
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,37 @@ | ||
const waitFor = require('./waitFor') | ||
|
||
describe('Utils > waitFor', () => { | ||
it('waits for the condition', async () => { | ||
let conditionValid = false | ||
|
||
setTimeout(() => { | ||
conditionValid = true | ||
}, 6) | ||
|
||
await expect(waitFor(() => conditionValid, { delay: 5 })) | ||
}) | ||
|
||
it('rejects the promise if the callback fail', async () => { | ||
await expect( | ||
waitFor( | ||
() => { | ||
throw new Error('callback failed!') | ||
}, | ||
{ delay: 1 } | ||
) | ||
).rejects.toHaveProperty('message', 'callback failed!') | ||
}) | ||
|
||
it('rejects the promise if the callback never succeeds', async () => { | ||
await expect(waitFor(() => false, { delay: 1, maxWait: 2 })).rejects.toHaveProperty( | ||
'message', | ||
'Timeout' | ||
) | ||
}) | ||
|
||
it('rejects the promise with a custom timeout message', async () => { | ||
await expect( | ||
waitFor(() => false, { delay: 1, maxWait: 2, timeoutMessage: 'foo bar' }) | ||
).rejects.toHaveProperty('message', 'foo bar') | ||
}) | ||
}) |