Elffuss BraidHub

kikocis/java-driver public

Update test to not share Cluster instance, test all errors.
Changesete02131eec8c7c6cd923eae496e1cedc4ea35efcefc1a977040e672d28d2c52b4
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-08-28 16:37 -05:00
CommitterAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-09-14 13:55 -05:00
Parents44dc7480709d

Browse the files at this changeset

2 files changed

modified driver-core/src/test/java/com/datastax/driver/core/QueryLoggerErrorsTest.java +60 −98
@@ -16,23 +16,21 @@
1616 package com.datastax.driver.core;
1717
1818 import org.apache.log4j.Logger;
19+import org.scassandra.http.client.PrimingRequest;
1920 import org.testng.annotations.AfterMethod;
2021 import org.testng.annotations.BeforeMethod;
22+import org.testng.annotations.DataProvider;
2123 import org.testng.annotations.Test;
2224
2325 import static org.apache.log4j.Level.DEBUG;
2426 import static org.apache.log4j.Level.INFO;
2527 import static org.assertj.core.api.Assertions.assertThat;
2628 import static org.assertj.core.api.Assertions.fail;
27-import static org.scassandra.http.client.PrimingRequest.Result.read_request_timeout;
28-import static org.scassandra.http.client.PrimingRequest.Result.unavailable;
29-import static org.scassandra.http.client.PrimingRequest.Result.write_request_timeout;
29+import static org.scassandra.http.client.PrimingRequest.Result.*;
3030 import static org.scassandra.http.client.PrimingRequest.queryBuilder;
31+import static org.scassandra.http.client.PrimingRequest.then;
3132
32-import com.datastax.driver.core.exceptions.NoHostAvailableException;
33-import com.datastax.driver.core.exceptions.ReadTimeoutException;
34-import com.datastax.driver.core.exceptions.UnavailableException;
35-import com.datastax.driver.core.exceptions.WriteTimeoutException;
33+import com.datastax.driver.core.exceptions.*;
3634
3735 import static com.datastax.driver.core.QueryLogger.builder;
3836
@@ -49,38 +47,34 @@
4947 private MemoryAppender slowAppender;
5048 private MemoryAppender errorAppender;
5149
52- private QueryLogger queryLogger;
50+ private Cluster cluster = null;
51+ private Session session = null;
52+ private QueryLogger queryLogger = null;
5353
5454 @BeforeMethod(groups = { "short", "unit" })
55- public void startCapturingLogs() {
55+ public void setUp() {
56+ slow.setLevel(INFO);
57+ error.setLevel(INFO);
5658 slow.addAppender(slowAppender = new MemoryAppender());
5759 error.addAppender(errorAppender = new MemoryAppender());
60+
61+ queryLogger = null;
62+
63+ cluster = createClusterBuilder().build();
64+ session = cluster.connect();
5865 }
5966
6067 @AfterMethod(groups = { "short", "unit" })
61- public void stopCapturingLogs() {
68+ public void tearDown() {
6269 slow.setLevel(null);
6370 error.setLevel(null);
6471 slow.removeAppender(slowAppender);
6572 error.removeAppender(errorAppender);
66- }
67-
68- @BeforeMethod(groups = { "short", "unit" })
69- @AfterMethod(groups = { "short", "unit" })
70- public void resetLogLevels() {
71- slow.setLevel(INFO);
72- error.setLevel(INFO);
73- }
7473
75- @BeforeMethod(groups = { "short", "unit" })
76- public void resetQueryLogger() {
7774 queryLogger = null;
78- }
7975
80- @AfterMethod(groups = { "short", "unit" })
81- public void unregisterQueryLogger() {
82- if(cluster != null && queryLogger != null) {
83- cluster.unregister(queryLogger);
76+ if(cluster != null) {
77+ cluster.close();
8478 }
8579 }
8680
@@ -96,7 +90,7 @@
9690 primingClient.prime(
9791 queryBuilder()
9892 .withQuery(query)
99- .withFixedDelay(100)
93+ .withThen(then().withFixedDelay(100L))
10094 .build()
10195 );
10296 // when
@@ -105,7 +99,7 @@
10599 String line = slowAppender.waitAndGet(5000);
106100 assertThat(line)
107101 .contains("Query too slow")
108- .contains("127.0.0.1")
102+ .contains(ip)
109103 .contains(query);
110104 }
111105
@@ -120,7 +114,7 @@
120114 primingClient.prime(
121115 queryBuilder()
122116 .withQuery(query)
123- .withFixedDelay(100)
117+ .withThen(then().withFixedDelay(100L))
124118 .build()
125119 );
126120 // when
@@ -134,74 +128,34 @@
134128 String line = errorAppender.waitAndGet(5000);
135129 assertThat(line)
136130 .contains("Query error")
137- .contains("127.0.0.1")
131+ .contains(ip)
138132 .contains(Integer.toString(scassandra.getBinaryPort()))
139133 .contains(query)
140134 .contains(OperationTimedOutException.class.getName());
141135 }
142136
143- @Test(groups = "short")
144- public void should_log_read_timeout_errors() throws Exception {
145- // given
146- error.setLevel(DEBUG);
147- queryLogger = builder(cluster).build();
148- cluster.register(queryLogger);
149- String query = "SELECT foo FROM bar";
150- primingClient.prime(
151- queryBuilder()
152- .withQuery(query)
153- .withResult(read_request_timeout)
154- .build()
155- );
156- // when
157- try {
158- session.execute(query);
159- fail("Should have thrown ReadTimeoutException");
160- } catch (ReadTimeoutException e) {
161- // ok
162- }
163- // then
164- String line = errorAppender.waitAndGet(5000);
165- assertThat(line)
166- .contains("Query error")
167- .contains("127.0.0.1")
168- .contains(Integer.toString(scassandra.getBinaryPort()))
169- .contains(query)
170- .contains(ReadTimeoutException.class.getName());
171- }
172-
173- @Test(groups = "short")
174- public void should_log_write_timeout_errors() throws Exception {
175- // given
176- error.setLevel(DEBUG);
177- queryLogger = builder(cluster).build();
178- cluster.register(queryLogger);
179- String query = "UPDATE test SET foo = 'bar' where qix = ?";
180- primingClient.prime(
181- queryBuilder()
182- .withQuery(query)
183- .withResult(write_request_timeout)
184- .build()
185- );
186- // when
187- try {
188- session.execute(query);
189- fail("Should have thrown WriteTimeoutException");
190- } catch (WriteTimeoutException e) {
191- // ok
192- }
193- // then
194- String line = errorAppender.waitAndGet(5000);
195- assertThat(line)
196- .contains("Query error")
197- .contains("127.0.0.1")
198- .contains(Integer.toString(scassandra.getBinaryPort()))
199- .contains(query)
200- .contains(WriteTimeoutException.class.getName());
137+ @DataProvider(name="errors")
138+ public static Object[][] createErrors() {
139+ return new Object[][] {
140+ { unavailable, NoHostAvailableException.class, UnavailableException.class },
141+ { write_request_timeout, WriteTimeoutException.class, WriteTimeoutException.class },
142+ { read_request_timeout, ReadTimeoutException.class, ReadTimeoutException.class },
143+ { server_error, NoHostAvailableException.class, ServerError.class },
144+ { protocol_error, ProtocolError.class, ProtocolError.class },
145+ { bad_credentials, AuthenticationException.class, AuthenticationException.class },
146+ { overloaded, NoHostAvailableException.class, OverloadedException.class },
147+ { is_bootstrapping, NoHostAvailableException.class, BootstrappingException.class },
148+ { truncate_error, TruncateException.class, TruncateException.class },
149+ { syntax_error, SyntaxError.class, SyntaxError.class },
150+ { invalid, InvalidQueryException.class, InvalidQueryException.class },
151+ { config_error, InvalidConfigurationInQueryException.class, InvalidConfigurationInQueryException.class },
152+ { already_exists, AlreadyExistsException.class, AlreadyExistsException.class },
153+ { unprepared, DriverInternalError.class, UnpreparedException.class }
154+ };
201155 }
202156
203- @Test(groups = "short")
204- public void should_log_unavailable_errors() throws Exception {
157+ @Test(groups = "short", dataProvider="errors")
158+ public void should_log_exception_from_the_given_result(PrimingRequest.Result result, Class<? extends Exception> expectedException, Class<? extends Exception> loggedException) throws Exception {
205159 // given
206160 error.setLevel(DEBUG);
207161 queryLogger = builder(cluster).build();
@@ -210,28 +164,36 @@
210164 primingClient.prime(
211165 queryBuilder()
212166 .withQuery(query)
213- .withResult(unavailable)
167+ .withThen(then().withResult(result))
214168 .build()
215169 );
216170 // when
217- // when
218171 try {
219172 session.execute(query);
220173 fail("Should have thrown NoHostAvailableException");
221- } catch(NoHostAvailableException e) {
222- // ok
223- Throwable error = e.getErrors().get(hostAddress);
224- assertThat(error).isNotNull();
225- assertThat(error).isInstanceOf(UnavailableException.class);
174+ } catch(Exception e) {
175+ if(e instanceof NoHostAvailableException) {
176+ assertThat(expectedException).isEqualTo(NoHostAvailableException.class);
177+ // ok
178+ Throwable error = ((NoHostAvailableException)e).getErrors().get(hostAddress);
179+ assertThat(error).isNotNull();
180+ if(loggedException.getSuperclass() == DriverInternalError.class) {
181+ assertThat(error).isOfAnyClassIn(DriverException.class);
182+ } else {
183+ assertThat(error).isOfAnyClassIn(loggedException);
184+ }
185+ } else {
186+ assertThat(e).isInstanceOf(expectedException);
187+ }
226188 }
227189 // then
228190 String line = errorAppender.waitAndGet(5000);
229191 assertThat(line)
230192 .contains("Query error")
231- .contains("127.0.0.1")
193+ .contains(ip)
232194 .contains(Integer.toString(scassandra.getBinaryPort()))
233195 .contains(query)
234- .contains(UnavailableException.class.getName());
196+ .contains(loggedException.getName());
235197 }
236198
237199 }
No newline at the end of the file
modified driver-core/src/test/java/com/datastax/driver/core/ScassandraTestBase.java +2 −1
@@ -56,9 +56,10 @@
5656
5757 protected CurrentClient currentClient;
5858
59+ protected static String ip = CCMBridge.IP_PREFIX + "1";
60+
5961 @BeforeClass(groups = { "short", "long" })
6062 public void startScassandra() {
61- String ip = CCMBridge.IP_PREFIX + "1";
6263 scassandra = ScassandraFactory.createServer(ip, BINARY_PORT, ip, ADMIN_PORT);
6364 scassandra.start();
6465 primingClient = scassandra.primingClient();