Skip to content

[Firebase Segmentation] Add custom installation id cache layer and tests for it. #524

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 20 commits into from
Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
1962431
Add type arguments in StorageTaskManager (#517)
schmidt-sebastian Jun 13, 2019
b69b2c8
Output artifact list during local publishing. (#515)
allisonbm92 Jun 13, 2019
8881325
Implement Firebase segmentation SDK device local cache
diwu-arete Jun 13, 2019
771d23c
fix functions (#523)
VinayGuthal Jun 14, 2019
dc3f410
Set test type to release only in CI. (#522)
vkryachko Jun 14, 2019
864748f
[Firebase Segmentation] Add custom installation id cache layer and te…
diwu-arete Jun 14, 2019
0a3ebf6
Add test for updating cache
diwu-arete Jun 14, 2019
2d158ed
Switch to use SQLiteOpenHelper
diwu-arete Jun 15, 2019
18d4e7e
Minor fix to error message to match the admin sdk. (#525)
rsgowman Jun 17, 2019
d65c21c
Added clean task to smoke tests. (#527)
allisonbm92 Jun 17, 2019
3e2cc89
Update deps to post-androidx gms versions. (#526)
vkryachko Jun 17, 2019
f118d39
Switch to use SharedPreferences from SQLite.
diwu-arete Jun 17, 2019
4da5d31
Change the cache class to be singleton
diwu-arete Jun 18, 2019
c1f7644
Copy firebase-firestore-ktx dependencies on firestore into its own su…
Jun 18, 2019
d1ff0ec
Wrap shared pref commit in a async task.
diwu-arete Jun 18, 2019
2c5102c
Merge branch 'master' of github.com:firebase/firebase-android-sdk int…
diwu-arete Jun 18, 2019
41fbfee
Address comments
diwu-arete Jun 18, 2019
097ff36
Bump firestore version for release (#530)
vkryachko Jun 18, 2019
5fd2fa0
Google format fix
diwu-arete Jun 18, 2019
e950003
Merge branch 'master' of github.com:firebase/firebase-android-sdk int…
diwu-arete Jun 18, 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
11 changes: 8 additions & 3 deletions firebase-segmentation/firebase-segmentation.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,17 @@ dependencies {
implementation 'androidx.multidex:multidex:2.0.0'
implementation 'com.google.android.gms:play-services-tasks:16.0.1'

compileOnly "com.google.auto.value:auto-value-annotations:1.6.5"
annotationProcessor "com.google.auto.value:auto-value:1.6.2"

testImplementation 'androidx.test:core:1.2.0'
testImplementation 'junit:junit:4.12'
testImplementation "org.robolectric:robolectric:$robolectricVersion"

androidTestImplementation 'androidx.annotation:annotation:1.1.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation "androidx.annotation:annotation:1.1.0"
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation "com.google.truth:truth:$googleTruthVersion"
androidTestImplementation 'junit:junit:4.12'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.segmentation;

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

import androidx.test.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

/** Instrumented tests for {@link CustomInstallationIdCache} */
@RunWith(AndroidJUnit4.class)
public class CustomInstallationIdCacheTest {

private FirebaseApp firebaseApp0;
private FirebaseApp firebaseApp1;
private CustomInstallationIdCache cache;

@Before
public void setUp() {
FirebaseApp.clearInstancesForTest();
firebaseApp0 =
FirebaseApp.initializeApp(
InstrumentationRegistry.getContext(),
new FirebaseOptions.Builder().setApplicationId("1:123456789:android:abcdef").build());
firebaseApp1 =
FirebaseApp.initializeApp(
InstrumentationRegistry.getContext(),
new FirebaseOptions.Builder().setApplicationId("1:987654321:android:abcdef").build(),
"firebase_app_1");
cache = CustomInstallationIdCache.getInstance();
}

@After
public void cleanUp() throws Exception {
Tasks.await(cache.clearAll());
}

@Test
public void testReadCacheEntry_Null() {
assertNull(cache.readCacheEntryValue(firebaseApp0));
assertNull(cache.readCacheEntryValue(firebaseApp1));
}

@Test
public void testUpdateAndReadCacheEntry() throws Exception {
assertTrue(
Tasks.await(
cache.insertOrUpdateCacheEntry(
firebaseApp0,
CustomInstallationIdCacheEntryValue.create(
"123456", "cAAAAAAAAAA", CustomInstallationIdCache.CacheStatus.PENDING))));
CustomInstallationIdCacheEntryValue entryValue = cache.readCacheEntryValue(firebaseApp0);
assertThat(entryValue.getCustomInstallationId()).isEqualTo("123456");
assertThat(entryValue.getFirebaseInstanceId()).isEqualTo("cAAAAAAAAAA");
assertThat(entryValue.getCacheStatus())
.isEqualTo(CustomInstallationIdCache.CacheStatus.PENDING);
assertNull(cache.readCacheEntryValue(firebaseApp1));

assertTrue(
Tasks.await(
cache.insertOrUpdateCacheEntry(
firebaseApp0,
CustomInstallationIdCacheEntryValue.create(
"123456", "cAAAAAAAAAA", CustomInstallationIdCache.CacheStatus.SYNCED))));
entryValue = cache.readCacheEntryValue(firebaseApp0);
assertThat(entryValue.getCustomInstallationId()).isEqualTo("123456");
assertThat(entryValue.getFirebaseInstanceId()).isEqualTo("cAAAAAAAAAA");
assertThat(entryValue.getCacheStatus()).isEqualTo(CustomInstallationIdCache.CacheStatus.SYNCED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.segmentation;

import android.content.Context;
import android.content.SharedPreferences;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.google.android.gms.common.util.Strings;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskCompletionSource;
import com.google.firebase.FirebaseApp;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

class CustomInstallationIdCache {

// Status of each cache entry
// NOTE: never change the ordinal of the enum values because the enum values are stored in cache
// as their ordinal numbers.
enum CacheStatus {
// Cache entry is synced to Firebase backend
SYNCED,
// Cache entry is waiting for Firebase backend response or pending internal retry for retryable
// errors.
PENDING,
// Cache entry is not accepted by Firebase backend.
ERROR,
}

private static final String SHARED_PREFS_NAME = "CustomInstallationIdCache";

private static final String CUSTOM_INSTALLATION_ID_KEY = "Cid";
private static final String INSTANCE_ID_KEY = "Iid";
private static final String CACHE_STATUS_KEY = "Status";

private static CustomInstallationIdCache singleton = null;
private final Executor ioExecuter;
private final SharedPreferences prefs;

static CustomInstallationIdCache getInstance() {
Copy link
Member

Choose a reason for hiding this comment

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

Due to the way SharedPrefs are implemented, this class does not have to be a singleton, as multiple instances of sharedPrefs that point to the same file share the same thread-safe in-memory backing structure. As a side-effect of doing it your interface will become much simpler, as you won't have to pass FirebaseApp around to be able to read/write to the cache, e.g. something along these lines:

class Cache {
  // note: injecting direct deps only https://github.com/google/guice/wiki/InjectOnlyDirectDependencies
  Cache(String persistenceKey, SharedPreferences prefs) {}
  Value read();
  void write(Value value);
}

When your SDK initializes it can then construct this cache as new Cache(app.getPersistenceKey(), context.getSharedPreferences()); Note that Cache is completely decoupled from FirebaseApp, so there is not need to initialize FirebaseApp during tests, which leads to faster more isolated tests. wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. For passing in a String argument and SharedPreference argument from the caller, I don't like the idea, because I don't want the caller to pass in a random string or a random shared pref name.

  2. For singleton or non-singleton, I don't have a strong opinion. But the singleton mode can centralize management of all FirebaseApps in one shared pref file, which allows to clearAll() for example, and it doesn't hurt the test too much.

Copy link
Member

Choose a reason for hiding this comment

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

I don't feel strongly re 1.

  1. you can centralize all FirebaseApps in one shared pref file even if you don't use a singleton, as calling getSharedPreferences() with the same name+mode returns the same instance, clearAll can also be implemented as a static method that takes a Context. It's ultimately up to your team to decide the best approach here, but I feel like singleton does not provide many benefits due to the nature of how sharedPrefs work. If you do decide to use a singleton, pls make getInstance() thread-safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changing to thread-safe singleton.

If we find singleton pattern doesn't work well later, we can always change it.

if (singleton == null) {
singleton = new CustomInstallationIdCache();
}
return singleton;
}

private CustomInstallationIdCache() {
// Since different FirebaseApp in the same Android application should have the same application
// context and same dir path, so that use the context of the default FirebaseApp to create the
// shared preferences.
prefs =
FirebaseApp.getInstance()
.getApplicationContext()
.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);

ioExecuter = Executors.newFixedThreadPool(2);
}

@Nullable
synchronized CustomInstallationIdCacheEntryValue readCacheEntryValue(FirebaseApp firebaseApp) {
String cid =
prefs.getString(getSharedPreferencesKey(firebaseApp, CUSTOM_INSTALLATION_ID_KEY), null);
String iid = prefs.getString(getSharedPreferencesKey(firebaseApp, INSTANCE_ID_KEY), null);
int status = prefs.getInt(getSharedPreferencesKey(firebaseApp, CACHE_STATUS_KEY), -1);

if (Strings.isEmptyOrWhitespace(cid) || Strings.isEmptyOrWhitespace(iid) || status == -1) {
return null;
}

return CustomInstallationIdCacheEntryValue.create(cid, iid, CacheStatus.values()[status]);
}

synchronized Task<Boolean> insertOrUpdateCacheEntry(
FirebaseApp firebaseApp, CustomInstallationIdCacheEntryValue entryValue) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString(
getSharedPreferencesKey(firebaseApp, CUSTOM_INSTALLATION_ID_KEY),
entryValue.getCustomInstallationId());
editor.putString(
getSharedPreferencesKey(firebaseApp, INSTANCE_ID_KEY), entryValue.getFirebaseInstanceId());
editor.putInt(
getSharedPreferencesKey(firebaseApp, CACHE_STATUS_KEY),
entryValue.getCacheStatus().ordinal());
return commitSharedPreferencesEditAsync(editor);
}

synchronized Task<Boolean> clear(FirebaseApp firebaseApp) {
SharedPreferences.Editor editor = prefs.edit();
editor.remove(getSharedPreferencesKey(firebaseApp, CUSTOM_INSTALLATION_ID_KEY));
editor.remove(getSharedPreferencesKey(firebaseApp, INSTANCE_ID_KEY));
editor.remove(getSharedPreferencesKey(firebaseApp, CACHE_STATUS_KEY));
return commitSharedPreferencesEditAsync(editor);
}

@VisibleForTesting
synchronized Task<Boolean> clearAll() {
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
return commitSharedPreferencesEditAsync(editor);
}

private static String getSharedPreferencesKey(FirebaseApp firebaseApp, String key) {
return String.format("%s|%s", firebaseApp.getPersistenceKey(), key);
}

private Task<Boolean> commitSharedPreferencesEditAsync(SharedPreferences.Editor editor) {
TaskCompletionSource<Boolean> result = new TaskCompletionSource<Boolean>();
ioExecuter.execute(
new Runnable() {
@Override
public void run() {
result.setResult(editor.commit());
}
});
return result.getTask();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.segmentation;

import com.google.auto.value.AutoValue;
import com.google.firebase.segmentation.CustomInstallationIdCache.CacheStatus;

/**
* This class represents a cache entry value in {@link CustomInstallationIdCache}, which contains a
* Firebase instance id, a custom installation id and the cache status of this entry.
*/
@AutoValue
abstract class CustomInstallationIdCacheEntryValue {
abstract String getCustomInstallationId();

abstract String getFirebaseInstanceId();

abstract CacheStatus getCacheStatus();

static CustomInstallationIdCacheEntryValue create(
String customInstallationId, String firebaseInstanceId, CacheStatus cacheStatus) {
return new AutoValue_CustomInstallationIdCacheEntryValue(
customInstallationId, firebaseInstanceId, cacheStatus);
}
}