GH-243, GH-257 Added reactive consumer wrapper
- Added wrapper for an already reactive consumer to ensure that consumers can be consistently represented as Function<Flux, Mono> - Fixed the big that deal with inconsistent result in web environments due to inconsistent representation of the Consumers - Polished tests Resolves #243 Resolves #257
This commit is contained in:
@@ -40,7 +40,9 @@ import reactor.core.publisher.Mono;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.cloud.function.context.catalog.FunctionInspector;
|
||||
import org.springframework.cloud.function.context.message.MessageUtils;
|
||||
import org.springframework.cloud.function.core.FluxConsumer;
|
||||
import org.springframework.cloud.function.core.FluxWrapper;
|
||||
import org.springframework.cloud.function.core.FluxedConsumer;
|
||||
import org.springframework.cloud.function.json.JsonMapper;
|
||||
import org.springframework.cloud.function.web.util.HeaderUtils;
|
||||
import org.springframework.core.MethodParameter;
|
||||
@@ -200,7 +202,14 @@ public class RequestProcessor {
|
||||
flux = messages(wrapper, function == null ? consumer : function, flux);
|
||||
}
|
||||
Mono<ResponseEntity<?>> responseEntityMono = null;
|
||||
if (function != null) {
|
||||
|
||||
if (function instanceof FluxedConsumer || function instanceof FluxConsumer) {
|
||||
((Mono<?>) function.apply(flux)).subscribe();
|
||||
logger.debug("Handled POST with consumer");
|
||||
responseEntityMono = Mono
|
||||
.just(ResponseEntity.status(HttpStatus.ACCEPTED).build());
|
||||
}
|
||||
else {
|
||||
Flux<?> result = Flux.from(function.apply(flux));
|
||||
logger.debug("Handled POST with function");
|
||||
if (stream) {
|
||||
@@ -211,12 +220,6 @@ public class RequestProcessor {
|
||||
body == null ? null : !(body instanceof Collection), false);
|
||||
}
|
||||
}
|
||||
else if (consumer != null) {
|
||||
consumer.accept(flux);
|
||||
logger.debug("Handled POST with consumer");
|
||||
responseEntityMono = Mono
|
||||
.just(ResponseEntity.status(HttpStatus.ACCEPTED).build());
|
||||
}
|
||||
return responseEntityMono;
|
||||
}
|
||||
|
||||
|
||||
@@ -124,14 +124,23 @@ public class HttpPostIntegrationTests {
|
||||
assertThat(result.getBody()).isEqualTo(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void addFoosFlux() throws Exception {
|
||||
ResponseEntity<String> result = this.rest.exchange(RequestEntity
|
||||
.post(new URI("/addFoosFlux")).contentType(MediaType.APPLICATION_JSON)
|
||||
.body("[{\"value\":\"foo\"},{\"value\":\"bar\"}]"), String.class);
|
||||
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
|
||||
assertThat(this.test.list).hasSize(2);
|
||||
assertThat(result.getBody()).isEqualTo(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bareUpdates() throws Exception {
|
||||
ResponseEntity<String> result = this.rest.exchange(RequestEntity
|
||||
.post(new URI("/bareUpdates")).contentType(MediaType.APPLICATION_JSON)
|
||||
.body("[\"one\",\"two\"]"), String.class);
|
||||
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
|
||||
assertThat(this.test.list).hasSize(2);
|
||||
assertThat(result.getBody()).isEqualTo("[]");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -399,12 +408,6 @@ public class HttpPostIntegrationTests {
|
||||
};
|
||||
}
|
||||
|
||||
// @Bean
|
||||
// public Function<byte[],?> byteArrayInputFunction() {
|
||||
//// return value -> new Foo(value.getValue().trim().toUpperCase());
|
||||
// throw new UnsupportedOperationException("boom?");
|
||||
// }
|
||||
|
||||
@Bean
|
||||
public Function<Flux<Integer>, Flux<String>> wrap() {
|
||||
return flux -> flux.log().map(value -> ".." + value + "..");
|
||||
@@ -437,13 +440,22 @@ public class HttpPostIntegrationTests {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Consumer<Flux<Foo>> addFoos() {
|
||||
public Consumer<Flux<Foo>> addFoosFlux() {
|
||||
return flux -> flux.subscribe(value -> this.list.add(value.getValue()));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Consumer<Foo> addFoos() {
|
||||
return value -> {
|
||||
this.list.add(value.getValue());
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Consumer<String> bareUpdates() {
|
||||
return value -> this.list.add(value);
|
||||
return value -> {
|
||||
this.list.add(value);
|
||||
};
|
||||
}
|
||||
|
||||
@Bean("not/a")
|
||||
|
||||
@@ -128,9 +128,8 @@ public class HttpPostIntegrationTests {
|
||||
ResponseEntity<String> result = this.rest.exchange(RequestEntity
|
||||
.post(new URI("/bareUpdates")).contentType(MediaType.APPLICATION_JSON)
|
||||
.body("[\"one\",\"two\"]"), String.class);
|
||||
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
|
||||
assertThat(this.test.list).hasSize(2);
|
||||
assertThat(result.getBody()).isEqualTo("[]");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user