Skip to content
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 @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -161,16 +155,24 @@ public Builder setBlockIfPoolExhausted() {
* <p>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)");
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Object[]> data() {
List<Object[]> params = new ArrayList<>();
params.add(new Object[]{1, 1});
params.add(new Object[]{500, 600});
params.add(new Object[]{600, 500});

return params;
}

@Test
public void setMinMaxSessions() {
if (minSessions > maxSessions) {
expectedException.expect(IllegalArgumentException.class);
}
SessionPoolOptions options = SessionPoolOptions.newBuilder()
.setMinSessions(minSessions)
.setMaxSessions(maxSessions)
.build();

assertEquals(minSessions, options.getMinSessions());
assertEquals(maxSessions, options.getMaxSessions());
}
}