kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 5ce316cb6b8b10d51d4b18eba73f268e0b6235a0976968b363d1c3d661304f9a |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2016-01-15 19:59 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-01-15 19:59 +01:00 |
| Parents | 7e6c21d3caab |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/PoolingOptions.java +19 −1
| @@ -50,7 +50,21 @@ | ||
| 50 | 50 | */ |
| 51 | 51 | public class PoolingOptions { |
| 52 | 52 | |
| 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; | |
| 54 | 68 | |
| 55 | 69 | private static final Map<ProtocolVersion, Map<String, Integer>> DEFAULTS = ImmutableMap.<ProtocolVersion, Map<String, Integer>>of( |
| 56 | 70 | ProtocolVersion.V2, ImmutableMap.<String, Integer>builder() |
| @@ -139,6 +153,7 @@ | ||
| 139 | 153 | public synchronized PoolingOptions setCoreConnectionsPerHost(HostDistance distance, int newCoreConnections) { |
| 140 | 154 | if (distance == HostDistance.IGNORED) |
| 141 | 155 | throw new IllegalArgumentException("Cannot set core connections per host for " + distance + " hosts"); |
| 156 | + Preconditions.checkArgument(newCoreConnections >= 0, "core number of connections must be positive"); | |
| 142 | 157 | |
| 143 | 158 | if (maxConnections[distance.ordinal()] != UNSET) |
| 144 | 159 | checkConnectionsPerHostOrder(newCoreConnections, maxConnections[distance.ordinal()], distance); |
| @@ -182,6 +197,7 @@ | ||
| 182 | 197 | public synchronized PoolingOptions setMaxConnectionsPerHost(HostDistance distance, int newMaxConnections) { |
| 183 | 198 | if (distance == HostDistance.IGNORED) |
| 184 | 199 | throw new IllegalArgumentException("Cannot set max connections per host for " + distance + " hosts"); |
| 200 | + Preconditions.checkArgument(newMaxConnections >= 0, "max number of connections must be positive"); | |
| 185 | 201 | |
| 186 | 202 | if (coreConnections[distance.ordinal()] != UNSET) |
| 187 | 203 | checkConnectionsPerHostOrder(coreConnections[distance.ordinal()], newMaxConnections, distance); |
| @@ -206,6 +222,8 @@ | ||
| 206 | 222 | public synchronized PoolingOptions setConnectionsPerHost(HostDistance distance, int core, int max) { |
| 207 | 223 | if (distance == HostDistance.IGNORED) |
| 208 | 224 | 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"); | |
| 209 | 227 | |
| 210 | 228 | checkConnectionsPerHostOrder(core, max, distance); |
| 211 | 229 | coreConnections[distance.ordinal()] = core; |
modified driver-core/src/test/java/com/datastax/driver/core/PoolingOptionsTest.java +44 −0
| @@ -93,5 +93,49 @@ | ||
| 93 | 93 | assertThat(options.getCoreConnectionsPerHost(LOCAL)).isEqualTo(10); |
| 94 | 94 | assertThat(options.getMaxConnectionsPerHost(LOCAL)).isEqualTo(15); |
| 95 | 95 | } |
| 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 | + } | |
| 96 | 140 | } |
| 97 | 141 |