Skip to content

Commit 83f1bd0

Browse files
committed
Tests
1 parent 962b8df commit 83f1bd0

File tree

1 file changed

+63
-56
lines changed

1 file changed

+63
-56
lines changed

Parse/src/test/java/com/parse/ParseQueryTest.java

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import static org.hamcrest.CoreMatchers.instanceOf;
3030
import static org.junit.Assert.assertEquals;
31+
import static org.junit.Assert.assertFalse;
3132
import static org.junit.Assert.assertNotSame;
3233
import static org.junit.Assert.assertNull;
3334
import static org.junit.Assert.assertSame;
@@ -105,73 +106,56 @@ public void testSetUser() throws ParseException {
105106
}
106107

107108
@Test
108-
public void testQueriesOnlyRunOneNetworkConnection() throws ParseException {
109-
TestQueryController controller = new TestQueryController();
110-
ParseCorePlugins.getInstance().registerQueryController(controller);
109+
public void testMultipleQueries() throws ParseException {
110+
TestQueryController controller1 = new TestQueryController();
111+
TestQueryController controller2 = new TestQueryController();
111112

112-
TaskCompletionSource<Void> tcs = new TaskCompletionSource();
113-
controller.await(tcs.getTask());
113+
TaskCompletionSource<Void> tcs1 = new TaskCompletionSource<>();
114+
TaskCompletionSource<Void> tcs2 = new TaskCompletionSource<>();
115+
controller1.await(tcs1.getTask());
116+
controller2.await(tcs2.getTask());
114117

115118
ParseQuery<ParseObject> query = ParseQuery.getQuery("TestObject");
116119
query.setUser(new ParseUser());
117-
Task<Void> task = query.findInBackground().makeVoid();
118120

119-
try {
120-
query.find();
121-
fail("Should've thrown an exception.");
122-
} catch (RuntimeException e) {
123-
// do nothing
124-
}
121+
ParseCorePlugins.getInstance().registerQueryController(controller1);
122+
query.findInBackground();
123+
assertTrue(query.isRunning());
125124

126-
try {
127-
query.findInBackground(new FindCallback<ParseObject>() {
128-
@Override
129-
public void done(List<ParseObject> objects, ParseException e) {
130-
}
131-
});
132-
fail("Should've thrown an exception.");
133-
} catch (RuntimeException e) {
134-
// do nothing
135-
}
125+
ParseCorePlugins.getInstance().reset();
126+
ParseCorePlugins.getInstance().registerQueryController(controller2);
127+
query.countInBackground();
128+
assertTrue(query.isRunning());
136129

137-
try {
138-
query.count();
139-
fail("Should've thrown an exception.");
140-
} catch (RuntimeException e) {
141-
// do nothing
142-
}
130+
// Stop the first operation.
131+
tcs1.setResult(null);
132+
assertTrue(query.isRunning());
143133

144-
try {
145-
query.countInBackground(new CountCallback() {
146-
@Override
147-
public void done(int count, ParseException e) {
148-
}
149-
});
150-
fail("Should've thrown an exception.");
151-
} catch (RuntimeException e) {
152-
// do nothing
153-
}
134+
// Stop the second.
135+
tcs2.setResult(null);
136+
assertFalse(query.isRunning());
137+
}
154138

155-
try {
156-
query.get("abc");
157-
fail("Should've thrown an exception.");
158-
} catch (RuntimeException e) {
159-
// do nothing
160-
}
139+
@Test
140+
public void testMultipleQueriesWithInflightChanges() throws ParseException {
141+
Parse.enableLocalDatastore(null);
142+
TestQueryController controller = new TestQueryController();
143+
TaskCompletionSource<Void> tcs = new TaskCompletionSource<>();
144+
controller.await(tcs.getTask());
161145

162-
try {
163-
query.getInBackground("abc", new GetCallback<ParseObject>() {
164-
@Override
165-
public void done(ParseObject object, ParseException e) {
166-
}
167-
});
168-
fail("Should've thrown an exception.");
169-
} catch (RuntimeException e) {
170-
// do nothing
171-
}
146+
ParseQuery<ParseObject> query = ParseQuery.getQuery("TestObject");
147+
query.setUser(new ParseUser());
172148

173-
tcs.setResult(null);
174-
ParseTaskUtils.wait(task);
149+
ParseCorePlugins.getInstance().registerQueryController(controller);
150+
List<Task<Void>> tasks = Arrays.asList(
151+
query.fromNetwork().findInBackground().makeVoid(),
152+
query.fromLocalDatastore().findInBackground().makeVoid(),
153+
query.setLimit(10).findInBackground().makeVoid(),
154+
query.whereEqualTo("key", "value").countInBackground().makeVoid());
155+
assertTrue(query.isRunning());
156+
tcs.trySetResult(null);
157+
ParseTaskUtils.wait(Task.whenAll(tasks));
158+
assertFalse(query.isRunning());
175159
}
176160

177161
@Test
@@ -240,6 +224,29 @@ public void testCountWithCallbackLimit() throws ParseException {
240224
assertEquals(0, state.getValue().limit());
241225
}
242226

227+
@Test
228+
public void testIsRunning() throws ParseException {
229+
TestQueryController controller = new TestQueryController();
230+
ParseCorePlugins.getInstance().registerQueryController(controller);
231+
TaskCompletionSource<Void> tcs = new TaskCompletionSource<>();
232+
controller.await(tcs.getTask());
233+
234+
ParseQuery<ParseObject> query = ParseQuery.getQuery("TestObject");
235+
query.setUser(new ParseUser());
236+
assertFalse(query.isRunning());
237+
query.findInBackground();
238+
assertTrue(query.isRunning());
239+
tcs.setResult(null);
240+
assertFalse(query.isRunning());
241+
// Run another
242+
tcs = new TaskCompletionSource<>();
243+
controller.await(tcs.getTask());
244+
query.findInBackground();
245+
assertTrue(query.isRunning());
246+
query.cancel();
247+
assertFalse(query.isRunning());
248+
}
249+
243250
@Test
244251
public void testQueryCancellation() throws ParseException {
245252
TestQueryController controller = new TestQueryController();

0 commit comments

Comments
 (0)