Skip to content

Commit 4d2be45

Browse files
hive-botclaude
andcommitted
refactor: treat a document wait timeout too long for nanoseconds as no limit
The new translateDocumentWaitUntilDone(handle, timeout) and the timeout option of translateDocument() threw ArithmeticException for a Duration longer than about 292 years. Such a timeout now means no limit. The timeout tests get a JUnit timeout, and the polling tests shorten the wait between status requests instead of sleeping 5 seconds. Re-wrap long CHANGELOG lines. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent cf7dc39 commit 4d2be45

5 files changed

Lines changed: 45 additions & 9 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
`DocumentTranslationOptions.setExtraBodyParameters()` now return the options
1818
class, so further setters can be chained after them.
1919
- Added the `setStyleProfile()` option to `TextTranslationOptions` and
20-
`DocumentTranslationOptions`, sent as `style_profile_id` on text and document translation. It accepts a
21-
style profile ID or a `StyleProfile` object.
20+
`DocumentTranslationOptions`, sent as `style_profile_id` on text and
21+
document translation. It accepts a style profile ID or a `StyleProfile`
22+
object.
2223
- Added the `TagHandlingMode`, `TagHandlingVersion` and `ModelType` enums and
2324
`TextTranslationOptions.setTagHandling()`, `setTagHandlingVersion()` and
2425
`setModelType()` overloads that take them; the string setters remain.
@@ -31,8 +32,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3132
- Added `TextTranslationOptions.setStyleRule(String)` and
3233
`setTranslationMemory(String)`, taking the style rule or translation memory
3334
ID.
34-
- Added `TextTranslationOptions.setGlossaryIds(IGlossary, IGlossary...)`, taking glossary
35-
objects.
35+
- Added `TextTranslationOptions.setGlossaryIds(IGlossary, IGlossary...)`,
36+
taking glossary objects.
3637
- Added `Translator.translateDocumentGetStatus()`, which returns the status of
3738
a document translation.
3839
- Added a `translateDocumentWaitUntilDone(DocumentHandle, Duration)` overload

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,8 @@ arguments, `translateDocument()` accepts an optional
393393
- `setTimeout()`: a `Duration` limiting how long `translateDocument()` waits
394394
for the translation after the upload. When it expires, a
395395
`DocumentTranslationException` with the document handle is thrown. Not sent
396-
to the API; by default there is no limit.
396+
to the API; by default there is no limit, and a duration longer than about
397+
292 years also means no limit.
397398
- `setOutputFormat()`: the file format of the translated document, as a file
398399
extension, for example `docx`. By default the translated document has the
399400
same format as the input document.

‎deepl-java/src/main/java/com/deepl/api/DocumentTranslationOptions.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ public DocumentTranslationOptions setFilename(@Nullable String filename) {
288288
* after the upload. When it expires, <code>translateDocument</code> throws a {@link
289289
* DocumentTranslationException} that carries the document handle, so the document can still be
290290
* retrieved. This is a client-side limit and is not sent to the API. By default, this value is
291-
* <code>null</code> and there is no limit.
291+
* <code>null</code> and there is no limit; a duration too long to count in nanoseconds (about 292
292+
* years) also means no limit.
292293
*/
293294
public DocumentTranslationOptions setTimeout(@Nullable Duration timeout) {
294295
this.timeout = timeout;

‎deepl-java/src/main/java/com/deepl/api/Translator.java‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class Translator {
2828
protected final Parser jsonParser = new Parser();
2929
protected final HttpClientWrapper httpClientWrapper;
3030
protected final DeepLApiVersion apiVersion;
31+
// Package-private so offline tests can shorten the wait between document status requests.
32+
int documentPollIntervalMillis = 5000;
3133

3234
/**
3335
* Initializes a new Translator object using your Authentication Key.
@@ -582,11 +584,19 @@ public DocumentStatus translateDocumentWaitUntilDone(DocumentHandle handle)
582584
public DocumentStatus translateDocumentWaitUntilDone(
583585
DocumentHandle handle, @Nullable Duration timeout)
584586
throws InterruptedException, DeepLException {
585-
long deadline = timeout != null ? System.nanoTime() + timeout.toNanos() : 0;
587+
Long timeoutNanos = null;
588+
if (timeout != null) {
589+
try {
590+
timeoutNanos = timeout.toNanos();
591+
} catch (ArithmeticException exception) {
592+
// Longer than about 292 years: treated as no limit.
593+
}
594+
}
595+
long deadline = timeoutNanos != null ? System.nanoTime() + timeoutNanos : 0;
586596
DocumentStatus status = translateDocumentGetStatus(handle);
587597
while (status.ok() && !status.done()) {
588598
long sleepMillis = calculateDocumentWaitTimeMillis(status.getSecondsRemaining());
589-
if (timeout != null) {
599+
if (timeoutNanos != null) {
590600
long remainingMillis = (deadline - System.nanoTime()) / 1_000_000;
591601
if (remainingMillis <= 0) {
592602
throw new DeepLException(
@@ -1197,6 +1207,6 @@ private static String encodePathSegment(String value) throws DeepLException {
11971207

11981208
private int calculateDocumentWaitTimeMillis(Long secondsRemaining) {
11991209
// secondsRemaining is currently unreliable, so just poll equidistantly
1200-
return 5000;
1210+
return documentPollIntervalMillis;
12011211
}
12021212
}

‎deepl-java/src/offlineTest/java/com/deepl/api/TranslateDocumentTest.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.List;
3737
import java.util.Map;
3838
import org.junit.jupiter.api.Test;
39+
import org.junit.jupiter.api.Timeout;
3940
import org.junit.jupiter.api.extension.RegisterExtension;
4041
import org.junit.jupiter.api.io.TempDir;
4142

@@ -695,6 +696,7 @@ void keepsAnUnknownStatusAsOkAndNotDone() throws Exception {
695696

696697
@Test
697698
void keepsPollingAfterAnUnknownStatus() throws Exception {
699+
client.documentPollIntervalMillis = 10;
698700
wm.stubFor(
699701
post("/v2/document/D1")
700702
.inScenario("unknown")
@@ -712,6 +714,26 @@ void keepsPollingAfterAnUnknownStatus() throws Exception {
712714
}
713715

714716
@Test
717+
void treatsATimeoutTooLongForNanosecondsAsNoLimit() throws Exception {
718+
wm.stubFor(
719+
post("/v2/document/D1")
720+
.inScenario("long")
721+
.whenScenarioStateIs(Scenario.STARTED)
722+
.willSetStateTo("done")
723+
.willReturn(okJson("{\"document_id\":\"D1\",\"status\":\"queued\"}")));
724+
wm.stubFor(
725+
post("/v2/document/D1")
726+
.inScenario("long")
727+
.whenScenarioStateIs("done")
728+
.willReturn(okJson(DONE)));
729+
client.documentPollIntervalMillis = 10;
730+
DocumentStatus status =
731+
client.translateDocumentWaitUntilDone(HANDLE_OBJECT, Duration.ofSeconds(Long.MAX_VALUE));
732+
assertTrue(status.done());
733+
}
734+
735+
@Test
736+
@Timeout(30)
715737
void throwsDeepLExceptionWhenTheWaitTimeoutExpires() {
716738
wm.stubFor(
717739
post("/v2/document/D1")
@@ -724,6 +746,7 @@ void throwsDeepLExceptionWhenTheWaitTimeoutExpires() {
724746
}
725747

726748
@Test
749+
@Timeout(30)
727750
void throwsDocumentTranslationExceptionWithTheHandleWhenTheTimeoutOptionExpires()
728751
throws Exception {
729752
File input = writeInput("example.txt", "a");

0 commit comments

Comments
 (0)