Skip to content

Commit

Permalink
Allow QueryOptions to set timeout to 0.
Browse files Browse the repository at this point in the history
If it is zero, the read timeout will be disabled for this statement.

Closes spring-projects#1494
  • Loading branch information
seungh0 committed Jun 12, 2024
1 parent 100b267 commit de11972
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ public int hashCode() {
* Builder for {@link QueryOptions}.
*
* @author Mark Paluch
* @author Seungho Kang
* @since 1.5
*/
public static class QueryOptionsBuilder {
Expand Down Expand Up @@ -482,8 +483,7 @@ public QueryOptionsBuilder readTimeout(long readTimeout, TimeUnit timeUnit) {
@Deprecated
public QueryOptionsBuilder readTimeout(Duration readTimeout) {

Assert.isTrue(!readTimeout.isZero() && !readTimeout.isNegative(),
"ReadTimeout must be greater than equal to zero");
Assert.isTrue(!readTimeout.isNegative(), "ReadTimeout must be greater than equal to zero");

this.timeout = readTimeout;

Expand Down Expand Up @@ -548,7 +548,7 @@ public QueryOptionsBuilder serialConsistencyLevel(ConsistencyLevel consistencyLe
*/
public QueryOptionsBuilder timeout(Duration timeout) {

Assert.isTrue(!timeout.isZero() && !timeout.isNegative(), "ReadTimeout must be greater than equal to zero");
Assert.isTrue(!timeout.isNegative(), "ReadTimeout must be greater than equal to zero");

this.timeout = timeout;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* @author Mark Paluch
* @author Tomasz Lelek
* @author Sam Lightfoot
* @author Seungho Kang
*/
class QueryOptionsUnitTests {

Expand Down Expand Up @@ -86,4 +87,34 @@ void buildQueryOptionsMutate() {
assertThat(mutated.getRoutingKeyspace()).isEqualTo(CqlIdentifier.fromCql("rksl"));
assertThat(mutated.getRoutingKey()).isEqualTo(ByteBuffer.allocate(1));
}

@Test // GH-1494
void buildZeroDurationTimeoutQueryOptions() {

QueryOptions queryOptions = QueryOptions.builder().timeout(Duration.ofSeconds(0)).build();

assertThat(queryOptions.getTimeout()).isEqualTo(Duration.ZERO);
}

@Test // GH-1494
void shouldRejectNegativeDurationTimeoutQueryOptions() {

assertThatIllegalArgumentException().isThrownBy(
() -> QueryOptions.builder().timeout(Duration.ofSeconds(-1)).build());
}

@Test // GH-1494
void buildZeroDurationReadTimeoutQueryOptions() {

QueryOptions queryOptions = QueryOptions.builder().readTimeout(Duration.ofSeconds(0)).build();

assertThat(queryOptions.getReadTimeout()).isEqualTo(Duration.ZERO);
}

@Test // GH-1494
void shouldRejectNegativeDurationReadTimeoutQueryOptions() {

assertThatIllegalArgumentException().isThrownBy(
() -> QueryOptions.builder().readTimeout(Duration.ofSeconds(-1)).build());
}
}

0 comments on commit de11972

Please sign in to comment.