use of com.microsoft.azure.mobile.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceAndroidTest method putTooManyLogs.
@Test
public void putTooManyLogs() throws PersistenceException, IOException {
/* Initialize database persistence. */
DatabasePersistence persistence = new DatabasePersistence("test-persistence", "putTooManyLogs", 1, 2);
/* Set a mock log serializer. */
LogSerializer logSerializer = new DefaultLogSerializer();
logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
persistence.setLogSerializer(logSerializer);
try {
/* Generate too many logs and persist. */
Log log1 = AndroidTestUtils.generateMockLog();
Log log2 = AndroidTestUtils.generateMockLog();
Log log3 = AndroidTestUtils.generateMockLog();
Log log4 = AndroidTestUtils.generateMockLog();
persistence.putLog("test-p1", log1);
persistence.putLog("test-p1", log2);
persistence.putLog("test-p1", log3);
persistence.putLog("test-p1", log4);
/* Get logs from persistence. */
List<Log> outputLogs = new ArrayList<>();
persistence.getLogs("test-p1", 4, outputLogs);
assertEquals(2, outputLogs.size());
assertEquals(log3, outputLogs.get(0));
assertEquals(log4, outputLogs.get(1));
assertEquals(2, persistence.countLogs("test-p1"));
} finally {
/* Close. */
//noinspection ThrowFromFinallyBlock
persistence.close();
}
}
use of com.microsoft.azure.mobile.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.
the class DatabasePersistenceAndroidTest method getLogs.
@Test
public void getLogs() throws PersistenceException, IOException {
/* Initialize database persistence. */
DatabasePersistence persistence = new DatabasePersistence("test-persistence", "getLogs", 1);
/* Set a mock log serializer. */
LogSerializer logSerializer = new DefaultLogSerializer();
logSerializer.addLogFactory(MOCK_LOG_TYPE, new MockLogFactory());
persistence.setLogSerializer(logSerializer);
try {
/* Test constants. */
int numberOfLogs = 10;
int sizeForGetLogs = 4;
/* Generate a log and persist. */
Log[] logs = new Log[numberOfLogs];
for (int i = 0; i < logs.length; i++) logs[i] = AndroidTestUtils.generateMockLog();
/* Put. */
for (Log log : logs) persistence.putLog("test", log);
/* Get. */
getAllLogs(persistence, numberOfLogs, sizeForGetLogs);
/* Clear ids, we should be able to get the logs again in the same sequence. */
persistence.clearPendingLogState();
getAllLogs(persistence, numberOfLogs, sizeForGetLogs);
/* Count. */
assertEquals(10, persistence.countLogs("test"));
/* Clear. Nothing to get after. */
persistence.mDatabaseStorage.clear();
List<Log> outputLogs = new ArrayList<>();
assertNull(persistence.getLogs("test", sizeForGetLogs, outputLogs));
assertTrue(outputLogs.isEmpty());
assertEquals(0, persistence.countLogs("test"));
} finally {
/* Close. */
//noinspection ThrowFromFinallyBlock
persistence.close();
}
}
use of com.microsoft.azure.mobile.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.
the class Crashes method getChannelListener.
@Override
protected Channel.GroupListener getChannelListener() {
return new Channel.GroupListener() {
/** Process callback (template method) */
private void processCallback(Log log, final CallbackProcessor callbackProcessor) {
if (log instanceof ManagedErrorLog) {
ManagedErrorLog errorLog = (ManagedErrorLog) log;
if (errorLog.getFatal()) {
final ErrorReport report = buildErrorReport(errorLog);
UUID id = errorLog.getId();
if (report != null) {
/* Clean up before calling callbacks if requested. */
if (callbackProcessor.shouldDeleteThrowable()) {
removeStoredThrowable(id);
}
/* Call back. */
HandlerUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
callbackProcessor.onCallBack(report);
}
});
} else
MobileCenterLog.warn(LOG_TAG, "Cannot find crash report for the error log: " + id);
}
} else {
if (!(log instanceof ErrorAttachmentLog))
MobileCenterLog.warn(LOG_TAG, "A different type of log comes to crashes: " + log.getClass().getName());
}
}
@Override
public void onBeforeSending(Log log) {
processCallback(log, new CallbackProcessor() {
@Override
public boolean shouldDeleteThrowable() {
return false;
}
@Override
public void onCallBack(ErrorReport report) {
mCrashesListener.onBeforeSending(report);
}
});
}
@Override
public void onSuccess(Log log) {
processCallback(log, new CallbackProcessor() {
@Override
public boolean shouldDeleteThrowable() {
return true;
}
@Override
public void onCallBack(ErrorReport report) {
mCrashesListener.onSendingSucceeded(report);
}
});
}
@Override
public void onFailure(Log log, final Exception e) {
processCallback(log, new CallbackProcessor() {
@Override
public boolean shouldDeleteThrowable() {
return true;
}
@Override
public void onCallBack(ErrorReport report) {
mCrashesListener.onSendingFailed(report, e);
}
});
}
};
}
use of com.microsoft.azure.mobile.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.
the class SessionTrackerTest method pastSessions.
@Test
public void pastSessions() {
/* Get a current session. */
UUID firstSid, currentSid;
long firstSessionTime = mMockTime;
{
Log log = newEvent();
mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
assertNotNull(log.getSid());
currentSid = firstSid = log.getSid();
}
/* Verify session reused for second log. */
{
Log log = newEvent();
mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
assertEquals(currentSid, log.getSid());
}
/* Past log: correlation will fail and use current session. */
{
Log log = newEvent();
log.setToffset(123L);
mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
assertEquals(currentSid, log.getSid());
}
/* No usage from background for a long time, should produce a new session but we'll correlate, correlation does not trigger a new session. */
{
spendTime(30000);
Log log = newEvent();
log.setToffset(firstSessionTime + 1);
mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
assertEquals(currentSid, log.getSid());
Set<String> sessions = StorageHelper.PreferencesStorage.getStringSet("sessions");
assertNotNull(sessions);
assertEquals(1, sessions.size());
}
/* Trigger a second session. */
{
Log log = newEvent();
mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
assertNotEquals(currentSid, log.getSid());
Set<String> sessions = StorageHelper.PreferencesStorage.getStringSet("sessions");
assertNotNull(sessions);
assertEquals(2, sessions.size());
}
/* Correlate log to previous. */
{
spendTime(30000);
Log log = newEvent();
log.setToffset(firstSessionTime + 1);
mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
assertEquals(firstSid, log.getSid());
Set<String> sessions = StorageHelper.PreferencesStorage.getStringSet("sessions");
assertNotNull(sessions);
assertEquals(2, sessions.size());
}
/* Re-test with persistence now, no current session but same correlation will work and no session will be triggered on the new instance. */
mSessionTracker = new SessionTracker(mChannel, TEST_GROUP);
{
Log log = newEvent();
log.setToffset(firstSessionTime + 1);
mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
assertEquals(firstSid, log.getSid());
Set<String> sessions = StorageHelper.PreferencesStorage.getStringSet("sessions");
assertNotNull(sessions);
assertEquals(2, sessions.size());
}
/* Failed correlation without an active session will start a new session. */
{
Log log = newEvent();
log.setToffset(1);
mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
assertNotNull(log.getSid());
Set<String> sessions = StorageHelper.PreferencesStorage.getStringSet("sessions");
assertNotNull(sessions);
assertEquals(3, sessions.size());
}
/* Clear sessions. */
mSessionTracker.clearSessions();
verifyStatic();
StorageHelper.PreferencesStorage.remove("sessions");
}
use of com.microsoft.azure.mobile.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.
the class AnalyticsSerializerTest method someBatch.
@Test
public void someBatch() throws JSONException {
LogContainer expectedContainer = new LogContainer();
Device device = new Device();
device.setSdkName("mobilecenter.android");
device.setSdkVersion("1.2.3");
device.setModel("S5");
device.setOemName("HTC");
device.setOsName("Android");
device.setOsVersion("4.0.3");
device.setOsBuild("LMY47X");
device.setOsApiLevel(15);
device.setLocale("en_US");
device.setTimeZoneOffset(120);
device.setScreenSize("800x600");
device.setAppVersion("3.2.1");
device.setAppBuild("42");
List<Log> logs = new ArrayList<>();
{
logs.add(new StartSessionLog());
}
expectedContainer.setLogs(logs);
{
PageLog pageLog = new PageLog();
pageLog.setName("home");
logs.add(pageLog);
}
{
PageLog pageLog = new PageLog();
pageLog.setName("settings");
pageLog.setProperties(new HashMap<String, String>() {
{
put("from", "home_menu");
put("orientation", "portrait");
}
});
logs.add(pageLog);
}
{
EventLog eventLog = new EventLog();
eventLog.setId(UUIDUtils.randomUUID());
eventLog.setName("subscribe");
logs.add(eventLog);
}
{
EventLog eventLog = new EventLog();
eventLog.setId(UUIDUtils.randomUUID());
eventLog.setName("click");
eventLog.setProperties(new HashMap<String, String>() {
{
put("x", "1");
put("y", "2");
}
});
logs.add(eventLog);
}
UUID sid = UUIDUtils.randomUUID();
for (Log log : logs) {
log.setSid(sid);
log.setDevice(device);
}
LogSerializer serializer = new DefaultLogSerializer();
serializer.addLogFactory(StartSessionLog.TYPE, new StartSessionLogFactory());
serializer.addLogFactory(PageLog.TYPE, new PageLogFactory());
serializer.addLogFactory(EventLog.TYPE, new EventLogFactory());
String payload = serializer.serializeContainer(expectedContainer);
android.util.Log.v(TAG, payload);
LogContainer actualContainer = serializer.deserializeContainer(payload);
Assert.assertEquals(expectedContainer, actualContainer);
}
Aggregations