diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionPoolOptions.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionPoolOptions.java index 0781c11447e1..1e972f7fcaf4 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionPoolOptions.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SessionPoolOptions.java @@ -90,8 +90,6 @@ public static class Builder { * in parallel. Defaults to 0. */ public Builder setMinSessions(int minSessions) { - Preconditions.checkArgument( - maxSessions >= minSessions, "Min sessions must be <= max sessions"); this.minSessions = minSessions; return this; } @@ -103,8 +101,6 @@ public Builder setMinSessions(int minSessions) { * can either block or fail. Defaults to 2000. */ public Builder setMaxSessions(int maxSessions) { - Preconditions.checkArgument( - maxSessions >= minSessions, "Max sessions must be >= min" + "sessions"); this.maxSessions = maxSessions; return this; } @@ -127,8 +123,6 @@ public Builder setMaxIdleSessions(int maxIdleSessions) { * query "Select 1". Default value is 30 minutes. */ public Builder setKeepAliveIntervalMinutes(int intervalMinutes) { - Preconditions.checkArgument( - intervalMinutes < 60, "Keep alive interval should be less than" + "60 minutes"); this.keepAliveIntervalMinutes = intervalMinutes; return this; } @@ -161,16 +155,24 @@ public Builder setBlockIfPoolExhausted() { *
Default value is 0.2. */ public Builder setWriteSessionsFraction(float writeSessionsFraction) { - Preconditions.checkArgument( - writeSessionsFraction >= 0 && writeSessionsFraction <= 1, - "Fraction of write sessions must be between 0 and 1 (inclusive)"); this.writeSessionsFraction = writeSessionsFraction; return this; } /** Build a SessionPoolOption object */ public SessionPoolOptions build() { + validate(); return new SessionPoolOptions(this); } + + private void validate() { + Preconditions.checkArgument(maxSessions >= minSessions, + "Min sessions(%s) must be <= max sessions(%s)", minSessions, maxSessions); + Preconditions.checkArgument( + keepAliveIntervalMinutes < 60, "Keep alive interval should be less than" + "60 minutes"); + Preconditions.checkArgument( + writeSessionsFraction >= 0 && writeSessionsFraction <= 1, + "Fraction of write sessions must be between 0 and 1 (inclusive)"); + } } } diff --git a/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionPoolOptionsTest.java b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionPoolOptionsTest.java new file mode 100644 index 000000000000..9c97fa51174e --- /dev/null +++ b/google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionPoolOptionsTest.java @@ -0,0 +1,67 @@ +/* + * Copyright 2017 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.cloud.spanner; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import static junit.framework.TestCase.assertEquals; +import static org.junit.runners.Parameterized.Parameter; +import static org.junit.runners.Parameterized.Parameters; + +/** + * Unit tests for {@link com.google.cloud.spanner.SessionPoolOptions} + */ +@RunWith(Parameterized.class) +public class SessionPoolOptionsTest { + @Rule + public ExpectedException expectedException = ExpectedException.none(); + @Parameter + public int minSessions; + @Parameter(1) + public int maxSessions; + + @Parameters(name = "min sessions = {0}, max sessions = {1}") + public static Collection