Skip to content

Fix: Multiple "current" ParseInstallation instances being created #208

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 1 commit into from
Oct 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,10 @@ public Task<Void> then(Task<Void> task) throws Exception {

@Override
public Task<ParseInstallation> getAsync() {
final ParseInstallation cachedCurrent;
synchronized (mutex) {
cachedCurrent = currentInstallation;
}

if (cachedCurrent != null) {
return Task.forResult(cachedCurrent);
if (currentInstallation != null) {
return Task.forResult(currentInstallation);
}
}

return taskQueue.enqueue(new Continuation<Void, Task<ParseInstallation>>() {
Expand All @@ -85,26 +82,32 @@ public Task<ParseInstallation> then(Task<Void> toAwait) throws Exception {
return toAwait.continueWithTask(new Continuation<Void, Task<ParseInstallation>>() {
@Override
public Task<ParseInstallation> then(Task<Void> task) throws Exception {
return store.getAsync();
}
}).continueWith(new Continuation<ParseInstallation, ParseInstallation>() {
@Override
public ParseInstallation then(Task<ParseInstallation> task) throws Exception {
ParseInstallation current = task.getResult();
if (current == null) {
current = ParseObject.create(ParseInstallation.class);
current.updateDeviceInfo(installationId);
} else {
installationId.set(current.getInstallationId());
PLog.v(TAG, "Successfully deserialized Installation object");
}

synchronized (mutex) {
currentInstallation = current;
if (currentInstallation != null) {
return Task.forResult(currentInstallation);
}
}
return current;

return store.getAsync().continueWith(new Continuation<ParseInstallation, ParseInstallation>() {
@Override
public ParseInstallation then(Task<ParseInstallation> task) throws Exception {
ParseInstallation current = task.getResult();
if (current == null) {
current = ParseObject.create(ParseInstallation.class);
current.updateDeviceInfo(installationId);
} else {
installationId.set(current.getInstallationId());
PLog.v(TAG, "Successfully deserialized Installation object");
}

synchronized (mutex) {
currentInstallation = current;
}
return current;
}
}, ParseExecutors.io());
}
}, ParseExecutors.io());
});
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,35 @@ public void testGetAsyncWithNoInstallation() throws Exception {
assertEquals("android", currentInstallation.get(KEY_DEVICE_TYPE));
}

@Test
public void testGetAsyncWithNoInstallationRaceCondition() throws ParseException {
// Mock installationId
InstallationId installationId = mock(InstallationId.class);
when(installationId.get()).thenReturn("testInstallationId");
//noinspection unchecked
ParseObjectStore<ParseInstallation> store = mock(ParseObjectStore.class);
Task<ParseInstallation>.TaskCompletionSource tcs = Task.create();
when(store.getAsync()).thenReturn(tcs.getTask());

// Create test controller
CachedCurrentInstallationController controller =
new CachedCurrentInstallationController(store, installationId);

Task<ParseInstallation> taskA = controller.getAsync();
Task<ParseInstallation> taskB = controller.getAsync();

tcs.setResult(null);
ParseInstallation installationA = ParseTaskUtils.wait(taskA);
ParseInstallation installationB = ParseTaskUtils.wait(taskB);

verify(store, times(1)).getAsync();
assertSame(controller.currentInstallation, installationA);
assertSame(controller.currentInstallation, installationB);
// Make sure device info is updated
assertEquals("testInstallationId", installationA.getInstallationId());
assertEquals("android", installationA.get(KEY_DEVICE_TYPE));
}

//endregion

//region testExistsAsync
Expand Down