2626import static org .junit .Assert .assertTrue ;
2727
2828import java .io .Serializable ;
29- import java .util .ArrayList ;
30- import java .util .Arrays ;
31- import java .util .Collections ;
32- import java .util .List ;
3329import org .apache .beam .runners .spark .SparkPipelineOptions ;
3430import org .apache .beam .runners .spark .StreamingTest ;
3531import org .apache .beam .runners .spark .io .CreateStream ;
5248import org .apache .beam .sdk .transforms .Flatten ;
5349import org .apache .beam .sdk .transforms .PTransform ;
5450import org .apache .beam .sdk .transforms .ParDo ;
55- import org .apache .beam .sdk .transforms .SerializableFunction ;
5651import org .apache .beam .sdk .transforms .windowing .FixedWindows ;
57- import org .apache .beam .sdk .transforms .windowing .IntervalWindow ;
5852import org .apache .beam .sdk .transforms .windowing .Window ;
5953import org .apache .beam .sdk .values .KV ;
6054import org .apache .beam .sdk .values .PBegin ;
@@ -235,48 +229,6 @@ public void process(
235229 }
236230 }
237231
238- private static class VerifyStatefulOutput
239- implements SerializableFunction <Iterable <KV <Integer , Integer >>, Void > {
240- private final List <Integer > expectedKey1 ;
241- private final List <Integer > expectedKey2 ;
242-
243- VerifyStatefulOutput (List <Integer > expectedKey1 , List <Integer > expectedKey2 ) {
244- this .expectedKey1 = expectedKey1 ;
245- this .expectedKey2 = expectedKey2 ;
246- }
247-
248- @ Override
249- public Void apply (Iterable <KV <Integer , Integer >> input ) {
250- List <Integer > key1Values = new ArrayList <>();
251- List <Integer > key2Values = new ArrayList <>();
252- for (KV <Integer , Integer > kv : input ) {
253- if (kv .getKey () == 1 ) {
254- key1Values .add (kv .getValue ());
255- } else if (kv .getKey () == 2 ) {
256- key2Values .add (kv .getValue ());
257- }
258- }
259- verifyKey (key1Values , expectedKey1 );
260- verifyKey (key2Values , expectedKey2 );
261- return null ;
262- }
263-
264- private void verifyKey (List <Integer > actualValues , List <Integer > expectedElements ) {
265- assertEquals (expectedElements .size (), actualValues .size ());
266- Collections .sort (actualValues );
267- List <Integer > differences = new ArrayList <>();
268- int prev = 0 ;
269- for (int v : actualValues ) {
270- differences .add (v - prev );
271- prev = v ;
272- }
273- List <Integer > expectedSorted = new ArrayList <>(expectedElements );
274- Collections .sort (expectedSorted );
275- Collections .sort (differences );
276- assertEquals (expectedSorted , differences );
277- }
278- }
279-
280232 @ Category (StreamingTest .class )
281233 @ Test
282234 public void shouldRejectEventTimeTimer () {
@@ -357,7 +309,15 @@ public void shouldProcessGlobalWidowStatefulParDo() {
357309 p .apply (createStreamingSource (p )).apply (ParDo .of (new StatefulDoFn ()));
358310
359311 PAssert .that (result )
360- .satisfies (new VerifyStatefulOutput (Arrays .asList (1 , 2 , 3 ), Arrays .asList (4 , 5 , 6 )));
312+ .containsInAnyOrder (
313+ // key 1
314+ KV .of (1 , 1 ), // 1
315+ KV .of (1 , 3 ), // 1 + 2
316+ KV .of (1 , 6 ), // 3 + 3
317+ // key 2
318+ KV .of (2 , 4 ), // 4
319+ KV .of (2 , 9 ), // 4 + 5
320+ KV .of (2 , 15 )); // 9 + 6
361321
362322 p .run ().waitUntilFinish ();
363323 }
@@ -370,26 +330,28 @@ public void shouldProcessWindowedStatefulParDo() {
370330 .apply (Window .into (FixedWindows .of (Duration .standardSeconds (1L ))))
371331 .apply (ParDo .of (new StatefulDoFn ()));
372332
373- IntervalWindow window1 = new IntervalWindow (new Instant (0 ), Duration .standardSeconds (1L ));
374- IntervalWindow window2 = new IntervalWindow (new Instant (1000 ), Duration .standardSeconds (1L ));
375- IntervalWindow window3 = new IntervalWindow (new Instant (2000 ), Duration .standardSeconds (1L ));
376- IntervalWindow window4 = new IntervalWindow (new Instant (3000 ), Duration .standardSeconds (1L ));
377-
378- PAssert .that (result )
379- .inWindow (window1 )
380- .satisfies (new VerifyStatefulOutput (Arrays .asList (1 , 2 , 3 ), Collections .emptyList ()));
381-
382- PAssert .that (result )
383- .inWindow (window2 )
384- .satisfies (new VerifyStatefulOutput (Collections .emptyList (), Arrays .asList (4 , 5 , 6 )));
385-
386- PAssert .that (result )
387- .inWindow (window3 )
388- .satisfies (new VerifyStatefulOutput (Arrays .asList (7 , 8 , 9 ), Collections .emptyList ()));
389-
390333 PAssert .that (result )
391- .inWindow (window4 )
392- .satisfies (new VerifyStatefulOutput (Collections .emptyList (), Arrays .asList (10 , 11 , 12 )));
334+ .containsInAnyOrder (
335+ // Windowed Key 1
336+ KV .of (1 , 1 ), // 1
337+ KV .of (1 , 3 ), // 1 + 2
338+ KV .of (1 , 6 ), // 3 + 3
339+
340+ // Windowed Key 2
341+ KV .of (2 , 4 ), // 4
342+ KV .of (2 , 9 ), // 4 + 5
343+ KV .of (2 , 15 ), // 9 + 6
344+
345+ // Windowed Key 1
346+ KV .of (1 , 7 ), // 7
347+ KV .of (1 , 15 ), // 7 + 8
348+ KV .of (1 , 24 ), // 15 + 9
349+
350+ // Windowed Key 2
351+ KV .of (2 , 10 ), // 10
352+ KV .of (2 , 21 ), // 10 + 11
353+ KV .of (2 , 33 ) // 21 + 12
354+ );
393355
394356 p .run ().waitUntilFinish ();
395357 }
0 commit comments