Bug 1272817 - Return early if file is empty + regression test. r=sebastian

MozReview-Commit-ID: FPdDr2t1wEy

--HG--
extra : rebase_source : 4fd7975262ac3af23d0a5f6b921cbe7a0863760e
This commit is contained in:
Michael Comella 2016-05-13 18:14:25 -07:00
Родитель cee6eaeeaa
Коммит 9b29ebd0df
2 изменённых файлов: 14 добавлений и 0 удалений

Просмотреть файл

@ -150,6 +150,12 @@ public class TelemetryJSONFilePingStore implements TelemetryPingStore {
* @return the JSON object from the given file or null if there is an error.
*/
private JSONObject lockAndReadJSONFromFile(final File file) {
// lockAndReadFileAndCloseStream doesn't handle file size of 0.
if (file.length() == 0) {
Log.w(LOGTAG, "Unexpected empty file: " + file.getName() + ". Ignoring");
return null;
}
final FileInputStream inputStream;
try {
inputStream = new FileInputStream(file);

Просмотреть файл

@ -120,6 +120,14 @@ public class TestTelemetryJSONFilePingStore {
}
}
@Test // regression test: bug 1272817
public void testGetAllPingsHandlesEmptyFiles() throws Exception {
final int expectedPingCount = 3;
writeTestPingsToStore(expectedPingCount, "whatever");
assertTrue("Empty file is created", testStore.getPingFile(getDocID()).createNewFile());
assertEquals("Returned pings only contains valid files", expectedPingCount, testStore.getAllPings().size());
}
@Test
public void testMaybePrunePingsDoesNothingIfAtMax() throws Exception {
final int pingCount = TelemetryJSONFilePingStore.MAX_PING_COUNT;