use of com.microsoft.appcenter.persistence.Persistence.PersistenceException in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceAndroidTest method putNormalLogCloseToMaxSizeKeepsCritical.
@Test
public void putNormalLogCloseToMaxSizeKeepsCritical() throws PersistenceException {
/* Initialize database persistence. */
DatabasePersistence persistence = new DatabasePersistence(sContext);
assertTrue(persistence.setMaxStorageSize(MAX_STORAGE_SIZE_IN_BYTES));
/* Set a mock log serializer. */
LogSerializer logSerializer = new DefaultLogSerializer();
logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
persistence.setLogSerializer(logSerializer);
try {
/* Generate some logs that will be kept as they are critical. */
List<Log> expectedLogs = new ArrayList<>();
int someLogCount = 3;
for (int i = 0; i < someLogCount; i++) {
MockLog log = AndroidTestUtils.generateMockLog();
persistence.putLog(log, "test-p1", CRITICAL);
expectedLogs.add(log);
}
assertEquals(someLogCount, persistence.countLogs("test-p1"));
/* Generate some normal priority logs that will be evicted. */
for (int i = 0; i < 20; i++) {
persistence.putLog(AndroidTestUtils.generateMockLog(), "test-p1", NORMAL);
}
/*
* Generate a normal priority log that is so large that will fail but not discard
* critical logs.
*/
LogWithProperties log = AndroidTestUtils.generateMockLog();
int size = 30 * 1024;
Map<String, String> properties = new HashMap<>();
properties.put("key", generateString(size, 'x'));
log.setProperties(properties);
try {
persistence.putLog(log, "test-p1", NORMAL);
fail("Expected persistence exception");
} catch (PersistenceException ignore) {
}
/* Get logs from persistence: critical were kept. */
List<Log> outputLogs = new ArrayList<>();
persistence.getLogs("test-p1", Collections.<String>emptyList(), expectedLogs.size() + 1, outputLogs);
assertTrue(expectedLogs.size() >= persistence.countLogs("test-p1"));
assertThat(expectedLogs, hasItems(outputLogs.toArray(new Log[0])));
} finally {
persistence.close();
}
}
use of com.microsoft.appcenter.persistence.Persistence.PersistenceException in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceAndroidTest method putLogTooLargeIsNotEvenTried.
@Test
public void putLogTooLargeIsNotEvenTried() throws PersistenceException {
/* Initialize database persistence. */
int someLogCount = 3;
DatabasePersistence persistence = new DatabasePersistence(sContext);
assertTrue(persistence.setMaxStorageSize(MAX_STORAGE_SIZE_IN_BYTES));
/* Set a mock log serializer. */
LogSerializer logSerializer = new DefaultLogSerializer();
logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
persistence.setLogSerializer(logSerializer);
try {
/* Generate some logs that will be evicted. */
for (int i = 0; i < someLogCount; i++) {
persistence.putLog(AndroidTestUtils.generateMockLog(), "test-p1", NORMAL);
}
assertEquals(someLogCount, persistence.countLogs("test-p1"));
/* Generate a log that is so large that it eventually fails. */
LogWithProperties log = AndroidTestUtils.generateMockLog();
int size = 32 * 1024;
Map<String, String> properties = new HashMap<>();
properties.put("key", generateString(size, 'x'));
log.setProperties(properties);
try {
persistence.putLog(log, "test-p1", NORMAL);
fail("putLog was expected to fail");
} catch (PersistenceException e) {
/* Verify the behavior: not inserted and database isn't empty. */
assertEquals(someLogCount, persistence.countLogs("test-p1"));
}
} finally {
persistence.close();
}
}
Aggregations