Skip to content

Commit c19946b

Browse files
committed
Revert "ci failing : fixed spotless violations"
This reverts commit 8d8a00d
1 parent 8d8a00d commit c19946b

File tree

2 files changed

+99
-105
lines changed

2 files changed

+99
-105
lines changed

parse/src/main/java/com/parse/ParseFileController.java

+72-72
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ParseFileController {
2525
private final File cachePath;
2626
private final List<String> currentlyDownloadedFilesNames = new ArrayList<>();
2727

28+
2829
private ParseHttpClient fileClient;
2930

3031
public ParseFileController(ParseHttpClient restClient, File cachePath) {
@@ -171,80 +172,79 @@ public Task<File> fetchAsync(
171172
if (cancellationToken != null && cancellationToken.isCancelled()) {
172173
return Task.cancelled();
173174
}
174-
return Task.call(
175-
() -> {
176-
final File cacheFile = getCacheFile(state);
177-
178-
synchronized (lock) {
179-
if (currentlyDownloadedFilesNames.contains(state.name())) {
180-
while (currentlyDownloadedFilesNames.contains(state.name())) {
181-
lock.wait();
182-
}
183-
}
175+
return Task.call(() -> {
176+
final File cacheFile = getCacheFile(state);
184177

185-
if (cacheFile.exists()) {
186-
return cacheFile;
187-
} else {
188-
currentlyDownloadedFilesNames.add(state.name());
189-
}
178+
synchronized (lock) {
179+
if (currentlyDownloadedFilesNames.contains(state.name())) {
180+
while (currentlyDownloadedFilesNames.contains(state.name())) {
181+
lock.wait();
190182
}
183+
}
191184

192-
try {
193-
if (cancellationToken != null && cancellationToken.isCancelled()) {
194-
throw new CancellationException();
195-
}
196-
197-
// Generate the temp file path for caching ParseFile content based on
198-
// ParseFile's url
199-
// The reason we do not write to the cacheFile directly is because there
200-
// is no way we can
201-
// verify if a cacheFile is complete or not. If download is interrupted
202-
// in the middle, next
203-
// time when we download the ParseFile, since cacheFile has already
204-
// existed, we will return
205-
// this incomplete cacheFile
206-
final File tempFile = getTempFile(state);
207-
208-
// network
209-
final ParseFileRequest request =
210-
new ParseFileRequest(
211-
ParseHttpRequest.Method.GET, state.url(), tempFile);
212-
213-
// We do not need to delete the temp file since we always try to
214-
// overwrite it
215-
Task<Void> downloadTask =
216-
request.executeAsync(
217-
fileClient(),
218-
null,
219-
downloadProgressCallback,
220-
cancellationToken);
221-
ParseTaskUtils.wait(downloadTask);
222-
223-
// If the top-level task was cancelled, don't
224-
// actually set the data -- just move on.
225-
if (cancellationToken != null && cancellationToken.isCancelled()) {
226-
throw new CancellationException();
227-
}
228-
if (downloadTask.isFaulted()) {
229-
ParseFileUtils.deleteQuietly(tempFile);
230-
throw new RuntimeException(downloadTask.getError());
231-
}
232-
233-
// Since we give the cacheFile pointer to
234-
// developers, it is not safe to guarantee
235-
// cacheFile always does not exist here, so it is
236-
// better to delete it manually,
237-
// otherwise moveFile may throw an exception.
238-
ParseFileUtils.deleteQuietly(cacheFile);
239-
ParseFileUtils.moveFile(tempFile, cacheFile);
240-
return cacheFile;
241-
} finally {
242-
synchronized (lock) {
243-
currentlyDownloadedFilesNames.remove(state.name());
244-
lock.notifyAll();
245-
}
246-
}
247-
},
248-
ParseExecutors.io());
185+
if (cacheFile.exists()) {
186+
return cacheFile;
187+
} else {
188+
currentlyDownloadedFilesNames.add(state.name());
189+
}
190+
}
191+
192+
try {
193+
if (cancellationToken != null && cancellationToken.isCancelled()) {
194+
throw new CancellationException();
195+
}
196+
197+
// Generate the temp file path for caching ParseFile content based on
198+
// ParseFile's url
199+
// The reason we do not write to the cacheFile directly is because there
200+
// is no way we can
201+
// verify if a cacheFile is complete or not. If download is interrupted
202+
// in the middle, next
203+
// time when we download the ParseFile, since cacheFile has already
204+
// existed, we will return
205+
// this incomplete cacheFile
206+
final File tempFile = getTempFile(state);
207+
208+
// network
209+
final ParseFileRequest request =
210+
new ParseFileRequest(
211+
ParseHttpRequest.Method.GET, state.url(), tempFile);
212+
213+
// We do not need to delete the temp file since we always try to
214+
// overwrite it
215+
Task<Void> downloadTask = request.executeAsync(
216+
fileClient(),
217+
null,
218+
downloadProgressCallback,
219+
cancellationToken
220+
);
221+
ParseTaskUtils.wait(downloadTask);
222+
223+
// If the top-level task was cancelled, don't
224+
// actually set the data -- just move on.
225+
if (cancellationToken != null && cancellationToken.isCancelled()) {
226+
throw new CancellationException();
227+
}
228+
if (downloadTask.isFaulted()) {
229+
ParseFileUtils.deleteQuietly(tempFile);
230+
throw new RuntimeException(downloadTask.getError());
231+
}
232+
233+
// Since we give the cacheFile pointer to
234+
// developers, it is not safe to guarantee
235+
// cacheFile always does not exist here, so it is
236+
// better to delete it manually,
237+
// otherwise moveFile may throw an exception.
238+
ParseFileUtils.deleteQuietly(cacheFile);
239+
ParseFileUtils.moveFile(tempFile, cacheFile);
240+
return cacheFile;
241+
} finally {
242+
synchronized (lock) {
243+
currentlyDownloadedFilesNames.remove(state.name());
244+
lock.notifyAll();
245+
}
246+
}
247+
248+
}, ParseExecutors.io());
249249
}
250250
}

parse/src/test/java/com/parse/ParseFileControllerTest.java

+27-33
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.net.URL;
3131
import java.util.concurrent.CountDownLatch;
3232
import java.util.concurrent.atomic.AtomicReference;
33+
3334
import org.json.JSONObject;
3435
import org.junit.After;
3536
import org.junit.Before;
@@ -343,15 +344,16 @@ public void testFetchAsyncFailure() throws Exception {
343344
assertFalse(controller.getTempFile(state).exists());
344345
}
345346

347+
346348
@Test
347349
public void testFetchAsyncConcurrentCallsSuccess() throws Exception {
348350
byte[] data = "hello".getBytes();
349351
ParseHttpResponse mockResponse =
350-
new ParseHttpResponse.Builder()
351-
.setStatusCode(200)
352-
.setTotalSize((long) data.length)
353-
.setContent(new ByteArrayInputStream(data))
354-
.build();
352+
new ParseHttpResponse.Builder()
353+
.setStatusCode(200)
354+
.setTotalSize((long) data.length)
355+
.setContent(new ByteArrayInputStream(data))
356+
.build();
355357

356358
ParseHttpClient fileClient = mock(ParseHttpClient.class);
357359
when(fileClient.execute(any(ParseHttpRequest.class))).thenReturn(mockResponse);
@@ -364,39 +366,31 @@ public void testFetchAsyncConcurrentCallsSuccess() throws Exception {
364366
AtomicReference<File> file1Ref = new AtomicReference<>();
365367
AtomicReference<File> file2Ref = new AtomicReference<>();
366368

367-
new Thread(
368-
() -> {
369-
try {
370-
file1Ref.set(
371-
ParseTaskUtils.wait(
372-
controller.fetchAsync(state, null, null, null)));
373-
} catch (ParseException e) {
374-
throw new RuntimeException(e);
375-
} finally {
376-
countDownLatch.countDown();
377-
}
378-
})
379-
.start();
380-
381-
new Thread(
382-
() -> {
383-
try {
384-
file2Ref.set(
385-
ParseTaskUtils.wait(
386-
controller.fetchAsync(state, null, null, null)));
387-
} catch (ParseException e) {
388-
throw new RuntimeException(e);
389-
} finally {
390-
countDownLatch.countDown();
391-
}
392-
})
393-
.start();
369+
new Thread(() -> {
370+
try {
371+
file1Ref.set(ParseTaskUtils.wait(controller.fetchAsync(state, null, null, null)));
372+
} catch (ParseException e) {
373+
throw new RuntimeException(e);
374+
} finally {
375+
countDownLatch.countDown();
376+
}
377+
}).start();
378+
379+
new Thread(() -> {
380+
try {
381+
file2Ref.set(ParseTaskUtils.wait(controller.fetchAsync(state, null, null, null)));
382+
} catch (ParseException e) {
383+
throw new RuntimeException(e);
384+
} finally {
385+
countDownLatch.countDown();
386+
}
387+
}).start();
394388

395389
countDownLatch.await();
396390

397391
File result1 = file1Ref.get();
398392
File result2 = file2Ref.get();
399-
393+
400394
assertTrue(result1.exists());
401395
assertEquals("hello", ParseFileUtils.readFileToString(result1, "UTF-8"));
402396
assertTrue(result2.exists());

0 commit comments

Comments
 (0)