Example usage for java.time Duration ofSeconds

List of usage examples for java.time Duration ofSeconds

Introduction

In this page you can find the example usage for java.time Duration ofSeconds.

Prototype

public static Duration ofSeconds(long seconds) 

Source Link

Document

Obtains a Duration representing a number of seconds.

Usage

From source file:org.springframework.cloud.gateway.rsocket.socketacceptor.GatewaySocketAcceptorTests.java

@Test
public void asyncFilter() {

    AsyncFilter filter = new AsyncFilter();

    RSocket socket = new GatewaySocketAcceptor(this.factory, singletonList(filter), this.meterRegistry,
            this.properties).accept(this.setupPayload, this.sendingSocket).block(Duration.ofSeconds(5));

    assertThat(filter.invoked()).isTrue();
    assertThat(socket).isNotNull();/*from   w ww . j a  v  a 2s  .  c  om*/
}

From source file:org.springframework.cloud.gateway.rsocket.socketacceptor.GatewaySocketAcceptorTests.java

@Test(expected = IllegalStateException.class)
public void handleErrorFromFilter() {

    ExceptionFilter filter = new ExceptionFilter();

    new GatewaySocketAcceptor(this.factory, singletonList(filter), this.meterRegistry, this.properties)
            .accept(this.setupPayload, this.sendingSocket).block(Duration.ofSeconds(5));

}

From source file:org.springframework.cloud.gateway.test.HttpBinCompatibleController.java

@RequestMapping(path = "/delay/{sec}", produces = MediaType.APPLICATION_JSON_VALUE)
public Mono<Map<String, Object>> get(ServerWebExchange exchange, @PathVariable int sec)
        throws InterruptedException {
    int delay = Math.min(sec, 10);
    return Mono.just(get(exchange)).delayElement(Duration.ofSeconds(delay));
}

From source file:org.springframework.cloud.gateway.test.sse.SseIntegrationTests.java

@Test
public void sseAsString() {
    Flux<String> result = this.webClient.get().uri("/string").accept(TEXT_EVENT_STREAM).exchange()
            .flatMapMany(response -> response.bodyToFlux(String.class));

    StepVerifier.create(result).expectNext("foo 0").expectNext("foo 1").thenCancel()
            .verify(Duration.ofSeconds(5L));
}

From source file:org.springframework.cloud.gateway.test.sse.SseIntegrationTests.java

@Test
public void sseAsPerson() {
    Flux<Person> result = this.webClient.get().uri("/person").accept(TEXT_EVENT_STREAM).exchange()
            .flatMapMany(response -> response.bodyToFlux(Person.class));

    StepVerifier.create(result).expectNext(new Person("foo 0")).expectNext(new Person("foo 1")).thenCancel()
            .verify(Duration.ofSeconds(5L));
}

From source file:org.springframework.cloud.gateway.test.sse.SseIntegrationTests.java

@Test
@SuppressWarnings("Duplicates")
public void sseAsEvent() {
    ResolvableType type = forClassWithGenerics(ServerSentEvent.class, String.class);
    Flux<ServerSentEvent<String>> result = this.webClient.get().uri("/event").accept(TEXT_EVENT_STREAM)
            .exchange().flatMapMany(/*from w w  w. ja  v a  2 s.c  o  m*/
                    response -> response.body(toFlux(new ParameterizedTypeReference<ServerSentEvent<String>>() {
                    })));

    StepVerifier.create(result).consumeNextWith(event -> {
        assertThat(event.id()).isEqualTo("0");
        assertThat(event.data()).isEqualTo("foo");
        assertThat(event.comment()).isEqualTo("bar");
        assertThat(event.event()).isNull();
        assertThat(event.retry()).isNull();
    }).consumeNextWith(event -> {
        assertThat(event.id()).isEqualTo("1");
        assertThat(event.data()).isEqualTo("foo");
        assertThat(event.comment()).isEqualTo("bar");
        assertThat(event.event()).isNull();
        assertThat(event.retry()).isNull();
    }).thenCancel().verify(Duration.ofSeconds(5L));
}

From source file:org.springframework.cloud.gateway.test.sse.SseIntegrationTests.java

@Test
@SuppressWarnings("Duplicates")
public void sseAsEventWithoutAcceptHeader() {
    Flux<ServerSentEvent<String>> result = this.webClient.get().uri("/event").accept(TEXT_EVENT_STREAM)
            .exchange().flatMapMany(/*w  w  w.  j  ava 2  s. c om*/
                    response -> response.body(toFlux(new ParameterizedTypeReference<ServerSentEvent<String>>() {
                    })));

    StepVerifier.create(result).consumeNextWith(event -> {
        assertThat(event.id()).isEqualTo("0");
        assertThat(event.data()).isEqualTo("foo");
        assertThat(event.comment()).isEqualTo("bar");
        assertThat(event.event()).isNull();
        assertThat(event.retry()).isNull();
    }).consumeNextWith(event -> {
        assertThat(event.id()).isEqualTo("1");
        assertThat(event.data()).isEqualTo("foo");
        assertThat(event.comment()).isEqualTo("bar");
        assertThat(event.event()).isNull();
        assertThat(event.retry()).isNull();
    }).thenCancel().verify(Duration.ofSeconds(5L));
}

From source file:org.springframework.cloud.gateway.test.websocket.WebSocketIntegrationTests.java

@Test
public void subProtocol() throws Exception {
    String protocol = "echo-v1";
    String protocol2 = "echo-v2";
    AtomicReference<HandshakeInfo> infoRef = new AtomicReference<>();
    MonoProcessor<Object> output = MonoProcessor.create();

    client.execute(getUrl("/sub-protocol"), new WebSocketHandler() {
        @Override/* www .j  a  va  2s. c  om*/
        public List<String> getSubProtocols() {
            return Arrays.asList(protocol, protocol2);
        }

        @Override
        public Mono<Void> handle(WebSocketSession session) {
            infoRef.set(session.getHandshakeInfo());
            return session.receive().map(WebSocketMessage::getPayloadAsText).subscribeWith(output).then();
        }
    }).block(Duration.ofMillis(5000));

    HandshakeInfo info = infoRef.get();
    assertThat(info.getHeaders().getFirst("Upgrade")).isEqualToIgnoringCase("websocket");

    assertThat(info.getHeaders().getFirst("Sec-WebSocket-Protocol")).isEqualTo(protocol);
    assertThat(info.getSubProtocol()).as("Wrong protocol accepted").isEqualTo(protocol);
    assertThat(output.block(Duration.ofSeconds(5))).as("Wrong protocol detected on the server side")
            .isEqualTo(protocol);
}

From source file:org.springframework.cloud.netflix.turbine.stream.TurbineController.java

public TurbineController(PublishSubject<Map<String, Object>> hystrixSubject) {
    Observable<Map<String, Object>> stream = StreamAggregator
            .aggregateGroupedStreams(//from   ww  w .  j  a v  a 2  s  .c om
                    hystrixSubject.groupBy(data -> InstanceKey.create((String) data.get("instanceId"))))
            .doOnUnsubscribe(() -> log.info("Unsubscribing aggregation."))
            .doOnSubscribe(() -> log.info("Starting aggregation")).flatMap(o -> o);
    Flux<Map<String, Object>> ping = Flux.interval(Duration.ofSeconds(5), Duration.ofSeconds(10))
            .map(l -> Collections.singletonMap("type", (Object) "ping")).share();
    flux = Flux.merge(RxReactiveStreams.toPublisher(stream), ping).share()
            .map(map -> JsonUtility.mapToJson(map));
}

From source file:org.springframework.core.io.buffer.LeakAwareDataBufferFactory.java

/**
 * Checks whether all of the data buffers allocated by this factory have also been released.
 * If not, then an {@link AssertionError} is thrown. Typically used from a JUnit {@link After}
 * method.//from  w  w  w .  j  a  v  a2s .c  om
 */
public void checkForLeaks() {
    Instant start = Instant.now();
    while (true) {
        if (this.created.stream().noneMatch(LeakAwareDataBuffer::isAllocated)) {
            return;
        }
        if (Instant.now().isBefore(start.plus(Duration.ofSeconds(5)))) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException ex) {
                // ignore
            }
            continue;
        }
        List<AssertionError> errors = this.created.stream().filter(LeakAwareDataBuffer::isAllocated)
                .map(LeakAwareDataBuffer::leakError).collect(Collectors.toList());

        errors.forEach(it -> logger.error("Leaked error: ", it));
        throw new AssertionError(errors.size() + " buffer leaks detected (see logs above)");
    }
}