java.time.Clock.fixed()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(8.4k)|赞(0)|评价(0)|浏览(181)

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

Clock.fixed介绍

[英]Obtains a clock that always returns the same instant.

This clock simply returns the specified instant. As such, it is not a clock in the conventional sense. The main use case for this is in testing, where the fixed clock ensures tests are not dependent on the current clock.

The returned implementation is immutable, thread-safe and Serializable.
[中]获取始终返回同一时刻的时钟。
该时钟只返回指定的瞬间。因此,它不是传统意义上的时钟。这方面的主要用例是在测试中,固定时钟确保测试不依赖于当前时钟。
返回的实现是不可变的、线程安全的和可序列化的。

代码示例

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

private void executeAtSimulatedTime(Runnable runnable) {
  Clock previousClock = GenericEventMessage.clock;
  try {
    GenericEventMessage.clock = Clock.fixed(currentTime(), ZoneOffset.UTC);
    runnable.run();
  } finally {
    GenericEventMessage.clock = previousClock;
  }
}

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

public void initializeTransaction()
{
  this.transaction = Clock.fixed( system.instant(), timezone() );
  this.statement = null;
}

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

public void initializeStatement()
{
  if ( this.statement == null ) // this is the first statement in the transaction, use the transaction time
  {
    this.statement = this.transaction;
  }
  else // this is not the first statement in the transaction, initialize with a new time
  {
    this.statement = Clock.fixed( system.instant(), timezone() );
  }
}

代码示例来源:origin: apache/flink

@Test
public void testElapsedTime() {
  final long elapsedTime = new TransactionHolder<>(new Object(), 0)
    .elapsedTime(Clock.fixed(Instant.ofEpochMilli(1000), ZoneOffset.UTC));
  assertThat(elapsedTime, equalTo(1000L));
}

代码示例来源: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: 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: pholser/junit-quickcheck

@Override public Clock generate(SourceOfRandomness random, GenerationStatus status) {
    return Clock.fixed(random.nextInstant(min, max), UTC_ZONE_ID);
  }
}

代码示例来源:origin: apache/metron

@Test
public void testRfc3164Timestamp() throws ParseException {
  String originalTimestamp = "Oct  9 13:42:11";
  // Fixed clock is behind parsed timestamp, but by less than 4 days. Expect year of clock.
  ZonedDateTime fixedInstant =
      ZonedDateTime.of(2016, 10, 8, 18, 30, 30, 0, ZoneOffset.UTC);
  Clock fixedClock = Clock.fixed(fixedInstant.toInstant(), fixedInstant.getZone());
  assertEquals(SyslogUtils.parseTimestampToEpochMillis(originalTimestamp, fixedClock), 1476020531000L);
}

代码示例来源:origin: apache/metron

@Test
public void testRfc3164TimestampBackDate() throws ParseException {
  String originalTimestamp = "Oct  9 13:42:11";
  // Fixed clock is behind parsed timestamp by more than 4 days. Expect year of clock - 1.
  ZonedDateTime fixedInstant =
      ZonedDateTime.of(2016, 10, 1, 18, 30, 30, 0, ZoneOffset.UTC);
  Clock fixedClock = Clock.fixed(fixedInstant.toInstant(), fixedInstant.getZone());
  assertEquals(SyslogUtils.parseTimestampToEpochMillis(originalTimestamp, fixedClock), 1444398131000L);
}

代码示例来源:origin: apache/metron

@Test
public void testShortTimestamp() {
  String rawMessage = "<174>Jan  5 14:52:35 10.22.8.212 %ASA-6-302015: Built inbound UDP connection 76245506 for outside:10.22.8.110/49886 (10.22.8.110/49886) to inside:192.111.72.8/8612 (192.111.72.8/8612) (user.name)";
  ZonedDateTime fixedInstant =
      ZonedDateTime.of(2016, 1, 6, 1, 30, 30, 0, ZoneOffset.UTC);
  Clock fixedClock = Clock.fixed(fixedInstant.toInstant(), fixedInstant.getZone());
  BasicAsaParser fixedClockParser = new BasicAsaParser();
  fixedClockParser.deviceClock = fixedClock;
  fixedClockParser.init();
  JSONObject asaJson = fixedClockParser.parse(rawMessage.getBytes()).get(0);
  assertEquals(asaJson.get("original_string"), rawMessage);
  assertTrue(asaJson.get("ip_src_addr").equals("10.22.8.110"));
  assertTrue(asaJson.get("ip_dst_addr").equals("192.111.72.8"));
  assertTrue(asaJson.get("ip_src_port").equals(49886));
  assertTrue(asaJson.get("ip_dst_port").equals(8612));
  assertTrue((long) asaJson.get("timestamp") == 1452005555000L);
}

代码示例来源:origin: stackoverflow.com

import static org.junit.Assert.*;
import org.junit.Test;
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneOffset;

@Test public void checkSampleUTCTime(){
  Clock fixedClock = Clock.fixed(Instant.parse("2016-04-21T11:54:20.00Z"), ZoneOffset.UTC);
  assertEquals(1461239660000L,App.getMillis(fixedClock));
}

代码示例来源:origin: org.vesalainen.nmea/parser

/**
 * Creates a GPSClock in live or fixed mode depending on argument.
 * @param live 
 */
public GPSClock(boolean live)
{
  this(live ? Clock.systemUTC() : Clock.fixed(Instant.now(), ZoneOffset.UTC));
}

代码示例来源:origin: com.haulmont.cuba/cuba-global

@Override
  public Clock getClock() {
    ZonedDateTime now = timeSource.now();
    return Clock.fixed(now.toInstant(), now.getZone());
  }
}

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

@Override
public GivenCleaner run(GivenTime given, ImmutableMap<String, String> params) {
  overrideComponents().set(Clock.class, "clock", Clock.fixed(Instant.ofEpochMilli(given.getTime().getMillis()), ZoneId.systemDefault()));
  return NoopGivenCleaner.INSTANCE;
}

代码示例来源:origin: org.neo4j/neo4j-kernel

public void initializeStatement()
{
  if ( this.statement == null ) // this is the first statement in the transaction, use the transaction time
  {
    this.statement = this.transaction;
  }
  else // this is not the first statement in the transaction, initialize with a new time
  {
    this.statement = Clock.fixed( system.instant(), timezone() );
  }
}

代码示例来源:origin: org.neo4j/neo4j-kernel

public void initializeTransaction()
{
  this.transaction = Clock.fixed( system.instant(), timezone() );
  this.statement = null;
}

代码示例来源:origin: otto-de/edison-microservice

@Test
public void shouldReturnAllJobs() throws IOException {
  // given
  JobInfo firstJob = newJobInfo("42", "TEST", fixed(ofEpochMilli(0), systemDefault()), "localhost");
  JobInfo secondJob = newJobInfo("42", "TEST", fixed(ofEpochMilli(1), systemDefault()), "localhost");
  when(jobService.findJobs(Optional.<String>empty(), 100)).thenReturn(asList(firstJob, secondJob));
  // when
  Object job = jobsController.getJobsAsJson(null, 100, false, false, mock(HttpServletRequest.class));
  // then
  assertThat(job, is(asList(representationOf(firstJob, null, false, "", ""), representationOf(secondJob, null, false, "", ""))));
}

代码示例来源:origin: otto-de/edison-microservice

private JobInfo jobInfoWithRuntime(int finishAmount, ChronoUnit unit) {
    final Clock clock = fixed(Instant.now(), systemDefault());
    final OffsetDateTime startTime = now(clock);
    final OffsetDateTime finishedTime = startTime.plus(finishAmount, unit);
    return newJobInfo("foo", "TEST", startTime, finishedTime, of(finishedTime), OK, emptyList(), clock, "localhost");
  }
}

代码示例来源:origin: otto-de/edison-microservice

@Test
public void shouldFindAll() {
  // given
  repository.createOrUpdate(newJobInfo("oldest", "FOO", fixed(Instant.now().minusSeconds(1), systemDefault()), "localhost"));
  repository.createOrUpdate(newJobInfo("youngest", "FOO", fixed(Instant.now(), systemDefault()), "localhost"));
  // when
  final List<JobInfo> jobInfos = repository.findAll();
  // then
  assertThat(jobInfos.size(), is(2));
  assertThat(jobInfos.get(0).getJobId(), is("youngest"));
  assertThat(jobInfos.get(1).getJobId(), is("oldest"));
}

代码示例来源:origin: otto-de/edison-microservice

@Test
public void shouldFindRunningJobsWithoutUpdatedSinceSpecificDate() throws Exception {
  // given
  repository.createOrUpdate(newJobInfo("deadJob", "FOO", fixed(Instant.now().minusSeconds(10), systemDefault()), "localhost"));
  repository.createOrUpdate(newJobInfo("running", "FOO", fixed(Instant.now(), systemDefault()), "localhost"));
  // when
  final List<JobInfo> jobInfos = repository.findRunningWithoutUpdateSince(now().minus(5, ChronoUnit.SECONDS));
  // then
  assertThat(jobInfos, IsCollectionWithSize.hasSize(1));
  assertThat(jobInfos.get(0).getJobId(), is("deadJob"));
}

相关文章