Skip to content

Commit 10fe142

Browse files
authored
Move log and non-fatal persistence to disk write worker (#6133)
Other metadata persistence is already on the right work from the previous pr #6120 - Move log and non-fatal persistence to disk write worker - Refine unit tests - Remove unused functions and interface
1 parent c928402 commit 10fe142

File tree

6 files changed

+89
-131
lines changed

6 files changed

+89
-131
lines changed

firebase-crashlytics/src/androidTest/java/com/google/firebase/crashlytics/internal/common/CrashlyticsCoreTest.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ public void whenAvailable(
7070

7171
private CrashlyticsCore crashlyticsCore;
7272
private BreadcrumbSource mockBreadcrumbSource;
73+
private static final CrashlyticsWorker commonWorker =
74+
new CrashlyticsWorker(TestOnlyExecutors.background());
75+
private static final CrashlyticsWorker diskWriteWorker =
76+
new CrashlyticsWorker(TestOnlyExecutors.background());
7377

7478
@Override
7579
protected void setUp() throws Exception {
@@ -93,7 +97,7 @@ public void testCustomAttributes() throws Exception {
9397

9498
final String id = "id012345";
9599
crashlyticsCore.setUserId(id);
96-
crashlyticsCore.commonWorker.await();
100+
commonWorker.await();
97101
assertEquals(id, metadata.getUserId());
98102

99103
final StringBuffer idBuffer = new StringBuffer(id);
@@ -104,13 +108,13 @@ public void testCustomAttributes() throws Exception {
104108
final String superLongId = longId + "more chars";
105109

106110
crashlyticsCore.setUserId(superLongId);
107-
crashlyticsCore.commonWorker.await();
111+
commonWorker.await();
108112
assertEquals(longId, metadata.getUserId());
109113

110114
final String key1 = "key1";
111115
final String value1 = "value1";
112116
crashlyticsCore.setCustomKey(key1, value1);
113-
crashlyticsCore.commonWorker.await();
117+
commonWorker.await();
114118
assertEquals(value1, metadata.getCustomKeys().get(key1));
115119

116120
// Adding an existing key with the same value should return false
@@ -124,7 +128,7 @@ public void testCustomAttributes() throws Exception {
124128

125129
// test truncation of custom keys and attributes
126130
crashlyticsCore.setCustomKey(superLongId, superLongValue);
127-
crashlyticsCore.commonWorker.await();
131+
commonWorker.await();
128132
assertNull(metadata.getCustomKeys().get(superLongId));
129133
assertEquals(longValue, metadata.getCustomKeys().get(longId));
130134

@@ -133,28 +137,28 @@ public void testCustomAttributes() throws Exception {
133137
final String key = "key" + i;
134138
final String value = "value" + i;
135139
crashlyticsCore.setCustomKey(key, value);
136-
crashlyticsCore.commonWorker.await();
140+
commonWorker.await();
137141
assertEquals(value, metadata.getCustomKeys().get(key));
138142
}
139143
// should be full now, extra key, value pairs will be dropped.
140144
final String key = "new key";
141145
crashlyticsCore.setCustomKey(key, "some value");
142-
crashlyticsCore.commonWorker.await();
146+
commonWorker.await();
143147
assertFalse(metadata.getCustomKeys().containsKey(key));
144148

145149
// should be able to update existing keys
146150
crashlyticsCore.setCustomKey(key1, longValue);
147-
crashlyticsCore.commonWorker.await();
151+
commonWorker.await();
148152
assertEquals(longValue, metadata.getCustomKeys().get(key1));
149153

150154
// when we set a key to null, it should still exist with an empty value
151155
crashlyticsCore.setCustomKey(key1, null);
152-
crashlyticsCore.commonWorker.await();
156+
commonWorker.await();
153157
assertEquals("", metadata.getCustomKeys().get(key1));
154158

155159
// keys and values are trimmed.
156160
crashlyticsCore.setCustomKey(" " + key1 + " ", " " + longValue + " ");
157-
crashlyticsCore.commonWorker.await();
161+
commonWorker.await();
158162
assertTrue(metadata.getCustomKeys().containsKey(key1));
159163
assertEquals(longValue, metadata.getCustomKeys().get(key1));
160164
}
@@ -205,7 +209,7 @@ public void testBulkCustomKeys() throws Exception {
205209
keysAndValues.put(intKey, String.valueOf(intValue));
206210

207211
crashlyticsCore.setCustomKeys(keysAndValues);
208-
crashlyticsCore.commonWorker.await();
212+
commonWorker.await();
209213

210214
assertEquals(stringValue, metadata.getCustomKeys().get(stringKey));
211215
assertEquals(trimmedValue, metadata.getCustomKeys().get(trimmedKey));
@@ -226,7 +230,7 @@ public void testBulkCustomKeys() throws Exception {
226230
addlKeysAndValues.put(key, value);
227231
}
228232
crashlyticsCore.setCustomKeys(addlKeysAndValues);
229-
crashlyticsCore.commonWorker.await();
233+
commonWorker.await();
230234

231235
// Ensure all keys have been set
232236
assertEquals(UserMetadata.MAX_ATTRIBUTES, metadata.getCustomKeys().size(), DELTA);
@@ -244,7 +248,7 @@ public void testBulkCustomKeys() throws Exception {
244248
extraKeysAndValues.put(key, value);
245249
}
246250
crashlyticsCore.setCustomKeys(extraKeysAndValues);
247-
crashlyticsCore.commonWorker.await();
251+
commonWorker.await();
248252

249253
// Make sure the extra keys were not added
250254
for (int i = UserMetadata.MAX_ATTRIBUTES; i < UserMetadata.MAX_ATTRIBUTES + 10; ++i) {
@@ -270,7 +274,7 @@ public void testBulkCustomKeys() throws Exception {
270274
updatedKeysAndValues.put(intKey, String.valueOf(updatedIntValue));
271275

272276
crashlyticsCore.setCustomKeys(updatedKeysAndValues);
273-
crashlyticsCore.commonWorker.await();
277+
commonWorker.await();
274278

275279
assertEquals(updatedStringValue, metadata.getCustomKeys().get(stringKey));
276280
assertFalse(Boolean.parseBoolean(metadata.getCustomKeys().get(booleanKey)));
@@ -443,8 +447,8 @@ CrashlyticsCore build(Context context) {
443447
new FileStore(context),
444448
mock(CrashlyticsAppQualitySessionsSubscriber.class),
445449
mock(RemoteConfigDeferredProxy.class),
446-
new CrashlyticsWorker(TestOnlyExecutors.background()),
447-
new CrashlyticsWorker(TestOnlyExecutors.background()));
450+
commonWorker,
451+
diskWriteWorker);
448452
return crashlyticsCore;
449453
}
450454
}

firebase-crashlytics/src/androidTest/java/com/google/firebase/crashlytics/internal/common/SessionReportingCoordinatorTest.java

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333

3434
import com.google.android.gms.tasks.Task;
3535
import com.google.android.gms.tasks.Tasks;
36+
import com.google.firebase.concurrent.TestOnlyExecutors;
37+
import com.google.firebase.crashlytics.internal.CrashlyticsWorker;
3638
import com.google.firebase.crashlytics.internal.metadata.LogFileManager;
3739
import com.google.firebase.crashlytics.internal.metadata.UserMetadata;
3840
import com.google.firebase.crashlytics.internal.model.CrashlyticsReport;
@@ -69,6 +71,8 @@ public class SessionReportingCoordinatorTest {
6971

7072
private SessionReportingCoordinator reportingCoordinator;
7173

74+
private CrashlyticsWorker diskWriteWorker = new CrashlyticsWorker(TestOnlyExecutors.background());
75+
7276
@Before
7377
public void setUp() {
7478
MockitoAnnotations.initMocks(this);
@@ -80,7 +84,8 @@ public void setUp() {
8084
reportSender,
8185
logFileManager,
8286
reportMetadata,
83-
idManager);
87+
idManager,
88+
diskWriteWorker);
8489
}
8590

8691
@Test
@@ -116,7 +121,8 @@ public void testFatalEvent_persistsHighPriorityEventWithAllThreadsForSessionId()
116121
}
117122

118123
@Test
119-
public void testNonFatalEvent_persistsNormalPriorityEventWithoutAllThreadsForSessionId() {
124+
public void testNonFatalEvent_persistsNormalPriorityEventWithoutAllThreadsForSessionId()
125+
throws Exception {
120126
final String eventType = "error";
121127
final String sessionId = "testSessionId";
122128
final long timestamp = System.currentTimeMillis();
@@ -126,6 +132,8 @@ public void testNonFatalEvent_persistsNormalPriorityEventWithoutAllThreadsForSes
126132
reportingCoordinator.onBeginSession(sessionId, timestamp);
127133
reportingCoordinator.persistNonFatalEvent(mockException, mockThread, sessionId, timestamp);
128134

135+
diskWriteWorker.await();
136+
129137
final boolean expectedAllThreads = false;
130138
final boolean expectedHighPriority = false;
131139

@@ -136,7 +144,7 @@ public void testNonFatalEvent_persistsNormalPriorityEventWithoutAllThreadsForSes
136144
}
137145

138146
@Test
139-
public void testNonFatalEvent_addsLogsToEvent() {
147+
public void testNonFatalEvent_addsLogsToEvent() throws Exception {
140148
long timestamp = System.currentTimeMillis();
141149

142150
mockEventInteractions();
@@ -149,14 +157,16 @@ public void testNonFatalEvent_addsLogsToEvent() {
149157
reportingCoordinator.onBeginSession(sessionId, timestamp);
150158
reportingCoordinator.persistNonFatalEvent(mockException, mockThread, sessionId, timestamp);
151159

160+
diskWriteWorker.await();
161+
152162
verify(mockEventBuilder)
153163
.setLog(CrashlyticsReport.Session.Event.Log.builder().setContent(testLog).build());
154164
verify(mockEventBuilder).build();
155165
verify(logFileManager, never()).clearLog();
156166
}
157167

158168
@Test
159-
public void testNonFatalEvent_addsNoLogsToEventWhenNoneAvailable() {
169+
public void testNonFatalEvent_addsNoLogsToEventWhenNoneAvailable() throws Exception {
160170
long timestamp = System.currentTimeMillis();
161171

162172
mockEventInteractions();
@@ -168,6 +178,8 @@ public void testNonFatalEvent_addsNoLogsToEventWhenNoneAvailable() {
168178
reportingCoordinator.onBeginSession(sessionId, timestamp);
169179
reportingCoordinator.persistNonFatalEvent(mockException, mockThread, sessionId, timestamp);
170180

181+
diskWriteWorker.await();
182+
171183
verify(mockEventBuilder, never()).setLog(any(CrashlyticsReport.Session.Event.Log.class));
172184
verify(mockEventBuilder).build();
173185
verify(logFileManager, never()).clearLog();
@@ -212,7 +224,7 @@ public void testFatalEvent_addsNoLogsToEventWhenNoneAvailable() {
212224
}
213225

214226
@Test
215-
public void testNonFatalEvent_addsSortedKeysToEvent() {
227+
public void testNonFatalEvent_addsSortedKeysToEvent() throws Exception {
216228
final long timestamp = System.currentTimeMillis();
217229

218230
mockEventInteractions();
@@ -243,6 +255,8 @@ public void testNonFatalEvent_addsSortedKeysToEvent() {
243255
reportingCoordinator.onBeginSession(sessionId, timestamp);
244256
reportingCoordinator.persistNonFatalEvent(mockException, mockThread, sessionId, timestamp);
245257

258+
diskWriteWorker.await();
259+
246260
verify(mockEventAppBuilder).setCustomAttributes(expectedCustomAttributes);
247261
verify(mockEventAppBuilder).setInternalKeys(expectedCustomAttributes);
248262
verify(mockEventAppBuilder).build();
@@ -252,7 +266,7 @@ public void testNonFatalEvent_addsSortedKeysToEvent() {
252266
}
253267

254268
@Test
255-
public void testNonFatalEvent_addsNoKeysToEventWhenNoneAvailable() {
269+
public void testNonFatalEvent_addsNoKeysToEventWhenNoneAvailable() throws Exception {
256270
final long timestamp = System.currentTimeMillis();
257271

258272
mockEventInteractions();
@@ -266,6 +280,8 @@ public void testNonFatalEvent_addsNoKeysToEventWhenNoneAvailable() {
266280
reportingCoordinator.onBeginSession(sessionId, timestamp);
267281
reportingCoordinator.persistNonFatalEvent(mockException, mockThread, sessionId, timestamp);
268282

283+
diskWriteWorker.await();
284+
269285
verify(mockEventAppBuilder, never()).setCustomAttributes(anyList());
270286
verify(mockEventAppBuilder, never()).build();
271287
verify(mockEventBuilder, never()).setApp(mockEventApp);
@@ -274,7 +290,7 @@ public void testNonFatalEvent_addsNoKeysToEventWhenNoneAvailable() {
274290
}
275291

276292
@Test
277-
public void testNonFatalEvent_addRolloutsEvent() {
293+
public void testNonFatalEvent_addRolloutsEvent() throws Exception {
278294
long timestamp = System.currentTimeMillis();
279295
String sessionId = "testSessionId";
280296
mockEventInteractions();
@@ -287,6 +303,8 @@ public void testNonFatalEvent_addRolloutsEvent() {
287303
reportingCoordinator.onBeginSession(sessionId, timestamp);
288304
reportingCoordinator.persistNonFatalEvent(mockException, mockThread, sessionId, timestamp);
289305

306+
diskWriteWorker.await();
307+
290308
verify(mockEventAppBuilder, never()).setCustomAttributes(anyList());
291309
verify(mockEventAppBuilder, never()).build();
292310
verify(mockEventBuilder, never()).setApp(mockEventApp);
@@ -417,35 +435,6 @@ public void testFatalEvent_addRolloutsToEvent() {
417435
verify(mockEventBuilder, times(2)).build();
418436
}
419437

420-
@Test
421-
public void onLog_writesToLogFileManager() {
422-
long timestamp = System.currentTimeMillis();
423-
String log = "this is a log";
424-
425-
reportingCoordinator.onLog(timestamp, log);
426-
427-
verify(logFileManager).writeToLog(timestamp, log);
428-
}
429-
430-
@Test
431-
public void onCustomKey_writesToReportMetadata() {
432-
final String key = "key";
433-
final String value = "value";
434-
435-
reportingCoordinator.onCustomKey(key, value);
436-
437-
verify(reportMetadata).setCustomKey(key, value);
438-
}
439-
440-
@Test
441-
public void onUserId_writesUserToReportMetadata() {
442-
final String userId = "testUser";
443-
444-
reportingCoordinator.onUserId(userId);
445-
446-
verify(reportMetadata).setUserId(userId);
447-
}
448-
449438
@Test
450439
public void testFinalizeSessionWithNativeEvent_createsCrashlyticsReportWithNativePayload() {
451440
byte[] testBytes = {0, 2, 20, 10};

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/common/CrashlyticsCore.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ public class CrashlyticsCore {
9696

9797
private final RemoteConfigDeferredProxy remoteConfigDeferredProxy;
9898

99-
@VisibleForTesting final CrashlyticsWorker commonWorker;
100-
@VisibleForTesting final CrashlyticsWorker diskWriteWorker;
99+
private final CrashlyticsWorker commonWorker;
100+
private final CrashlyticsWorker diskWriteWorker;
101101

102102
// region Constructors
103103

@@ -172,7 +172,8 @@ public boolean onPreExecute(AppData appData, SettingsProvider settingsProvider)
172172
stackTraceTrimmingStrategy,
173173
settingsProvider,
174174
onDemandCounter,
175-
sessionsSubscriber);
175+
sessionsSubscriber,
176+
diskWriteWorker);
176177

177178
controller =
178179
new CrashlyticsController(
@@ -337,7 +338,11 @@ public void logException(@NonNull Throwable throwable) {
337338
*/
338339
public void log(final String msg) {
339340
final long timestamp = System.currentTimeMillis() - startTime;
340-
commonWorker.submit(() -> controller.writeToLog(timestamp, msg));
341+
// queuing up on common worker to maintain the order
342+
commonWorker.submit(
343+
() -> {
344+
diskWriteWorker.submit(() -> controller.writeToLog(timestamp, msg));
345+
});
341346
}
342347

343348
public void setUserId(String identifier) {

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/common/CrashlyticsLifecycleEvents.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)