Guest User

Untitled

a guest
Jan 22nd, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. import org.junit.Before;
  2. import org.junit.Test;
  3. import org.springframework.http.ResponseEntity;
  4. import org.springframework.web.client.RestTemplate;
  5.  
  6. import java.net.URI;
  7. import java.util.ArrayList;
  8. import java.util.Collection;
  9. import java.util.concurrent.CompletableFuture;
  10. import java.util.stream.IntStream;
  11.  
  12. import static org.junit.Assert.*;
  13.  
  14. public class CompletableFutureTest {
  15.  
  16. private Collection<CompletableFuture<Void>> futures;
  17.  
  18. private Collection<ResponseEntity<String>> results;
  19.  
  20. private final RestTemplate restTemplate = new RestTemplate();
  21.  
  22. @Before
  23. public void setup() {
  24. this.futures = new ArrayList<>();
  25. this.results = new ArrayList<>();
  26. }
  27.  
  28. @Test
  29. public void asyncHttpCallsTest() {
  30. IntStream.range(0, 20).forEach(i -> {
  31. this.futures.add(CompletableFuture.runAsync(() -> {
  32. this.results.add(this.restTemplate.getForEntity(URI.create("http://www.google.com"), String.class));
  33. }));
  34. });
  35.  
  36. this.futures.forEach(CompletableFuture::join);
  37.  
  38. Assert.assertEquals(20, this.results.size());
  39. }
  40.  
  41. }
Add Comment
Please, Sign In to add comment