@Suppress public void testUrlWithNoAppLinkData() { String testNoAppLinkUrlString = "https://fb.me/732873156764191_no_app_link"; Uri testNoAppLinkUrl = Uri.parse(testNoAppLinkUrlString); try { executeResolverOnBlockerThread(new FacebookAppLinkResolver(), testNoAppLinkUrl); getTestBlocker().waitForSignals(1); assertNotNull(resolveTask); Task<AppLink> singleUrlResolveTask = (Task<AppLink>)resolveTask; assertTrue(singleUrlResolveTask.isCompleted() && !singleUrlResolveTask.isCancelled() && !singleUrlResolveTask.isFaulted()); AppLink appLink = singleUrlResolveTask.getResult(); assertNull(appLink); } catch (Exception e) { // Forcing the test to fail with details assertNull(e); } }
/* package */ Task<Void> open(final SQLiteOpenHelper helper) { synchronized (currentLock) { current = current.continueWith(new Continuation<Void, SQLiteDatabase>() { @Override public SQLiteDatabase then(Task<Void> task) { // get*Database() is synchronous and calls through SQLiteOpenHelper#onCreate, onUpdate, // etc. return (openFlags & SQLiteDatabase.OPEN_READONLY) == SQLiteDatabase.OPEN_READONLY ? helper.getReadableDatabase() : helper.getWritableDatabase(); } }, dbExecutor).continueWithTask(new Continuation<SQLiteDatabase, Task<Void>>() { @Override public Task<Void> then(Task<SQLiteDatabase> task) { db = task.getResult(); return task.makeVoid(); } }, Task.BACKGROUND_EXECUTOR); // We want to jump off the dbExecutor return current; } }
task.waitForCompletion(); if (task.isFaulted()) { Exception error = task.getError(); if (error instanceof ParseException) { throw (ParseException) error; } else if (task.isCancelled()) { throw new RuntimeException(new CancellationException()); return task.getResult(); } catch (InterruptedException e) { throw new RuntimeException(e);
@Override public <T extends ParseObject> Task<List<T>> findAsync(ParseQuery.State<T> state, ParseUser user, Task<Void> cancellationToken) { final AtomicBoolean cancelled = new AtomicBoolean(false); cancellationToken.continueWith(new Continuation<Void, Void>() { @Override public Void then(Task<Void> task) { cancelled.set(true); return null; } }); return await(Task.<Void>forResult(null).continueWithTask(new Continuation<Void, Task<Void>>() { @Override public Task<Void> then(Task<Void> task) { if (cancelled.get()) { return Task.cancelled(); } return task; } })).cast(); }
@Test public void testSubscribeInBackgroundFail() throws Exception { ParsePushChannelsController controller = mock(ParsePushChannelsController.class); ParseException exception = new ParseException(ParseException.OTHER_CAUSE, "error"); when(controller.subscribeInBackground(anyString())).thenReturn(Task.<Void>forError(exception)); ParseCorePlugins.getInstance().registerPushChannelsController(controller); Task<Void> pushTask = ParsePush.subscribeInBackground("test"); pushTask.waitForCompletion(); verify(controller, times(1)).subscribeInBackground("test"); assertTrue(pushTask.isFaulted()); assertSame(exception, pushTask.getError()); }
@Override public AppLink then(Task<Map<Uri, AppLink>> resolveUrisTask) throws Exception { return resolveUrisTask.getResult().get(uri); } });
@Override public Task<Void> then(Task<Void> ignore) { synchronized (tasksLock) { // It might be better to return an aggregate error here. for (Task<Void> task : tasks) { if (task.isFaulted() || task.isCancelled()) { return task; } } tasks.clear(); return Task.forResult(null); } } });
@Test public void testSaveAsyncAlreadyCancelled() throws Exception { ParseHttpClient restClient = mock(ParseHttpClient.class); ParseFileController controller = new ParseFileController(restClient, null); ParseFile.State state = new ParseFile.State.Builder().build(); Task<Void> cancellationToken = Task.cancelled(); Task<ParseFile.State> task = controller.saveAsync(state, (byte[]) null, null, null, cancellationToken); task.waitForCompletion(); verify(restClient, times(0)).execute(any(ParseHttpRequest.class)); assertTrue(task.isCancelled()); }
@Test public void testCallFunctionInBackgroundFailure() throws Exception { // TODO(mengyan): Remove once we no longer rely on retry logic. ParseRequest.setDefaultInitialRetryDelay(1L); ParseHttpClient restClient = mock(ParseHttpClient.class); when(restClient.execute(any(ParseHttpRequest.class))).thenThrow(new IOException()); ParseCloudCodeController controller = new ParseCloudCodeController(restClient); Task<String> cloudCodeTask = controller.callFunctionInBackground("test", new HashMap<String, Object>(), "sessionToken"); // Do not use ParseTaskUtils.wait() since we do not want to throw the exception cloudCodeTask.waitForCompletion(); // TODO(mengyan): Abstract out command runner so we don't have to account for retries. verify(restClient, times(5)).execute(any(ParseHttpRequest.class)); assertTrue(cloudCodeTask.isFaulted()); Exception error = cloudCodeTask.getError(); assertThat(error, instanceOf(ParseException.class)); assertEquals(ParseException.CONNECTION_FAILED, ((ParseException) error).getCode()); }
@Override public T then(Task<List<T>> task) throws Exception { if (task.isFaulted()) { throw task.getError(); } if (task.getResult() != null && task.getResult().size() > 0) { return task.getResult().get(0); } throw new ParseException(ParseException.OBJECT_NOT_FOUND, "no results found for query"); } });
@Override public Task<JSONObject> then(Task<JSONObject> task) { synchronized (taskQueueSyncLock) { pendingEventuallyTasks.remove(uuid); uuidToOperationSet.remove(uuid); uuidToEventuallyPin.remove(uuid); } Exception error = task.getError(); if (error != null) { tcs.trySetError(error); } else if (task.isCancelled()) { tcs.trySetCancelled(); } else { tcs.trySetResult(task.getResult()); } return tcs.getTask(); } });
@Override public Task<T> then(Task<T> task) { final T object = task.getResult(); if (object == null) { return task; } return Task.whenAll(Arrays.asList( from.deleteAsync(), to.setAsync(object) )).continueWith(new Continuation<Void, T>() { @Override public T then(Task<Void> task) { return object; } }); } });
@Override public Task<Void> then(Task<Void> task) { synchronized (mutex) { if (task.isFaulted() || task.isCancelled()) { removeAuthData(authType); restoreAnonymity(oldAnonymousData); return task; } return synchronizeAuthDataAsync(authType); } } });
@Override public Task<ParseConfig> then(Task<String> task) { final String sessionToken = task.getResult(); return toAwait.continueWithTask(new Continuation<Void, Task<ParseConfig>>() { @Override public Task<ParseConfig> then(Task<Void> task) { return getConfigController().getAsync(sessionToken); } }); } });
@Test public void testCallFunctionAsync() throws Exception { ParseCloudCodeController controller = mockParseCloudCodeControllerWithResponse("result"); ParseCorePlugins.getInstance().registerCloudCodeController(controller); Map<String, Object> parameters = new HashMap<>(); parameters.put("key1", Arrays.asList(1, 2, 3)); parameters.put("key2", "value1"); Task cloudCodeTask = ParseCloud.callFunctionInBackground("name", parameters); ParseTaskUtils.wait(cloudCodeTask); verify(controller, times(1)).callFunctionInBackground(eq("name"), eq(parameters), isNull(String.class)); assertTrue(cloudCodeTask.isCompleted()); assertNull(cloudCodeTask.getError()); assertThat(cloudCodeTask.getResult(), instanceOf(String.class)); assertEquals("result", cloudCodeTask.getResult()); }
private ParseCurrentConfigController mockParseCurrentConfigControllerWithResponse( final ParseConfig result) { ParseCurrentConfigController controller = mock(ParseCurrentConfigController.class); when(controller.getCurrentConfigAsync()) .thenReturn(Task.forResult(result)); return controller; }