kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | e02131eec8c7c6cd923eae496e1cedc4ea35efcefc1a977040e672d28d2c52b4 |
|---|---|
| Author | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-08-28 16:37 -05:00 |
| Committer | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-09-14 13:55 -05:00 |
| Parents | 44dc7480709d |
2 files changed
modified driver-core/src/test/java/com/datastax/driver/core/QueryLoggerErrorsTest.java +60 −98
| @@ -16,23 +16,21 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | import org.apache.log4j.Logger; |
| 19 | +import org.scassandra.http.client.PrimingRequest; | |
| 19 | 20 | import org.testng.annotations.AfterMethod; |
| 20 | 21 | import org.testng.annotations.BeforeMethod; |
| 22 | +import org.testng.annotations.DataProvider; | |
| 21 | 23 | import org.testng.annotations.Test; |
| 22 | 24 | |
| 23 | 25 | import static org.apache.log4j.Level.DEBUG; |
| 24 | 26 | import static org.apache.log4j.Level.INFO; |
| 25 | 27 | import static org.assertj.core.api.Assertions.assertThat; |
| 26 | 28 | 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.*; | |
| 30 | 30 | import static org.scassandra.http.client.PrimingRequest.queryBuilder; |
| 31 | +import static org.scassandra.http.client.PrimingRequest.then; | |
| 31 | 32 | |
| 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.*; | |
| 36 | 34 | |
| 37 | 35 | import static com.datastax.driver.core.QueryLogger.builder; |
| 38 | 36 | |
| @@ -49,38 +47,34 @@ | ||
| 49 | 47 | private MemoryAppender slowAppender; |
| 50 | 48 | private MemoryAppender errorAppender; |
| 51 | 49 | |
| 52 | - private QueryLogger queryLogger; | |
| 50 | + private Cluster cluster = null; | |
| 51 | + private Session session = null; | |
| 52 | + private QueryLogger queryLogger = null; | |
| 53 | 53 | |
| 54 | 54 | @BeforeMethod(groups = { "short", "unit" }) |
| 55 | - public void startCapturingLogs() { | |
| 55 | + public void setUp() { | |
| 56 | + slow.setLevel(INFO); | |
| 57 | + error.setLevel(INFO); | |
| 56 | 58 | slow.addAppender(slowAppender = new MemoryAppender()); |
| 57 | 59 | error.addAppender(errorAppender = new MemoryAppender()); |
| 60 | + | |
| 61 | + queryLogger = null; | |
| 62 | + | |
| 63 | + cluster = createClusterBuilder().build(); | |
| 64 | + session = cluster.connect(); | |
| 58 | 65 | } |
| 59 | 66 | |
| 60 | 67 | @AfterMethod(groups = { "short", "unit" }) |
| 61 | - public void stopCapturingLogs() { | |
| 68 | + public void tearDown() { | |
| 62 | 69 | slow.setLevel(null); |
| 63 | 70 | error.setLevel(null); |
| 64 | 71 | slow.removeAppender(slowAppender); |
| 65 | 72 | 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 | - } | |
| 74 | 73 | |
| 75 | - @BeforeMethod(groups = { "short", "unit" }) | |
| 76 | - public void resetQueryLogger() { | |
| 77 | 74 | queryLogger = null; |
| 78 | - } | |
| 79 | 75 | |
| 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(); | |
| 84 | 78 | } |
| 85 | 79 | } |
| 86 | 80 | |
| @@ -96,7 +90,7 @@ | ||
| 96 | 90 | primingClient.prime( |
| 97 | 91 | queryBuilder() |
| 98 | 92 | .withQuery(query) |
| 99 | - .withFixedDelay(100) | |
| 93 | + .withThen(then().withFixedDelay(100L)) | |
| 100 | 94 | .build() |
| 101 | 95 | ); |
| 102 | 96 | // when |
| @@ -105,7 +99,7 @@ | ||
| 105 | 99 | String line = slowAppender.waitAndGet(5000); |
| 106 | 100 | assertThat(line) |
| 107 | 101 | .contains("Query too slow") |
| 108 | - .contains("127.0.0.1") | |
| 102 | + .contains(ip) | |
| 109 | 103 | .contains(query); |
| 110 | 104 | } |
| 111 | 105 | |
| @@ -120,7 +114,7 @@ | ||
| 120 | 114 | primingClient.prime( |
| 121 | 115 | queryBuilder() |
| 122 | 116 | .withQuery(query) |
| 123 | - .withFixedDelay(100) | |
| 117 | + .withThen(then().withFixedDelay(100L)) | |
| 124 | 118 | .build() |
| 125 | 119 | ); |
| 126 | 120 | // when |
| @@ -134,74 +128,34 @@ | ||
| 134 | 128 | String line = errorAppender.waitAndGet(5000); |
| 135 | 129 | assertThat(line) |
| 136 | 130 | .contains("Query error") |
| 137 | - .contains("127.0.0.1") | |
| 131 | + .contains(ip) | |
| 138 | 132 | .contains(Integer.toString(scassandra.getBinaryPort())) |
| 139 | 133 | .contains(query) |
| 140 | 134 | .contains(OperationTimedOutException.class.getName()); |
| 141 | 135 | } |
| 142 | 136 | |
| 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 | + }; | |
| 201 | 155 | } |
| 202 | 156 | |
| 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 { | |
| 205 | 159 | // given |
| 206 | 160 | error.setLevel(DEBUG); |
| 207 | 161 | queryLogger = builder(cluster).build(); |
| @@ -210,28 +164,36 @@ | ||
| 210 | 164 | primingClient.prime( |
| 211 | 165 | queryBuilder() |
| 212 | 166 | .withQuery(query) |
| 213 | - .withResult(unavailable) | |
| 167 | + .withThen(then().withResult(result)) | |
| 214 | 168 | .build() |
| 215 | 169 | ); |
| 216 | 170 | // when |
| 217 | - // when | |
| 218 | 171 | try { |
| 219 | 172 | session.execute(query); |
| 220 | 173 | 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 | + } | |
| 226 | 188 | } |
| 227 | 189 | // then |
| 228 | 190 | String line = errorAppender.waitAndGet(5000); |
| 229 | 191 | assertThat(line) |
| 230 | 192 | .contains("Query error") |
| 231 | - .contains("127.0.0.1") | |
| 193 | + .contains(ip) | |
| 232 | 194 | .contains(Integer.toString(scassandra.getBinaryPort())) |
| 233 | 195 | .contains(query) |
| 234 | - .contains(UnavailableException.class.getName()); | |
| 196 | + .contains(loggedException.getName()); | |
| 235 | 197 | } |
| 236 | 198 | |
| 237 | 199 | } |
| 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 @@ | ||
| 56 | 56 | |
| 57 | 57 | protected CurrentClient currentClient; |
| 58 | 58 | |
| 59 | + protected static String ip = CCMBridge.IP_PREFIX + "1"; | |
| 60 | + | |
| 59 | 61 | @BeforeClass(groups = { "short", "long" }) |
| 60 | 62 | public void startScassandra() { |
| 61 | - String ip = CCMBridge.IP_PREFIX + "1"; | |
| 62 | 63 | scassandra = ScassandraFactory.createServer(ip, BINARY_PORT, ip, ADMIN_PORT); |
| 63 | 64 | scassandra.start(); |
| 64 | 65 | primingClient = scassandra.primingClient(); |