Elffuss BraidHub

kikocis/java-driver public

Minor improvements on PoolingOptions.

- expose UNSET
- fail on negative connection options.
Changeset5ce316cb6b8b10d51d4b18eba73f268e0b6235a0976968b363d1c3d661304f9a
Authorolim7t <omichallat+github@gmail.com> · 2016-01-15 19:59 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2016-01-15 19:59 +01:00
Parents7e6c21d3caab

Browse the files at this changeset

2 files changed

modified driver-core/src/main/java/com/datastax/driver/core/PoolingOptions.java +19 −1
@@ -50,7 +50,21 @@
5050 */
5151 public class PoolingOptions {
5252
53- static final int UNSET = Integer.MIN_VALUE;
53+ /**
54+ * The value returned for connection options when they have not been set by the client, and the protocol version
55+ * is not known yet.
56+ * <p/>
57+ * Once a {@code PoolingOptions} object is associated to a {@link Cluster} and that cluster initializes, the
58+ * protocol version will be detected, and connection options will take their default values for that protocol
59+ * version.
60+ * <p/>
61+ * The methods that may return this value are:
62+ * {@link #getCoreConnectionsPerHost(HostDistance)},
63+ * {@link #getMaxConnectionsPerHost(HostDistance)},
64+ * {@link #getNewConnectionThreshold(HostDistance)},
65+ * {@link #getMaxRequestsPerConnection(HostDistance)}.
66+ */
67+ public static final int UNSET = Integer.MIN_VALUE;
5468
5569 private static final Map<ProtocolVersion, Map<String, Integer>> DEFAULTS = ImmutableMap.<ProtocolVersion, Map<String, Integer>>of(
5670 ProtocolVersion.V2, ImmutableMap.<String, Integer>builder()
@@ -139,6 +153,7 @@
139153 public synchronized PoolingOptions setCoreConnectionsPerHost(HostDistance distance, int newCoreConnections) {
140154 if (distance == HostDistance.IGNORED)
141155 throw new IllegalArgumentException("Cannot set core connections per host for " + distance + " hosts");
156+ Preconditions.checkArgument(newCoreConnections >= 0, "core number of connections must be positive");
142157
143158 if (maxConnections[distance.ordinal()] != UNSET)
144159 checkConnectionsPerHostOrder(newCoreConnections, maxConnections[distance.ordinal()], distance);
@@ -182,6 +197,7 @@
182197 public synchronized PoolingOptions setMaxConnectionsPerHost(HostDistance distance, int newMaxConnections) {
183198 if (distance == HostDistance.IGNORED)
184199 throw new IllegalArgumentException("Cannot set max connections per host for " + distance + " hosts");
200+ Preconditions.checkArgument(newMaxConnections >= 0, "max number of connections must be positive");
185201
186202 if (coreConnections[distance.ordinal()] != UNSET)
187203 checkConnectionsPerHostOrder(coreConnections[distance.ordinal()], newMaxConnections, distance);
@@ -206,6 +222,8 @@
206222 public synchronized PoolingOptions setConnectionsPerHost(HostDistance distance, int core, int max) {
207223 if (distance == HostDistance.IGNORED)
208224 throw new IllegalArgumentException("Cannot set connections per host for " + distance + " hosts");
225+ Preconditions.checkArgument(core >= 0, "core number of connections must be positive");
226+ Preconditions.checkArgument(max >= 0, "max number of connections must be positive");
209227
210228 checkConnectionsPerHostOrder(core, max, distance);
211229 coreConnections[distance.ordinal()] = core;
modified driver-core/src/test/java/com/datastax/driver/core/PoolingOptionsTest.java +44 −0
@@ -93,5 +93,49 @@
9393 assertThat(options.getCoreConnectionsPerHost(LOCAL)).isEqualTo(10);
9494 assertThat(options.getMaxConnectionsPerHost(LOCAL)).isEqualTo(15);
9595 }
96+
97+ @Test(groups = "unit")
98+ public void should_leave_connection_options_unset_until_protocol_version_known() {
99+ PoolingOptions options = new PoolingOptions();
100+
101+ assertThat(options.getCoreConnectionsPerHost(LOCAL)).isEqualTo(PoolingOptions.UNSET);
102+ assertThat(options.getCoreConnectionsPerHost(REMOTE)).isEqualTo(PoolingOptions.UNSET);
103+ assertThat(options.getMaxConnectionsPerHost(LOCAL)).isEqualTo(PoolingOptions.UNSET);
104+ assertThat(options.getMaxConnectionsPerHost(REMOTE)).isEqualTo(PoolingOptions.UNSET);
105+ assertThat(options.getNewConnectionThreshold(LOCAL)).isEqualTo(PoolingOptions.UNSET);
106+ assertThat(options.getNewConnectionThreshold(REMOTE)).isEqualTo(PoolingOptions.UNSET);
107+ assertThat(options.getMaxRequestsPerConnection(LOCAL)).isEqualTo(PoolingOptions.UNSET);
108+ assertThat(options.getMaxRequestsPerConnection(REMOTE)).isEqualTo(PoolingOptions.UNSET);
109+ }
110+
111+ @Test(groups = "unit")
112+ public void should_reject_negative_connection_options_even_when_protocol_version_unknown() {
113+ PoolingOptions options = new PoolingOptions();
114+
115+ try {
116+ options.setCoreConnectionsPerHost(LOCAL, -1);
117+ fail("expected an IllegalArgumentException");
118+ } catch (IllegalArgumentException e) { /*expected*/ }
119+ try {
120+ options.setMaxConnectionsPerHost(LOCAL, -1);
121+ fail("expected an IllegalArgumentException");
122+ } catch (IllegalArgumentException e) { /*expected*/ }
123+ try {
124+ options.setConnectionsPerHost(LOCAL, -1, 1);
125+ fail("expected an IllegalArgumentException");
126+ } catch (IllegalArgumentException e) { /*expected*/ }
127+ try {
128+ options.setConnectionsPerHost(LOCAL, -2, -1);
129+ fail("expected an IllegalArgumentException");
130+ } catch (IllegalArgumentException e) { /*expected*/ }
131+ try {
132+ options.setNewConnectionThreshold(LOCAL, -1);
133+ fail("expected an IllegalArgumentException");
134+ } catch (IllegalArgumentException e) { /*expected*/ }
135+ try {
136+ options.setMaxRequestsPerConnection(LOCAL, -1);
137+ fail("expected an IllegalArgumentException");
138+ } catch (IllegalArgumentException e) { /*expected*/ }
139+ }
96140 }
97141