CachedCurrentInstallationController implements ParseCurrentInstallationController { @Override public Task<ParseInstallation> getAsync() { synchronized (mutex) { if (currentInstallation != null) { return Task.forResult(currentInstallation); } } return taskQueue.enqueue(new Continuation<Void, Task<ParseInstallation>>() { @Override public Task<ParseInstallation> then(Task<Void> toAwait) { return toAwait.continueWithTask(new Continuation<Void, Task<ParseInstallation>>() { @Override public Task<ParseInstallation> then(Task<Void> task) { synchronized (mutex) { if (currentInstallation != null) { return Task.forResult(currentInstallation); } } return store.getAsync().continueWith(new Continuation<ParseInstallation, ParseInstallation>() { @Override public ParseInstallation then(Task<ParseInstallation> task) { 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()); } }); } }); } CachedCurrentInstallationController(
            ParseObjectStore<ParseInstallation> store, InstallationId installationId); @Override Task<Void> setAsync(final ParseInstallation installation); @Override Task<ParseInstallation> getAsync(); @Override Task<Boolean> existsAsync(); @Override void clearFromMemory(); @Override void clearFromDisk(); @Override boolean isCurrent(ParseInstallation installation);  }
@Test public void testGetAsyncFromMemory() throws Exception { CachedCurrentInstallationController controller = new CachedCurrentInstallationController(null, null); ParseInstallation currentInstallationInMemory = new ParseInstallation(); controller.currentInstallation = currentInstallationInMemory; ParseInstallation currentInstallation = ParseTaskUtils.wait(controller.getAsync()); assertSame(currentInstallationInMemory, currentInstallation); }
@Test public void testGetAsyncFromStore() throws Exception { InstallationId installationId = mock(InstallationId.class); ParseObjectStore<ParseInstallation> store = mock(ParseObjectStore.class); ParseInstallation installation = mock(ParseInstallation.class); when(installation.getInstallationId()).thenReturn("testInstallationId"); when(store.getAsync()).thenReturn(Task.forResult(installation)); CachedCurrentInstallationController controller = new CachedCurrentInstallationController(store, installationId); ParseInstallation currentInstallation = ParseTaskUtils.wait(controller.getAsync()); verify(store, times(1)).getAsync(); verify(installationId, times(1)).set("testInstallationId"); assertSame(installation, controller.currentInstallation); assertSame(installation, currentInstallation); }
@Test public void testGetAsyncWithNoInstallation() throws Exception { InstallationId installationId = mock(InstallationId.class); when(installationId.get()).thenReturn("testInstallationId"); ParseObjectStore<ParseInstallation> store = mock(ParseObjectStore.class); when(store.getAsync()).thenReturn(Task.<ParseInstallation>forResult(null)); CachedCurrentInstallationController controller = new CachedCurrentInstallationController(store, installationId); ParseInstallation currentInstallation = ParseTaskUtils.wait(controller.getAsync()); verify(store, times(1)).getAsync(); assertSame(controller.currentInstallation, currentInstallation); assertEquals("testInstallationId", currentInstallation.getInstallationId()); assertEquals("android", currentInstallation.get(KEY_DEVICE_TYPE)); }
@Test public void testGetAsyncWithNoInstallationRaceCondition() throws ParseException { InstallationId installationId = mock(InstallationId.class); when(installationId.get()).thenReturn("testInstallationId"); ParseObjectStore<ParseInstallation> store = mock(ParseObjectStore.class); TaskCompletionSource<ParseInstallation> tcs = new TaskCompletionSource(); when(store.getAsync()).thenReturn(tcs.getTask()); 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); assertEquals("testInstallationId", installationA.getInstallationId()); assertEquals("android", installationA.get(KEY_DEVICE_TYPE)); }