|
5 | 5 | import org.dataloader.stats.Statistics;
|
6 | 6 | import org.dataloader.stats.StatisticsCollector;
|
7 | 7 | import org.dataloader.stats.context.IncrementBatchLoadCountByStatisticsContext;
|
| 8 | +import org.dataloader.stats.context.IncrementBatchLoadExceptionCountStatisticsContext; |
| 9 | +import org.dataloader.stats.context.IncrementCacheHitCountStatisticsContext; |
8 | 10 | import org.dataloader.stats.context.IncrementLoadCountStatisticsContext;
|
| 11 | +import org.dataloader.stats.context.IncrementLoadErrorCountStatisticsContext; |
9 | 12 | import org.junit.Test;
|
10 | 13 |
|
11 | 14 | import java.util.ArrayList;
|
12 | 15 | import java.util.List;
|
13 | 16 | import java.util.concurrent.CompletableFuture;
|
14 | 17 |
|
15 | 18 | import static java.util.Arrays.asList;
|
| 19 | +import static java.util.Collections.singletonList; |
16 | 20 | import static java.util.concurrent.CompletableFuture.completedFuture;
|
17 | 21 | import static org.dataloader.DataLoaderFactory.newDataLoader;
|
18 | 22 | import static org.hamcrest.Matchers.equalTo;
|
| 23 | +import static org.hamcrest.Matchers.hasSize; |
19 | 24 | import static org.junit.Assert.assertThat;
|
20 | 25 |
|
21 | 26 | /**
|
@@ -201,4 +206,116 @@ public void stats_are_collected_on_exceptions() {
|
201 | 206 | assertThat(stats.getBatchLoadExceptionCount(), equalTo(2L));
|
202 | 207 | assertThat(stats.getLoadErrorCount(), equalTo(3L));
|
203 | 208 | }
|
| 209 | + |
| 210 | + /** |
| 211 | + * A simple {@link StatisticsCollector} that stores the contexts passed to it. |
| 212 | + */ |
| 213 | + private static class ContextPassingStatisticsCollector implements StatisticsCollector { |
| 214 | + |
| 215 | + public List<IncrementLoadCountStatisticsContext<?>> incrementLoadCountStatisticsContexts = new ArrayList<>(); |
| 216 | + public List<IncrementLoadErrorCountStatisticsContext<?>> incrementLoadErrorCountStatisticsContexts = new ArrayList<>(); |
| 217 | + public List<IncrementBatchLoadCountByStatisticsContext<?>> incrementBatchLoadCountByStatisticsContexts = new ArrayList<>(); |
| 218 | + public List<IncrementBatchLoadExceptionCountStatisticsContext<?>> incrementBatchLoadExceptionCountStatisticsContexts = new ArrayList<>(); |
| 219 | + public List<IncrementCacheHitCountStatisticsContext<?>> incrementCacheHitCountStatisticsContexts = new ArrayList<>(); |
| 220 | + |
| 221 | + @Override |
| 222 | + public <K> long incrementLoadCount(IncrementLoadCountStatisticsContext<K> context) { |
| 223 | + incrementLoadCountStatisticsContexts.add(context); |
| 224 | + return 0; |
| 225 | + } |
| 226 | + |
| 227 | + @Deprecated |
| 228 | + @Override |
| 229 | + public long incrementLoadCount() { |
| 230 | + return 0; |
| 231 | + } |
| 232 | + |
| 233 | + @Override |
| 234 | + public <K> long incrementLoadErrorCount(IncrementLoadErrorCountStatisticsContext<K> context) { |
| 235 | + incrementLoadErrorCountStatisticsContexts.add(context); |
| 236 | + return 0; |
| 237 | + } |
| 238 | + |
| 239 | + @Deprecated |
| 240 | + @Override |
| 241 | + public long incrementLoadErrorCount() { |
| 242 | + return 0; |
| 243 | + } |
| 244 | + |
| 245 | + @Override |
| 246 | + public <K> long incrementBatchLoadCountBy(long delta, IncrementBatchLoadCountByStatisticsContext<K> context) { |
| 247 | + incrementBatchLoadCountByStatisticsContexts.add(context); |
| 248 | + return 0; |
| 249 | + } |
| 250 | + |
| 251 | + @Deprecated |
| 252 | + @Override |
| 253 | + public long incrementBatchLoadCountBy(long delta) { |
| 254 | + return 0; |
| 255 | + } |
| 256 | + |
| 257 | + @Override |
| 258 | + public <K> long incrementBatchLoadExceptionCount(IncrementBatchLoadExceptionCountStatisticsContext<K> context) { |
| 259 | + incrementBatchLoadExceptionCountStatisticsContexts.add(context); |
| 260 | + return 0; |
| 261 | + } |
| 262 | + |
| 263 | + @Deprecated |
| 264 | + @Override |
| 265 | + public long incrementBatchLoadExceptionCount() { |
| 266 | + return 0; |
| 267 | + } |
| 268 | + |
| 269 | + @Override |
| 270 | + public <K> long incrementCacheHitCount(IncrementCacheHitCountStatisticsContext<K> context) { |
| 271 | + incrementCacheHitCountStatisticsContexts.add(context); |
| 272 | + return 0; |
| 273 | + } |
| 274 | + |
| 275 | + @Deprecated |
| 276 | + @Override |
| 277 | + public long incrementCacheHitCount() { |
| 278 | + return 0; |
| 279 | + } |
| 280 | + |
| 281 | + @Override |
| 282 | + public Statistics getStatistics() { |
| 283 | + return null; |
| 284 | + } |
| 285 | + } |
| 286 | + |
| 287 | + @Test |
| 288 | + public void context_is_passed_through_to_collector() { |
| 289 | + ContextPassingStatisticsCollector statisticsCollector = new ContextPassingStatisticsCollector(); |
| 290 | + DataLoader<String, Try<String>> loader = newDataLoader(batchLoaderThatBlows, |
| 291 | + DataLoaderOptions.newOptions().setStatisticsCollector(() -> statisticsCollector) |
| 292 | + ); |
| 293 | + |
| 294 | + loader.load("key", "keyContext"); |
| 295 | + assertThat(statisticsCollector.incrementLoadCountStatisticsContexts, hasSize(1)); |
| 296 | + assertThat(statisticsCollector.incrementLoadCountStatisticsContexts.get(0).getKey(), equalTo("key")); |
| 297 | + assertThat(statisticsCollector.incrementLoadCountStatisticsContexts.get(0).getCallContext(), equalTo("keyContext")); |
| 298 | + |
| 299 | + loader.load("key", "keyContext"); |
| 300 | + assertThat(statisticsCollector.incrementCacheHitCountStatisticsContexts, hasSize(1)); |
| 301 | + assertThat(statisticsCollector.incrementCacheHitCountStatisticsContexts.get(0).getKey(), equalTo("key")); |
| 302 | + assertThat(statisticsCollector.incrementCacheHitCountStatisticsContexts.get(0).getCallContext(), equalTo("keyContext")); |
| 303 | + |
| 304 | + loader.dispatch(); |
| 305 | + assertThat(statisticsCollector.incrementBatchLoadCountByStatisticsContexts, hasSize(1)); |
| 306 | + assertThat(statisticsCollector.incrementBatchLoadCountByStatisticsContexts.get(0).getKeys(), equalTo(singletonList("key"))); |
| 307 | + assertThat(statisticsCollector.incrementBatchLoadCountByStatisticsContexts.get(0).getCallContexts(), equalTo(singletonList("keyContext"))); |
| 308 | + |
| 309 | + loader.load("exception", "exceptionKeyContext"); |
| 310 | + loader.dispatch(); |
| 311 | + assertThat(statisticsCollector.incrementBatchLoadExceptionCountStatisticsContexts, hasSize(1)); |
| 312 | + assertThat(statisticsCollector.incrementBatchLoadExceptionCountStatisticsContexts.get(0).getKeys(), equalTo(singletonList("exception"))); |
| 313 | + assertThat(statisticsCollector.incrementBatchLoadExceptionCountStatisticsContexts.get(0).getCallContexts(), equalTo(singletonList("exceptionKeyContext"))); |
| 314 | + |
| 315 | + loader.load("error", "errorKeyContext"); |
| 316 | + loader.dispatch(); |
| 317 | + assertThat(statisticsCollector.incrementLoadErrorCountStatisticsContexts, hasSize(1)); |
| 318 | + assertThat(statisticsCollector.incrementLoadErrorCountStatisticsContexts.get(0).getKey(), equalTo("error")); |
| 319 | + assertThat(statisticsCollector.incrementLoadErrorCountStatisticsContexts.get(0).getCallContext(), equalTo("errorKeyContext")); |
| 320 | + } |
204 | 321 | }
|
0 commit comments