use of org.apache.beam.examples.complete.game.LeaderBoard.CalculateUserScores in project beam by apache.
the class LeaderBoardTest method testUserScore.
/**
* A test where elements arrive both on-time and late in {@link CalculateUserScores}, which emits
* output into the {@link GlobalWindow}. All elements that arrive should be taken into account,
* even if they arrive later than the maximum allowed lateness.
*/
@Test
public void testUserScore() {
TestStream<GameActionInfo> infos = TestStream.create(AvroCoder.of(GameActionInfo.class)).addElements(event(TestUser.BLUE_ONE, 12, Duration.ZERO), event(TestUser.RED_ONE, 3, Duration.ZERO)).advanceProcessingTime(Duration.standardMinutes(7)).addElements(event(TestUser.RED_ONE, 4, Duration.standardMinutes(2)), event(TestUser.BLUE_TWO, 3, Duration.ZERO), event(TestUser.BLUE_ONE, 3, Duration.standardMinutes(3))).advanceProcessingTime(Duration.standardMinutes(5)).advanceWatermarkTo(baseTime.plus(ALLOWED_LATENESS).plus(Duration.standardHours(12))).addElements(event(TestUser.RED_ONE, 3, Duration.standardMinutes(7)), event(TestUser.RED_ONE, 2, (ALLOWED_LATENESS).plus(Duration.standardHours(13)))).advanceProcessingTime(Duration.standardMinutes(6)).addElements(event(TestUser.BLUE_TWO, 5, Duration.standardMinutes(12))).advanceProcessingTime(Duration.standardMinutes(20)).advanceWatermarkToInfinity();
PCollection<KV<String, Integer>> userScores = p.apply(infos).apply(new CalculateUserScores(ALLOWED_LATENESS));
// User scores are emitted in speculative panes in the Global Window - this matcher choice
// ensures that panes emitted by the watermark advancing to positive infinity are not included,
// as that will not occur outside of tests
PAssert.that(userScores).inEarlyGlobalWindowPanes().containsInAnyOrder(KV.of(TestUser.BLUE_ONE.getUser(), 15), KV.of(TestUser.RED_ONE.getUser(), 7), KV.of(TestUser.RED_ONE.getUser(), 12), KV.of(TestUser.BLUE_TWO.getUser(), 3), KV.of(TestUser.BLUE_TWO.getUser(), 8));
p.run().waitUntilFinish();
}
Aggregations