kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 0385b2721a89238444d9b2c343d3fcc6ca45a3bca443a9fc8d1248d7e3ffcd14 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-09-08 16:06 -07:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-09-08 16:06 -07:00 |
| Parents | fe8bf3701324, 6b311d3696f1 |
1 file changed
added driver-core/src/test/java/com/datastax/driver/core/HostConnectionPoolTest.java +96 −0
| @@ -0,0 +1,96 @@ | ||
| 1 | +package com.datastax.driver.core; | |
| 2 | + | |
| 3 | +import com.codahale.metrics.Gauge; | |
| 4 | +import com.google.common.collect.Lists; | |
| 5 | +import org.testng.annotations.Test; | |
| 6 | + | |
| 7 | +import java.util.Collection; | |
| 8 | +import java.util.concurrent.CountDownLatch; | |
| 9 | +import java.util.concurrent.ExecutorService; | |
| 10 | +import java.util.concurrent.Executors; | |
| 11 | +import java.util.concurrent.ScheduledExecutorService; | |
| 12 | +import java.util.concurrent.atomic.AtomicBoolean; | |
| 13 | + | |
| 14 | +import static java.util.concurrent.TimeUnit.SECONDS; | |
| 15 | +import static org.testng.Assert.fail; | |
| 16 | + | |
| 17 | +public class HostConnectionPoolTest extends CCMBridge.PerClassSingleNodeCluster { | |
| 18 | + @Override | |
| 19 | + protected Collection<String> getTableDefinitions() { | |
| 20 | + StringBuilder sb = new StringBuilder("CREATE TABLE Java349 (mykey INT primary key"); | |
| 21 | + for (int i = 0; i < 1000; i++) { | |
| 22 | + sb.append(", column").append(i).append(" INT"); | |
| 23 | + } | |
| 24 | + sb.append(")"); | |
| 25 | + return Lists.newArrayList(sb.toString()); | |
| 26 | + } | |
| 27 | + | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * Test for #JAVA-349 - Negative openConnection count results in broken connection release. | |
| 31 | + * <p/> | |
| 32 | + * This test uses a table named "Java349" with 1000 column and performs asynchronously 100k insertions. While the | |
| 33 | + * insertions are being executed, the number of opened connection is monitored. | |
| 34 | + * <p/> | |
| 35 | + * If at anytime, the number of opened connections is negative, this test will fail. | |
| 36 | + * | |
| 37 | + * @throws InterruptedException | |
| 38 | + */ | |
| 39 | + @Test(groups = "long") | |
| 40 | + public void open_connections_metric_should_always_be_positive() throws InterruptedException { | |
| 41 | + // Track progress in a dedicated thread | |
| 42 | + int numberOfInserts = 100 * 1000; | |
| 43 | + final CountDownLatch pendingInserts = new CountDownLatch(numberOfInserts); | |
| 44 | + ExecutorService progressReportExecutor = Executors.newSingleThreadExecutor(); | |
| 45 | + final Runnable progressReporter = new Runnable() { | |
| 46 | + @Override | |
| 47 | + public void run() { | |
| 48 | + pendingInserts.countDown(); | |
| 49 | + } | |
| 50 | + }; | |
| 51 | + | |
| 52 | + // Track opened connections in a dedicated thread every one second | |
| 53 | + final AtomicBoolean negativeOpenConnectionCountSpotted = new AtomicBoolean(false); | |
| 54 | + final Gauge<Integer> openConnections = cluster.getMetrics().getOpenConnections(); | |
| 55 | + ScheduledExecutorService openConnectionsWatcherExecutor = Executors.newScheduledThreadPool(1); | |
| 56 | + final Runnable openConnectionsWatcher = new Runnable() { | |
| 57 | + @Override | |
| 58 | + public void run() { | |
| 59 | + Integer value = openConnections.getValue(); | |
| 60 | + if (value < 0) { | |
| 61 | + System.err.println("Negative value spotted for openConnection metric: " + value); | |
| 62 | + negativeOpenConnectionCountSpotted.set(true); | |
| 63 | + } | |
| 64 | + } | |
| 65 | + }; | |
| 66 | + openConnectionsWatcherExecutor.scheduleAtFixedRate(openConnectionsWatcher, 1, 1, SECONDS); | |
| 67 | + | |
| 68 | + // Insert 100k lines in a newly created 1k columns table | |
| 69 | + PreparedStatement insertStatement = session.prepare(generateJava349InsertStatement()); | |
| 70 | + for (int key = 0; key < numberOfInserts; key++) { | |
| 71 | + session.executeAsync(insertStatement.bind(key)).addListener(progressReporter, progressReportExecutor); | |
| 72 | + } | |
| 73 | + | |
| 74 | + // Wait for all inserts to happen and stop connections and progress tracking | |
| 75 | + pendingInserts.await(); | |
| 76 | + openConnectionsWatcherExecutor.shutdownNow(); | |
| 77 | + progressReportExecutor.shutdownNow(); | |
| 78 | + | |
| 79 | + if (negativeOpenConnectionCountSpotted.get()) { | |
| 80 | + fail("Negative value spotted for open connection count"); | |
| 81 | + } | |
| 82 | + } | |
| 83 | + | |
| 84 | + private String generateJava349InsertStatement() { | |
| 85 | + StringBuilder sb = new StringBuilder("INSERT INTO Java349 (mykey"); | |
| 86 | + for (int i = 0; i < 1000; i++) { | |
| 87 | + sb.append(", column").append(i); | |
| 88 | + } | |
| 89 | + sb.append(") VALUES (?"); | |
| 90 | + for (int i = 0; i < 1000; i++) { | |
| 91 | + sb.append(", ").append(i); | |
| 92 | + } | |
| 93 | + sb.append(");"); | |
| 94 | + return sb.toString(); | |
| 95 | + } | |
| 96 | +} |