Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change CacheControl to Interceptor based Fetcher #556

Merged
merged 12 commits into from
Jul 18, 2017
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
public class ApolloCallbackTest {
@Rule public final MockWebServer server = new MockWebServer();
private ApolloClient apolloClient;
private static final int TIMEOUT_SECONDS = 2;

@Before public void setUp() throws Exception {
apolloClient = ApolloClient.builder()
Expand Down Expand Up @@ -63,7 +64,7 @@ public class ApolloCallbackTest {
countDownLatch.countDown();
}
}, callbackHandler));
countDownLatch.await(2, TimeUnit.SECONDS);
countDownLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
assertThat(invoked.get()).isTrue();
}

Expand All @@ -84,7 +85,7 @@ public class ApolloCallbackTest {
countDownLatch.countDown();
}
}, callbackHandler));
countDownLatch.await(2, TimeUnit.SECONDS);
countDownLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
assertThat(invoked.get()).isTrue();
}

Expand All @@ -106,7 +107,7 @@ public class ApolloCallbackTest {
countDownLatch.countDown();
}
}, callbackHandler));
countDownLatch.await(2, TimeUnit.SECONDS);
countDownLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
assertThat(invoked.get()).isTrue();
}

Expand Down Expand Up @@ -136,7 +137,7 @@ public class ApolloCallbackTest {
fail("Expected onResponse");
}
}, callbackHandler));
countDownLatch.await(2, TimeUnit.SECONDS);
countDownLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
assertThat(invoked.get()).isTrue();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@

public final class TestUtils {
public static final Query EMPTY_QUERY = new Query() {

OperationName operationName = new OperationName() {
@Override public String name() {
return "EmptyQuery";
}
};

@Override public String queryDocument() {
return "";
}
Expand All @@ -34,7 +41,7 @@ public final class TestUtils {
}

@Nonnull @Override public OperationName name() {
return null;
return operationName;
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ public ApolloCallback(@Nonnull ApolloCall.Callback<T> callback, @Nonnull Handler
});
}

@Override public void onCompleted() {
handler.post(new Runnable() {
@Override public void run() {
delegate.onCompleted();
}
});
}

@Override public void onFailure(@Nonnull final ApolloException e) {
handler.post(new Runnable() {
@Override public void run() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ public class ApolloIdlingResourceTest {
private static final String IDLING_RESOURCE_NAME = "apolloIdlingResource";

private static final Query EMPTY_QUERY = new Query() {

OperationName operationName = new OperationName() {
@Override public String name() {
return "EmptyQuery";
}
};

@Override public String queryDocument() {
return "";
}
Expand All @@ -66,7 +73,7 @@ public class ApolloIdlingResourceTest {
}

@Nonnull @Override public OperationName name() {
return null;
return operationName;
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import okhttp3.mockwebserver.MockWebServer;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

public class ApolloCallTest {
private static final long TIME_OUT_SECONDS = 3;
Expand All @@ -39,61 +38,57 @@ public void setup() {
}

@Test
public void apolloCallNotCalled_WhenCanceled() throws Exception {
final NamedCountDownLatch responseLatch = new NamedCountDownLatch("apolloCallNotCalled_WhenCanceled", 1);
public void cancelCallBeforeEnqueueTriggersOnFailure() throws Exception {
final NamedCountDownLatch responseLatch = new NamedCountDownLatch("cancelCallBeforeEnqueueTriggersOnFailure", 1);

EpisodeHeroNameQuery query = EpisodeHeroNameQuery.builder().episode(Episode.EMPIRE).build();
mockWebServer.enqueue(mockResponse("EpisodeHeroNameResponseWithId.json"));

final AtomicReference<ApolloException> errorRef = new AtomicReference<>();
ApolloCall<EpisodeHeroNameQuery.Data> apolloCall = apolloClient.query(query);

apolloCall.cancel();

apolloCall.enqueue(new ApolloCall.Callback<EpisodeHeroNameQuery.Data>() {
@Override public void onResponse(@Nonnull Response<EpisodeHeroNameQuery.Data> response) {
responseLatch.countDown();
if (responseLatch.getCount() == 0) {
fail("Received callback, although apollo call has already been canceled");
}
}

@Override public void onFailure(@Nonnull ApolloException e) {
responseLatch.countDown();
fail(e.getMessage());
errorRef.set(e);
}
});

//Wait for 3 seconds to check that callback is not called.
//Test is successful if timeout is reached.
responseLatch.await(TIME_OUT_SECONDS, TimeUnit.SECONDS);
responseLatch.awaitOrThrowWithTimeout(TIME_OUT_SECONDS, TimeUnit.SECONDS);
assertThat(errorRef.get()).isInstanceOf(ApolloCanceledException.class);
}

@Test
public void apolloCanceledExceptionEnqueue() throws Exception {
final NamedCountDownLatch responseLatch = new NamedCountDownLatch("apolloCanceledExceptionEnqueue", 1);
public void cancelAfterCallingEnqueueHasNoCallback() throws Exception {
final NamedCountDownLatch responseLatch = new NamedCountDownLatch("cancelAfterCallingEnqueueHasNoCallback", 1);

EpisodeHeroNameQuery query = EpisodeHeroNameQuery.builder().episode(Episode.EMPIRE).build();
mockWebServer.enqueue(mockResponse("EpisodeHeroNameResponseWithId.json")
.setBodyDelay(TIME_OUT_SECONDS, TimeUnit.SECONDS));

final AtomicReference<ApolloException> errorRef = new AtomicReference<>();
ApolloCall<EpisodeHeroNameQuery.Data> apolloCall = apolloClient.query(query);
final AtomicReference<String> errorState = new AtomicReference<>(null);
apolloCall.enqueue(new ApolloCall.Callback<EpisodeHeroNameQuery.Data>() {
@Override public void onResponse(@Nonnull Response<EpisodeHeroNameQuery.Data> response) {
errorState.set("onResponse should not be called after cancel");
responseLatch.countDown();
}

@Override public void onFailure(@Nonnull ApolloException e) {
errorRef.set(e);
errorState.set("onFailure should not be called after cancel");
responseLatch.countDown();
}
});

Thread.sleep(500);
apolloCall.cancel();
responseLatch.await(TIME_OUT_SECONDS, TimeUnit.SECONDS);

assertThat(errorRef.get()).isInstanceOf(ApolloCanceledException.class);
assertThat(errorState.get()).isNull();
}

@Test
Expand Down
Loading