Skip to content

Commit

Permalink
[ISSUE-1462] Use low retry-backoff value when running tests
Browse files Browse the repository at this point in the history
- Because we use a mock server
  • Loading branch information
HoussemNasri committed Jan 29, 2025
1 parent f3b3405 commit ced7adc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@

public class DavClient {
public static final int MAX_CALENDAR_OBJECT_UPDATE_RETRIES = 5;
public static final Duration MIN_CALENDAR_OBJECT_UPDATE_RETRY_BACKOFF = Duration.ofMillis(100);

private static final Logger LOGGER = LoggerFactory.getLogger(DavClient.class);
private static final Duration DEFAULT_RESPONSE_TIMEOUT = Duration.ofSeconds(10);
Expand All @@ -67,10 +66,14 @@ public class DavClient {
private static final String ACCEPT_VCARD_JSON = "application/vcard+json";
private static final String ACCEPT_XML = "application/xml";
private static final String CONTENT_TYPE_VCARD = "application/vcard";
private static final boolean SHOULD_RETRY_CALENDAR_OBJECT_UPDATE = true;

private final HttpClient client;
private final DavConfiguration config;
public final Duration calendarObjectUpdateRetryBackoff =
Optional.ofNullable(System.getProperty("MIN_CALENDAR_OBJECT_UPDATE_RETRY_BACKOFF_IN_MILLS"))
.map(Long::parseLong)
.map(Duration::ofMillis)
.orElse(Duration.ofMillis(100));

public DavClient(DavConfiguration config) {
this.config = config;
Expand Down Expand Up @@ -157,8 +160,8 @@ public Mono<Void> updateCalendarObject(String username, URI calendarObjectUri, F
.map(calendarObjectUpdater)
.flatMap(updatedCalendarObject -> doUpdateCalendarObject(username, updatedCalendarObject))
.retryWhen(
Retry.backoff(MAX_CALENDAR_OBJECT_UPDATE_RETRIES, MIN_CALENDAR_OBJECT_UPDATE_RETRY_BACKOFF)
.filter(e -> e instanceof DavClientException && ((DavClientException) e).shouldRetry())
Retry.backoff(MAX_CALENDAR_OBJECT_UPDATE_RETRIES, calendarObjectUpdateRetryBackoff)
.filter(RetriableDavClientException.class::isInstance)
.onRetryExhaustedThrow((retryBackoffSpec, retrySignal) ->
new DavClientException("Max retries exceeded for calendar update", retrySignal.failure())));
}
Expand All @@ -184,10 +187,10 @@ private static Mono<Void> handleCalendarObjectUpdateResponse(DavCalendarObject u
() -> LOGGER.info("Calendar object '{}' updated successfully.",
updatedCalendarObject.uri()));
} else if (response.status() == HttpResponseStatus.PRECONDITION_FAILED) {
return Mono.error(new DavClientException(
return Mono.error(new RetriableDavClientException(
String.format(
"Precondition failed (ETag mismatch) when updating calendar object '%s'. Retry may be needed.",
updatedCalendarObject.uri()), SHOULD_RETRY_CALENDAR_OBJECT_UPDATE));
updatedCalendarObject.uri())));
} else {
return Mono.error(new DavClientException(
String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,12 @@
package com.linagora.tmail.dav;

public class DavClientException extends RuntimeException {
private final boolean shouldRetry;

public DavClientException(String message) {
this(message, false);
}

public DavClientException(String message, boolean shouldRetry) {
super(message);
this.shouldRetry = shouldRetry;
}

public DavClientException(String message, Throwable cause) {
this(message, cause, false);
}

public DavClientException(String message, Throwable cause, boolean shouldRetry) {
super(message, cause);
this.shouldRetry = shouldRetry;
}

public boolean shouldRetry() {
return shouldRetry;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/********************************************************************
* As a subpart of Twake Mail, this file is edited by Linagora. *
* *
* https://twake-mail.com/ *
* https://linagora.com *
* *
* This file is subject to The Affero Gnu Public License *
* version 3. *
* *
* https://www.gnu.org/licenses/agpl-3.0.en.html *
* *
* This program is distributed in the hope that it will be *
* useful, but WITHOUT ANY WARRANTY; without even the implied *
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR *
* PURPOSE. See the GNU Affero General Public License for *
* more details. *
********************************************************************/

package com.linagora.tmail.dav;

public class RetriableDavClientException extends DavClientException {

public RetriableDavClientException(String message) {
super(message);
}

public RetriableDavClientException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.apache.http.HttpStatus;
import org.apache.james.core.MailAddress;
import org.apache.james.util.ClassLoaderUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand Down Expand Up @@ -79,9 +80,15 @@ public class DavClientTest {

@BeforeEach
void setup() {
System.setProperty("MIN_CALENDAR_OBJECT_UPDATE_RETRY_BACKOFF_IN_MILLS", "10");
client = new DavClient(davServerExtension.getDavConfiguration());
}

@AfterEach
void tearDown() {
System.clearProperty("MIN_CALENDAR_OBJECT_UPDATE_RETRY_BACKOFF_IN_MILLS");
}

@Test
void existsCollectedContactShouldReturnTrueWhenHTTPResponseIs200() {
String collectedContactUid = UUID.randomUUID().toString();
Expand Down

0 comments on commit ced7adc

Please sign in to comment.