예제 #1
0
        /// <summary>
        /// Method that creates a new session using the provider supplied.  This method
        /// also verifies that the session was created, then modifies the session, then
        /// deletes the session and verifies deletion from the store and the memory cache
        /// in the provider.
        /// </summary>
        /// <param name="provider">The MongoDB provider to use for the test.</param>
        private void VerifySessionPersistence(MongoDBSessionStateProvider provider)
        {
            string itemName  = "DummyItem";
            string itemValue = "Value";

            HttpRequest  request  = null;
            HttpResponse response = null;
            HttpContext  context  = GetContext(out request, out response);

            TestContext.WriteLine("Creating Store Data");
            var dataStore = provider.CreateNewStoreData(context, (_TimeoutInSeconds / 60));

            dataStore.Items[itemName] = itemValue;

            var sessionId = _SessionIdManager.CreateSessionID(context);

            TestContext.WriteLine("Writing Store Data");
            provider.SetAndReleaseItemExclusive(context, sessionId, dataStore, null, true);

            System.Threading.Thread.Sleep(2000);

            bool     locked;
            TimeSpan lockAge;
            object   lockId;

            System.Web.SessionState.SessionStateActions actions;

            TestContext.WriteLine("Retrieving new Store Data");
            var retrievedDataStore = provider.GetItemExclusive(context, sessionId, out locked, out lockAge, out lockId, out actions);

            if (retrievedDataStore == null || retrievedDataStore.Items.Count == 0)
            {
                Assert.Fail("Retrieved data store does not contain session data");
            }

            TestContext.WriteLine("Testing Store Data");
            var dummyValue = retrievedDataStore.Items[itemName];

            Assert.AreEqual(itemValue, dummyValue);

            itemValue = "NewValue";
            retrievedDataStore.Items[itemName] = itemValue;

            TestContext.WriteLine("Updating Store Data");
            provider.SetAndReleaseItemExclusive(context, sessionId, retrievedDataStore, lockId, false);

            System.Threading.Thread.Sleep(2000);

            var retrievedDataStore2 = provider.GetItemExclusive(context, sessionId, out locked, out lockAge, out lockId, out actions);

            if (retrievedDataStore2 == null || retrievedDataStore2.Items.Count == 0)
            {
                Assert.Fail("Retrieved data store does not contain session data");
            }

            TestContext.WriteLine("Testing Store Data");
            Assert.AreEqual(itemValue, retrievedDataStore2.Items[itemName]);

            TestContext.WriteLine("Releasing Store Data");
            provider.ReleaseItemExclusive(context, sessionId, lockId);

            System.Threading.Thread.Sleep(2000);

            retrievedDataStore2 = provider.GetItemExclusive(context, sessionId, out locked, out lockAge, out lockId, out actions);
            TestContext.WriteLine("Deleting Store Data");
            provider.RemoveItem(context, sessionId, lockId, retrievedDataStore2);

            System.Threading.Thread.Sleep(2000);

            TestContext.WriteLine("Ensuring store was deleted");
            var retrievedDataStore3 = provider.GetItem(context, sessionId, out locked, out lockAge, out lockId, out actions);

            Assert.IsNull(retrievedDataStore3);

            System.Threading.Thread.Sleep(2000);

            TestContext.WriteLine("Ensuring cache is empty");
            ISessionStateData sessionStateDataFromCache = null;
            bool sessionExistsInCache = CheckSessionExistsInCache(provider, sessionId, out sessionStateDataFromCache);

            if (sessionExistsInCache)
            {
                Assert.Fail("Session should have been removed but still exists in cache");
            }

            TestContext.WriteLine("Success");
        }
예제 #2
0
        /// <summary>
        /// Method that creates a new session and touches it right before the expiry a couple of times.
        /// The final time it will not wait until after the expiration which the method will then
        /// expect the session to no longer be available.
        /// </summary>
        /// <param name="provider">The MongoDB provider to use for the test.</param>
        private void VerifySessionSlidingExpiration(MongoDBSessionStateProvider provider)
        {
            HttpRequest  request  = null;
            HttpResponse response = null;
            HttpContext  context  = GetContext(out request, out response);

            var sessionId = _SessionIdManager.CreateSessionID(context);
            var dataStore = provider.CreateNewStoreData(context, (_TimeoutInSeconds / 60));

            dataStore.Items["Dummy"] = "Value";

            TestContext.WriteLine("Creating dummy session with id {0}", sessionId);

            provider.SetAndReleaseItemExclusive(context, sessionId, dataStore, null, true);

            int iterations = 4;

            for (int i = 0; i < iterations; i++)
            {
                bool isLastIteration = (i == iterations - 1);

                int counter = _TimeoutInSeconds + (isLastIteration ? 10 : -10);
                TestContext.WriteLine("Waiting {0} seconds (expiration set to {1} seconds)...", counter, _TimeoutInSeconds);

                while (counter > 0)
                {
                    System.Threading.Thread.Sleep(1000);
                    counter--;
                }

                TestContext.WriteLine("Retrieving session again to reset expiry");

                bool     locked;
                TimeSpan lockAge;
                object   lockId;
                System.Web.SessionState.SessionStateActions actions;

                var dataStore2 = provider.GetItemExclusive(context, sessionId, out locked, out lockAge, out lockId, out actions);
                if (isLastIteration)
                {
                    if (dataStore2 != null)
                    {
                        Assert.Fail("Session has NOT expired.");
                    }

                    ISessionStateData dataFromCache;
                    bool existsInCache = CheckSessionExistsInCache(provider, sessionId, out dataFromCache);

                    if (existsInCache)
                    {
                        Assert.Fail("Session has expired however it still exists in the memory cache.");
                    }
                }
                else
                {
                    if (dataStore2 == null || dataStore2.Items.Count == 0)
                    {
                        Assert.Fail("Session Missing prior to expiry??");
                    }
                    else
                    {
                        TestContext.WriteLine("Session retrieved successfully during iteration {0}", (i + 1));
                    }
                }

                if (lockId != null)
                {
                    provider.ReleaseItemExclusive(context, sessionId, lockId);
                }
            }
        }