Skip to content

FIS getAuthToken implementation. #769

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

Merged
merged 31 commits into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
c54292e
FIS getAuthToken implementation.
ankitaj224 Sep 5, 2019
b4b6ba8
FIS getAuthToken implementation.
ankitaj224 Sep 5, 2019
b8bfc1a
Merge branch 'fis_auth' of github.com:firebase/firebase-android-sdk i…
ankitaj224 Sep 5, 2019
7a2a112
Merge branch 'fis_auth' of github.com:firebase/firebase-android-sdk i…
ankitaj224 Sep 5, 2019
42c20f6
Merge branch 'fis_auth' of github.com:firebase/firebase-android-sdk i…
ankitaj224 Sep 5, 2019
46d936c
Merge branch 'fis_sdk' of github.com:firebase/firebase-android-sdk in…
ankitaj224 Sep 5, 2019
48fd7fe
Merge branch 'fis_sdk' of github.com:firebase/firebase-android-sdk in…
ankitaj224 Sep 5, 2019
82fb69e
1. Addressing Ciaran's comments
ankitaj224 Sep 6, 2019
fe04e15
1. Addressing Ciaran's comments
ankitaj224 Sep 6, 2019
e91cb7e
Merge branch 'fis_auth' of github.com:firebase/firebase-android-sdk i…
ankitaj224 Sep 6, 2019
8569952
Merge branch 'fis_auth' of github.com:firebase/firebase-android-sdk i…
ankitaj224 Sep 6, 2019
b74b163
Merge branch 'fis_auth' of github.com:firebase/firebase-android-sdk i…
ankitaj224 Sep 6, 2019
58a30e3
Updated IntDef usage in firebase-installations
ankitaj224 Sep 6, 2019
96e30c2
Using CountDownLatch to await instead of executor.awaitTermination
ankitaj224 Sep 7, 2019
9e3e34d
Addressing Di's comments.
ankitaj224 Sep 10, 2019
824cb63
Addressing Di's comments.
ankitaj224 Sep 10, 2019
90673c3
Merge branch 'fis_auth' of github.com:firebase/firebase-android-sdk i…
ankitaj224 Sep 10, 2019
f5b4f8c
Addressing Ciaran's comments to replace latch with a custom listener.
ankitaj224 Sep 10, 2019
3e29941
Adding onSuccess to AwaitListener.
ankitaj224 Sep 10, 2019
75f2581
Cleaning up the code as per the ciaran's comments.
ankitaj224 Sep 11, 2019
91e49bc
Addressing Di's comments.
ankitaj224 Sep 11, 2019
4578880
1. Handling multiple calls to getAUthToken()
ankitaj224 Sep 13, 2019
7d19b50
Fixing api-information presubmit check.
ankitaj224 Sep 13, 2019
fe04884
Merge branch 'fis_sdk' of github.com:firebase/firebase-android-sdk in…
ankitaj224 Sep 13, 2019
f1d1d37
Merge branch 'fis_sdk' of github.com:firebase/firebase-android-sdk in…
ankitaj224 Sep 13, 2019
ec65040
updating api.txt for firbase-installations-interop
ankitaj224 Sep 13, 2019
396d273
Merge branch 'fis_sdk' of github.com:firebase/firebase-android-sdk in…
ankitaj224 Sep 13, 2019
81b4aeb
Addressing Ciaran's comments.
ankitaj224 Sep 13, 2019
3932d3b
Fixing check-changed - GoogleJavaFormat error.
ankitaj224 Sep 16, 2019
8d4d811
Addressing ciaran`s comments
ankitaj224 Sep 16, 2019
3f44b77
nit fixes to executor : use unblocking queue.
ankitaj224 Sep 16, 2019
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
2 changes: 1 addition & 1 deletion firebase-installations-interop/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ package com.google.firebase.installations {
ctor public InstallationTokenResult.Builder();
method @NonNull public abstract com.google.firebase.installations.InstallationTokenResult build();
method @NonNull public abstract com.google.firebase.installations.InstallationTokenResult.Builder setToken(@NonNull String);
method @NonNull public abstract com.google.firebase.installations.InstallationTokenResult.Builder setTokenExpirationInSecs(@NonNull long);
method @NonNull public abstract com.google.firebase.installations.InstallationTokenResult.Builder setTokenExpirationInSecs(long);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public abstract static class Builder {
public abstract Builder setToken(@NonNull String value);

@NonNull
public abstract Builder setTokenExpirationInSecs(@NonNull long value);
public abstract Builder setTokenExpirationInSecs(long value);

@NonNull
public abstract InstallationTokenResult build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,17 @@
import com.google.firebase.installations.remote.InstallationResponse;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.mockito.AdditionalAnswers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.internal.stubbing.answers.AnswersWithDelay;
import org.mockito.internal.stubbing.answers.Returns;

/**
Expand Down Expand Up @@ -126,7 +125,7 @@ public class FirebaseInstallationsInstrumentedTest {
public void setUp() throws FirebaseInstallationServiceException {
MockitoAnnotations.initMocks(this);
FirebaseApp.clearInstancesForTest();
executor = new ThreadPoolExecutor(0, 4, 10L, TimeUnit.SECONDS, new SynchronousQueue<>());
executor = Executors.newSingleThreadExecutor();
firebaseApp =
FirebaseApp.initializeApp(
ApplicationProvider.getApplicationContext(),
Expand Down Expand Up @@ -268,13 +267,12 @@ public void testGetId_PersistedFidError_BackendOk() throws InterruptedException
Tasks.await(firebaseInstallations.getId());
fail("Could not update local storage.");
} catch (ExecutionException expected) {
Throwable cause = expected.getCause();
assertWithMessage("Exception class doesn't match")
.that(expected)
.hasCauseThat()
.isInstanceOf(FirebaseInstallationsException.class);
assertWithMessage("Exception status doesn't match")
.that(((FirebaseInstallationsException) cause).getStatus())
.that(((FirebaseInstallationsException) expected.getCause()).getStatus())
.isEqualTo(FirebaseInstallationsException.Status.CLIENT_ERROR);
}
}
Expand Down Expand Up @@ -310,13 +308,12 @@ public void testGetAuthToken_PersistedFidError_failure() throws Exception {
firebaseInstallations.getAuthToken(FirebaseInstallationsApi.DO_NOT_FORCE_REFRESH));
fail("Could not update local storage.");
} catch (ExecutionException expected) {
Throwable cause = expected.getCause();
assertWithMessage("Exception class doesn't match")
.that(expected)
.hasCauseThat()
.isInstanceOf(FirebaseInstallationsException.class);
Copy link
Contributor

Choose a reason for hiding this comment

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

Truth has built-in support for unpacking exceptions

assertWithMessage("w/e").that(expected).hasCauseThat().isInstanceOf(FirebaseInstallationsException.class)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Did you also want the status to be changed? I tried but dint find a easy way.

Copy link
Contributor

Choose a reason for hiding this comment

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

You'd have to create a custom truth subject for that and tbh I don't think it's worth it atm

assertWithMessage("Exception status doesn't match")
.that(((FirebaseInstallationsException) cause).getStatus())
.that(((FirebaseInstallationsException) expected.getCause()).getStatus())
.isEqualTo(FirebaseInstallationsException.Status.SDK_INTERNAL_ERROR);
}
}
Expand Down Expand Up @@ -395,22 +392,21 @@ public void testGetAuthToken_serverError_failure() throws Exception {
Tasks.await(firebaseInstallations.getAuthToken(FirebaseInstallationsApi.FORCE_REFRESH));
fail("getAuthToken() failed due to Server Error.");
} catch (ExecutionException expected) {
Throwable cause = expected.getCause();
assertWithMessage("Exception class doesn't match")
.that(expected)
.hasCauseThat()
.isInstanceOf(FirebaseInstallationsException.class);
assertWithMessage("Exception status doesn't match")
.that(((FirebaseInstallationsException) cause).getStatus())
.that(((FirebaseInstallationsException) expected.getCause()).getStatus())
.isEqualTo(FirebaseInstallationsException.Status.SDK_INTERNAL_ERROR);
Copy link
Contributor

Choose a reason for hiding this comment

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

Truth

}
}

@Test
public void testGetAuthToken_multipleCallsDoNotForceRefresh_fetchedNewTokenOnce()
throws Exception {
// Using mockPersistedFid to ensure the order of returning persistedFidEntry to 2 tasks
// triggered simultaneously. Task2 waits for Task1 to complete.On Task1 completion, task2 reads
// Using mockPersistedFid to ensure the order of returning persistedFidEntry to 2 tasks
// triggered simultaneously. Task2 waits for Task1 to complete. On Task1 completion, task2 reads
// the UPDATED_AUTH_TOKEN_FID_ENTRY by Task1 on execution.
when(mockPersistedFid.readPersistedFidEntryValue())
Copy link
Contributor

Choose a reason for hiding this comment

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

add a comment explaining what this means

.thenReturn(
Expand Down Expand Up @@ -444,19 +440,19 @@ public void testGetAuthToken_multipleCallsDoNotForceRefresh_fetchedNewTokenOnce(
public void testGetAuthToken_multipleCallsForceRefresh_fetchedNewTokenTwice() throws Exception {
when(mockPersistedFid.readPersistedFidEntryValue()).thenReturn(REGISTERED_FID_ENTRY);
// Use a mock ServiceClient for network calls with delay(1000ms) to ensure first task is not
// completed before the second task starts. Hence, we can test multiple calls to getAUthToken()
// completed before the second task starts. Hence, we can test multiple calls to getAuthToken()
// and verify one task waits for another task to complete.

doAnswer(
new AnswersWithDelay(
AdditionalAnswers.answersWithDelay(
1000,
new Returns(
InstallationTokenResult.builder()
.setToken(TEST_AUTH_TOKEN_3)
.setTokenExpirationInSecs(TEST_TOKEN_EXPIRATION_TIMESTAMP)
.build())))
.doAnswer(
new AnswersWithDelay(
AdditionalAnswers.answersWithDelay(
1000,
new Returns(
InstallationTokenResult.builder()
Expand All @@ -480,10 +476,10 @@ public void testGetAuthToken_multipleCallsForceRefresh_fetchedNewTokenTwice() th
// As we cannot ensure which task got executed first, verifying with both expected values
assertWithMessage("Persisted Auth Token doesn't match")
.that(task1.getResult().getToken())
.isAnyOf(TEST_AUTH_TOKEN_3, TEST_AUTH_TOKEN_4);
.isEqualTo(TEST_AUTH_TOKEN_3);
Copy link
Contributor

Choose a reason for hiding this comment

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

nice

assertWithMessage("Persisted Auth Token doesn't match")
.that(task2.getResult().getToken())
.isAnyOf(TEST_AUTH_TOKEN_4, TEST_AUTH_TOKEN_3);
.isEqualTo(TEST_AUTH_TOKEN_4);
verify(backendClientReturnsOk, times(2))
.generateAuthToken(TEST_API_KEY, TEST_FID_1, TEST_PROJECT_ID, TEST_REFRESH_TOKEN);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
import com.google.firebase.installations.remote.FirebaseInstallationServiceException;
import com.google.firebase.installations.remote.InstallationResponse;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
Expand Down Expand Up @@ -61,7 +60,7 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
FirebaseInstallations(FirebaseApp firebaseApp) {
this(
DefaultClock.getInstance(),
new ThreadPoolExecutor(0, 1, 30L, TimeUnit.SECONDS, new SynchronousQueue<>()),
Executors.newSingleThreadExecutor(),
firebaseApp,
new FirebaseInstallationServiceClient(),
new PersistedFid(firebaseApp),
Expand Down