Skip to content

fix: allow zero durations to be set for connection properties #3916

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

Merged
merged 1 commit into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ public Duration convert(String value) {
} else {
duration = Duration.ofMillis(Long.parseLong(value.trim()));
}
if (duration.isZero()) {
// Converters should return null for invalid values.
if (duration.isNegative()) {
return null;
}
return duration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ public StatementResult statementShowReturnCommitStats() {

@Override
public StatementResult statementSetMaxCommitDelay(Duration duration) {
getConnection().setMaxCommitDelay(duration == null || duration.isZero() ? null : duration);
getConnection().setMaxCommitDelay(duration);
return noResult(SET_MAX_COMMIT_DELAY);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,14 @@ public void testSetStatementTimeout() {
assertThat(connection.hasStatementTimeout(), is(false));
boolean gotException = false;
try {
log("@EXPECT EXCEPTION INVALID_ARGUMENT");
// log("@EXPECT EXCEPTION INVALID_ARGUMENT");
log(String.format("SET STATEMENT_TIMEOUT='0%s';", getTimeUnitAbbreviation(unit)));
connection.setStatementTimeout(0L, unit);
connection.clearStatementTimeout();
// connection.setStatementTimeout(0L, unit);
} catch (IllegalArgumentException e) {
gotException = true;
}
assertThat(gotException, is(true));
assertThat(gotException, is(false));
log(
String.format(
"@EXPECT RESULT_SET 'STATEMENT_TIMEOUT',%s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void testConvert() throws CompileException {
DurationConverter converter = new DurationConverter(allowedValues);
assertThat(converter.convert("'100ms'"), is(equalTo(Duration.ofMillis(100L))));
assertThat(converter.convert("100"), is(equalTo(Duration.ofMillis(100))));
assertThat(converter.convert("'0ms'"), is(nullValue()));
assertThat(converter.convert("'0ms'"), is(Duration.ZERO));
assertThat(converter.convert("'-100ms'"), is(nullValue()));
assertThat(
converter.convert("'315576000000000ms'"), is(equalTo(Duration.ofSeconds(315576000000L))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,20 @@ public void testNoMaxCommitDelayByDefault() {
for (boolean autocommit : new boolean[] {true, false}) {
connection.setAutocommit(autocommit);
executeCommit(connection);
assertMaxCommitDelay(Duration.getDefaultInstance());
assertMaxCommitDelay(Duration.getDefaultInstance(), false);
mockSpanner.clearRequests();
}
}
}

@Test
public void testZeroMaxCommitDelay() {
try (Connection connection = createConnection()) {
for (boolean autocommit : new boolean[] {true, false}) {
connection.setAutocommit(autocommit);
connection.setMaxCommitDelay(java.time.Duration.ZERO);
executeCommit(connection);
assertMaxCommitDelay(Duration.getDefaultInstance(), true);
mockSpanner.clearRequests();
}
}
Expand All @@ -95,7 +108,19 @@ public void testMaxCommitDelayInConnectionString() {
for (boolean autocommit : new boolean[] {true, false}) {
connection.setAutocommit(autocommit);
executeCommit(connection);
assertMaxCommitDelay(Duration.newBuilder().setSeconds(1).build());
assertMaxCommitDelay(Duration.newBuilder().setSeconds(1).build(), true);
mockSpanner.clearRequests();
}
}
}

@Test
public void testZeroMaxCommitDelayInConnectionString() {
try (Connection connection = createConnection(";maxCommitDelay=0")) {
for (boolean autocommit : new boolean[] {true, false}) {
connection.setAutocommit(autocommit);
executeCommit(connection);
assertMaxCommitDelay(Duration.getDefaultInstance(), true);
mockSpanner.clearRequests();
}
}
Expand All @@ -121,20 +146,31 @@ public void testSetMaxCommitDelay() {
() -> {
executeCommit(connection);
assertMaxCommitDelay(
Duration.newBuilder()
.setNanos((int) TimeUnit.MILLISECONDS.toNanos(40))
.build());
Duration.newBuilder().setNanos((int) TimeUnit.MILLISECONDS.toNanos(40)).build(),
true);
mockSpanner.clearRequests();
});

if (useSql) {
// This is translated to Duration.ZERO.
connection.execute(
Statement.of(String.format("set %smax_commit_delay=null", getVariablePrefix())));
} else {
connection.setMaxCommitDelay(null);
}
executeCommit(connection);
assertMaxCommitDelay(Duration.getDefaultInstance());
// The SQL statement set max_commit_delay=null is translated to Duration.ZERO.
assertMaxCommitDelay(Duration.getDefaultInstance(), useSql);
mockSpanner.clearRequests();

if (useSql) {
connection.execute(
Statement.of(String.format("set %smax_commit_delay=0", getVariablePrefix())));
} else {
connection.setMaxCommitDelay(java.time.Duration.ZERO);
}
executeCommit(connection);
assertMaxCommitDelay(Duration.getDefaultInstance(), true);
mockSpanner.clearRequests();
}
}
Expand All @@ -150,10 +186,11 @@ void executeCommit(Connection connection) {
}
}

private void assertMaxCommitDelay(Duration expected) {
private void assertMaxCommitDelay(Duration expected, boolean hasMaxCommitDelay) {
List<CommitRequest> requests = mockSpanner.getRequestsOfType(CommitRequest.class);
assertEquals(1, requests.size());
CommitRequest request = requests.get(0);
assertEquals(expected, request.getMaxCommitDelay());
assertEquals(hasMaxCommitDelay, request.hasMaxCommitDelay());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void testConvert() throws CompileException {

assertEquals(
Duration.ofNanos((int) TimeUnit.MILLISECONDS.toNanos(100L)), converter.convert("'100ms'"));
assertNull(converter.convert("'0ms'"));
assertEquals(Duration.ZERO, converter.convert("'0ms'"));
assertNull(converter.convert("'-100ms'"));
assertEquals(Duration.ofSeconds(315576000000L), converter.convert("'315576000000000ms'"));
assertEquals(Duration.ofSeconds(1L), converter.convert("'1s'"));
Expand Down
Loading