public void TimeoutSessionManager_Created_From_Null_State_Is_Fresh()
        {
            var timeout = TimeSpan.FromHours(1.25);

            var sessionManager = new TimeoutSessionManager(null, timeout);

            Assert.AreEqual(timeout, sessionManager.Timeout);
            Assert.IsNull(sessionManager.Referrer);
        }
        public void TimeoutSessionManager_Created_From_State_Provides_Same_State()
        {
            var expected = CreateSampleState();

            var sessionManager = new TimeoutSessionManager(expected, TimeSpan.FromDays(1));
            var actual = sessionManager.GetState();

            Assert.AreEqual(expected.VisitorId, actual.VisitorId);
        }
        public void TimeoutSessionManager_Can_Be_Created_From_State()
        {
            var timeout = TimeSpan.FromMinutes(5);
            var state = CreateSampleState();

            var sessionManager = new TimeoutSessionManager(state, timeout);

            Assert.AreEqual(timeout, sessionManager.Timeout);
            Assert.AreEqual(state.VisitorId, sessionManager.Visitor.ClientId);
        }
        public void TimeoutSessionManager_Can_Be_Created_From_State()
        {
            var timeout = TimeSpan.FromMinutes(5);
            var state = CreateSampleState();

            var sessionManager = new TimeoutSessionManager(state, timeout);

            Assert.AreEqual(timeout, sessionManager.Timeout);
            Assert.AreEqual(state.PreviousSessionStartedAt, sessionManager.PreviousSessionStartedAt);
        }
        public void UrchinUriBuilder_CreateCookieSubstituteParameter()
        {
            var @event = new EventActivity("action", "catgory", "label", 123, true);
            var sessionState = new SessionState
                {
                    SessionNumber = 5,
                    VisitorId = new Guid("FFFCCBCB-9A87-4987-BD20-CE7C81F96CD2"),
                    FirstVisitAt = new DateTimeOffset(2012, 10, 10, 13, 14, 15, TimeSpan.Zero),
                    PreviousSessionStartedAt = new DateTimeOffset(2012, 12, 10, 13, 14, 15, TimeSpan.Zero),
                    SessionStartedAt = new DateTimeOffset(2012, 12, 14, 13, 14, 15, TimeSpan.Zero),
                };
            var sessionManager = new TimeoutSessionManager(sessionState, TimeSpan.FromMinutes(5));

            var cookieSubstitute = UrchinUriBuilder.CreateCookieSubstituteParameter(sessionManager, 1);

            Assert.AreEqual("__utma=1.1159017511.1349874855.1355145255.1355490855.5;", cookieSubstitute);
        }
        public void TimeoutSessionManager_Creates_New_Session_In_A_Thread_Safe_Way()
        {
            var timeout = TimeSpan.FromSeconds(2);
            var sessionManager = new TimeoutSessionManager(null, timeout);

            Task.Delay(timeout + TimeSpan.FromSeconds(1)).Wait();

            Task.WaitAll(
                Task.Run(() => { for (var i = 0; i < 500; i++) sessionManager.Hit(); }),
                Task.Run(() => { for (var i = 0; i < 500; i++) sessionManager.Hit(); }),
                Task.Run(() => { for (var i = 0; i < 500; i++) sessionManager.Hit(); })
            );

            Task.Delay(timeout + TimeSpan.FromSeconds(1)).Wait();

            Task.WaitAll(
                Task.Run(() => { for (var i = 0; i < 500; i++) sessionManager.Hit(); }),
                Task.Run(() => { for (var i = 0; i < 500; i++) sessionManager.Hit(); }),
                Task.Run(() => { for (var i = 0; i < 500; i++) sessionManager.Hit(); })
            );

            Assert.AreEqual(3, sessionManager.Session.Number);
            Assert.AreEqual(1500, sessionManager.Session.HitCount);
        }
        public void TimeoutSessionManager_Creates_New_Session_When_Hit_After_Timeout()
        {
            var timeout = TimeSpan.FromSeconds(2);
            var sessionManager = new TimeoutSessionManager(null, timeout);

            Assert.AreEqual(1, sessionManager.Session.Number);

            var starting = DateTimeOffset.Now;
            Task.Delay(timeout + TimeSpan.FromSeconds(1)).Wait();

            sessionManager.Hit();
            Assert.AreEqual(2, sessionManager.Session.Number);
            Assert.IsTrue(sessionManager.Session.StartedAt >= starting, "Session StartedAt too early");
            Assert.IsTrue(sessionManager.Session.StartedAt <= DateTimeOffset.Now, "Session StartedAt too late");
        }