Skip to content

Instantly share code, notes, and snippets.

@bbejeck
Created December 23, 2011 05:25
Show Gist options
  • Save bbejeck/1513247 to your computer and use it in GitHub Desktop.
Save bbejeck/1513247 to your computer and use it in GitHub Desktop.
Source for Guava Functions and Java 8 Lambdas
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);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment