Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.junit.Before;
- import org.junit.Test;
- import org.springframework.http.ResponseEntity;
- import org.springframework.web.client.RestTemplate;
- import java.net.URI;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.concurrent.CompletableFuture;
- import java.util.stream.IntStream;
- import static org.junit.Assert.*;
- public class CompletableFutureTest {
- private Collection<CompletableFuture<Void>> futures;
- private Collection<ResponseEntity<String>> results;
- private final RestTemplate restTemplate = new RestTemplate();
- @Before
- public void setup() {
- this.futures = new ArrayList<>();
- this.results = new ArrayList<>();
- }
- @Test
- public void asyncHttpCallsTest() {
- IntStream.range(0, 20).forEach(i -> {
- this.futures.add(CompletableFuture.runAsync(() -> {
- this.results.add(this.restTemplate.getForEntity(URI.create("http://www.google.com"), String.class));
- }));
- });
- this.futures.forEach(CompletableFuture::join);
- Assert.assertEquals(20, this.results.size());
- }
- }
Add Comment
Please, Sign In to add comment