.withTerminationPerInput(Growth.afterTotalOf(standardSeconds(1))) .withPollInterval(Duration.millis(1)) .withOutputCoder(KvCoder.of(StringUtf8Coder.of(), VarIntCoder.of())))
private void testMultiplePolls(boolean terminationConditionElapsesBeforeOutputIsFinal) { List<Integer> all = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); PCollection<Integer> res = p.apply(Create.of("a")) .apply( Watch.growthOf( new TimedPollFn<String, Integer>( all, standardSeconds(1) /* timeToOutputEverything */, standardSeconds(3) /* timeToDeclareOutputFinal */, standardSeconds(30) /* timeToFail */)) .withTerminationPerInput( Growth.afterTotalOf( standardSeconds( // At 2 seconds, all output has been yielded, but not yet // declared final - so polling should terminate per termination // condition. // At 3 seconds, all output has been yielded (and declared final), // so polling should terminate because of that without waiting for // 100 seconds. terminationConditionElapsesBeforeOutputIsFinal ? 2 : 100))) .withPollInterval(Duration.millis(300)) .withOutputCoder(VarIntCoder.of())) .apply("Drop input", Values.create()); PAssert.that(res).containsInAnyOrder(all); p.run(); }
Requirements.empty()), KV::getKey) .withTerminationPerInput(Growth.afterTotalOf(standardSeconds(5))) .withPollInterval(Duration.millis(100)) .withOutputCoder(KvCoder.of(VarIntCoder.of(), StringUtf8Coder.of()))
@Test public void testTerminationConditionsEitherOf() { Instant now = Instant.now(); Watch.Growth.AfterTotalOf<Object> a = Growth.afterTotalOf(standardSeconds(5)); Watch.Growth.AfterTotalOf<Object> b = Growth.afterTotalOf(standardSeconds(10)); Watch.Growth.BinaryCombined< Object, KV<Instant, ReadableDuration>, KV<Instant, ReadableDuration>> c = eitherOf(a, b); KV<KV<Instant, ReadableDuration>, KV<Instant, ReadableDuration>> state = c.forNewInput(now, null); assertFalse(c.canStopPolling(now.plus(standardSeconds(3)), state)); assertTrue(c.canStopPolling(now.plus(standardSeconds(7)), state)); assertTrue(c.canStopPolling(now.plus(standardSeconds(12)), state)); }
@Test public void testTerminationConditionsAllOf() { Instant now = Instant.now(); Watch.Growth.AfterTotalOf<Object> a = Growth.afterTotalOf(standardSeconds(5)); Watch.Growth.AfterTotalOf<Object> b = Growth.afterTotalOf(standardSeconds(10)); Watch.Growth.BinaryCombined< Object, KV<Instant, ReadableDuration>, KV<Instant, ReadableDuration>> c = allOf(a, b); KV<KV<Instant, ReadableDuration>, KV<Instant, ReadableDuration>> state = c.forNewInput(now, null); assertFalse(c.canStopPolling(now.plus(standardSeconds(3)), state)); assertFalse(c.canStopPolling(now.plus(standardSeconds(7)), state)); assertTrue(c.canStopPolling(now.plus(standardSeconds(12)), state)); }
/** * Returns a {@link TerminationCondition} that holds after the given time has elapsed after the * current input was seen. */ public static <InputT> AfterTotalOf<InputT> afterTotalOf(ReadableDuration timeSinceInput) { return afterTotalOf(SerializableFunctions.<InputT, ReadableDuration>constant(timeSinceInput)); }
@Test public void testTerminationConditionsAfterTotalOf() { Instant now = Instant.now(); Watch.Growth.AfterTotalOf<Object> c = Growth.afterTotalOf(standardSeconds(5)); KV<Instant, ReadableDuration> state = c.forNewInput(now, null); assertFalse(c.canStopPolling(now, state)); assertFalse(c.canStopPolling(now.plus(standardSeconds(3)), state)); assertTrue(c.canStopPolling(now.plus(standardSeconds(6)), state)); }