// Path: src/main/java/com/spotify/trickle/Fallbacks.java
// public static <T> AsyncFunction<Throwable, T> always(@Nullable final T value) {
//   return new AsyncFunction<Throwable, T>() {
//     @Nullable
//     @Override
//     public ListenableFuture<T> apply(@Nullable Throwable input) {
//       return immediateFuture(value);
//     }
//   };
// }
// 
// Path: src/main/java/com/spotify/trickle/Trickle.java
// public static <R> ConfigurableGraph<R> call(Func0<R> func) {
//   return new GraphBuilder<R>(func);
// }
// 
// Path: src/test/java/com/spotify/trickle/Util.java
// static Matcher<Throwable> hasAncestor(final Throwable expected) {
//   return new TypeSafeMatcher<Throwable>() {
//     @Override
//     protected boolean matchesSafely(Throwable item) {
//       for (Throwable cause = item ; cause != null ; cause = cause.getCause()) {
//         if (cause.equals(expected)) {
//           return true;
//         }
//       }
// 
//       return false;
//     }
// 
//     @Override
//     public void describeTo(Description description) {
//       description.appendText("with parent cause " + expected);
//     }
//   };
// }


import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.AsyncFunction;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.SettableFuture;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static com.google.common.util.concurrent.Futures.immediateFailedFuture;
import static com.google.common.util.concurrent.Futures.immediateFuture;
import static com.spotify.trickle.Fallbacks.always;
import static com.spotify.trickle.Trickle.call;
import static com.spotify.trickle.Util.hasAncestor;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
  @Test
  public void shouldForwardValues() throws Exception {
    Func0<String> first = new Func0<String>() {
      @Override
      public ListenableFuture<String> run() {
        return immediateFuture("hi there!");
      }
    };
    Func1<String, Integer> second = new Func1<String, Integer>() {
      @Override
      public ListenableFuture<Integer> run(String arg) {
        return immediateFuture(arg.length());
      }
    };

    Graph<String> g1 = call(first);
    Graph<Integer> graph = call(second).with(g1);

    assertThat(graph.run().get(), equalTo("hi there!".length()));
  }

  @Test
  public void shouldReturnDefaultForFailedCallWithDefault() throws Exception {
    Func0<String> node = new Func0<String>() {
      @Override
      public ListenableFuture<String> run() {
        throw new RuntimeException("expected");
      }
    };

// Path: src/main/java/com/spotify/trickle/Fallbacks.java
// public static <T> AsyncFunction<Throwable, T> always(@Nullable final T value) {
//   return new AsyncFunction<Throwable, T>() {
//     @Nullable
//     @Override
//     public ListenableFuture<T> apply(@Nullable Throwable input) {
//       return immediateFuture(value);
//     }
//   };
// }
// 
// Path: src/main/java/com/spotify/trickle/Trickle.java
// public static <R> ConfigurableGraph<R> call(Func0<R> func) {
//   return new GraphBuilder<R>(func);
// }
// 
// Path: src/test/java/com/spotify/trickle/Util.java
// static Matcher<Throwable> hasAncestor(final Throwable expected) {
//   return new TypeSafeMatcher<Throwable>() {
//     @Override
//     protected boolean matchesSafely(Throwable item) {
//       for (Throwable cause = item ; cause != null ; cause = cause.getCause()) {
//         if (cause.equals(expected)) {
//           return true;
//         }
//       }
// 
//       return false;
//     }
// 
//     @Override
//     public void describeTo(Description description) {
//       description.appendText("with parent cause " + expected);
//     }
//   };
// }


// Path: src/test/java/com/spotify/trickle/TrickleTest.java
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.AsyncFunction;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.SettableFuture;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static com.google.common.util.concurrent.Futures.immediateFailedFuture;
import static com.google.common.util.concurrent.Futures.immediateFuture;
import static com.spotify.trickle.Fallbacks.always;
import static com.spotify.trickle.Trickle.call;
import static com.spotify.trickle.Util.hasAncestor;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

  @Test
  public void shouldForwardValues() throws Exception {
    Func0<String> first = new Func0<String>() {
      @Override
      public ListenableFuture<String> run() {
        return immediateFuture("hi there!");
      }
    };
    Func1<String, Integer> second = new Func1<String, Integer>() {
      @Override
      public ListenableFuture<Integer> run(String arg) {
        return immediateFuture(arg.length());
      }
    };

    Graph<String> g1 = call(first);
    Graph<Integer> graph = call(second).with(g1);

    assertThat(graph.run().get(), equalTo("hi there!".length()));
  }

  @Test
  public void shouldReturnDefaultForFailedCallWithDefault() throws Exception {
    Func0<String> node = new Func0<String>() {
      @Override
      public ListenableFuture<String> run() {
        throw new RuntimeException("expected");
      }
    };

    Graph<String> graph = call(node).fallback(always("fallback response"));