|
33 | 33 | import com.google.cloud.dataflow.sdk.util.CloudCounterUtils;
|
34 | 34 | import com.google.cloud.dataflow.sdk.util.common.Counter.CounterMean;
|
35 | 35 |
|
| 36 | +import org.junit.Rule; |
36 | 37 | import org.junit.Test;
|
| 38 | +import org.junit.rules.ExpectedException; |
37 | 39 | import org.junit.runner.RunWith;
|
38 | 40 | import org.junit.runners.JUnit4;
|
39 | 41 |
|
40 | 42 | import java.util.Arrays;
|
41 | 43 | import java.util.HashSet;
|
| 44 | +import java.util.List; |
42 | 45 | import java.util.Set;
|
43 | 46 |
|
44 | 47 | /**
|
|
47 | 50 | @RunWith(JUnit4.class)
|
48 | 51 | public class CounterTest {
|
49 | 52 |
|
| 53 | + @Rule |
| 54 | + public ExpectedException thrown = ExpectedException.none(); |
| 55 | + |
50 | 56 | private static MetricUpdate flush(Counter<?> c) {
|
51 | 57 | // TODO: Move this out into a separate Counter test.
|
52 | 58 | return CounterTestUtils.extractCounterUpdate(c, true);
|
@@ -211,6 +217,13 @@ public void testSumLong() {
|
211 | 217 | c.resetToValue(100L).addValue(17L).addValue(49L);
|
212 | 218 | expectedTotal = expectedDelta = 166;
|
213 | 219 | assertOK(expectedTotal, expectedDelta, c);
|
| 220 | + |
| 221 | + Counter<Long> other = Counter.longs("sum-long", SUM); |
| 222 | + other.addValue(12L); |
| 223 | + expectedDelta = 12L; |
| 224 | + expectedTotal += 12L; |
| 225 | + c.merge(other); |
| 226 | + assertOK(expectedTotal, expectedDelta, c); |
214 | 227 | }
|
215 | 228 |
|
216 | 229 | @Test
|
@@ -241,6 +254,13 @@ public void testSumDouble() {
|
241 | 254 | c.resetToValue(Math.sqrt(17)).addValue(17.0).addValue(49.0);
|
242 | 255 | expectedTotal = expectedDelta = Math.sqrt(17.0) + 17.0 + 49.0;
|
243 | 256 | assertOK(expectedTotal, expectedDelta, c);
|
| 257 | + |
| 258 | + Counter<Double> other = Counter.doubles("sum-double", SUM); |
| 259 | + other.addValue(12 * Math.PI); |
| 260 | + expectedDelta = 12 * Math.PI; |
| 261 | + expectedTotal += 12 * Math.PI; |
| 262 | + c.merge(other); |
| 263 | + assertOK(expectedTotal, expectedDelta, c); |
244 | 264 | }
|
245 | 265 |
|
246 | 266 |
|
@@ -272,6 +292,12 @@ public void testMaxLong() {
|
272 | 292 | c.resetToValue(100L).addValue(171L).addValue(49L);
|
273 | 293 | expectedTotal = expectedDelta = 171;
|
274 | 294 | assertOK(expectedTotal, expectedDelta, c);
|
| 295 | + |
| 296 | + Counter<Long> other = Counter.longs("max-long", MAX); |
| 297 | + other.addValue(12L); |
| 298 | + expectedDelta = 12L; |
| 299 | + c.merge(other); |
| 300 | + assertOK(expectedTotal, expectedDelta, c); |
275 | 301 | }
|
276 | 302 |
|
277 | 303 | @Test
|
@@ -300,6 +326,12 @@ public void testMaxDouble() {
|
300 | 326 | c.resetToValue(Math.sqrt(17)).addValue(171.0).addValue(49.0);
|
301 | 327 | expectedTotal = expectedDelta = 171.0;
|
302 | 328 | assertOK(expectedTotal, expectedDelta, c);
|
| 329 | + |
| 330 | + Counter<Double> other = Counter.doubles("max-double", MAX); |
| 331 | + other.addValue(12 * Math.PI); |
| 332 | + expectedDelta = 12 * Math.PI; |
| 333 | + c.merge(other); |
| 334 | + assertOK(expectedTotal, expectedDelta, c); |
303 | 335 | }
|
304 | 336 |
|
305 | 337 |
|
@@ -331,6 +363,12 @@ public void testMinLong() {
|
331 | 363 | c.resetToValue(100L).addValue(171L).addValue(49L);
|
332 | 364 | expectedTotal = expectedDelta = 49;
|
333 | 365 | assertOK(expectedTotal, expectedDelta, c);
|
| 366 | + |
| 367 | + Counter<Long> other = Counter.longs("min-long", MIN); |
| 368 | + other.addValue(42L); |
| 369 | + expectedTotal = expectedDelta = 42L; |
| 370 | + c.merge(other); |
| 371 | + assertOK(expectedTotal, expectedDelta, c); |
334 | 372 | }
|
335 | 373 |
|
336 | 374 | @Test
|
@@ -359,6 +397,12 @@ public void testMinDouble() {
|
359 | 397 | c.resetToValue(Math.sqrt(17)).addValue(171.0).addValue(0.0);
|
360 | 398 | expectedTotal = expectedDelta = 0.0;
|
361 | 399 | assertOK(expectedTotal, expectedDelta, c);
|
| 400 | + |
| 401 | + Counter<Double> other = Counter.doubles("min-double", MIN); |
| 402 | + other.addValue(42 * Math.E); |
| 403 | + expectedDelta = 42 * Math.E; |
| 404 | + c.merge(other); |
| 405 | + assertOK(expectedTotal, expectedDelta, c); |
362 | 406 | }
|
363 | 407 |
|
364 | 408 |
|
@@ -419,6 +463,15 @@ public void testMeanLong() {
|
419 | 463 | expTotal = expDelta = 166;
|
420 | 464 | expCountTotal = expCountDelta = 5;
|
421 | 465 | assertMean(expTotal, expDelta, expCountTotal, expCountDelta, c);
|
| 466 | + |
| 467 | + Counter<Long> other = Counter.longs("mean-long", MEAN); |
| 468 | + other.addValue(12L).addValue(44L).addValue(-5L); |
| 469 | + expTotal += 12L + 44L - 5L; |
| 470 | + expDelta += 12L + 44L - 5L; |
| 471 | + expCountTotal += 3; |
| 472 | + expCountDelta += 3; |
| 473 | + c.merge(other); |
| 474 | + assertMean(expTotal, expDelta, expCountTotal, expCountDelta, c); |
422 | 475 | }
|
423 | 476 |
|
424 | 477 | @Test
|
@@ -458,6 +511,15 @@ public void testMeanDouble() {
|
458 | 511 | expTotal = expDelta = Math.sqrt(17.0) + 17.0 + 49.0;
|
459 | 512 | expCountTotal = expCountDelta = 5;
|
460 | 513 | assertMean(expTotal, expDelta, expCountTotal, expCountDelta, c);
|
| 514 | + |
| 515 | + Counter<Double> other = Counter.doubles("mean-double", MEAN); |
| 516 | + other.addValue(3 * Math.PI).addValue(12 * Math.E); |
| 517 | + expTotal += 3 * Math.PI + 12 * Math.E; |
| 518 | + expDelta += 3 * Math.PI + 12 * Math.E; |
| 519 | + expCountTotal += 2; |
| 520 | + expCountDelta += 2; |
| 521 | + c.merge(other); |
| 522 | + assertMean(expTotal, expDelta, expCountTotal, expCountDelta, c); |
461 | 523 | }
|
462 | 524 |
|
463 | 525 |
|
@@ -590,4 +652,40 @@ public void testExtraction() {
|
590 | 652 | assertEquals(cloudCountersFromSet, cloudCountersFromArray);
|
591 | 653 | assertEquals(2, cloudCountersFromSet.size());
|
592 | 654 | }
|
| 655 | + |
| 656 | + @Test |
| 657 | + public void testMergeIncompatibleCounters() { |
| 658 | + Counter<Long> longSums = Counter.longs("longsums", SUM); |
| 659 | + Counter<Long> longMean = Counter.longs("longmean", MEAN); |
| 660 | + Counter<Long> longMin = Counter.longs("longmin", MIN); |
| 661 | + |
| 662 | + Counter<Long> otherLongSums = Counter.longs("othersums", SUM); |
| 663 | + Counter<Long> otherLongMean = Counter.longs("otherlongmean", MEAN); |
| 664 | + |
| 665 | + Counter<Double> doubleSums = Counter.doubles("doublesums", SUM); |
| 666 | + Counter<Double> doubleMean = Counter.doubles("doublemean", MEAN); |
| 667 | + |
| 668 | + Counter<Boolean> boolAnd = Counter.booleans("and", AND); |
| 669 | + Counter<Boolean> boolOr = Counter.booleans("or", OR); |
| 670 | + |
| 671 | + List<Counter<Long>> longCounters = |
| 672 | + Arrays.asList(longSums, longMean, longMin, otherLongSums, otherLongMean); |
| 673 | + for (Counter<Long> left : longCounters) { |
| 674 | + for (Counter<Long> right : longCounters) { |
| 675 | + if (left != right) { |
| 676 | + assertIncompatibleMerge(left, right); |
| 677 | + } |
| 678 | + } |
| 679 | + } |
| 680 | + |
| 681 | + assertIncompatibleMerge(doubleSums, doubleMean); |
| 682 | + assertIncompatibleMerge(boolAnd, boolOr); |
| 683 | + } |
| 684 | + |
| 685 | + private <T> void assertIncompatibleMerge(Counter<T> left, Counter<T> right) { |
| 686 | + thrown.expect(IllegalArgumentException.class); |
| 687 | + thrown.expectMessage("Counters"); |
| 688 | + thrown.expectMessage("are incompatible"); |
| 689 | + left.merge(right); |
| 690 | + } |
593 | 691 | }
|
0 commit comments