/// <summary> /// Recreate a SessionManager from state. /// </summary> /// <param name="sessionState">SessionState containing details captured from a previous SessionManager or null if no previous SessionManager.</param> /// <param name="sampleRate">Sample rate to determine likelyhood of a new installation being tracked or not.</param> /// <returns>Recreated SessionManager.</returns> public SessionManager(SessionState sessionState, double sampleRate = 100.0) { if (sessionState != null) { visitor = new Visitor(sessionState.VisitorId, sessionState.FirstVisitAt); Session = new Session(sessionState.SessionStartedAt, sessionState.SessionNumber, sessionState.SessionHitCount, sessionState.HitId); Referrer = sessionState.Referrer; PreviousSessionStartedAt = sessionState.PreviousSessionStartedAt; lastActivityAt = sessionState.LastActivityAt; SessionStatus = sessionState.SessionStatus; VisitorStatus = sessionState.VisitorStatus; } else { var localStorage = ApplicationData.Current.LocalSettings; Guid guid = Guid.Empty; if (localStorage.Values.ContainsKey(guidKey) ) { guid = (Guid)localStorage.Values[guidKey]; } else { // Need a better way to get a unique guid guid = Guid.NewGuid(); localStorage.Values[guidKey] = guid; } visitor = new Visitor(guid); Session = new Session(); PreviousSessionStartedAt = Session.StartedAt; SessionStatus = SessionStatus.Starting; var willTrackThisVisitor = ShouldTrackThisNewVisitor(sampleRate); VisitorStatus = willTrackThisVisitor ? VisitorStatus.Active : VisitorStatus.SampledOut; if (!willTrackThisVisitor) Debug.WriteLine("Will not track this visitor. Sampling rate of {0} excluded them", sampleRate); } }
public void Session_IncreaseHitCount_Increases_HitCount() { var session = new Session(); session.IncreaseHitCount(); session.IncreaseHitCount(); Assert.AreEqual(2, session.HitCount); }
public void Session_Created_Started_Now() { var start = DateTimeOffset.Now; var session = new Session(); var end = DateTimeOffset.Now; Assert.IsTrue(session.StartedAt >= start, "StartedtAt too early expected after {0} found {1}", start, session.StartedAt); Assert.IsTrue(session.StartedAt <= end, "StartedtAt too late expected before {0} found {1}", end, session.StartedAt); }
public void Session_IncreaseHitCount_Increased_HitCount_With_Thread_Safety() { var session = new Session(); Task.WaitAll( Task.Run(() => { for (var i = 0; i < 5000; i++) session.IncreaseHitCount(); }), Task.Run(() => { for (var i = 0; i < 5000; i++) session.IncreaseHitCount(); }), Task.Run(() => { for (var i = 0; i < 5000; i++) session.IncreaseHitCount(); }), Task.Run(() => { for (var i = 0; i < 5000; i++) session.IncreaseHitCount(); }), Task.Run(() => { for (var i = 0; i < 5000; i++) session.IncreaseHitCount(); }) ); Assert.AreEqual(25000, session.HitCount); }
public void Session_Created_With_Parameters_Sets_Properties() { var startedAt = DateTimeOffset.Now.Subtract(new TimeSpan(1, 2, 3, 4, 5)); const int sessionNumber = 29; const int hitCount = 101; const int hitId = 123456; var session = new Session(startedAt, sessionNumber, hitCount, hitId); Assert.AreEqual(startedAt, session.StartedAt); Assert.AreEqual(sessionNumber, session.Number); Assert.AreEqual(hitCount, session.HitCount); Assert.AreEqual(hitId, session.HitId); }
/// <summary> /// Recreate a SessionManager from state. /// </summary> /// <param name="timeout">How long before a session will expire if no activity is seen.</param> /// <param name="sessionState">SessionState containing details captured from a previous SessionManager.</param> /// <returns>Recreated SessionManager.</returns> public SessionManager(TimeSpan timeout, SessionState sessionState) { this.timeout = timeout; if (sessionState != null) { visitor = new Visitor(sessionState.VisitorId, sessionState.FirstVisitAt); Session = new Session(sessionState.SessionStartedAt, sessionState.SessionNumber, sessionState.SessionHitCount, sessionState.HitId); Referrer = sessionState.Referrer; PreviousSessionStartedAt = sessionState.PreviousSessionStartedAt; lastActivityAt = sessionState.LastActivityAt; } else { visitor = new Visitor(); Session = new Session(); PreviousSessionStartedAt = Session.StartedAt; } }
/// <summary> /// Start a new session for this Visitor. /// </summary> /// <param name="startedAt">When this session started at.</param> private void StartNewSession(DateTimeOffset startedAt) { lock (newSessionLock) { PreviousSessionStartedAt = Session.StartedAt; Session = new Session(startedAt, Session.Number + 1); } }
public void Session_Created_Is_Session_Number_One() { var session = new Session(); Assert.AreEqual(1, session.Number); }
public void Session_Created_Has_Zero_Hits() { var session = new Session(); Assert.AreEqual(0, session.HitCount); }
/// <summary> /// Start a new session. /// </summary> /// <param name="startedAt">When this session started.</param> protected void StartNewSession(DateTimeOffset startedAt) { lock (newSessionLock) { PreviousSessionStartedAt = Session.StartedAt; Session = new Session(startedAt, Session.Number + 1); SessionStatus = SessionStatus.Starting; } }