block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-server-epoll-18

huangapple go评论123阅读模式
英文:

block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-server-epoll-18

问题

I am trying to post data using WebTestClient But unfortunately such message is thrown:

java.lang.AssertionError: Status expected:<200> but was:<500>

POST http://localhost:41087/movie
Accept: [application/json;charset=UTF-8]
WebTestClient-Request-Id: 1
Content-Type: [application/json;charset=UTF-8]
Content-Length: [101]

{"movieId":1,"title":"Title for movie 1","genre":"Genre for movie 1","address":"Address for movie 1"}

< 500 INTERNAL_SERVER_ERROR Internal Server Error
< Content-Type: [application/json;charset=UTF-8]
< Content-Length: [226]

{"timestamp":"2020-01-03T10:02:18.467+0000","path":"/movie","status":500,"error":"Internal Server Error","message":"block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-server-epoll-18"}

at org.springframework.test.web.reactive.server.ExchangeResult.assertWithDiagnostics(ExchangeResult.java:200)
at org.springframework.test.web.reactive.server.StatusAssertions.isEqualTo(StatusAssertions.java:59)
at org.springframework.test.web.reactive.server.StatusAssertions.isEqualTo(StatusAssertions.java:51)
at com.geborskimateusz.microservices.core.movie.MovieServiceApplicationTests.postAndVerify(MovieServiceApplicationTests.java:111)
at com.geborskimateusz.microservices.core.movie.MovieServiceApplicationTests.getMovieNotFound(MovieServiceApplicationTests.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:515)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:171)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:167)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:114)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:59)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:105)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:110)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:110)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.AssertionError: Status expected:<200> but was:<500>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:55)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:82)
at org.springframework.test.web.reactive.server.StatusAssertions.lambda$isEqualTo$0(StatusAssertions.java:59)
at org.springframework.test.web.reactive.server.ExchangeResult.assertWithDiagnostics(ExchangeResult.java:197)
... 43 more

Why is that happening?, cant I block value or is it something wrong with post code?
I am totally new to this reactive abstracion, such implementation of WebTestClient post worked with non reactive approach. Here is code that fails:

Here postAndVerify fails:

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = RANDOM_PORT, properties = {"spring.data.mongodb.port: 0"})
public class MovieServiceApplicationTests {

@Autowired
WebTestClient webTestClient;

@Autowired
MovieRepository movieRepository;

@Test
public void getMovie() {
Integer given = 1;

postAndVerify(given, HttpStatus.OK);

getAndVerify(given, HttpStatus.OK)
.jsonPath("$.movieId").isEqualTo(given)
.jsonPath("$.genre").isNotEmpty()
.jsonPath("$.title").isNotEmpty()
.jsonPath("$.address").isNotEmpty();

}

private WebTestClient.BodyContentSpec postAndVerify(Integer id, HttpStatus httpStatus) {
Movie movie = Movie.builder()
.movieId(id)
.title("Title for movie " + id)
.genre("Genre for movie " + id)
.address("Address for movie " + id)
.build();

return webTestClient.post()
.uri("/movie")
.body(Mono.just(movie), Movie.class)
.accept(MediaType.APPLICATION_JSON_UTF8)
.exchange()
.expectStatus().isEqualTo(httpStatus)
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
.expectBody();
}

}

And implementation of create method looks like this:

@Override
public Movie createMovie(Movie movie) {
MovieEntity movieEntity = movieMapper.apiToEntity(movie);

Mono movieMono = movieRepository.save(movieEntity)
.onErrorMap(DuplicateKeyException.class, ex -> new InvalidInputException

英文:

I am trying to post data using WebTestClient But unfortunately such message is thrown:

  1. java.lang.AssertionError: Status expected:&lt;200&gt; but was:&lt;500&gt;
  2. &gt; POST http://localhost:41087/movie
  3. &gt; Accept: [application/json;charset=UTF-8]
  4. &gt; WebTestClient-Request-Id: [1]
  5. &gt; Content-Type: [application/json;charset=UTF-8]
  6. &gt; Content-Length: [101]
  7. {&quot;movieId&quot;:1,&quot;title&quot;:&quot;Title for movie 1&quot;,&quot;genre&quot;:&quot;Genre for movie 1&quot;,&quot;address&quot;:&quot;Address for movie 1&quot;}
  8. &lt; 500 INTERNAL_SERVER_ERROR Internal Server Error
  9. &lt; Content-Type: [application/json;charset=UTF-8]
  10. &lt; Content-Length: [226]
  11. {&quot;timestamp&quot;:&quot;2020-01-03T10:02:18.467+0000&quot;,&quot;path&quot;:&quot;/movie&quot;,&quot;status&quot;:500,&quot;error&quot;:&quot;Internal Server Error&quot;,&quot;message&quot;:&quot;block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-server-epoll-18&quot;}
  12. at org.springframework.test.web.reactive.server.ExchangeResult.assertWithDiagnostics(ExchangeResult.java:200)
  13. at org.springframework.test.web.reactive.server.StatusAssertions.isEqualTo(StatusAssertions.java:59)
  14. at org.springframework.test.web.reactive.server.StatusAssertions.isEqualTo(StatusAssertions.java:51)
  15. at com.geborskimateusz.microservices.core.movie.MovieServiceApplicationTests.postAndVerify(MovieServiceApplicationTests.java:111)
  16. at com.geborskimateusz.microservices.core.movie.MovieServiceApplicationTests.getMovieNotFound(MovieServiceApplicationTests.java:58)
  17. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  18. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  19. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  20. at java.lang.reflect.Method.invoke(Method.java:498)
  21. at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:515)
  22. at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)
  23. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:171)
  24. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
  25. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:167)
  26. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:114)
  27. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:59)
  28. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:105)
  29. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
  30. at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95)
  31. at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)
  32. at java.util.ArrayList.forEach(ArrayList.java:1257)
  33. at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
  34. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:110)
  35. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
  36. at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95)
  37. at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)
  38. at java.util.ArrayList.forEach(ArrayList.java:1257)
  39. at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
  40. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:110)
  41. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
  42. at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:95)
  43. at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)
  44. at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
  45. at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
  46. at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
  47. at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)
  48. at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)
  49. at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)
  50. at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)
  51. at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
  52. at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
  53. at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
  54. at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
  55. at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  56. Caused by: java.lang.AssertionError: Status expected:&lt;200&gt; but was:&lt;500&gt;
  57. at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:55)
  58. at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:82)
  59. at org.springframework.test.web.reactive.server.StatusAssertions.lambda$isEqualTo$0(StatusAssertions.java:59)
  60. at org.springframework.test.web.reactive.server.ExchangeResult.assertWithDiagnostics(ExchangeResult.java:197)
  61. ... 43 more

Why is that happening?, cant I block value or is it something wrong with post code?
I am totally new to this reactive abstracion, such implementation of WebTestClient post worked with non reactive approach. Here is code that fails:

Here postAndVerify fails:

  1. @ExtendWith(SpringExtension.class)
  2. @SpringBootTest(webEnvironment = RANDOM_PORT, properties = {&quot;spring.data.mongodb.port: 0&quot;})
  3. public class MovieServiceApplicationTests {
  4. @Autowired
  5. WebTestClient webTestClient;
  6. @Autowired
  7. MovieRepository movieRepository;
  8. @Test
  9. public void getMovie() {
  10. Integer given = 1;
  11. postAndVerify(given, HttpStatus.OK);
  12. getAndVerify(given, HttpStatus.OK)
  13. .jsonPath(&quot;$.movieId&quot;).isEqualTo(given)
  14. .jsonPath(&quot;$.genre&quot;).isNotEmpty()
  15. .jsonPath(&quot;$.title&quot;).isNotEmpty()
  16. .jsonPath(&quot;$.address&quot;).isNotEmpty();
  17. }
  18. private WebTestClient.BodyContentSpec postAndVerify(Integer id, HttpStatus httpStatus) {
  19. Movie movie = Movie.builder()
  20. .movieId(id)
  21. .title(&quot;Title for movie &quot; + id)
  22. .genre(&quot;Genre for movie &quot; + id)
  23. .address(&quot;Address for movie &quot; + id)
  24. .build();
  25. return webTestClient.post()
  26. .uri(&quot;/movie&quot;)
  27. .body(Mono.just(movie), Movie.class)
  28. .accept(MediaType.APPLICATION_JSON_UTF8)
  29. .exchange()
  30. .expectStatus().isEqualTo(httpStatus)
  31. .expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
  32. .expectBody();
  33. }
  34. }

And implementation of create method looks like this:

  1. @Override
  2. public Movie createMovie(Movie movie) {
  3. MovieEntity movieEntity = movieMapper.apiToEntity(movie);
  4. Mono&lt;Movie&gt; movieMono = movieRepository.save(movieEntity)
  5. .onErrorMap(DuplicateKeyException.class, ex -&gt; new InvalidInputException(&quot;Duplicate key for movieId: &quot; + movie.getMovieId()))
  6. .log()
  7. .map(movieMapper::entityToApi);
  8. return movieMono.block();
  9. }

答案1

得分: 3

反应性方法在非阻塞应用程序中使用。你选择使用反应性,因为你不想阻塞,但仍然这样做。

解决方案很简单。在反应性应用程序中,你不应该调用阻塞。

  1. return movieMono.block();

从你的 createMovie 方法中,你应该返回一个 Mono<Movie>,然后继续链式操作。但由于你没有提供调用 createMovie 的实现,我不能再为你提供更多帮助。

英文:

The reactive approach is used in non-blocking applications. You choose to use reactive because you don't want to block, and still you do it.

The solution is simple. You should never call block in a reactive application.

  1. return movieMono.block();

From your createMovie you should return a Mono&lt;Movie&gt; and then keep chaining on. But since you have not provided your implementation of who is calling createMovie i can't help you more than this.

答案2

得分: 2

请阅读此答案。它应该有所帮助。

正如Thomas所提到的,阻塞非阻塞API并不是一个好主意。

英文:

Please, read this answer. It should help.

As Thomas mentioned, blocking a non-blocking API is't a good idea.

huangapple
  • 本文由 发表于 2020年1月3日 18:08:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576619.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定