new client project, general cleanup, testing work

This commit is contained in:
Eugen Paraschiv
2017-12-13 18:59:17 +02:00
parent 819e9fe8d8
commit 471ed5d992
15 changed files with 393 additions and 7 deletions
@@ -3,13 +3,19 @@ package com.baeldung.reactive.controller;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import java.time.Duration;
import java.util.Random;
import java.util.stream.Stream;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.baeldung.reactive.model.Foo;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;
@RestController
public class FooReactiveController {
@@ -19,11 +25,18 @@ public class FooReactiveController {
return Mono.just(new Foo(id, randomAlphabetic(6)));
}
@GetMapping("/foos")
@GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE, value = "/foos")
public Flux<Foo> getAllFoos2() {
final Flux<Foo> foosFlux = Flux.fromStream(Stream.generate(() -> new Foo(new Random().nextLong(), randomAlphabetic(6))));
final Flux<Long> emmitFlux = Flux.interval(Duration.ofSeconds(1));
return Flux.zip(foosFlux, emmitFlux).map(Tuple2::getT1);
}
@GetMapping(produces = MediaType.TEXT_EVENT_STREAM_VALUE, value = "/foos2")
public Flux<Foo> getAllFoos() {
final Flux<Foo> flux = Flux.<Foo> create(fluxSink -> {
while (true) {
fluxSink.next(new Foo(System.currentTimeMillis(), randomAlphabetic(6)));
fluxSink.next(new Foo(new Random().nextLong(), randomAlphabetic(6)));
}
}).sample(Duration.ofSeconds(1)).log();
@@ -1,4 +1,4 @@
package com.baeldung.reactive.controller;
package com.baeldung.reactive.model;
import lombok.AllArgsConstructor;
import lombok.Data;
@@ -1 +0,0 @@
hello
@@ -1 +0,0 @@
test
@@ -4,10 +4,11 @@ import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import static org.junit.Assert.assertNotNull;
import java.time.Duration;
import java.util.Random;
import org.junit.jupiter.api.Test;
import com.baeldung.reactive.controller.Foo;
import com.baeldung.reactive.model.Foo;
import reactor.core.publisher.Flux;
@@ -17,7 +18,7 @@ public class FluxUnitTest {
public void whenFluxIsConstructed_thenCorrect() {
final Flux<Foo> flux = Flux.<Foo> create(fluxSink -> {
while (true) {
fluxSink.next(new Foo(System.currentTimeMillis(), randomAlphabetic(6)));
fluxSink.next(new Foo(new Random().nextLong(), randomAlphabetic(6)));
}
}).sample(Duration.ofSeconds(1)).log();