kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | e9f1ac56c49a1a4220fd7b6105179e1c55482e3e945ac4f96aa859da8048f07e |
|---|---|
| Author | Joaquin Casares <joaquin@datastax.com> · 2014-07-17 13:45 -05:00 |
| Committer | Joaquin Casares <joaquin@datastax.com> · 2014-07-17 13:45 -05:00 |
| Parents | 48eee9fbeb84 |
1 file changed
modified driver-core/src/test/java/com/datastax/driver/core/SessionTest.java +89 −0
| @@ -130,4 +130,93 @@ | ||
| 130 | 130 | cluster.getConfiguration().getProtocolOptions().setCompression(ProtocolOptions.Compression.NONE); |
| 131 | 131 | } |
| 132 | 132 | } |
| 133 | + | |
| 134 | + @Test(groups = "short") | |
| 135 | + public void sessionMemoryLeakTest() throws Exception { | |
| 136 | + // Checking for JAVA-342 | |
| 137 | + | |
| 138 | + // give the driver time to close other sessions in this class | |
| 139 | + Thread.sleep(10); | |
| 140 | + | |
| 141 | + // create a new cluster object and ensure 0 sessions and connections | |
| 142 | + Cluster cluster = Cluster.builder().addContactPoints(CCMBridge.IP_PREFIX + '1').build(); | |
| 143 | + assertEquals(cluster.manager.sessions.size(), 0); | |
| 144 | + assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 0); | |
| 145 | + | |
| 146 | + Session session = cluster.connect(); | |
| 147 | + assertEquals(cluster.manager.sessions.size(), 1); | |
| 148 | + assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 5); | |
| 149 | + | |
| 150 | + // ensure sessions.size() returns to 0 with only 1 active connection | |
| 151 | + session.close(); | |
| 152 | + assertEquals(cluster.manager.sessions.size(), 0); | |
| 153 | + assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1); | |
| 154 | + | |
| 155 | + // give the driver time to close sessions | |
| 156 | + Thread.sleep(10); | |
| 157 | + | |
| 158 | + try { | |
| 159 | + for (int i = 0; i < 10000; ++i) { | |
| 160 | + assertEquals(cluster.manager.sessions.size(), 0); | |
| 161 | + assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1); | |
| 162 | + | |
| 163 | + session = cluster.connect(); | |
| 164 | + assertEquals(cluster.manager.sessions.size(), 1); | |
| 165 | + assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 5); | |
| 166 | + session.close(); | |
| 167 | + | |
| 168 | + // give the driver time to close sessions | |
| 169 | + Thread.sleep(10); | |
| 170 | + | |
| 171 | + // ensure sessions.size() always returns to 0 with only 1 active connection | |
| 172 | + assertEquals(cluster.manager.sessions.size(), 0); | |
| 173 | + assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1); | |
| 174 | + } | |
| 175 | + } finally { | |
| 176 | + cluster.close(); | |
| 177 | + } | |
| 178 | + } | |
| 179 | + | |
| 180 | + @Test(groups = "short") | |
| 181 | + public void connectionLeakTest() throws Exception { | |
| 182 | + // Checking for JAVA-342 | |
| 183 | + | |
| 184 | + // give the driver time to close other sessions in this class | |
| 185 | + Thread.sleep(10); | |
| 186 | + | |
| 187 | + // create a new cluster object and ensure 0 sessions and connections | |
| 188 | + Cluster cluster = Cluster.builder().addContactPoints(CCMBridge.IP_PREFIX + '1').build(); | |
| 189 | + assertEquals(cluster.manager.sessions.size(), 0); | |
| 190 | + assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 0); | |
| 191 | + | |
| 192 | + Session session = cluster.connect(); | |
| 193 | + assertEquals(cluster.manager.sessions.size(), 1); | |
| 194 | + assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 3); | |
| 195 | + | |
| 196 | + // ensure sessions.size() returns to 0 with only 1 active connection | |
| 197 | + session.close(); | |
| 198 | + assertEquals(cluster.manager.sessions.size(), 0); | |
| 199 | + assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1); | |
| 200 | + | |
| 201 | + try { | |
| 202 | + Session thisSession; | |
| 203 | + | |
| 204 | + // ensure bootstrapping a node does not create additional connections | |
| 205 | + cassandraCluster.bootstrapNode(2); | |
| 206 | + assertEquals(cluster.manager.sessions.size(), 0); | |
| 207 | + assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1); | |
| 208 | + | |
| 209 | + thisSession = cluster.connect(); | |
| 210 | + assertEquals(cluster.manager.sessions.size(), 1); | |
| 211 | + assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 5); | |
| 212 | + | |
| 213 | + // ensure bootstrapping a node does not create additional connections that won't get cleaned up | |
| 214 | + thisSession.close(); | |
| 215 | + assertEquals(cluster.manager.sessions.size(), 0); | |
| 216 | + assertEquals((int) cluster.getMetrics().getOpenConnections().getValue(), 1); | |
| 217 | + } finally { | |
| 218 | + cluster.close(); | |
| 219 | + } | |
| 220 | + | |
| 221 | + } | |
| 133 | 222 | } |