|
19 | 19 | import static com.google.cloud.dataflow.sdk.util.common.Counter.AggregationKind.MAX;
|
20 | 20 | import static com.google.cloud.dataflow.sdk.util.common.Counter.AggregationKind.SUM;
|
21 | 21 | import static org.hamcrest.Matchers.containsInAnyOrder;
|
| 22 | +import static org.hamcrest.Matchers.equalTo; |
22 | 23 | import static org.junit.Assert.assertFalse;
|
23 | 24 | import static org.junit.Assert.assertSame;
|
24 | 25 | import static org.junit.Assert.assertThat;
|
@@ -109,6 +110,70 @@ public void testAddOrReuseWithIncompatibleTypesThrowsException() {
|
109 | 110 | set.addOrReuseCounter(c1Incompatible);
|
110 | 111 | }
|
111 | 112 |
|
| 113 | + @Test |
| 114 | + public void testMergeWithDifferentNamesAddsAll() { |
| 115 | + Counter<?> c1 = Counter.longs("c1", SUM); |
| 116 | + Counter<?> c2 = Counter.ints("c2", MAX); |
| 117 | + |
| 118 | + set.add(c1); |
| 119 | + set.add(c2); |
| 120 | + |
| 121 | + CounterSet newSet = new CounterSet(); |
| 122 | + newSet.merge(set); |
| 123 | + |
| 124 | + assertThat(newSet, containsInAnyOrder(c1, c2)); |
| 125 | + } |
| 126 | + |
| 127 | + @SuppressWarnings("unchecked") |
| 128 | + @Test |
| 129 | + public void testMergeWithSameNamesMerges() { |
| 130 | + Counter<Long> c1 = Counter.longs("c1", SUM); |
| 131 | + Counter<Integer> c2 = Counter.ints("c2", MAX); |
| 132 | + |
| 133 | + set.add(c1); |
| 134 | + set.add(c2); |
| 135 | + |
| 136 | + c1.addValue(3L); |
| 137 | + c2.addValue(22); |
| 138 | + |
| 139 | + CounterSet newSet = new CounterSet(); |
| 140 | + Counter<Long> c1Prime = Counter.longs("c1", SUM); |
| 141 | + Counter<Integer> c2Prime = Counter.ints("c2", MAX); |
| 142 | + |
| 143 | + c1Prime.addValue(7L); |
| 144 | + c2Prime.addValue(14); |
| 145 | + |
| 146 | + newSet.add(c1Prime); |
| 147 | + newSet.add(c2Prime); |
| 148 | + |
| 149 | + newSet.merge(set); |
| 150 | + |
| 151 | + assertThat((Counter<Long>) newSet.getExistingCounter("c1"), equalTo(c1Prime)); |
| 152 | + assertThat((Long) newSet.getExistingCounter("c1").getAggregate(), equalTo(10L)); |
| 153 | + |
| 154 | + assertThat((Counter<Integer>) newSet.getExistingCounter("c2"), equalTo(c2Prime)); |
| 155 | + assertThat((Integer) newSet.getExistingCounter("c2").getAggregate(), equalTo(22)); |
| 156 | + } |
| 157 | + |
| 158 | + @SuppressWarnings("unchecked") |
| 159 | + @Test |
| 160 | + public void testMergeWithIncompatibleTypesThrowsException() { |
| 161 | + Counter<Long> c1 = Counter.longs("c1", SUM); |
| 162 | + |
| 163 | + set.add(c1); |
| 164 | + |
| 165 | + CounterSet newSet = new CounterSet(); |
| 166 | + Counter<Long> c1Prime = Counter.longs("c1", MAX); |
| 167 | + |
| 168 | + newSet.add(c1Prime); |
| 169 | + |
| 170 | + thrown.expect(IllegalArgumentException.class); |
| 171 | + thrown.expectMessage("c1"); |
| 172 | + thrown.expectMessage("incompatible counters with the same name"); |
| 173 | + |
| 174 | + newSet.merge(set); |
| 175 | + } |
| 176 | + |
112 | 177 | @Test
|
113 | 178 | public void testAddCounterMutatorAddCounterAddsCounter() {
|
114 | 179 | Counter<?> c1 = Counter.longs("c1", SUM);
|
|
0 commit comments