-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update error message when a task errors because of a warehouse * adds retry promise func and unit test * Refactors the retry promise function and adds tests * Fixes comments and refactors * Adds test for operations case, minor refactoring * Upgrade package
- Loading branch information
1 parent
05856fb
commit 988479e
Showing
10 changed files
with
257 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export async function retry<T>(func: () => Promise<T>, retries: number): Promise<T> { | ||
try { | ||
return await func(); | ||
} catch (e) { | ||
if (retries === 0) { | ||
throw e; | ||
} | ||
return await retry(func, retries - 1); | ||
} | ||
} |
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,56 @@ | ||
import { retry } from "@dataform/api/utils/retry"; | ||
import { expect } from "chai"; | ||
|
||
describe("retry", () => { | ||
it("doesn't retry if the function succeeds", async () => { | ||
let calledTimes = 0; | ||
const succeedingFunc = async () => { | ||
calledTimes += 1; | ||
return "success"; | ||
}; | ||
const result = await retry(succeedingFunc, 2); | ||
expect(result).to.eq("success"); | ||
expect(calledTimes).eq(1); | ||
}); | ||
|
||
it("doesn't retry if the function fails and retries is 0", async () => { | ||
let calledTimes = 0; | ||
const failingFunc = async () => { | ||
calledTimes += 1; | ||
throw new Error("an error"); | ||
}; | ||
try { | ||
await retry(failingFunc, 0); | ||
} catch (e) { | ||
expect(e.toString()).to.eq("Error: an error"); | ||
} | ||
expect(calledTimes).eq(1); | ||
}); | ||
|
||
it("calls the function three times if the function fails and retries is 2", async () => { | ||
let calledTimes = 0; | ||
const failingFunc = async () => { | ||
calledTimes += 1; | ||
throw new Error("an error"); | ||
}; | ||
try { | ||
await retry(failingFunc, 2); | ||
} catch (e) {} | ||
expect(calledTimes).eq(3); | ||
}); | ||
|
||
it("will eventually return a success if the function has failed before", async () => { | ||
let calledTimes = 0; | ||
const failingFunc = async () => { | ||
if (calledTimes > 1) { | ||
return "success"; | ||
} | ||
calledTimes += 1; | ||
throw new Error("an error"); | ||
}; | ||
try { | ||
await retry(failingFunc, 2); | ||
} catch (e) {} | ||
expect(calledTimes).eq(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
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# NOTE: If you change the format of this line, you must change the bash command | ||
# in /scripts/publish to extract the version string correctly. | ||
DF_VERSION = "1.3.9" | ||
DF_VERSION = "1.4.0" |