Skip to content

Commit

Permalink
Improve exception message when renew lock or session idle timeout are…
Browse files Browse the repository at this point in the history
… negative (#39901)

* Improve exception message when renew lock or session idle timeout are negative
  • Loading branch information
lmolkova authored Apr 24, 2024
1 parent c07a4a0 commit 99424f4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1573,7 +1573,7 @@ private ServiceBusSessionProcessorClientBuilder() {
* @throws IllegalArgumentException If {code maxAutoLockRenewDuration} is negative.
*/
public ServiceBusSessionProcessorClientBuilder maxAutoLockRenewDuration(Duration maxAutoLockRenewDuration) {
validateAndThrow(maxAutoLockRenewDuration);
validateAndThrow(maxAutoLockRenewDuration, "maxAutoLockRenewDuration");
sessionReceiverClientBuilder.maxAutoLockRenewDuration(maxAutoLockRenewDuration);
return this;
}
Expand All @@ -1593,7 +1593,7 @@ public ServiceBusSessionProcessorClientBuilder maxAutoLockRenewDuration(Duration
* @throws IllegalArgumentException If {code maxAutoLockRenewDuration} is negative.
*/
public ServiceBusSessionProcessorClientBuilder sessionIdleTimeout(Duration sessionIdleTimeout) {
validateAndThrow(sessionIdleTimeout);
validateAndThrow(sessionIdleTimeout, "sessionIdleTimeout");
sessionReceiverClientBuilder.sessionIdleTimeout(sessionIdleTimeout);
return this;
}
Expand Down Expand Up @@ -1842,7 +1842,7 @@ public ServiceBusSessionReceiverClientBuilder disableAutoComplete() {
* @throws IllegalArgumentException If {code maxAutoLockRenewDuration} is negative.
*/
public ServiceBusSessionReceiverClientBuilder maxAutoLockRenewDuration(Duration maxAutoLockRenewDuration) {
validateAndThrow(maxAutoLockRenewDuration);
validateAndThrow(maxAutoLockRenewDuration, "maxAutoLockRenewDuration");
this.maxAutoLockRenewDuration = maxAutoLockRenewDuration;
return this;
}
Expand All @@ -1857,7 +1857,7 @@ public ServiceBusSessionReceiverClientBuilder maxAutoLockRenewDuration(Duration
* @throws IllegalArgumentException If {code maxAutoLockRenewDuration} is negative.
*/
ServiceBusSessionReceiverClientBuilder sessionIdleTimeout(Duration sessionIdleTimeout) {
validateAndThrow(sessionIdleTimeout);
validateAndThrow(sessionIdleTimeout, "sessionIdleTimeout");
this.sessionIdleTimeout = sessionIdleTimeout;
return this;
}
Expand Down Expand Up @@ -2391,7 +2391,7 @@ public ServiceBusProcessorClientBuilder processError(Consumer<ServiceBusErrorCon
* @throws IllegalArgumentException If {code maxAutoLockRenewDuration} is negative.
*/
public ServiceBusProcessorClientBuilder maxAutoLockRenewDuration(Duration maxAutoLockRenewDuration) {
validateAndThrow(maxAutoLockRenewDuration);
validateAndThrow(maxAutoLockRenewDuration, "maxAutoLockRenewDuration");
serviceBusReceiverClientBuilder.maxAutoLockRenewDuration(maxAutoLockRenewDuration);
return this;
}
Expand Down Expand Up @@ -2515,7 +2515,7 @@ public ServiceBusReceiverClientBuilder disableAutoComplete() {
* @throws IllegalArgumentException If {code maxAutoLockRenewDuration} is negative.
*/
public ServiceBusReceiverClientBuilder maxAutoLockRenewDuration(Duration maxAutoLockRenewDuration) {
validateAndThrow(maxAutoLockRenewDuration);
validateAndThrow(maxAutoLockRenewDuration, "maxAutoLockRenewDuration");
this.maxAutoLockRenewDuration = maxAutoLockRenewDuration;
return this;
}
Expand Down Expand Up @@ -2796,10 +2796,10 @@ private void validateAndThrow(int prefetchCount) {
}
}

private void validateAndThrow(Duration maxLockRenewalDuration) {
private void validateAndThrow(Duration maxLockRenewalDuration, String parameterName) {
if (maxLockRenewalDuration != null && maxLockRenewalDuration.isNegative()) {
throw LOGGER.logExceptionAsError(new IllegalArgumentException(
"'maxLockRenewalDuration' cannot be negative."));
String.format("'%s' cannot be negative.", parameterName)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertThrowsExactly;

public class ServiceBusClientBuilderUnitTest {

Expand Down Expand Up @@ -85,48 +86,54 @@ public void testThrowsWithTokenCredentialIfFullyQualifiedNameIsMissing() {

@Test
public void testThrowsIfNegativeMaxLockDuration() {
assertThrows(IllegalArgumentException.class,
assertThrowsExactly(IllegalArgumentException.class,
() -> createMinimalValidClientBuilder()
.sessionReceiver()
.maxAutoLockRenewDuration(Duration.ofSeconds(-1))
.queueName("fakequeue")
.buildAsyncClient());
assertThrows(IllegalArgumentException.class,
.buildAsyncClient(),
"'maxAutoLockRenewDuration' cannot be negative.");
assertThrowsExactly(IllegalArgumentException.class,
() -> createMinimalValidClientBuilder()
.receiver()
.maxAutoLockRenewDuration(Duration.ofSeconds(-1))
.queueName("fakequeue")
.buildClient());
assertThrows(IllegalArgumentException.class,
.buildClient(),
"'maxAutoLockRenewDuration' cannot be negative.");
assertThrowsExactly(IllegalArgumentException.class,
() -> createMinimalValidClientBuilder()
.processor()
.maxAutoLockRenewDuration(Duration.ofSeconds(-1))
.queueName("fakequeue")
.processMessage(x -> { })
.processError(x -> { })
.buildProcessorClient());
assertThrows(IllegalArgumentException.class,
.buildProcessorClient(),
"'maxAutoLockRenewDuration' cannot be negative.");
assertThrowsExactly(IllegalArgumentException.class,
() -> createMinimalValidClientBuilder()
.sessionProcessor()
.maxAutoLockRenewDuration(Duration.ofSeconds(-1))
.queueName("fakequeue")
.buildProcessorClient());
.buildProcessorClient(),
"'maxAutoLockRenewDuration' cannot be negative.");
}

@Test
public void testThrowsIfNegativeSessionIdle() {
assertThrows(IllegalArgumentException.class,
assertThrowsExactly(IllegalArgumentException.class,
() -> createMinimalValidClientBuilder()
.sessionReceiver()
.sessionIdleTimeout(Duration.ofSeconds(-1))
.queueName("fakequeue")
.buildAsyncClient());
assertThrows(IllegalArgumentException.class,
.buildAsyncClient(),
"'sessionIdleTimeout' cannot be negative.");
assertThrowsExactly(IllegalArgumentException.class,
() -> createMinimalValidClientBuilder()
.sessionProcessor()
.sessionIdleTimeout(Duration.ofSeconds(-1))
.queueName("fakequeue")
.buildProcessorClient());
.buildProcessorClient(),
"'sessionIdleTimeout' cannot be negative.");
}

@Test
Expand Down

0 comments on commit 99424f4

Please sign in to comment.