Skip to content

Commit 31da68c

Browse files
committed
Remvoe ParseFile in-memory cache
1 parent 95db225 commit 31da68c

File tree

2 files changed

+54
-20
lines changed

2 files changed

+54
-20
lines changed

Parse/src/main/java/com/parse/ParseFile.java

+3-15
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ public Task<Void> then(Task<State> task) throws Exception {
327327
state = task.getResult();
328328
// Since we have successfully uploaded the file, we do not need to hold the file pointer
329329
// anymore.
330+
data = null;
330331
file = null;
331332
return task.makeVoid();
332333
}
@@ -424,31 +425,18 @@ public byte[] getData() throws ParseException {
424425
* @return A Task that is resolved when the data has been fetched.
425426
*/
426427
public Task<byte[]> getDataInBackground(final ProgressCallback progressCallback) {
427-
// If data is already available, just return immediately.
428-
if (data != null) {
429-
// in-memory
430-
return Task.forResult(data);
431-
}
432-
433428
final Task<Void>.TaskCompletionSource cts = Task.create();
434429
currentTasks.add(cts);
435430

436431
return taskQueue.enqueue(new Continuation<Void, Task<byte[]>>() {
437432
@Override
438433
public Task<byte[]> then(Task<Void> toAwait) throws Exception {
439-
// If data is already available, just return immediately.
440-
if (data != null) {
441-
// in-memory
442-
return Task.forResult(data);
443-
}
444-
445434
return fetchInBackground(progressCallback, toAwait, cts.getTask()).onSuccess(new Continuation<File, byte[]>() {
446435
@Override
447436
public byte[] then(Task<File> task) throws Exception {
448437
File file = task.getResult();
449438
try {
450-
data = ParseFileUtils.readFileToByteArray(file);
451-
return data;
439+
return ParseFileUtils.readFileToByteArray(file);
452440
} catch (IOException e) {
453441
// do nothing
454442
}
@@ -550,7 +538,7 @@ public Task<File> then(Task<File> task) throws Exception {
550538
* @return A Task that is resolved when the data has been fetched.
551539
*/
552540
public Task<File> getFileInBackground() {
553-
return getFileInBackground((ProgressCallback)null);
541+
return getFileInBackground((ProgressCallback) null);
554542
}
555543

556544
/**

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

+51-5
Original file line numberDiff line numberDiff line change
@@ -58,31 +58,29 @@ public void testConstructor() throws Exception {
5858
String contentType = "content_type";
5959
File file = temporaryFolder.newFile(name);
6060

61+
// TODO(mengyan): After we have proper staging strategy, we should verify the staging file's
62+
// content is the same with the original file.
63+
6164
ParseFile parseFile = new ParseFile(name, data, contentType);
6265
assertEquals("name", parseFile.getName());
63-
assertEquals("hello", new String(parseFile.getData()));
6466
assertEquals("content_type", parseFile.getState().mimeType());
6567
assertTrue(parseFile.isDirty());
6668

6769
parseFile = new ParseFile(data);
6870
assertEquals("file", parseFile.getName()); // Default
69-
assertEquals("hello", new String(parseFile.getData()));
7071
assertEquals(null, parseFile.getState().mimeType());
7172
assertTrue(parseFile.isDirty());
7273

7374
parseFile = new ParseFile(name, data);
7475
assertEquals("name", parseFile.getName());
75-
assertEquals("hello", new String(parseFile.getData()));
7676
assertEquals(null, parseFile.getState().mimeType());
7777
assertTrue(parseFile.isDirty());
7878

7979
parseFile = new ParseFile(data, contentType);
8080
assertEquals("file", parseFile.getName()); // Default
81-
assertEquals("hello", new String(parseFile.getData()));
8281
assertEquals("content_type", parseFile.getState().mimeType());
8382
assertTrue(parseFile.isDirty());
8483

85-
// TODO(mengyan): Test file pointer in ParseFile when we have proper stage strategy
8684
parseFile = new ParseFile(file);
8785
assertEquals(name, parseFile.getName()); // Default
8886
assertEquals(null, parseFile.getState().mimeType());
@@ -291,6 +289,22 @@ public void testGetDataAsyncSuccess() throws Exception {
291289
assertEquals(url, stateCaptor.getValue().url());
292290
// Verify the data we get is correct
293291
assertArrayEquals(content.getBytes(), data);
292+
293+
// Make sure we always get the data from network
294+
byte[] dataAgain = ParseTaskUtils.wait(parseFile.getDataInBackground());
295+
296+
// Verify controller get the correct data
297+
ArgumentCaptor<ParseFile.State> stateCaptorAgain =
298+
ArgumentCaptor.forClass(ParseFile.State.class);
299+
verify(controller, times(2)).fetchAsync(
300+
stateCaptorAgain.capture(),
301+
anyString(),
302+
any(ProgressCallback.class),
303+
Matchers.<Task<Void>>any()
304+
);
305+
assertEquals(url, stateCaptorAgain.getValue().url());
306+
// Verify the data we get is correct
307+
assertArrayEquals(content.getBytes(), dataAgain);
294308
}
295309

296310
@Test
@@ -325,6 +339,22 @@ public void testGetDataStreamAsyncSuccess() throws Exception {
325339
assertEquals(url, stateCaptor.getValue().url());
326340
// Verify the data we get is correct
327341
assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(dataStream));
342+
343+
// Make sure we always get the data from network
344+
InputStream dataStreamAgain = ParseTaskUtils.wait(parseFile.getDataStreamInBackground());
345+
346+
// Verify controller get the correct data
347+
ArgumentCaptor<ParseFile.State> stateCaptorAgain =
348+
ArgumentCaptor.forClass(ParseFile.State.class);
349+
verify(controller, times(2)).fetchAsync(
350+
stateCaptorAgain.capture(),
351+
anyString(),
352+
any(ProgressCallback.class),
353+
Matchers.<Task<Void>>any()
354+
);
355+
assertEquals(url, stateCaptorAgain.getValue().url());
356+
// Verify the data we get is correct
357+
assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(dataStreamAgain));
328358
}
329359

330360
@Test
@@ -359,6 +389,22 @@ public void testGetFileAsyncSuccess() throws Exception {
359389
assertEquals(url, stateCaptor.getValue().url());
360390
// Verify the data we get is correct
361391
assertArrayEquals(content.getBytes(), ParseFileUtils.readFileToByteArray(fetchedFile));
392+
393+
// Make sure we always get the data from network
394+
File fetchedFileAgain = ParseTaskUtils.wait(parseFile.getFileInBackground());
395+
396+
// Verify controller get the correct data
397+
ArgumentCaptor<ParseFile.State> stateCaptorAgain =
398+
ArgumentCaptor.forClass(ParseFile.State.class);
399+
verify(controller, times(2)).fetchAsync(
400+
stateCaptorAgain.capture(),
401+
anyString(),
402+
any(ProgressCallback.class),
403+
Matchers.<Task<Void>>any()
404+
);
405+
assertEquals(url, stateCaptorAgain.getValue().url());
406+
// Verify the data we get is correct
407+
assertArrayEquals(content.getBytes(), ParseFileUtils.readFileToByteArray(fetchedFileAgain));
362408
}
363409

364410
//endregion

0 commit comments

Comments
 (0)