java.time.Duration.ofHours()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(301)

本文整理了Java中java.time.Duration.ofHours()方法的一些代码示例,展示了Duration.ofHours()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Duration.ofHours()方法的具体详情如下:
包路径:java.time.Duration
类名称:Duration
方法名:ofHours

Duration.ofHours介绍

[英]Obtains an instance of Duration from a number of standard length hours.

The seconds are calculated based on the standard definition of an hour, where each hour is 3600 seconds. The nanosecond in second field is set to zero.
[中]从标准长度小时数中获取持续时间实例。
秒数根据小时的标准定义计算,其中每小时为3600秒。第二个字段中的纳秒设置为零。

代码示例

代码示例来源:origin: confluentinc/ksql

@Test
public void shouldGetNewCommandsIteratorCorrectly() {
 // Given:
 when(commandConsumer.poll(any(Duration.class))).thenReturn(consumerRecords);
 // When:
 final Iterable<ConsumerRecord<CommandId, Command>> newCommands = commandTopic
   .getNewCommands(Duration.ofHours(1));
 // Then:
 assertThat(newCommands, sameInstance(consumerRecords));
}

代码示例来源:origin: ben-manes/caffeine

@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, population = Population.FULL,
  expiry = CacheExpiry.MOCKITO, expiryTime = Expire.ONE_MINUTE)
public void getExpiresAfter_duration(Cache<Integer, Integer> cache, CacheContext context,
  VarExpiration<Integer, Integer> expireAfterVar) {
 assertThat(expireAfterVar.getExpiresAfter(context.absentKey()),
   is(Optional.empty()));
 assertThat(expireAfterVar.getExpiresAfter(context.firstKey()),
   is(Optional.of(Duration.ofMinutes(1L))));
 when(context.expiry().expireAfterUpdate(any(), any(), anyLong(), anyLong()))
   .thenReturn(TimeUnit.HOURS.toNanos(1));
 cache.put(context.firstKey(), context.absentValue());
 assertThat(expireAfterVar.getExpiresAfter(context.firstKey()),
   is(Optional.of(Duration.ofHours(1))));
 assertThat(expireAfterVar.getExpiresAfter(context.lastKey()),
   is(Optional.of(Duration.ofMinutes(1))));
}

代码示例来源:origin: reactor/reactor-core

@Test
public void monoApiTestDuration() {
  StepVerifier.withVirtualTime(() -> Mono.just("foo").delayElement(Duration.ofHours(1)))
        .expectSubscription()
        .expectNoEvent(Duration.ofHours(1))
        .expectNext("foo")
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void noDelayGreaterThanSize() {
  StepVerifier.create(Flux.range(1, 12)
              .windowTimeout(5, Duration.ofHours(1))
              .concatMap(Flux::collectList)
  )
        .assertNext(l -> assertThat(l).containsExactly(1, 2, 3, 4, 5))
        .assertNext(l -> assertThat(l).containsExactly(6, 7, 8, 9, 10))
        .assertNext(l -> assertThat(l).containsExactly(11, 12))
        .verifyComplete();
}

代码示例来源:origin: spring-projects/spring-security

@Override
  protected void configure(HttpSecurity http) throws Exception {
    Clock justOverOneHourAfterExpiry =
        Clock.fixed(Instant.ofEpochMilli(4687181595000L), ZoneId.systemDefault());
    JwtTimestampValidator jwtValidator = new JwtTimestampValidator(Duration.ofHours(1));
    jwtValidator.setClock(justOverOneHourAfterExpiry);
    this.jwtDecoder.setJwtValidator(jwtValidator);
    // @formatter:off
    http
      .oauth2ResourceServer()
        .jwt();
  }
}

代码示例来源:origin: reactor/reactor-core

@Test
public void longDelaysStartEndEmitEmptyWindows() {
  StepVerifier.withVirtualTime(() ->
    Mono.just("foo")
      .delayElement(Duration.ofMillis(400 + 400 + 300))
      .concatWith(Mono.delay(Duration.ofMillis(100 + 400 + 100)).then(Mono.empty()))
      .windowTimeout(1000, Duration.ofMillis(400))
      .concatMap(Flux::collectList)
  )
        .thenAwait(Duration.ofHours(1))
        .assertNext(l -> assertThat(l).isEmpty())
        .assertNext(l -> assertThat(l).isEmpty())
        .assertNext(l -> assertThat(l).containsExactly("foo"))
        .assertNext(l -> assertThat(l).isEmpty())
        .assertNext(l -> assertThat(l).isEmpty()) //closing window
        .verifyComplete();
}

代码示例来源:origin: debezium/debezium

@Test
public void maxHandlesDifferentUnits() {
  Duration sixtyOneMinutes = Duration.ofMinutes(61);
  Duration oneHour = Duration.ofHours(1);
  assertThat(Temporals.max(sixtyOneMinutes, oneHour)).isEqualTo(sixtyOneMinutes);
}

代码示例来源:origin: reactor/reactor-core

@Test
public void timeoutDurationMessageDefault() {
  StepVerifier.withVirtualTime(() -> Mono.never()
                      .timeout(Duration.ofHours(1)))
        .thenAwait(Duration.ofHours(2))
        .expectErrorMessage("Did not observe any item or terminal signal within " +
            "3600000ms in 'source(MonoNever)' (and no fallback has been " +
            "configured)")
        .verify();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void timeoutDurationMessageDefault() {
  StepVerifier.withVirtualTime(() -> Flux.never()
                      .timeout(Duration.ofHours(1)))
        .thenAwait(Duration.ofHours(2))
        .expectErrorMessage("Did not observe any item or terminal signal within " +
            "3600000ms in 'source(FluxNever)' (and no fallback has been configured)")
        .verify();
}

代码示例来源:origin: spring-projects/spring-security

@Override
  protected void configure(HttpSecurity http) throws Exception {
    Clock nearlyAnHourFromTokenExpiry =
        Clock.fixed(Instant.ofEpochMilli(4687181540000L), ZoneId.systemDefault());
    JwtTimestampValidator jwtValidator = new JwtTimestampValidator(Duration.ofHours(1));
    jwtValidator.setClock(nearlyAnHourFromTokenExpiry);
    this.jwtDecoder.setJwtValidator(jwtValidator);
    // @formatter:off
    http
      .oauth2ResourceServer()
        .jwt();
    // @formatter:on
  }
}

代码示例来源:origin: reactor/reactor-core

@Test
public void verifyVirtualTimeOnNext() {
  StepVerifier.withVirtualTime(() -> Flux.just("foo", "bar", "foobar")
                      .delayElements(Duration.ofHours(1))
                      .log())
        .thenAwait(Duration.ofHours(1))
        .expectNext("foo")
        .thenAwait(Duration.ofHours(1))
        .expectNext("bar")
        .thenAwait(Duration.ofHours(1))
        .expectNext("foobar")
        .expectComplete()
        .verify();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void virtualTimeSchedulerVeryLong() {
  StepVerifier.withVirtualTime(() -> Flux.interval(Duration.ofMillis(1))
                      .map(tick -> new Date())
                      .take(100000)
                      .collectList())
        .thenAwait(Duration.ofHours(1000))
        .consumeNextWith(list -> Assert.assertTrue(list.size() == 100000))
        .verifyComplete();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void verifyVirtualTimeOnComplete() {
  StepVerifier.withVirtualTime(() -> Flux.empty()
                      .delaySubscription(Duration.ofHours(1))
                      .log())
        .thenAwait(Duration.ofHours(1))
        .expectComplete()
        .verify();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void timeoutDurationMessageWithName() {
  StepVerifier.withVirtualTime(() -> Mono.never()
                      .name("Name")
                      .timeout(Duration.ofHours(1)))
        .thenAwait(Duration.ofHours(2))
        .expectErrorMessage("Did not observe any item or terminal signal within " +
            "3600000ms in 'Name' (and no fallback has been " +
            "configured)")
        .verify();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void timeoutDurationMessageWithName() {
  StepVerifier.withVirtualTime(() -> Flux.never()
      .name("Name")
      .timeout(Duration.ofHours(1)))
      .thenAwait(Duration.ofHours(2))
      .expectErrorMessage("Did not observe any item or terminal signal within " +
          "3600000ms in 'Name' (and no fallback has been configured)")
      .verify();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void cancelUpstreamOnceWhenCancelled() {
  VirtualTimeScheduler vts = VirtualTimeScheduler.create();
  AtomicLong upstreamCancelCount = new AtomicLong();
  Mono<String> source = Mono.just("foo").log().hide()
      .doOnCancel(() -> upstreamCancelCount.incrementAndGet());
  StepVerifier.withVirtualTime(
      () -> new MonoDelayElement<>(source, 2, TimeUnit.SECONDS, vts),
      () -> vts, Long.MAX_VALUE)
        .expectSubscription()
        .expectNoEvent(Duration.ofSeconds(1))
        .thenCancel()
        .verify();
  vts.advanceTimeBy(Duration.ofHours(1));
  assertThat(upstreamCancelCount.get()).isEqualTo(1);
}

代码示例来源:origin: neo4j/neo4j

private static Stream<AnyValue> unsupportedValues()
{
  return Stream.of(
      nodeValue( 42, stringArray( "Person" ), EMPTY_MAP ),
      newRelationshipValue(),
      pointValue( CoordinateReferenceSystem.WGS84, new double[2] ),
      byteArray( new byte[]{1, 2, 3} ),
      Values.of( Duration.ofHours( 1 ) ),
      Values.of( LocalDate.now() ),
      Values.of( LocalTime.now() ),
      Values.of( OffsetTime.now() ),
      Values.of( LocalDateTime.now() ),
      Values.of( ZonedDateTime.now() )
  );
}

代码示例来源:origin: reactor/reactor-core

@Test(timeout = 3000)
public void verifyVirtualTimeOnNextIntervalManual() {
  VirtualTimeScheduler vts = VirtualTimeScheduler.create();
  StepVerifier.withVirtualTime(() -> Flux.interval(Duration.ofMillis(1000), vts)
                      .map(d -> "t" + d))
        .then(() -> vts.advanceTimeBy(Duration.ofHours(1)))
        .expectNextCount(3600)
        .thenCancel()
        .verify();
}

代码示例来源:origin: reactor/reactor-core

@Test
public void gh783_firstSmallAdvance() {
  StepVerifier.withVirtualTime(() -> Flux.just("foo").flatMap(message -> Flux.interval(Duration.ofMinutes(5)).take(12)))
        .expectSubscription()
        .expectNoEvent(Duration.ofMinutes(3))
        .thenAwait(Duration.ofHours(1))
        .expectNextCount(12)
        .expectComplete()
        .verify(Duration.ofMillis(500));
}

代码示例来源:origin: debezium/debezium

@Test
public void shouldCommitPeriodically() {
  // 10 hours
  OffsetCommitPolicy policy = OffsetCommitPolicy.periodic(Configuration.create().with(EmbeddedEngine.OFFSET_FLUSH_INTERVAL_MS, 10 * 60 * 60 * 1000).build());
  assertThat(policy.performCommit(0, Duration.ofNanos(0))).isFalse();
  assertThat(policy.performCommit(10000, Duration.ofHours(9))).isFalse();
  assertThat(policy.performCommit(0, Duration.ofHours(10))).isTrue();
}

相关文章