|
24 | 24 | import com.google.cloud.spanner.Mutation; |
25 | 25 | import java.io.Serializable; |
26 | 26 | import java.util.ArrayList; |
| 27 | +import java.util.Arrays; |
27 | 28 | import java.util.Collections; |
28 | 29 | import java.util.Comparator; |
29 | 30 | import java.util.List; |
|
49 | 50 | import org.apache.beam.sdk.testing.PAssert; |
50 | 51 | import org.apache.beam.sdk.testing.TestPipeline; |
51 | 52 | import org.apache.beam.sdk.transforms.DoFn; |
| 53 | +import org.apache.beam.sdk.transforms.FlatMapElements; |
52 | 54 | import org.apache.beam.sdk.transforms.ParDo; |
53 | 55 | import org.apache.beam.sdk.values.KV; |
54 | 56 | import org.apache.beam.sdk.values.PCollection; |
| 57 | +import org.apache.beam.sdk.values.TypeDescriptors; |
55 | 58 | import org.joda.time.Duration; |
56 | 59 | import org.joda.time.Instant; |
57 | 60 | import org.junit.BeforeClass; |
@@ -154,47 +157,33 @@ public void testTransactionBoundaries() { |
154 | 157 | .apply( |
155 | 158 | ParDo.of(new SpannerChangeStreamOrderedByTimestampAndTransactionIdIT.ToStringFn())); |
156 | 159 |
|
157 | | - // Assert that the returned PCollection contains all six transactions (in string representation) |
158 | | - // and that each transaction contains, in order, the list of mutations added. |
159 | | - PAssert.that(tokens) |
| 160 | + // Split into per-record lines so the assertion doesn't depend on how many flushes occurred. |
| 161 | + // getRecordString() guarantees one \n-terminated line per record (keys + mod type only). |
| 162 | + PCollection<String> changeRecordLines = |
| 163 | + tokens.apply( |
| 164 | + FlatMapElements.into(TypeDescriptors.strings()) |
| 165 | + .via( |
| 166 | + (String token) -> |
| 167 | + Arrays.stream(token.split("\n", -1)) |
| 168 | + .filter(line -> !line.isEmpty()) |
| 169 | + .collect(Collectors.toList()))); |
| 170 | + |
| 171 | + PAssert.that(changeRecordLines) |
160 | 172 | .containsInAnyOrder( |
161 | | - // Insert Singer 0 into the table. |
162 | | - "{\"SingerId\":\"0\"},INSERT\n" |
163 | | - |
164 | | - // Insert Singer 1 and 2 into the table, |
165 | | - + "{\"SingerId\":\"1\"}{\"SingerId\":\"2\"},INSERT\n" |
166 | | - |
167 | | - // Delete Singer 1 and Insert Singer 3 into the table. |
168 | | - + "{\"SingerId\":\"1\"},DELETE\n" |
169 | | - + "{\"SingerId\":\"3\"},INSERT\n" |
170 | | - |
171 | | - // Delete Singers 2 and 3. |
172 | | - + "{\"SingerId\":\"2\"}{\"SingerId\":\"3\"},DELETE\n" |
173 | | - |
174 | | - // Delete Singer 0. |
175 | | - + "{\"SingerId\":\"0\"},DELETE\n", |
176 | | - |
177 | | - // Second batch of transactions. |
178 | | - // Insert Singer 1 and 2 into the table, |
179 | | - "{\"SingerId\":\"1\"}{\"SingerId\":\"2\"},INSERT\n" |
180 | | - |
181 | | - // Delete Singer 1 and Insert Singer 3 into the table. |
182 | | - + "{\"SingerId\":\"1\"},DELETE\n" |
183 | | - + "{\"SingerId\":\"3\"},INSERT\n" |
184 | | - |
185 | | - // Delete Singers 2 and 3. |
186 | | - + "{\"SingerId\":\"2\"}{\"SingerId\":\"3\"},DELETE\n", |
187 | | - |
188 | | - // Third batch of transactions. |
189 | | - // Insert Singer 1 and 2 into the table, |
190 | | - "{\"SingerId\":\"1\"}{\"SingerId\":\"2\"},INSERT\n" |
191 | | - |
192 | | - // Delete Singer 1 and Insert Singer 3 into the table. |
193 | | - + "{\"SingerId\":\"1\"},DELETE\n" |
194 | | - + "{\"SingerId\":\"3\"},INSERT\n" |
195 | | - |
196 | | - // Delete Singers 2 and 3. |
197 | | - + "{\"SingerId\":\"2\"}{\"SingerId\":\"3\"},DELETE\n"); |
| 173 | + "{\"SingerId\":\"0\"},INSERT", |
| 174 | + "{\"SingerId\":\"0\"},DELETE", |
| 175 | + "{\"SingerId\":\"1\"}{\"SingerId\":\"2\"},INSERT", |
| 176 | + "{\"SingerId\":\"1\"}{\"SingerId\":\"2\"},INSERT", |
| 177 | + "{\"SingerId\":\"1\"}{\"SingerId\":\"2\"},INSERT", |
| 178 | + "{\"SingerId\":\"1\"},DELETE", |
| 179 | + "{\"SingerId\":\"1\"},DELETE", |
| 180 | + "{\"SingerId\":\"1\"},DELETE", |
| 181 | + "{\"SingerId\":\"3\"},INSERT", |
| 182 | + "{\"SingerId\":\"3\"},INSERT", |
| 183 | + "{\"SingerId\":\"3\"},INSERT", |
| 184 | + "{\"SingerId\":\"2\"}{\"SingerId\":\"3\"},DELETE", |
| 185 | + "{\"SingerId\":\"2\"}{\"SingerId\":\"3\"},DELETE", |
| 186 | + "{\"SingerId\":\"2\"}{\"SingerId\":\"3\"},DELETE"); |
198 | 187 |
|
199 | 188 | pipeline |
200 | 189 | .runWithAdditionalOptionArgs(Collections.singletonList("--streaming")) |
|
0 commit comments