use of com.microsoft.appcenter.ingestion.models.LogWithProperties in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceAndroidTest method putNormalLogCloseToMaxSizeClearsEverything.
@Test
public void putNormalLogCloseToMaxSizeClearsEverything() 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 of both priority that will be evicted. */
int someLogCount = 3;
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 will empty all the database and
* eventually fails because close to the limit we check and the overhead of columns/index
* is larger than max size.
*/
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) {
}
/* Verify the behavior: not inserted and database now empty. */
assertEquals(0, persistence.countLogs("test-p1"));
} finally {
persistence.close();
}
}
use of com.microsoft.appcenter.ingestion.models.LogWithProperties in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceAndroidTest method putLargeLogFails.
@Test
public void putLargeLogFails() {
/* Initialize database persistence. */
String path = Constants.FILES_PATH;
Constants.FILES_PATH = null;
DatabasePersistence persistence = new DatabasePersistence(sContext);
/* Set a mock log serializer. */
LogSerializer logSerializer = new DefaultLogSerializer();
logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
persistence.setLogSerializer(logSerializer);
try {
/* Initial count is 0. */
assertEquals(0, persistence.countLogs("test-p1"));
/* Generate a large log and persist. */
LogWithProperties log = AndroidTestUtils.generateMockLog();
int size = 2 * 1024 * 1024;
StringBuilder largeValue = new StringBuilder(size);
for (int i = 0; i < size; i++) {
largeValue.append("x");
}
Map<String, String> properties = new HashMap<>();
properties.put("key", largeValue.toString());
log.setProperties(properties);
persistence.putLog(log, "test-p1", NORMAL);
fail("putLog was expected to fail");
} catch (Persistence.PersistenceException e) {
assertTrue(e.getCause() instanceof IOException);
/* Make sure database entry has been removed. */
assertEquals(0, persistence.countLogs("test-p1"));
} finally {
persistence.close();
/* Restore path. */
Constants.FILES_PATH = path;
}
}
use of com.microsoft.appcenter.ingestion.models.LogWithProperties in project mobile-center-sdk-android by Microsoft.
the class SasquatchAnalyticsListener method onSendingSucceeded.
@Override
public void onSendingSucceeded(com.microsoft.appcenter.ingestion.models.Log log) {
String message = null;
if (log instanceof EventLog) {
message = String.format("%s\nName: %s", mContext.getString(R.string.event_sent_succeeded), ((EventLog) log).getName());
} else if (log instanceof PageLog) {
message = String.format("%s\nName: %s", mContext.getString(R.string.page_sent_succeeded), ((PageLog) log).getName());
} else if (log instanceof CommonSchemaLog) {
CommonSchemaLog commonSchemaLog = (CommonSchemaLog) log;
message = String.format("%s\nName: %s", mContext.getString(R.string.event_sent_succeeded), commonSchemaLog.getName());
if (commonSchemaLog.getData() != null) {
message += String.format("\nProperties: %s", commonSchemaLog.getData().getProperties().toString());
}
}
if (log instanceof LogWithProperties) {
if (((LogWithProperties) log).getProperties() != null) {
message += String.format("\nProperties: %s", new JSONObject(((LogWithProperties) log).getProperties()).toString());
}
}
if (message != null) {
notifySendingCompletion(message);
}
analyticsIdlingResource.decrement();
}
use of com.microsoft.appcenter.ingestion.models.LogWithProperties in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceAndroidTest method putLargeLogFailsToRead.
@Test
public void putLargeLogFailsToRead() throws PersistenceException {
/* Initialize database persistence. */
DatabasePersistence persistence = new DatabasePersistence(sContext);
/* Set a mock log serializer. */
LogSerializer logSerializer = new DefaultLogSerializer();
logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
persistence.setLogSerializer(logSerializer);
try {
/* Initial count is 0. */
assertEquals(0, persistence.countLogs("test-p1"));
/* Generate a large log and persist. */
LogWithProperties log = AndroidTestUtils.generateMockLog();
int size = 2 * 1024 * 1024;
StringBuilder largeValue = new StringBuilder(size);
for (int i = 0; i < size; i++) {
largeValue.append("x");
}
Map<String, String> properties = new HashMap<>();
properties.put("key", largeValue.toString());
log.setProperties(properties);
long id = persistence.putLog(log, "test-p1", NORMAL);
assertEquals(1, persistence.countLogs("test-p1"));
/* Verify large file. */
File file = persistence.getLargePayloadFile(persistence.getLargePayloadGroupDirectory("test-p1"), id);
assertNotNull(file);
String fileLog = FileManager.read(file);
assertNotNull(fileLog);
assertTrue(fileLog.length() >= size);
/* Delete the file. */
assertTrue(file.delete());
/* We won't be able to read the log now but persistence should delete the SQLite log on error. */
List<Log> outputLogs = new ArrayList<>();
persistence.getLogs("test-p1", Collections.<String>emptyList(), 1, outputLogs);
assertEquals(0, outputLogs.size());
assertEquals(0, persistence.countLogs("test-p1"));
} finally {
persistence.close();
}
}
use of com.microsoft.appcenter.ingestion.models.LogWithProperties in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceAndroidTest method putLargeLogAndDeleteAll.
@Test
public void putLargeLogAndDeleteAll() throws PersistenceException {
/* Initialize database persistence. */
DatabasePersistence persistence = new DatabasePersistence(sContext);
/* Set a mock log serializer. */
LogSerializer logSerializer = new DefaultLogSerializer();
logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
persistence.setLogSerializer(logSerializer);
try {
/* Initial count is 0. */
assertEquals(0, persistence.countLogs("test-p1"));
/* Generate a large log and persist. */
LogWithProperties log = AndroidTestUtils.generateMockLog();
int size = 2 * 1024 * 1024;
StringBuilder largeValue = new StringBuilder(size);
for (int i = 0; i < size; i++) {
largeValue.append("x");
}
Map<String, String> properties = new HashMap<>();
properties.put("key", largeValue.toString());
log.setProperties(properties);
long id = persistence.putLog(log, "test-p1", NORMAL);
/* Count logs. */
assertEquals(1, persistence.countLogs("test-p1"));
/* Get a log from persistence. */
List<Log> outputLogs = new ArrayList<>();
persistence.getLogs("test-p1", Collections.<String>emptyList(), 1, outputLogs);
assertEquals(1, outputLogs.size());
assertEquals(log, outputLogs.get(0));
assertEquals(1, persistence.countLogs("test-p1"));
/* Verify large file. */
File file = persistence.getLargePayloadFile(persistence.getLargePayloadGroupDirectory("test-p1"), id);
assertNotNull(file);
String fileLog = FileManager.read(file);
assertNotNull(fileLog);
assertTrue(fileLog.length() >= size);
/* Delete entire group. */
persistence.deleteLogs("test-p1");
assertEquals(0, persistence.countLogs("test-p1"));
/* Verify file delete and also parent directory since we used group deletion. */
assertFalse(file.exists());
assertNotNull(file.getParentFile());
assertFalse(file.getParentFile().exists());
} finally {
persistence.close();
}
}
Aggregations