@Override public void onPreparingLog(@NonNull Log log, @NonNull String groupName) { /* * Since we enqueue start session logs, skip them to avoid infinite loop. * Also skip start service log as it's always sent and should not trigger a session. */ if (log instanceof StartSessionLog || log instanceof StartServiceLog) { return; } /* * If the log has already specified a timestamp, try correlating with a past session. * Note that it can also find the current session but that's ok: in that case that means * its a log that will be associated to current session but won't trigger expiration logic. */ Date timestamp = log.getTimestamp(); if (timestamp != null) { SessionContext.SessionInfo pastSession = SessionContext.getInstance().getSessionAt(timestamp.getTime()); if (pastSession != null) { log.setSid(pastSession.getSessionId()); } } /* If the log does not have a timestamp yet, then we just correlate with current session. */ else { /* Set current session identifier. */ log.setSid(mSid); /* Record queued time only if the log is using current session. */ mLastQueuedLogTime = SystemClock.elapsedRealtime(); } }
@Test public void ignoreStartService() { Log startServiceLog = spy(new StartServiceLog()); mSessionTracker.onPreparingLog(startServiceLog, TEST_GROUP); verify(mChannel, never()).enqueue(any(Log.class), anyString(), anyInt()); verify(startServiceLog, never()).setSid(any(UUID.class)); }
@Test public void serialize() throws JSONException { LogContainer expectedContainer = new LogContainer(); List<Log> logs = new ArrayList<>(); { DistributionStartSessionLog log = new DistributionStartSessionLog(); log.setTimestamp(new Date()); logs.add(log); } expectedContainer.setLogs(logs); UUID sid = UUIDUtils.randomUUID(); for (Log log : logs) { log.setSid(sid); } /* Serialize and deserialize logs container. */ LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(DistributionStartSessionLog.TYPE, new DistributionStartSessionLogFactory()); String payload = serializer.serializeContainer(expectedContainer); LogContainer actualContainer = serializer.deserializeContainer(payload, null); /* Verify that logs container successfully deserialized. */ Assert.assertEquals(expectedContainer, actualContainer); } }
@Test public void serialize() throws JSONException { LogContainer expectedContainer = new LogContainer(); List<Log> logs = new ArrayList<>(); { PushInstallationLog log = new PushInstallationLog(); log.setTimestamp(new Date()); log.setPushToken("TEST"); logs.add(log); } expectedContainer.setLogs(logs); UUID sid = UUIDUtils.randomUUID(); for (Log log : logs) { log.setSid(sid); } LogSerializer serializer = new DefaultLogSerializer(); serializer.addLogFactory(PushInstallationLog.TYPE, new PushInstallationLogFactory()); String payload = serializer.serializeContainer(expectedContainer); LogContainer actualContainer = serializer.deserializeContainer(payload, null); Assert.assertEquals(expectedContainer, actualContainer); } }
log.setSid(sid); log.setDevice(device);