Elffuss BraidHub

kikocis/java-driver public

Fix some tests so they do not leak Cluster instances.
Changeset56a63006452dd0e034718f83afb215424d7bdb3d8745daaddeaa151f36b8650b
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-07-16 12:55 -05:00
CommitterAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-07-16 12:55 -05:00
Parentsb4f0888fb54b

Browse the files at this changeset

2 files changed

modified driver-core/src/test/java/com/datastax/driver/core/AuthenticationTest.java +17 −11
@@ -59,36 +59,42 @@
5959
6060 @Test(groups = "short")
6161 public void should_connect_with_credentials() throws InterruptedException {
62+ Cluster cluster = Cluster.builder().addContactPoint(CCMBridge.IP_PREFIX + '1')
63+ .withCredentials("cassandra", "cassandra")
64+ .build();
6265 try {
63- Cluster.builder().addContactPoint(CCMBridge.IP_PREFIX + '1')
64- .withCredentials("cassandra", "cassandra")
65- .build()
66- .connect();
66+ cluster.connect();
6767 } catch (NoHostAvailableException e) {
6868 logger.error(e.getCustomMessage(1, true, true));
69+ } finally {
70+ cluster.close();
6971 }
7072 }
7173
7274 @Test(groups = "short", expectedExceptions = AuthenticationException.class)
7375 public void should_fail_to_connect_with_wrong_credentials() throws InterruptedException {
76+ Cluster cluster = Cluster.builder().addContactPoint(CCMBridge.IP_PREFIX + '1')
77+ .withCredentials("bogus", "bogus")
78+ .build();
7479 try {
75- Cluster.builder().addContactPoint(CCMBridge.IP_PREFIX + '1')
76- .withCredentials("bogus", "bogus")
77- .build()
78- .connect();
80+ cluster.connect();
7981 } catch (NoHostAvailableException e) {
8082 logger.error(e.getCustomMessage(1, true, true));
83+ } finally {
84+ cluster.close();
8185 }
8286 }
8387
8488 @Test(groups = "short", expectedExceptions = AuthenticationException.class)
8589 public void should_fail_to_connect_without_credentials() throws InterruptedException {
90+ Cluster cluster = Cluster.builder().addContactPoint(CCMBridge.IP_PREFIX + '1')
91+ .build();
8692 try {
87- Cluster.builder().addContactPoint(CCMBridge.IP_PREFIX + '1')
88- .build()
89- .connect();
93+ cluster.connect();
9094 } catch (NoHostAvailableException e) {
9195 logger.error(e.getCustomMessage(1, true, true));
96+ } finally {
97+ cluster.close();
9298 }
9399 }
94100 }
modified driver-core/src/test/java/com/datastax/driver/core/policies/LatencyAwarePolicyTest.java +70 −58
@@ -70,23 +70,27 @@
7070 Cluster.Builder builder = super.createClusterBuilder();
7171 builder.withLoadBalancingPolicy(latencyAwarePolicy);
7272 Cluster cluster = builder.build();
73- cluster.init(); // force initialization of latency aware policy
74- LatencyTrackerBarrier barrier = new LatencyTrackerBarrier(1);
75- cluster.register(barrier); // add barrier to synchronize latency tracker threads with the current thread
76- Session session = cluster.connect();
77- // when
78- session.execute(query);
79- // then
80- // wait until trackers have been notified
81- barrier.await();
82- // make sure the updater is called at least once
83- latencyAwarePolicy.new Updater().run();
84- LatencyAwarePolicy.Snapshot snapshot = latencyAwarePolicy.getScoresSnapshot();
85- assertThat(snapshot.getAllStats()).hasSize(1);
86- LatencyAwarePolicy.Snapshot.Stats stats = snapshot.getStats(retrieveSingleHost(cluster));
87- assertThat(stats).isNotNull();
88- assertThat(stats.getMeasurementsCount()).isEqualTo(1);
89- assertThat(stats.getLatencyScore()).isNotEqualTo(-1);
73+ try {
74+ cluster.init(); // force initialization of latency aware policy
75+ LatencyTrackerBarrier barrier = new LatencyTrackerBarrier(1);
76+ cluster.register(barrier); // add barrier to synchronize latency tracker threads with the current thread
77+ Session session = cluster.connect();
78+ // when
79+ session.execute(query);
80+ // then
81+ // wait until trackers have been notified
82+ barrier.await();
83+ // make sure the updater is called at least once
84+ latencyAwarePolicy.new Updater().run();
85+ LatencyAwarePolicy.Snapshot snapshot = latencyAwarePolicy.getScoresSnapshot();
86+ assertThat(snapshot.getAllStats()).hasSize(1);
87+ LatencyAwarePolicy.Snapshot.Stats stats = snapshot.getStats(retrieveSingleHost(cluster));
88+ assertThat(stats).isNotNull();
89+ assertThat(stats.getMeasurementsCount()).isEqualTo(1);
90+ assertThat(stats.getLatencyScore()).isNotEqualTo(-1);
91+ } finally {
92+ cluster.close();
93+ }
9094 }
9195
9296 @Test(groups = "short")
@@ -105,29 +109,33 @@
105109 Cluster.Builder builder = super.createClusterBuilder();
106110 builder.withLoadBalancingPolicy(latencyAwarePolicy);
107111 Cluster cluster = builder.build();
108- cluster.init(); // force initialization of latency aware policy
109- LatencyTrackerBarrier barrier = new LatencyTrackerBarrier(1);
110- cluster.register(barrier);
111- Session session = cluster.connect();
112- // when
113112 try {
114- session.execute(query);
115- fail("Should have thrown NoHostAvailableException");
116- } catch(NoHostAvailableException e) {
117- // ok
118- Throwable error = e.getErrors().get(hostAddress);
119- assertThat(error).isNotNull();
120- assertThat(error).isInstanceOf(UnavailableException.class);
113+ cluster.init(); // force initialization of latency aware policy
114+ LatencyTrackerBarrier barrier = new LatencyTrackerBarrier(1);
115+ cluster.register(barrier);
116+ Session session = cluster.connect();
117+ // when
118+ try {
119+ session.execute(query);
120+ fail("Should have thrown NoHostAvailableException");
121+ } catch (NoHostAvailableException e) {
122+ // ok
123+ Throwable error = e.getErrors().get(hostAddress);
124+ assertThat(error).isNotNull();
125+ assertThat(error).isInstanceOf(UnavailableException.class);
126+ }
127+ // then
128+ // wait until trackers have been notified
129+ barrier.await();
130+ // make sure the updater is called at least once
131+ latencyAwarePolicy.new Updater().run();
132+ LatencyAwarePolicy.Snapshot snapshot = latencyAwarePolicy.getScoresSnapshot();
133+ assertThat(snapshot.getAllStats()).isEmpty();
134+ LatencyAwarePolicy.Snapshot.Stats stats = snapshot.getStats(retrieveSingleHost(cluster));
135+ assertThat(stats).isNull();
136+ } finally {
137+ cluster.close();
121138 }
122- // then
123- // wait until trackers have been notified
124- barrier.await();
125- // make sure the updater is called at least once
126- latencyAwarePolicy.new Updater().run();
127- LatencyAwarePolicy.Snapshot snapshot = latencyAwarePolicy.getScoresSnapshot();
128- assertThat(snapshot.getAllStats()).isEmpty();
129- LatencyAwarePolicy.Snapshot.Stats stats = snapshot.getStats(retrieveSingleHost(cluster));
130- assertThat(stats).isNull();
131139 }
132140
133141 @Test(groups= "short")
@@ -147,28 +155,32 @@
147155 builder.withLoadBalancingPolicy(latencyAwarePolicy);
148156 builder.withRetryPolicy(FallthroughRetryPolicy.INSTANCE);
149157 Cluster cluster = builder.build();
150- cluster.init(); // force initialization of latency aware policy
151- LatencyTrackerBarrier barrier = new LatencyTrackerBarrier(1);
152- cluster.register(barrier);
153- Session session = cluster.connect();
154- // when
155158 try {
156- session.execute(query);
157- fail("Should have thrown ReadTimeoutException");
158- } catch(ReadTimeoutException e) {
159- // ok
159+ cluster.init(); // force initialization of latency aware policy
160+ LatencyTrackerBarrier barrier = new LatencyTrackerBarrier(1);
161+ cluster.register(barrier);
162+ Session session = cluster.connect();
163+ // when
164+ try {
165+ session.execute(query);
166+ fail("Should have thrown ReadTimeoutException");
167+ } catch (ReadTimeoutException e) {
168+ // ok
169+ }
170+ // then
171+ // wait until trackers have been notified
172+ barrier.await();
173+ // make sure the updater is called at least once
174+ latencyAwarePolicy.new Updater().run();
175+ LatencyAwarePolicy.Snapshot snapshot = latencyAwarePolicy.getScoresSnapshot();
176+ assertThat(snapshot.getAllStats()).hasSize(1);
177+ LatencyAwarePolicy.Snapshot.Stats stats = snapshot.getStats(retrieveSingleHost(cluster));
178+ assertThat(stats).isNotNull();
179+ assertThat(stats.getMeasurementsCount()).isEqualTo(1);
180+ assertThat(stats.getLatencyScore()).isNotEqualTo(-1);
181+ } finally {
182+ cluster.close();
160183 }
161- // then
162- // wait until trackers have been notified
163- barrier.await();
164- // make sure the updater is called at least once
165- latencyAwarePolicy.new Updater().run();
166- LatencyAwarePolicy.Snapshot snapshot = latencyAwarePolicy.getScoresSnapshot();
167- assertThat(snapshot.getAllStats()).hasSize(1);
168- LatencyAwarePolicy.Snapshot.Stats stats = snapshot.getStats(retrieveSingleHost(cluster));
169- assertThat(stats).isNotNull();
170- assertThat(stats.getMeasurementsCount()).isEqualTo(1);
171- assertThat(stats.getLatencyScore()).isNotEqualTo(-1);
172184 }
173185
174186 }