-
Notifications
You must be signed in to change notification settings - Fork 615
getId() implementation with instrumented tests. #703
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
Changes from 1 commit
30cf6ed
d034bbb
f8b468c
8db7967
b507a3c
f5a7c05
90cdbaa
0dfa987
a71a1f3
0a92e2d
4cc0cee
f16db3f
f8e75fd
227bf3b
4be3847
68acde5
16544a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,15 +76,15 @@ public void testReadCacheEntry_Null() { | |
public void testUpdateAndReadCacheEntry() throws Exception { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add some comments explaining what this is testing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Thanks. PTAL. |
||
assertTrue( | ||
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cache0.insertOrUpdatePersistedFidEntry( | ||
PersistedFidEntryValue.builder() | ||
PersistedFidEntry.builder() | ||
.setFirebaseInstallationId(TEST_FID_1) | ||
.setAuthToken(TEST_AUTH_TOKEN) | ||
.setRefreshToken(TEST_REFRESH_TOKEN) | ||
.setPersistedStatus(PersistedFid.PersistedStatus.UNREGISTERED) | ||
.setTokenCreationEpochInSecs(TEST_CREATION_TIMESTAMP_1) | ||
.setExpiresInSecs(TEST_TOKEN_EXPIRATION_TIMESTAMP) | ||
.build())); | ||
PersistedFidEntryValue entryValue = cache0.readPersistedFidEntryValue(); | ||
PersistedFidEntry entryValue = cache0.readPersistedFidEntryValue(); | ||
assertThat(entryValue.getFirebaseInstallationId()).isEqualTo(TEST_FID_1); | ||
assertThat(entryValue.getAuthToken()).isEqualTo(TEST_AUTH_TOKEN); | ||
assertThat(entryValue.getRefreshToken()).isEqualTo(TEST_REFRESH_TOKEN); | ||
|
@@ -96,7 +96,7 @@ public void testUpdateAndReadCacheEntry() throws Exception { | |
|
||
assertTrue( | ||
cache0.insertOrUpdatePersistedFidEntry( | ||
PersistedFidEntryValue.builder() | ||
PersistedFidEntry.builder() | ||
.setFirebaseInstallationId(TEST_FID_1) | ||
.setAuthToken(TEST_AUTH_TOKEN) | ||
.setRefreshToken(TEST_REFRESH_TOKEN) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ | |
import com.google.android.gms.tasks.Tasks; | ||
import com.google.firebase.FirebaseApp; | ||
import com.google.firebase.installations.local.PersistedFid; | ||
import com.google.firebase.installations.local.PersistedFidEntryValue; | ||
import com.google.firebase.installations.local.PersistedFidEntry; | ||
import com.google.firebase.installations.remote.FirebaseInstallationServiceClient; | ||
import com.google.firebase.installations.remote.FirebaseInstallationServiceException; | ||
import com.google.firebase.installations.remote.InstallationResponse; | ||
|
@@ -147,7 +147,7 @@ String getName() { | |
* | ||
* Persisted Fid exists and is in Unregistered state | ||
* | | ||
* Register the Fid on FIS backened and update the PersistedFidEntry | ||
* Register the Fid on FIS Server and update the PersistedFidEntry | ||
* | | ||
* return the Persisted Fid | ||
* | ||
|
@@ -156,53 +156,47 @@ String getName() { | |
@WorkerThread | ||
private String getFirebaseInstallationId() throws FirebaseInstallationsException { | ||
|
||
PersistedFidEntryValue persistedFidEntryValue = persistedFid.readPersistedFidEntryValue(); | ||
PersistedFidEntry persistedFidEntry = persistedFid.readPersistedFidEntryValue(); | ||
|
||
if (persistedFidDoesNotExistsOrInErrorState(persistedFidEntryValue)) { | ||
return createFidAndPersistFidEntry(); | ||
if (persistedFidExistsAndRegistered(persistedFidEntry)) { | ||
return persistedFidEntry.getFirebaseInstallationId(); | ||
} | ||
|
||
if (persistedFidExistsAndRegistered(persistedFidEntryValue)) { | ||
return persistedFidEntryValue.getFirebaseInstallationId(); | ||
if (persistedFidMissingOrInErrorState(persistedFidEntry)) { | ||
return createFidAndPersistFidEntry(); | ||
} | ||
|
||
if (persistedFidExistsAndUnregistered(persistedFidEntryValue)) { | ||
registerAndSaveFid(persistedFidEntryValue.getFirebaseInstallationId()); | ||
return persistedFidEntryValue.getFirebaseInstallationId(); | ||
if (persistedFidExistsAndUnregistered(persistedFidEntry)) { | ||
Tasks.call(executor, () -> registerAndSaveFid(persistedFidEntry.getFirebaseInstallationId())); | ||
return persistedFidEntry.getFirebaseInstallationId(); | ||
} | ||
|
||
return null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can this method ever return NULL? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If delete() is called and there is no network availability, we mark registration status as deleted. In that case, it returns NULL. Is that not right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @andirayo Can you please check this? I think the issue is, we have introduced a state DELETED as per fis-android-sdk-design doc. But from your comments on my doc, seems like you don't want the DELETED state. We can change the above function as : private String getFirebaseInstallationId() throws FirebaseInstallationsException {
Wdyt? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Except for the "else if" in there (an else is not necessary after you "return" in the if-statement, I like it better yes. How about return createFidAndPersistFidEntry(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what do you all think about something like this? I haven't checked that it'll compile, but it seems to clearly express what we're trying to do:
public Task<FID> getFid() {
return getExistingFid()
.continueWith(orElse(this::createAndPersistNewFid))
.onSuccessTask(this::registerFidIfNecessary);
}
public static <F, T> Continuation<F, T> orElse(Supplier<T> supplier) {
return t -> {
if (t.isSuccessful()) { return t.getResult(); }
return supplier.get();
};
}
private FID createAndPersistNewFid() {
FID fid = Utils.createRandomFid();
persist(fid);
return fid;
}
private void registerFidIfNecessary(FID fid) {
// do network stuff
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ciarand I tried incorporating the changes you mentioned below:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have 2 concerns:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah we can't use the standard java.util.functional.Supplier interface. We can just define one in the parent class:
When you say "Exception handling by the Supplier while persisting the newly created fid" do you mean you're worried that you can't propagate exceptions or you're worried that they will be propagated? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated. Thanks for the comments. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mark it |
||
} | ||
|
||
private static boolean persistedFidDoesNotExistsOrInErrorState( | ||
PersistedFidEntryValue persistedFidEntryValue) { | ||
return persistedFidEntryValue == null | ||
|| persistedFidEntryValue.getPersistedStatus() | ||
== PersistedFid.PersistedStatus.REGISTER_ERROR; | ||
private static boolean persistedFidMissingOrInErrorState(PersistedFidEntry persistedFidEntry) { | ||
return persistedFidEntry == null | ||
|| persistedFidEntry.getPersistedStatus() == PersistedFid.PersistedStatus.REGISTER_ERROR; | ||
} | ||
|
||
private static boolean persistedFidExistsAndRegistered( | ||
PersistedFidEntryValue persistedFidEntryValue) { | ||
return persistedFidEntryValue != null | ||
&& persistedFidEntryValue.getPersistedStatus() == PersistedFid.PersistedStatus.REGISTERED; | ||
private static boolean persistedFidExistsAndRegistered(PersistedFidEntry persistedFidEntry) { | ||
return persistedFidEntry != null | ||
&& persistedFidEntry.getPersistedStatus() == PersistedFid.PersistedStatus.REGISTERED; | ||
} | ||
|
||
private static boolean persistedFidExistsAndUnregistered( | ||
PersistedFidEntryValue persistedFidEntryValue) { | ||
return persistedFidEntryValue != null | ||
&& persistedFidEntryValue.getPersistedStatus() == PersistedFid.PersistedStatus.UNREGISTERED; | ||
private static boolean persistedFidExistsAndUnregistered(PersistedFidEntry persistedFidEntry) { | ||
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return persistedFidEntry != null | ||
&& persistedFidEntry.getPersistedStatus() == PersistedFid.PersistedStatus.UNREGISTERED; | ||
} | ||
|
||
private String createFidAndPersistFidEntry() throws FirebaseInstallationsException { | ||
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
String fid = Utils.createRandomFid(); | ||
|
||
boolean firstUpdateCacheResult = | ||
persistedFid.insertOrUpdatePersistedFidEntry( | ||
PersistedFidEntryValue.builder() | ||
PersistedFidEntry.builder() | ||
.setFirebaseInstallationId(fid) | ||
.setPersistedStatus(PersistedFid.PersistedStatus.UNREGISTERED) | ||
.setTokenCreationEpochInSecs(0) | ||
.setExpiresInSecs(0) | ||
.build()); | ||
|
||
if (!firstUpdateCacheResult) { | ||
|
@@ -211,18 +205,17 @@ private String createFidAndPersistFidEntry() throws FirebaseInstallationsExcepti | |
FirebaseInstallationsException.Status.CLIENT_ERROR); | ||
} | ||
|
||
registerAndSaveFid(fid); | ||
|
||
Tasks.call(executor, () -> registerAndSaveFid(fid)); | ||
return fid; | ||
} | ||
|
||
/** | ||
* Registers the created Fid with FIS Servers if the Network is available and update the shared | ||
* prefs. | ||
*/ | ||
private void registerAndSaveFid(String fid) throws FirebaseInstallationsException { | ||
private Void registerAndSaveFid(String fid) throws FirebaseInstallationsException { | ||
if (!Utils.isNetworkAvailable(firebaseApp.getApplicationContext())) { | ||
return; | ||
return null; | ||
} | ||
|
||
try { | ||
|
@@ -236,7 +229,7 @@ private void registerAndSaveFid(String fid) throws FirebaseInstallationsExceptio | |
getApplicationId()); | ||
|
||
persistedFid.insertOrUpdatePersistedFidEntry( | ||
PersistedFidEntryValue.builder() | ||
PersistedFidEntry.builder() | ||
.setFirebaseInstallationId(fid) | ||
.setPersistedStatus(PersistedFid.PersistedStatus.REGISTERED) | ||
.setAuthToken(installationResponse.getAuthToken().getToken()) | ||
|
@@ -248,14 +241,13 @@ private void registerAndSaveFid(String fid) throws FirebaseInstallationsExceptio | |
|
||
} catch (FirebaseInstallationServiceException exception) { | ||
persistedFid.insertOrUpdatePersistedFidEntry( | ||
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PersistedFidEntryValue.builder() | ||
PersistedFidEntry.builder() | ||
.setFirebaseInstallationId(fid) | ||
.setPersistedStatus(PersistedFid.PersistedStatus.REGISTER_ERROR) | ||
.setTokenCreationEpochInSecs(0) | ||
.setExpiresInSecs(0) | ||
.build()); | ||
throw new FirebaseInstallationsException( | ||
exception.getMessage(), FirebaseInstallationsException.Status.SDK_INTERNAL_ERROR); | ||
} | ||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ | |
|
||
/** | ||
* A layer that locally persists a few Firebase Installation attributes on top the Firebase | ||
* Installation backend API. | ||
* Installation API. | ||
*/ | ||
public class PersistedFid { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about calling this LocalStorage or FisStorage or FirebaseInstallationsStorage? |
||
// Status of each presisted fid entry | ||
|
@@ -63,7 +63,7 @@ public PersistedFid(@NonNull FirebaseApp firebaseApp) { | |
} | ||
|
||
@Nullable | ||
public synchronized PersistedFidEntryValue readPersistedFidEntryValue() { | ||
public synchronized PersistedFidEntry readPersistedFidEntryValue() { | ||
String fid = prefs.getString(getSharedPreferencesKey(FIREBASE_INSTALLATION_ID_KEY), null); | ||
int status = prefs.getInt(getSharedPreferencesKey(PERSISTED_STATUS_KEY), -1); | ||
String authToken = prefs.getString(getSharedPreferencesKey(AUTH_TOKEN_KEY), null); | ||
|
@@ -76,7 +76,7 @@ public synchronized PersistedFidEntryValue readPersistedFidEntryValue() { | |
return null; | ||
} | ||
|
||
return PersistedFidEntryValue.builder() | ||
return PersistedFidEntry.builder() | ||
.setFirebaseInstallationId(fid) | ||
.setPersistedStatus(PersistedStatus.values()[status]) | ||
.setAuthToken(authToken) | ||
|
@@ -88,7 +88,7 @@ public synchronized PersistedFidEntryValue readPersistedFidEntryValue() { | |
|
||
@NonNull | ||
public synchronized boolean insertOrUpdatePersistedFidEntry( | ||
@NonNull PersistedFidEntryValue entryValue) { | ||
@NonNull PersistedFidEntry entryValue) { | ||
SharedPreferences.Editor editor = prefs.edit(); | ||
editor.putString( | ||
getSharedPreferencesKey(FIREBASE_INSTALLATION_ID_KEY), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,11 +19,11 @@ | |
import com.google.auto.value.AutoValue; | ||
|
||
/** | ||
* This class represents a cache entry value in {@link PersistedFid}, which contains a Firebase | ||
* instance id and the cache status of this entry. | ||
* This class represents a persisted fid entry in {@link PersistedFid}, which contains a few | ||
* Firebase Installation attributes and the persisted status of this entry. | ||
*/ | ||
@AutoValue | ||
public abstract class PersistedFidEntryValue { | ||
public abstract class PersistedFidEntry { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please feel free to ignore this comment. |
||
|
||
@NonNull | ||
public abstract String getFirebaseInstallationId(); | ||
|
@@ -44,10 +44,12 @@ public abstract class PersistedFidEntryValue { | |
@NonNull | ||
public abstract Builder toBuilder(); | ||
|
||
/** Returns a default Builder object to create an InstallationResponse object */ | ||
/** Returns a default Builder object to create an PersistedFidEntry object */ | ||
@NonNull | ||
public static PersistedFidEntryValue.Builder builder() { | ||
return new AutoValue_PersistedFidEntryValue.Builder(); | ||
public static PersistedFidEntry.Builder builder() { | ||
return new AutoValue_PersistedFidEntry.Builder() | ||
.setTokenCreationEpochInSecs(0) | ||
.setExpiresInSecs(0); | ||
} | ||
|
||
@AutoValue.Builder | ||
|
@@ -71,6 +73,6 @@ public abstract static class Builder { | |
public abstract Builder setTokenCreationEpochInSecs(long value); | ||
|
||
@NonNull | ||
public abstract PersistedFidEntryValue build(); | ||
public abstract PersistedFidEntry build(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the difference between PersistedFid and PersistedFidEntry?