forked from neo4j-contrib/neo4j-apoc-procedures
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Issue 4153] Adds a backoff strategy to OpenAI API calls #3
Open
mpetrini-larus
wants to merge
4
commits into
5.25
Choose a base branch
from
issue_4153
base: 5.25
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2635faa
[issue_4153] Stub version of ExtendedUtil.withBackOffRetries
mpetrini-larus 8d7874a
[issue_4153] Adds a backoff strategy to OpenAI API calls
mpetrini-larus 631d738
[issue_4153] Fixes PR review issues
mpetrini-larus 2761f73
Update extended/src/test/java/apoc/util/ExtendedUtilTest.java
mpetrini-larus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,106 @@ | ||
package apoc.util; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class ExtendedUtilTest { | ||
|
||
private static int i = 0; | ||
|
||
@Test | ||
public void testWithLinearBackOffRetriesWithSuccess() { | ||
i = 0; | ||
long start = System.currentTimeMillis(); | ||
int result = ExtendedUtil.withBackOffRetries( | ||
this::testFunction, | ||
true, -1, // test backoffRetry default value -> 5 | ||
runEx -> { | ||
if(!runEx.getMessage().contains("Expected")) | ||
throw new RuntimeException("Some Bad News..."); | ||
} | ||
); | ||
long time = System.currentTimeMillis() - start; | ||
|
||
assertEquals(4, result); | ||
|
||
// The method will attempt to execute the operation with a linear backoff strategy, | ||
// sleeping for 1 second, 2 seconds, and 3 seconds between retries. | ||
// This results in a total wait time of 6 seconds (1s + 2s + 3s) if the operation succeeds on the third attempt, | ||
// leading to an approximate execution time of 6 seconds. | ||
assertTrue(time > 5500); | ||
assertTrue(time < 6500); | ||
} | ||
|
||
@Test | ||
public void testWithExponentialBackOffRetriesWithSuccess() { | ||
i=0; | ||
long start = System.currentTimeMillis(); | ||
int result = ExtendedUtil.withBackOffRetries( | ||
this::testFunction, | ||
true, 0, // test backoffRetry default value -> 5 | ||
runEx -> { | ||
if(!runEx.getMessage().contains("Expected")) | ||
throw new RuntimeException("Some Bad News..."); | ||
}, | ||
Boolean.TRUE | ||
); | ||
long time = System.currentTimeMillis() - start; | ||
|
||
assertEquals(4, result); | ||
|
||
// The method will attempt to execute the operation with an exponential backoff strategy, | ||
// sleeping for 2 second, 4 seconds, and 8 seconds between retries. | ||
// This results in a total wait time of 14 seconds (2s + 4s + 8s) if the operation succeeds on the third attempt, | ||
// leading to an approximate execution time of 14 seconds. | ||
assertTrue(time > 13500); | ||
assertTrue(time < 14500); | ||
} | ||
|
||
@Test | ||
public void testBackOffRetriesWithError() { | ||
i=0; | ||
long start = System.currentTimeMillis(); | ||
assertThrows( | ||
RuntimeException.class, | ||
() -> ExtendedUtil.withBackOffRetries( | ||
this::testFunction, | ||
true, 2, | ||
runEx -> {} | ||
) | ||
); | ||
long time = System.currentTimeMillis() - start; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scrivi pure qua qualche riga di commento come sopra:
|
||
// The method is configured to retry the operation twice. | ||
// So, it will make two extra-attempts, waiting for 1 second and 2 seconds before failing and throwing an exception. | ||
// Resulting in an approximate execution time of 3 seconds. | ||
assertTrue(time > 2500); | ||
assertTrue(time < 3500); | ||
} | ||
|
||
@Test | ||
public void testWithoutBackOffRetriesWithError() { | ||
i=0; | ||
assertThrows( | ||
RuntimeException.class, | ||
() -> ExtendedUtil.withBackOffRetries( | ||
this::testFunction, | ||
false, 30, | ||
runEx -> {} | ||
) | ||
); | ||
|
||
// Retry strategy is not active and the testFunction is executed only once by raising an exception. | ||
assertEquals(1, i); | ||
} | ||
|
||
private int testFunction() { | ||
i++; | ||
if (i == 4) { | ||
throw new RuntimeException("Expected i not equal to 4"); | ||
} | ||
return i; | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Farei anch'io un'altra variabile, ma visto che l'implementazione viene dall'alto meglio rimettere
jsonPath
come primaThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
si ma poi non lo posso passare alla lambda sotto 😕
se la lascio com'era non è "effettivamente-statica" e la lambda non la digerisce
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Java maledetto, allora va bene così