public
Created

Source for Guava Functions and Java 8 Lambdas

  • Download Gist
LambdaGuavaTest.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
import bbejeck.guava.futures.SearchingTestBase;
import bbejeck.support.model.Person;
import com.google.common.base.Function;
import com.google.common.util.concurrent.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
 
import java.lang.SuppressWarnings;
import java.util.List;
import java.util.concurrent.*;
 
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
 
@SuppressWarnings("unchecked")
public class LambdaGuavaTest extends SearchingTestBase {
 
private int numberTasks;
private CountDownLatch startSignal;
private CountDownLatch doneSignal;
private ListeningExecutorService executorService;
 
@Before
public void setUp() throws Exception {
numberTasks = 5;
startSignal = new CountDownLatch(1);
doneSignal = new CountDownLatch(numberTasks);
executorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
}
 
 
@After
public void tearDown() {
executorService.shutdownNow();
}
 
 
@Test
public void testChainWithLambda() throws Exception {
 
ListenableFuture<List<String>> indexSearch = luceneSearcher.searchAsync("firstName:martin");
Function<List<String>, ListenableFuture<List<Person>>> queryFunction = ids -> (dbService.getPersonsByIdAsync(ids));
 
ListenableFuture<List<Person>> results = Futures.chain(indexSearch, queryFunction, executorService);
List<Person> persons = results.get(1, TimeUnit.SECONDS);
assertThat(persons.size(), is(74));
for (Person person : persons) {
assertThat(person.firstName, is("Martin"));
}
}
 
@Test
public void testTransformWithLambda() throws Exception {
 
ListenableFuture<List<String>> indexSearch = luceneSearcher.searchAsync("firstName:martin");
Function<List<String>, List<Person>> queryFunction = ids -> (dbService.getPersonsById(ids));
 
ListenableFuture<List<Person>> results = Futures.transform(indexSearch, queryFunction, executorService);
List<Person> persons = results.get(1, TimeUnit.SECONDS);
assertThat(persons.size(), is(74));
for (Person person : persons) {
assertThat(person.firstName, is("Martin"));
}
}
 
@Test
public void allAsListSuccess() throws Exception {
ListenableFuture<List<Person>> lf1 = getPersonsByFirstNameFuture("martin", false);
ListenableFuture<List<Person>> lf2 = getPersonsByFirstNameFuture("bob", false);
ListenableFuture<List<Person>> lf3 = getPersonsByFirstNameFuture("emily", false);
ListenableFuture<List<Person>> lf4 = getPersonsByFirstNameFuture("mona", false);
ListenableFuture<List<Person>> lf5 = getPersonsByFirstNameFuture("tom", false);
 
ListenableFuture<List<List<Person>>> lfResults = Futures.allAsList(lf1, lf2, lf3, lf4, lf5);
startSignal.countDown();
List<List<Person>> listOfPersonLists = lfResults.get();
assertThat(listOfPersonLists.size() > 0, is(true));
for (List<Person> personList : listOfPersonLists) {
assertThat(personList.size() > 0, is(true));
}
}
 
@Test(expected = ExecutionException.class)
public void allAsListSuccessOneFailure() throws Exception {
ListenableFuture<List<Person>> lf1 = getPersonsByFirstNameFuture("martin", false);
ListenableFuture<List<Person>> lf2 = getPersonsByFirstNameFuture("bob", false);
ListenableFuture<List<Person>> lf3 = getPersonsByFirstNameFuture("emily", true);
ListenableFuture<List<Person>> lf4 = getPersonsByFirstNameFuture("mona", false);
ListenableFuture<List<Person>> lf5 = getPersonsByFirstNameFuture("tom", false);
 
ListenableFuture<List<List<Person>>> lfResults = Futures.allAsList(lf1, lf2, lf3, lf4, lf5);
startSignal.countDown();
List<List<Person>> listOfPersonLists = lfResults.get();
fail("should not get here");
}
 
@Test
public void successfulAsListSuccessOneFailure() throws Exception {
ListenableFuture<List<Person>> lf1 = getPersonsByFirstNameFuture("martin", true);
ListenableFuture<List<Person>> lf2 = getPersonsByFirstNameFuture("bob", false);
ListenableFuture<List<Person>> lf3 = getPersonsByFirstNameFuture("emily", true);
ListenableFuture<List<Person>> lf4 = getPersonsByFirstNameFuture("mona", false);
ListenableFuture<List<Person>> lf5 = getPersonsByFirstNameFuture("tom", false);
 
ListenableFuture<List<List<Person>>> lfResults = Futures.successfulAsList(lf1, lf2, lf3, lf4, lf5);
startSignal.countDown();
List<List<Person>> listOfPersonLists = lfResults.get();
 
assertThat(listOfPersonLists.size() == 5, is(true));
 
//have null values failed
assertThat(listOfPersonLists.get(0), is(nullValue()));
assertThat(listOfPersonLists.get(2), is(nullValue()));
 
//succeeded returned valid results
assertThat(listOfPersonLists.get(1).size() > 0, is(true));
assertThat(listOfPersonLists.get(3).size() > 0, is(true));
assertThat(listOfPersonLists.get(4).size() > 0, is(true));
}
 
private ListenableFuture<List<Person>> getPersonsByFirstNameFuture(String firstName, boolean error) {
return executorService.submit(() -> {startSignal.await();
if (error) {
throw new RuntimeException("Ooops!");
}
List<String> ids = luceneSearcher.search("firstName:" + firstName);
return dbService.getPersonsById(ids);
});
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.