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

Espresso idling resource for Apollo GraphQL client #469

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
7a1664d
Add apollo-idling-resource module
VisheshVadhera Apr 19, 2017
3ad4610
Restructuring of the module
VisheshVadhera Apr 19, 2017
1210f32
Add IdlingResource class
VisheshVadhera Apr 19, 2017
0dda9b8
Add dispatcher
VisheshVadhera Apr 20, 2017
51817a7
Add ApolloTracker for tracking the requests
VisheshVadhera Apr 22, 2017
76780b3
Execute callback on calls finished
VisheshVadhera Apr 24, 2017
0af44d1
Register espresso callback with apolloTracker
VisheshVadhera Apr 24, 2017
989e102
Add basic test to check that idlingResource is being created properly
VisheshVadhera Apr 24, 2017
b68e8e5
Add test for idleNow
VisheshVadhera Apr 25, 2017
ff6fa0d
Add test for registerCallBack
VisheshVadhera Apr 26, 2017
7158335
Rename variables
VisheshVadhera Apr 26, 2017
873f393
Merge branch 'master' of github.com:apollographql/apollo-android into…
VisheshVadhera Apr 28, 2017
5525529
Change variable names
VisheshVadhera Apr 28, 2017
43a90b6
Add tests for apolloCallTracker
VisheshVadhera Apr 28, 2017
53801bf
Hide usage of apolloTracker in apolloClient
VisheshVadhera Apr 28, 2017
4483920
Fix tests in apolloCallTracker
VisheshVadhera Apr 28, 2017
190ae11
Add tests for syncCalls
VisheshVadhera Apr 28, 2017
664b033
Add test for checking that idleCallback is invoked
VisheshVadhera Apr 28, 2017
41dcb1d
Merge branch 'master' of github.com:apollographql/apollo-android into…
VisheshVadhera Apr 28, 2017
72484ae
Cleanup
VisheshVadhera Apr 28, 2017
cbf6574
Add docs for ApolloCallTracker
VisheshVadhera Apr 28, 2017
de9a829
Checkstyle
VisheshVadhera May 1, 2017
1cc6187
Merge branch 'master' of github.com:apollographql/apollo-android into…
VisheshVadhera May 2, 2017
07d4b91
Merge branch 'feature-espresso-idling-resource' of github.com:Vishesh…
VisheshVadhera May 2, 2017
5d23a9f
Temp fix for apollo-sample module
VisheshVadhera May 2, 2017
69a28d5
Revert the change made in last commit
VisheshVadhera May 3, 2017
d934cc0
Merge branch 'master' of github.com:apollographql/apollo-android into…
VisheshVadhera May 3, 2017
8d1a6d6
Fix the assertionError in ApolloPrefetchCallbackTest
VisheshVadhera May 3, 2017
7610a67
Move the test class to idling-resource package
VisheshVadhera May 3, 2017
c75d4c4
Add docs for new methods in apolloClient
VisheshVadhera May 3, 2017
79d2f99
Merge branch 'master' of github.com:apollographql/apollo-android into…
VisheshVadhera May 4, 2017
f5ba765
Merge branch 'master' of github.com:apollographql/apollo-android into…
VisheshVadhera May 9, 2017
85581e0
Change method names
VisheshVadhera May 11, 2017
7471952
Move the private method to the last
VisheshVadhera May 11, 2017
f3f1e48
Change method names
VisheshVadhera May 11, 2017
fa598f1
Change Deque with LinkedHashSet
VisheshVadhera May 11, 2017
0d495e1
Remove synchronized from setIdleCallback
VisheshVadhera May 11, 2017
8133815
Rename idleCallback to idleResourceCallback
VisheshVadhera May 12, 2017
1860193
Merge branch 'feature-espresso-idling-resource' of github.com:Vishesh…
VisheshVadhera May 12, 2017
f47672b
Fix checkstyle
VisheshVadhera May 12, 2017
c8102ba
Fix checkstyle
VisheshVadhera May 12, 2017
dd3cb51
Fix failing test
VisheshVadhera May 13, 2017
95c9383
Merge branch 'master' of github.com:apollographql/apollo-android into…
VisheshVadhera May 13, 2017
f9d36a8
Change package & module name
VisheshVadhera May 13, 2017
ac1e4a8
Merge branch 'master' of github.com:apollographql/apollo-android into…
VisheshVadhera May 13, 2017
516615b
Merge branch 'master' of github.com:apollographql/apollo-android into…
VisheshVadhera May 23, 2017
4fab454
Code review
VisheshVadhera May 24, 2017
57c401d
Merge branch 'master' of github.com:apollographql/apollo-android into…
VisheshVadhera May 29, 2017
c4e5ab5
Change the order of methods in the class
VisheshVadhera May 29, 2017
3dad93e
Add missing tracker to builder in RealApolloCall
VisheshVadhera May 29, 2017
9820c66
Fix tests in queryFetcher
VisheshVadhera May 30, 2017
2096424
Merge branch 'master' of github.com:apollographql/apollo-android into…
VisheshVadhera May 30, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Hide usage of apolloTracker in apolloClient
Not exposing apolloTracker to the clients. Instead they have to call apolloClient's getRunningCount method and setIdleCallback method which then delegates these calls to the apolloTracker
  • Loading branch information
VisheshVadhera committed Apr 28, 2017
commit 53801bf28dfb822d5cfcd8d2cb947e0ab0dede32
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static ApolloIdlingResource create(@Nonnull String name, @Nonnull ApolloC
private ApolloIdlingResource(String name, ApolloClient apolloClient) {
this.apolloClient = apolloClient;
this.name = name;
apolloClient.apolloCallTracker().setIdleCallback(new Runnable() {
apolloClient.setIdleCallback(new Runnable() {
@Override public void run() {
ResourceCallback callback = ApolloIdlingResource.this.callback;
if (callback != null) {
Expand All @@ -51,7 +51,7 @@ private ApolloIdlingResource(String name, ApolloClient apolloClient) {
}

@Override public boolean isIdleNow() {
return apolloClient.apolloCallTracker().getRunningCallsCount() == 0;
return apolloClient.getRunningCallsCount() == 0;
}

@Override public void registerIdleTransitionCallback(ResourceCallback callback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static Builder builder() {
private final HttpCacheControl defaultHttpCacheControl;
private final CacheControl defaultCacheControl;
private final ApolloLogger logger;
private final com.apollographql.apollo.internal.ApolloCallTracker callTracker = new com.apollographql.apollo.internal.ApolloCallTracker();
private final ApolloCallTracker callTracker = new ApolloCallTracker();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we need to make it lazy, so if I don't need to tracker we should avoid overhead of ApolloCallTracker (it uses synchronization blocks a lot)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering how we are going to lazily create it. If we had a builder method to supply the IdleCallback then it would have been possible, but I have already expressed my concern over why we shouldn't take that route.

Or maybe if we are thinking of creating the ApolloTracker when someone sets the idleCallback, then I am afraid that wouldn't give us an exact count of running calls e.g. if an async call is made and then an idleCallback is set, then the apolloTracker would give us 0 running calls even though we have a running call.

Do you have anything else in your mind?

Copy link
Contributor

@sav007 sav007 May 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant is if user doesn't care about keep tracking of calls, then why should we create it at all and have some synchronization overhead.

But at the same time for bigger picture we probably need it.
Then I would call it not a tracker but ActiveCallRegistry or smth like this, and it can be used not only for tracking how many calls we are running right now but potentially for batching requests. In this case we need to include as well ApolloWatcher.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably update this class later to enable this feature #452

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright.

private final List<ApolloInterceptor> applicationInterceptors;

private ApolloClient(Builder builder) {
Expand Down Expand Up @@ -142,8 +142,12 @@ public ApolloStore apolloStore() {
return apolloStore;
}

public com.apollographql.apollo.internal.ApolloCallTracker apolloCallTracker() {
return callTracker;
public void setIdleCallback(Runnable idleCallback) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove prefix set
I would make callback to be interface interface IdelCallback instead of generic Runnable

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And should it be part of Builder?

Copy link
Contributor Author

@VisheshVadhera VisheshVadhera May 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel it shouldn't be a part of the builder because it will force the clients to supply the callback at the time of creation of the ApolloClient which I feel is a bit limiting. For example if it is being used in conjunction with a DI framework, the clients might not have the callback prepared at all, for example in a Dagger2 module which contains a @Provides method for creating ApolloClient.

Keeping it separate from the builder lets the clients create ApolloClient in one place and supply the callback in some other place (whenever they have the callback ready).

callTracker.setIdleCallback(idleCallback);
}

public int getRunningCallsCount() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop prefix get and maybe just call it activeCallCount

return callTracker.getRunningCallsCount();
}

Response cachedHttpResponse(String cacheKey) throws IOException {
Expand Down Expand Up @@ -308,8 +312,8 @@ public Builder logger(@Nullable Logger logger) {
* after the response source is selected (either the server, cache or both). This method can be called multiple
* times for adding multiple application interceptors. </p>
*
* <p>Note: Interceptors will be called <b>in the order in which they are added to the list of interceptors</b>
* and if any of the interceptors tries to short circuit the responses, then subsequent interceptors <b>won't</b> be
* <p>Note: Interceptors will be called <b>in the order in which they are added to the list of interceptors</b> and
* if any of the interceptors tries to short circuit the responses, then subsequent interceptors <b>won't</b> be
* called.</p>
*
* @param interceptor Application level interceptor to add
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void testAsyncPrefetchInProgress() throws InterruptedException {
}
});

assertThat(apolloClient.apolloCallTracker().getRunningCallsCount()).isEqualTo(0);
assertThat(apolloClient.getRunningCallsCount()).isEqualTo(0);

prefetch.enqueue(new ApolloPrefetch.Callback() {
@Override public void onSuccess() {
Expand All @@ -74,7 +74,7 @@ public void testAsyncPrefetchInProgress() throws InterruptedException {
}
});

assertThat(apolloClient.apolloCallTracker().getRunningCallsCount()).isEqualTo(1);
assertThat(apolloClient.getRunningCallsCount()).isEqualTo(1);
}

@Test
Expand Down Expand Up @@ -108,7 +108,7 @@ public void testAsyncCallInProgress() {
}
});

assertThat(apolloClient.apolloCallTracker().getRunningCallsCount()).isEqualTo(0);
assertThat(apolloClient.getRunningCallsCount()).isEqualTo(0);

apolloCall.enqueue(new ApolloCall.Callback<Object>() {
@Override public void onResponse(@Nonnull Response<Object> response) {
Expand All @@ -120,6 +120,6 @@ public void testAsyncCallInProgress() {
}
});

assertThat(apolloClient.apolloCallTracker().getRunningCallsCount()).isEqualTo(1);
assertThat(apolloClient.getRunningCallsCount()).isEqualTo(1);
}
}