Skip to content

Commit d07ec25

Browse files
authored
Http client in Firebase Segmentation SDK to call backend service. (#573)
* Implement Firebase segmentation SDK device local cache * [Firebase Segmentation] Add custom installation id cache layer and tests for it. * Add test for updating cache * Switch to use SQLiteOpenHelper * Switch to use SharedPreferences from SQLite. * Change the cache class to be singleton * Wrap shared pref commit in a async task. * Address comments * Google format fix * Replace some deprecated code. * Package refactor * nit * nit * Add the state machine of updating custom installation id in the local cache and update to Firebase Segmentation backend. CL also contains unit tests. (The http client is not implemented yet.) * minor format fix * Address comments #1 * Http client in Firebase Segmentation SDK to call backend service. * Revert unintentional change * Fix connected device test * Fix connected device test * 1. Add a few annotations to make java code Kotlin friendly 2. Some fixes for the http request format * Fix java format * Fix API version * Change the segmentation API implementation to synchronous and put the entire synchronous code block in async task. * Fix a async getResult race issue. * OkHttpClient -> HttpsUrlConnection * Use gzip for compressing content and fix ourput stream memory leak risk. * Addressed a few comments
1 parent b4e11e9 commit d07ec25

File tree

8 files changed

+309
-232
lines changed

8 files changed

+309
-232
lines changed

firebase-segmentation/firebase-segmentation.gradle

+4-48
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,12 @@
1414

1515
plugins {
1616
id 'firebase-library'
17-
id 'com.google.protobuf'
1817
}
1918

2019
firebaseLibrary {
2120
testLab.enabled = true
2221
}
2322

24-
protobuf {
25-
// Configure the protoc executable
26-
protoc {
27-
// Download from repositories
28-
artifact = 'com.google.protobuf:protoc:3.4.0'
29-
}
30-
plugins {
31-
grpc {
32-
artifact = 'io.grpc:protoc-gen-grpc-java:1.12.0'
33-
}
34-
javalite {
35-
// The codegen for lite comes as a separate artifact
36-
artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
37-
}
38-
}
39-
generateProtoTasks {
40-
all().each { task ->
41-
task.builtins {
42-
// In most cases you don't need the full Java output
43-
// if you use the lite output.
44-
remove java
45-
}
46-
task.plugins {
47-
grpc {
48-
option 'lite'
49-
}
50-
javalite {}
51-
}
52-
}
53-
}
54-
}
55-
5623
android {
5724
compileSdkVersion project.targetSdkVersion
5825

@@ -63,13 +30,6 @@ android {
6330
versionName version
6431
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
6532
}
66-
sourceSets {
67-
main {
68-
proto {
69-
srcDir 'src/main/proto'
70-
}
71-
}
72-
}
7333
compileOptions {
7434
sourceCompatibility JavaVersion.VERSION_1_8
7535
targetCompatibility JavaVersion.VERSION_1_8
@@ -83,18 +43,14 @@ android {
8343

8444
dependencies {
8545
implementation project(':firebase-common')
86-
implementation project(':protolite-well-known-types')
8746

8847
implementation('com.google.firebase:firebase-iid:17.0.3') {
8948
exclude group: "com.google.firebase", module: "firebase-common"
9049
}
91-
implementation 'io.grpc:grpc-stub:1.21.0'
92-
implementation 'io.grpc:grpc-protobuf-lite:1.21.0'
93-
implementation 'io.grpc:grpc-okhttp:1.21.0'
50+
9451
implementation 'androidx.appcompat:appcompat:1.0.2'
95-
implementation 'androidx.multidex:multidex:2.0.0'
96-
implementation 'com.google.android.gms:play-services-tasks:16.0.1'
97-
implementation 'com.squareup.okhttp:okhttp:2.7.5'
52+
implementation 'androidx.multidex:multidex:2.0.1'
53+
implementation 'com.google.android.gms:play-services-tasks:17.0.0'
9854

9955
compileOnly "com.google.auto.value:auto-value-annotations:1.6.5"
10056
annotationProcessor "com.google.auto.value:auto-value:1.6.2"
@@ -103,7 +59,7 @@ dependencies {
10359
testImplementation 'junit:junit:4.12'
10460
testImplementation "org.robolectric:robolectric:$robolectricVersion"
10561

106-
androidTestImplementation "androidx.annotation:annotation:1.1.0"
62+
androidTestImplementation "androidx.annotation:annotation:1.0.0"
10763
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
10864
androidTestImplementation 'androidx.test:rules:1.2.0'
10965
androidTestImplementation 'androidx.test:runner:1.2.0'

firebase-segmentation/src/androidTest/java/com/google/firebase/segmentation/FirebaseSegmentationInstrumentedTest.java

+24-21
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,24 @@ public void setUp() {
6969
firebaseApp =
7070
FirebaseApp.initializeApp(
7171
ApplicationProvider.getApplicationContext(),
72-
new FirebaseOptions.Builder().setApplicationId("1:123456789:android:abcdef").build());
72+
new FirebaseOptions.Builder()
73+
.setApplicationId("1:123456789:android:abcdef")
74+
.setApiKey("api_key")
75+
.build());
7376
actualCache = new CustomInstallationIdCache(firebaseApp);
7477

7578
when(backendClientReturnsOk.updateCustomInstallationId(
79+
anyLong(), anyString(), anyString(), anyString(), anyString()))
80+
.thenReturn(SegmentationServiceClient.Code.OK);
81+
when(backendClientReturnsOk.clearCustomInstallationId(
7682
anyLong(), anyString(), anyString(), anyString()))
77-
.thenReturn(Tasks.forResult(SegmentationServiceClient.Code.OK));
78-
when(backendClientReturnsOk.clearCustomInstallationId(anyLong(), anyString(), anyString()))
79-
.thenReturn(Tasks.forResult(SegmentationServiceClient.Code.OK));
83+
.thenReturn(SegmentationServiceClient.Code.OK);
8084
when(backendClientReturnsError.updateCustomInstallationId(
85+
anyLong(), anyString(), anyString(), anyString(), anyString()))
86+
.thenReturn(SegmentationServiceClient.Code.SERVER_ERROR);
87+
when(backendClientReturnsError.clearCustomInstallationId(
8188
anyLong(), anyString(), anyString(), anyString()))
82-
.thenReturn(Tasks.forResult(SegmentationServiceClient.Code.SERVER_INTERNAL_ERROR));
83-
when(backendClientReturnsError.clearCustomInstallationId(anyLong(), anyString(), anyString()))
84-
.thenReturn(Tasks.forResult(SegmentationServiceClient.Code.SERVER_INTERNAL_ERROR));
89+
.thenReturn(SegmentationServiceClient.Code.SERVER_ERROR);
8590
when(firebaseInstanceId.getInstanceId())
8691
.thenReturn(
8792
Tasks.forResult(
@@ -98,13 +103,13 @@ public String getToken() {
98103
return "iid_token";
99104
}
100105
}));
101-
when(cacheReturnsError.insertOrUpdateCacheEntry(any())).thenReturn(Tasks.forResult(false));
106+
when(cacheReturnsError.insertOrUpdateCacheEntry(any())).thenReturn(false);
102107
when(cacheReturnsError.readCacheEntryValue()).thenReturn(null);
103108
}
104109

105110
@After
106111
public void cleanUp() throws Exception {
107-
Tasks.await(actualCache.clear());
112+
actualCache.clear();
108113
}
109114

110115
@Test
@@ -165,12 +170,11 @@ public void testUpdateCustomInstallationId_CacheError_BackendOk() throws Interru
165170

166171
@Test
167172
public void testClearCustomInstallationId_CacheOk_BackendOk() throws Exception {
168-
Tasks.await(
169-
actualCache.insertOrUpdateCacheEntry(
170-
CustomInstallationIdCacheEntryValue.create(
171-
CUSTOM_INSTALLATION_ID,
172-
FIREBASE_INSTANCE_ID,
173-
CustomInstallationIdCache.CacheStatus.SYNCED)));
173+
actualCache.insertOrUpdateCacheEntry(
174+
CustomInstallationIdCacheEntryValue.create(
175+
CUSTOM_INSTALLATION_ID,
176+
FIREBASE_INSTANCE_ID,
177+
CustomInstallationIdCache.CacheStatus.SYNCED));
174178
FirebaseSegmentation firebaseSegmentation =
175179
new FirebaseSegmentation(
176180
firebaseApp, firebaseInstanceId, actualCache, backendClientReturnsOk);
@@ -183,12 +187,11 @@ public void testClearCustomInstallationId_CacheOk_BackendOk() throws Exception {
183187

184188
@Test
185189
public void testClearCustomInstallationId_CacheOk_BackendError() throws Exception {
186-
Tasks.await(
187-
actualCache.insertOrUpdateCacheEntry(
188-
CustomInstallationIdCacheEntryValue.create(
189-
CUSTOM_INSTALLATION_ID,
190-
FIREBASE_INSTANCE_ID,
191-
CustomInstallationIdCache.CacheStatus.SYNCED)));
190+
actualCache.insertOrUpdateCacheEntry(
191+
CustomInstallationIdCacheEntryValue.create(
192+
CUSTOM_INSTALLATION_ID,
193+
FIREBASE_INSTANCE_ID,
194+
CustomInstallationIdCache.CacheStatus.SYNCED));
192195
FirebaseSegmentation firebaseSegmentation =
193196
new FirebaseSegmentation(
194197
firebaseApp, firebaseInstanceId, actualCache, backendClientReturnsError);

firebase-segmentation/src/androidTest/java/com/google/firebase/segmentation/local/CustomInstallationIdCacheTest.java

+8-13
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import androidx.test.core.app.ApplicationProvider;
2222
import androidx.test.ext.junit.runners.AndroidJUnit4;
23-
import com.google.android.gms.tasks.Tasks;
2423
import com.google.firebase.FirebaseApp;
2524
import com.google.firebase.FirebaseOptions;
2625
import org.junit.After;
@@ -55,8 +54,8 @@ public void setUp() {
5554

5655
@After
5756
public void cleanUp() throws Exception {
58-
Tasks.await(cache0.clear());
59-
Tasks.await(cache1.clear());
57+
cache0.clear();
58+
cache1.clear();
6059
}
6160

6261
@Test
@@ -68,12 +67,9 @@ public void testReadCacheEntry_Null() {
6867
@Test
6968
public void testUpdateAndReadCacheEntry() throws Exception {
7069
assertTrue(
71-
Tasks.await(
72-
cache0.insertOrUpdateCacheEntry(
73-
CustomInstallationIdCacheEntryValue.create(
74-
"123456",
75-
"cAAAAAAAAAA",
76-
CustomInstallationIdCache.CacheStatus.PENDING_UPDATE))));
70+
cache0.insertOrUpdateCacheEntry(
71+
CustomInstallationIdCacheEntryValue.create(
72+
"123456", "cAAAAAAAAAA", CustomInstallationIdCache.CacheStatus.PENDING_UPDATE)));
7773
CustomInstallationIdCacheEntryValue entryValue = cache0.readCacheEntryValue();
7874
assertThat(entryValue.getCustomInstallationId()).isEqualTo("123456");
7975
assertThat(entryValue.getFirebaseInstanceId()).isEqualTo("cAAAAAAAAAA");
@@ -82,10 +78,9 @@ public void testUpdateAndReadCacheEntry() throws Exception {
8278
assertNull(cache1.readCacheEntryValue());
8379

8480
assertTrue(
85-
Tasks.await(
86-
cache0.insertOrUpdateCacheEntry(
87-
CustomInstallationIdCacheEntryValue.create(
88-
"123456", "cAAAAAAAAAA", CustomInstallationIdCache.CacheStatus.SYNCED))));
81+
cache0.insertOrUpdateCacheEntry(
82+
CustomInstallationIdCacheEntryValue.create(
83+
"123456", "cAAAAAAAAAA", CustomInstallationIdCache.CacheStatus.SYNCED)));
8984
entryValue = cache0.readCacheEntryValue();
9085
assertThat(entryValue.getCustomInstallationId()).isEqualTo("123456");
9186
assertThat(entryValue.getFirebaseInstanceId()).isEqualTo("cAAAAAAAAAA");

0 commit comments

Comments
 (0)