Java Code Examples for org.joda.time.Duration#standardMinutes()

The following examples show how to use org.joda.time.Duration#standardMinutes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReduceFnRunnerTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testWatermarkHoldForLateNewWindow() throws Exception {
  Duration allowedLateness = Duration.standardMinutes(1);
  Duration gapDuration = Duration.millis(10);
  ReduceFnTester<Integer, Iterable<Integer>, IntervalWindow> tester =
      ReduceFnTester.nonCombining(
          WindowingStrategy.of(Sessions.withGapDuration(gapDuration))
              .withMode(AccumulationMode.DISCARDING_FIRED_PANES)
              .withTrigger(
                  Repeatedly.forever(
                      AfterWatermark.pastEndOfWindow()
                          .withLateFirings(AfterPane.elementCountAtLeast(1))))
              .withAllowedLateness(allowedLateness));
  tester.setAutoAdvanceOutputWatermark(false);

  assertEquals(null, tester.getWatermarkHold());
  assertEquals(null, tester.getOutputWatermark());
  tester.advanceInputWatermark(new Instant(40));
  injectElements(tester, 1);
  assertThat(tester.getWatermarkHold(), nullValue());
  injectElements(tester, 10);
  assertThat(tester.getWatermarkHold(), nullValue());
}
 
Example 2
Source File: CachingResolverTest.java    From monsoon with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void getTuplesTest() throws Exception {
    final CompletableFuture<Collection<ResolverTuple>> fut = new CompletableFuture<>();
    final List<ResolverTuple> RESULT = new ArrayList<>();

    when(mockedAsyncResolver.getTuples()).thenReturn((CompletableFuture) fut);

    final Collection<ResolverTuple> entries;
    try (CachingResolver cr = new CachingResolver(mockedAsyncResolver, Duration.standardMinutes(10), Duration.standardMinutes(60))) {
        fut.complete(RESULT);
        Thread.sleep(5000);  // Racy test, but this should work in most cases to allow callbacks to propagate.
        entries = cr.getTuples();
    }

    assertSame(RESULT, entries);
}
 
Example 3
Source File: FnApiWindowMappingFnTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testWindowMapping() throws Exception {
  TestSdkHarness testSdkHarness = new TestSdkHarness(GlobalWindow.INSTANCE);

  FnApiWindowMappingFn windowMappingFn =
      new FnApiWindowMappingFn(
          IdGenerators.decrementingLongs(),
          testSdkHarness,
          DATA_SERVICE,
          testSdkHarness,
          WINDOW_MAPPING_SPEC,
          IntervalWindowCoder.of(),
          GlobalWindow.Coder.INSTANCE);

  // Check mapping an element returns the expected result.
  BoundedWindow inputWindow = new IntervalWindow(Instant.now(), Duration.standardMinutes(1));
  assertEquals(GlobalWindow.INSTANCE, windowMappingFn.getSideInputWindow(inputWindow));
  assertEquals(inputWindow, ((KV) testSdkHarness.getInputValues().get(0).getValue()).getValue());

  // Check mapping a different element returns the expected result.
  BoundedWindow inputWindow2 = new IntervalWindow(Instant.now(), Duration.standardMinutes(2));
  assertEquals(GlobalWindow.INSTANCE, windowMappingFn.getSideInputWindow(inputWindow2));
  assertEquals(inputWindow2, ((KV) testSdkHarness.getInputValues().get(1).getValue()).getValue());

  // Check that mapping the same element returns a cached result.
  assertEquals(GlobalWindow.INSTANCE, windowMappingFn.getSideInputWindow(inputWindow));
  assertEquals(2, testSdkHarness.getInputValues().size());
}
 
Example 4
Source File: SlidingWindows.java    From beam with Apache License 2.0 5 votes vote down vote up
static Duration getDefaultPeriod(Duration size) {
  if (size.isLongerThan(Duration.standardHours(1))) {
    return Duration.standardHours(1);
  }
  if (size.isLongerThan(Duration.standardMinutes(1))) {
    return Duration.standardMinutes(1);
  }
  if (size.isLongerThan(Duration.standardSeconds(1))) {
    return Duration.standardSeconds(1);
  }
  return Duration.millis(1);
}
 
Example 5
Source File: AlertTest.java    From monsoon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void constructor_unknown() {
    Alert alert = new Alert(t0, alert_name, () -> "", Optional.empty(), Duration.standardMinutes(1), "test", EMPTY_MAP);

    assertEquals(t0, alert.getCur());
    assertEquals(t0, alert.getStart());
    assertEquals(alert_name, alert.getName());
    assertEquals(AlertState.UNKNOWN, alert.getAlertState());
    assertEquals(false, alert.isFiring());
    assertEquals(Optional.empty(), alert.isTriggered());
}
 
Example 6
Source File: AlertTest.java    From monsoon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void extend_ok() {
    Alert past_alert = new Alert(t_past, alert_name, () -> "", Optional.of(false), Duration.standardMinutes(1), "test", EMPTY_MAP);
    Alert alert = past_alert.extend(new Alert(t0, alert_name, () -> "", Optional.of(false), Duration.standardMinutes(1), "test", EMPTY_MAP));

    assertEquals(Optional.of(false), alert.isTriggered());
    assertEquals(false, alert.isFiring());
    assertEquals("Return timestamp of first detected ok", t_past, alert.getStart());
    assertEquals("Return timestamp of last update", t0, alert.getCur());
}
 
Example 7
Source File: AbstractTSCPairTest.java    From monsoon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void previousPairsSince_exact() {
    final Duration delta2 = Duration.standardMinutes(2);
    setup(ExpressionLookBack.fromScrapeCount(5));

    assertEquals("this test checks that the oldest collection has the same timestamp as was requested and is included",
            input.get(2).getTimestamp(), input.get(0).getTimestamp().minus(delta2));

    List<TimeSeriesCollectionPair> pairs = impl.getCollectionPairsSince(delta2);

    // 2 minutes in the past
    assertEquals(input.get(2).getTimestamp(), pairs.get(0).getPreviousCollection(0).get().getTimestamp());
    assertEquals(input.get(3).getTimestamp(), pairs.get(0).getPreviousCollection(1).get().getTimestamp());
    assertEquals(input.get(4).getTimestamp(), pairs.get(0).getPreviousCollection(2).get().getTimestamp());
    assertEquals(input.get(5).getTimestamp(), pairs.get(0).getPreviousCollection(3).get().getTimestamp());
    assertEquals(Optional.empty(), pairs.get(0).getPreviousCollection(4));

    // 1 minute in the past
    assertEquals(input.get(1).getTimestamp(), pairs.get(1).getPreviousCollection(0).get().getTimestamp());
    assertEquals(input.get(2).getTimestamp(), pairs.get(1).getPreviousCollection(1).get().getTimestamp());
    assertEquals(input.get(3).getTimestamp(), pairs.get(1).getPreviousCollection(2).get().getTimestamp());
    assertEquals(input.get(4).getTimestamp(), pairs.get(1).getPreviousCollection(3).get().getTimestamp());
    assertEquals(input.get(5).getTimestamp(), pairs.get(1).getPreviousCollection(4).get().getTimestamp());
    assertEquals(Optional.empty(), pairs.get(1).getPreviousCollection(5));

    // current collection
    assertEquals(input.get(0).getTimestamp(), pairs.get(2).getPreviousCollection(0).get().getTimestamp());
    assertEquals(input.get(1).getTimestamp(), pairs.get(2).getPreviousCollection(1).get().getTimestamp());
    assertEquals(input.get(2).getTimestamp(), pairs.get(2).getPreviousCollection(2).get().getTimestamp());
    assertEquals(input.get(3).getTimestamp(), pairs.get(2).getPreviousCollection(3).get().getTimestamp());
    assertEquals(input.get(4).getTimestamp(), pairs.get(2).getPreviousCollection(4).get().getTimestamp());
    assertEquals(input.get(5).getTimestamp(), pairs.get(2).getPreviousCollection(5).get().getTimestamp());
    assertEquals(Optional.empty(), pairs.get(2).getPreviousCollection(6));

    assertEquals(3, pairs.size());
}
 
Example 8
Source File: AlertTest.java    From monsoon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void extend_transition_firing() {
    Alert past_alert = new Alert(t_past, alert_name, () -> "", Optional.of(true), Duration.standardMinutes(1), "test", EMPTY_MAP);
    Alert alert = past_alert.extend(new Alert(t0, alert_name, () -> "", Optional.of(true), Duration.standardMinutes(1), "test", EMPTY_MAP));

    assertEquals(Optional.of(true), alert.isTriggered());
    assertEquals(true, alert.isFiring());
    assertEquals("Return timestamp of first detected trigger", t_past, alert.getStart());
    assertEquals("Return timestamp of last update", t0, alert.getCur());
}
 
Example 9
Source File: ClientServerTest.java    From monsoon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void streamSteppedFrom() {
    final DateTime begin = new DateTime(DateTimeZone.UTC);
    final Duration stepSize = Duration.standardMinutes(1);
    final List<TimeSeriesCollection> expected = generateCollection().collect(Collectors.toList());
    when(history.stream(Mockito.isA(DateTime.class), Mockito.isA(Duration.class)))
            .thenAnswer((invocation) -> generateCollection());

    final List<TimeSeriesCollection> result = client.stream(begin, stepSize).collect(Collectors.toList());

    assertEquals(expected, result);

    verify(history, times(1)).stream(Mockito.eq(begin), Mockito.eq(stepSize));
    verifyNoMoreInteractions(history);
}
 
Example 10
Source File: LateDataUtilsTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void beforeEndOfGlobalWindowSame() {
  FixedWindows windowFn = FixedWindows.of(Duration.standardMinutes(5));
  Duration allowedLateness = Duration.standardMinutes(2);
  WindowingStrategy<?, ?> strategy =
      WindowingStrategy.globalDefault()
          .withWindowFn(windowFn)
          .withAllowedLateness(allowedLateness);

  IntervalWindow window = windowFn.assignWindow(new Instant(10));
  assertThat(
      LateDataUtils.garbageCollectionTime(window, strategy),
      equalTo(window.maxTimestamp().plus(allowedLateness)));
}
 
Example 11
Source File: WindowTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
@Category(ValidatesRunner.class)
public void testPrimitiveDisplayData() {
  FixedWindows windowFn = FixedWindows.of(Duration.standardHours(5));
  AfterWatermark.FromEndOfWindow triggerBuilder = AfterWatermark.pastEndOfWindow();
  Duration allowedLateness = Duration.standardMinutes(10);
  Window.ClosingBehavior closingBehavior = Window.ClosingBehavior.FIRE_IF_NON_EMPTY;
  TimestampCombiner timestampCombiner = TimestampCombiner.END_OF_WINDOW;

  Window<?> window =
      Window.into(windowFn)
          .triggering(triggerBuilder)
          .accumulatingFiredPanes()
          .withAllowedLateness(allowedLateness, closingBehavior)
          .withTimestampCombiner(timestampCombiner);

  DisplayData primitiveDisplayData =
      Iterables.getOnlyElement(
          DisplayDataEvaluator.create().displayDataForPrimitiveTransforms(window));

  assertThat(primitiveDisplayData, hasDisplayItem("windowFn", windowFn.getClass()));
  assertThat(primitiveDisplayData, includesDisplayDataFor("windowFn", windowFn));

  assertThat(primitiveDisplayData, hasDisplayItem("trigger", triggerBuilder.toString()));
  assertThat(
      primitiveDisplayData,
      hasDisplayItem("accumulationMode", AccumulationMode.ACCUMULATING_FIRED_PANES.toString()));
  assertThat(primitiveDisplayData, hasDisplayItem("allowedLateness", allowedLateness));
  assertThat(primitiveDisplayData, hasDisplayItem("closingBehavior", closingBehavior.toString()));
  assertThat(
      primitiveDisplayData, hasDisplayItem("timestampCombiner", timestampCombiner.toString()));
}
 
Example 12
Source File: DurationConverterTest.java    From gson-jodatime-serialisers with MIT License 5 votes vote down vote up
/**
 *  Tests that the {@link Duration} can be round-tripped.
 */
@Test
public void testRoundtrip()
{
  final Gson gson = Converters.registerDuration(new GsonBuilder()).create();
  final Duration d = Duration.standardMinutes(30L);

  assertThat(gson.fromJson(gson.toJson(d), Duration.class), is(d));
}
 
Example 13
Source File: ClientServerTest.java    From monsoon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(timeout = 20000)
public void verySlowEvaluate() {
    final int LIMIT = 1;  // To make the test not take forever.
    final Duration stepSize = Duration.standardMinutes(1);
    final List<Collection<CollectHistory.NamedEvaluation>> expected = generateEvaluation().limit(LIMIT).collect(Collectors.toList());
    when(history.evaluate(Mockito.any(), Mockito.isA(Duration.class)))
            .thenAnswer((invocation) -> {
                return generateEvaluation()
                        .limit(LIMIT)
                        .map(x -> {
                            try {
                                Thread.sleep(10000);
                            } catch (InterruptedException ex) {
                                Logger.getLogger(ClientServerTest.class.getName()).log(Level.SEVERE, null, ex);
                            }
                            Logger.getLogger(ClientServerTest.class.getName()).log(Level.INFO, "emitting {0}", x);
                            return x;
                        });
            });

    final List<Collection<CollectHistory.NamedEvaluation>> result = client.evaluate(singletonMap("name", expr), stepSize).collect(Collectors.toList());

    assertEquals(expected, result);

    verify(history, times(1)).evaluate(
            Mockito.<Map>argThat(
                    Matchers.hasEntry(
                            Matchers.equalTo("name"),
                            new ExprEqualTo(expr))),
            Mockito.eq(stepSize));
    verifyNoMoreInteractions(history);
}
 
Example 14
Source File: ClientServerTest.java    From monsoon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(timeout = 20000)
public void slowEvaluate() {
    final int LIMIT = 10;  // To make the test not take forever.
    final Duration stepSize = Duration.standardMinutes(1);
    final List<Collection<CollectHistory.NamedEvaluation>> expected = generateEvaluation().limit(LIMIT).collect(Collectors.toList());
    when(history.evaluate(Mockito.any(), Mockito.isA(Duration.class)))
            .thenAnswer((invocation) -> {
                return generateEvaluation()
                        .limit(LIMIT)
                        .map(x -> {
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException ex) {
                                Logger.getLogger(ClientServerTest.class.getName()).log(Level.SEVERE, null, ex);
                            }
                            Logger.getLogger(ClientServerTest.class.getName()).log(Level.INFO, "emitting {0}", x);
                            return x;
                        });
            });

    final List<Collection<CollectHistory.NamedEvaluation>> result = client.evaluate(singletonMap("name", expr), stepSize).collect(Collectors.toList());

    assertEquals(expected, result);

    verify(history, times(1)).evaluate(
            Mockito.<Map>argThat(
                    Matchers.hasEntry(
                            Matchers.equalTo("name"),
                            new ExprEqualTo(expr))),
            Mockito.eq(stepSize));
    verifyNoMoreInteractions(history);
}
 
Example 15
Source File: ReaderCache.java    From beam with Apache License 2.0 4 votes vote down vote up
/** ReaderCache with default 1 minute expiration for readers. */
ReaderCache() {
  this(Duration.standardMinutes(1));
}
 
Example 16
Source File: ReduceFnRunnerTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@Test
public void testMergingWatermarkHoldAndLateDataFuzz() throws Exception {
  MetricsContainerImpl container = new MetricsContainerImpl("any");
  MetricsEnvironment.setCurrentContainer(container);
  // Test handling of late data. Specifically, ensure the watermark hold is correct.
  Duration allowedLateness = Duration.standardMinutes(100);
  long seed = ThreadLocalRandom.current().nextLong();
  LOG.info("Random seed: {}", seed);
  Random r = new Random(seed);

  Duration gapDuration = Duration.millis(10 + r.nextInt(40));
  LOG.info("Gap duration {}", gapDuration);

  ReduceFnTester<Integer, Iterable<Integer>, IntervalWindow> tester =
      ReduceFnTester.nonCombining(
          WindowingStrategy.of(Sessions.withGapDuration(gapDuration))
              .withMode(AccumulationMode.DISCARDING_FIRED_PANES)
              .withTrigger(
                  Repeatedly.forever(
                      AfterWatermark.pastEndOfWindow()
                          .withLateFirings(AfterPane.elementCountAtLeast(1))))
              .withAllowedLateness(allowedLateness));
  tester.setAutoAdvanceOutputWatermark(true);

  // Input watermark -> null
  assertEquals(null, tester.getWatermarkHold());
  assertEquals(null, tester.getOutputWatermark());

  // All on time data, verify watermark hold.
  List<Integer> times = new ArrayList<>();

  int numTs = 3 + r.nextInt(100);
  int maxTs = 1 + r.nextInt(400);
  LOG.info("Num ts {}", numTs);
  LOG.info("Max ts {}", maxTs);
  for (int i = numTs; i >= 0; --i) {
    times.add(r.nextInt(maxTs));
  }
  LOG.info("Times: {}", times);

  int split = 0;
  long watermark = 0;
  while (split < times.size()) {
    int nextSplit = split + r.nextInt(times.size());
    if (nextSplit > times.size()) {
      nextSplit = times.size();
    }
    LOG.info("nextSplit {}", nextSplit);
    injectElements(tester, times.subList(split, nextSplit));
    if (r.nextInt(3) == 0) {
      int nextWatermark = r.nextInt((int) (maxTs + gapDuration.getMillis()));
      if (nextWatermark > watermark) {
        Boolean enabled = r.nextBoolean();
        LOG.info("nextWatermark {} {}", nextWatermark, enabled);
        watermark = nextWatermark;
        tester.setAutoAdvanceOutputWatermark(enabled);
        tester.advanceInputWatermark(new Instant(watermark));
      }
    }
    split = nextSplit;
    Instant hold = tester.getWatermarkHold();
    if (hold != null) {
      assertThat(hold, greaterThanOrEqualTo(new Instant(watermark)));
      assertThat(watermark, lessThan(maxTs + gapDuration.getMillis()));
    }
  }
  tester.setAutoAdvanceOutputWatermark(true);
  watermark = gapDuration.getMillis() + maxTs;
  tester.advanceInputWatermark(new Instant(watermark));
  LOG.info("Output {}", tester.extractOutput());
  if (tester.getWatermarkHold() != null) {
    assertThat(tester.getWatermarkHold(), equalTo(new Instant(watermark).plus(allowedLateness)));
  }
  // Nothing dropped.
  long droppedElements =
      container
          .getCounter(
              MetricName.named(ReduceFnRunner.class, ReduceFnRunner.DROPPED_DUE_TO_CLOSED_WINDOW))
          .getCumulative()
          .longValue();
  assertEquals(0, droppedElements);
}
 
Example 17
Source File: StreamingDataflowWorkerOptions.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public Duration create(PipelineOptions options) {
  Duration period =
      Duration.parse(System.getProperty("windmill.global_get_config_refresh_period", "PT120S"));
  return period.isLongerThan(Duration.ZERO) ? period : Duration.standardMinutes(2);
}
 
Example 18
Source File: RegistryConfig.java    From nomulus with Apache License 2.0 4 votes vote down vote up
/**
 * Maximum amount of time it should ever take to upload an escrow deposit, before killing.
 *
 * @see google.registry.rde.RdeUploadAction
 */
@Provides
@Config("rdeUploadLockTimeout")
public static Duration provideRdeUploadLockTimeout() {
  return Duration.standardMinutes(30);
}
 
Example 19
Source File: TimestampFn.java    From kettle-beam with Apache License 2.0 4 votes vote down vote up
@Override public Duration getAllowedTimestampSkew() {
  return Duration.standardMinutes( 120 );
}
 
Example 20
Source File: RegistryConfig.java    From nomulus with Apache License 2.0 2 votes vote down vote up
/**
 * The maximum time we allow publishDnsUpdates to run.
 *
 * <p>This is the maximum lock duration for publishing the DNS updates, meaning it should allow
 * the various DnsWriters to publish and commit an entire batch (with a maximum number of items
 * set by provideDnsTldUpdateBatchSize).
 *
 * <p>Any update that takes longer than this timeout will be killed and retried from scratch.
 * Hence, a timeout that's too short can result in batches that retry over and over again,
 * failing forever.
 *
 * <p>If there are lock contention issues, they should be solved by changing the batch sizes or
 * the cron job rate, NOT by making this value smaller.
 *
 * @see google.registry.dns.PublishDnsUpdatesAction
 */
@Provides
@Config("publishDnsUpdatesLockDuration")
public static Duration providePublishDnsUpdatesLockDuration() {
  return Duration.standardMinutes(3);
}