public async void SetAndReleaseItemExclusiveAsync_Should_Create_New_SessionItem_If_Session_Is_New()
        {
            var provider = CreateProvider();

            provider.Initialize(DefaultProviderName, CreateSessionStateProviderConfig(), CreateSessionStateSection(),
                                createConnectionStringSettings());

            var sessionCollection = new SessionStateItemCollection();
            var now = DateTime.UtcNow;

            sessionCollection["test1"] = "test1";
            sessionCollection["test2"] = now;
            var data = new SessionStateStoreData(sessionCollection, new HttpStaticObjectsCollection(), TestTimeout);

            byte[] buff;
            int    length;

            SqlSessionStateProviderAsync.SerializeStoreData(data, DefaultItemLength, out buff, out length, false);

            var repoMoq            = new Mock <ISqlSessionStateRepository>();
            var sessionItemCreated = false;

            repoMoq.Setup(repo => repo.CreateOrUpdateSessionStateItemAsync(true, SqlSessionStateProviderAsync.AppendAppIdHash(TestSessionId),
                                                                           buff, length, TestTimeout, 0, 0))
            .Returns(Task.CompletedTask)
            .Callback(() => sessionItemCreated = true);
            provider.SqlSessionStateRepository = repoMoq.Object;
            var httpContext = CreateMoqHttpContextBase();

            await provider.SetAndReleaseItemExclusiveAsync(httpContext, TestSessionId, data, null, true, CancellationToken.None);

            Assert.True(sessionItemCreated);
        }
        public async void InitializeRequest_Should_Reset_OrigStreamLen()
        {
            var provider = CreateProvider();

            provider.Initialize(DefaultProviderName, CreateSessionStateProviderConfig(), CreateSessionStateSection(),
                                createConnectionStringSettings());
            Assert.Equal(0, provider.OrigStreamLen);

            var sessionCollection = new SessionStateItemCollection();
            var now = DateTime.UtcNow;

            sessionCollection["test1"] = "test1";
            sessionCollection["test2"] = now;
            var data = new SessionStateStoreData(sessionCollection, new HttpStaticObjectsCollection(), TestTimeout);

            byte[] buff;
            int    length;

            SqlSessionStateProviderAsync.SerializeStoreData(data, DefaultItemLength, out buff, out length, false);

            var repoMoq = new Mock <ISqlSessionStateRepository>();
            var ssItem  = new SessionItem(buff, true, TimeSpan.Zero, 1, SessionStateActions.None);

            repoMoq.Setup(repo => repo.GetSessionStateItemAsync(SqlSessionStateProviderAsync.AppendAppIdHash(TestSessionId), false)).Returns(Task.FromResult(ssItem));
            provider.SqlSessionStateRepository = repoMoq.Object;
            var httpContext = CreateMoqHttpContextBase();

            var getItemResult = await provider.GetItemAsync(httpContext, TestSessionId, CancellationToken.None);

            Assert.NotEqual(0, provider.OrigStreamLen);

            provider.InitializeRequest(httpContext);
            Assert.Equal(0, provider.OrigStreamLen);
        }
        public async void CreateUninitializedItem_Should_CreateUninitializedItem_In_Repository()
        {
            var provider = CreateProvider();

            provider.Initialize(DefaultProviderName, CreateSessionStateProviderConfig(), CreateSessionStateSection(),
                                createConnectionStringSettings());

            var    repoMoq   = new Mock <ISqlSessionStateRepository>();
            string sessionId = "";
            int    length    = 0;

            byte[] buff    = null;
            int    timeout = 0;

            repoMoq.Setup(repo => repo.CreateUninitializedSessionItemAsync(It.IsAny <string>(), It.IsAny <int>(), It.IsAny <byte[]>(), It.IsAny <int>()))
            .Returns(Task.CompletedTask).Callback <string, int, byte[], int>((id, len, bytes, to) => { sessionId = id; length = len; buff = bytes; timeout = to; });

            provider.SqlSessionStateRepository = repoMoq.Object;
            var httpContextBase = CreateMoqHttpContextBase();

            var expectedTimeout = 60;
            await provider.CreateUninitializedItemAsync(httpContextBase, TestSessionId, expectedTimeout, CancellationToken.None);

            Assert.Equal(SqlSessionStateProviderAsync.AppendAppIdHash(TestSessionId), sessionId);
            Assert.Equal(expectedTimeout, timeout);
            Assert.NotNull(buff);
            Assert.Equal(7, length);
        }
        public async void ReleaseItemExclusiveAsync_Should_Unlock_SessionItem()
        {
            var provider = CreateProvider();

            provider.Initialize(DefaultProviderName, CreateSessionStateProviderConfig(), CreateSessionStateSection(),
                                createConnectionStringSettings());

            var repoMoq  = new Mock <ISqlSessionStateRepository>();
            var isLocked = true;

            repoMoq.Setup(repo => repo.ReleaseSessionItemAsync(SqlSessionStateProviderAsync.AppendAppIdHash(TestSessionId), 1)).Returns(Task.CompletedTask)
            .Callback(() => isLocked           = false);
            provider.SqlSessionStateRepository = repoMoq.Object;
            var httpContext = CreateMoqHttpContextBase();

            await provider.ReleaseItemExclusiveAsync(httpContext, TestSessionId, 1, CancellationToken.None);

            Assert.False(isLocked);
        }
        public async void ResetItemTimeoutAsync_Should_Reset_SessionItem_Timeout()
        {
            var provider = CreateProvider();

            provider.Initialize(DefaultProviderName, CreateSessionStateProviderConfig(), CreateSessionStateSection(),
                                createConnectionStringSettings());

            var repoMoq = new Mock <ISqlSessionStateRepository>();
            var timeout = 100;

            repoMoq.Setup(repo => repo.ResetSessionItemTimeoutAsync(SqlSessionStateProviderAsync.AppendAppIdHash(TestSessionId)))
            .Returns(Task.CompletedTask)
            .Callback(() => timeout            = 200);
            provider.SqlSessionStateRepository = repoMoq.Object;
            var httpContext = CreateMoqHttpContextBase();

            await provider.ResetItemTimeoutAsync(httpContext, TestSessionId, CancellationToken.None);

            Assert.Equal(200, timeout);
        }
Esempio n. 6
0
        public async void SetAndReleaseItemExclusiveAsync_Should_Release_NonExclsive_SessionItem()
        {
            var provider = CreateProvider();

            provider.Initialize(DefaultProviderName, CreateSessionStateProviderConfig(), CreateSessionStateSection(),
                                createConnectionStringSettings());

            var sessionCollection = new SessionStateItemCollection();
            var now = DateTime.UtcNow;

            sessionCollection["test1"] = "test1";
            sessionCollection["test2"] = now;
            var data = new SessionStateStoreData(sessionCollection, new HttpStaticObjectsCollection(), TestTimeout);

            byte[] buff;
            int    length;

            SqlSessionStateProviderAsync.SerializeStoreData(data, DefaultItemLength, out buff, out length, false);

            var repoMoq = new Mock <IPostgresSessionStateRepository>();
            var ssItem  = new SessionItem(buff, false, TimeSpan.Zero, null, SessionStateActions.None);

            repoMoq.Setup(repo => repo.GetSessionStateItemAsync(SqlSessionStateProviderAsync.AppendAppIdHash(TestSessionId), false))
            .Returns(Task.FromResult(ssItem));
            provider.SqlSessionStateRepository = repoMoq.Object;
            var httpContext = CreateMoqHttpContextBase();

            var getItemResult = await provider.GetItemAsync(httpContext, TestSessionId, CancellationToken.None);

            var sessionReleased = false;

            repoMoq.Setup(repo => repo.CreateOrUpdateSessionStateItemAsync(false, SqlSessionStateProviderAsync.AppendAppIdHash(TestSessionId),
                                                                           buff, length, TestTimeout, 0, provider.OrigStreamLen))
            .Returns(Task.CompletedTask)
            .Callback(() => sessionReleased = true);

            await provider.SetAndReleaseItemExclusiveAsync(httpContext, TestSessionId, getItemResult.Item, null, false, CancellationToken.None);

            Assert.True(sessionReleased);
        }
        public async void GetItemAsync_Should_Return_SessionItem_If_SessionItem_Is_Unlocked(SessionStateActions action)
        {
            var provider = CreateProvider();

            provider.Initialize(DefaultProviderName, CreateSessionStateProviderConfig(), CreateSessionStateSection(),
                                createConnectionStringSettings());

            var sessionCollection = new SessionStateItemCollection();
            var now = DateTime.UtcNow;

            sessionCollection["test1"] = "test1";
            sessionCollection["test2"] = now;
            var data = new SessionStateStoreData(sessionCollection, new HttpStaticObjectsCollection(), TestTimeout);

            byte[] buff;
            int    length;

            SqlSessionStateProviderAsync.SerializeStoreData(data, DefaultItemLength, out buff, out length, false);

            var repoMoq = new Mock <ISqlSessionStateRepository>();
            var ssItem  = new SessionItem(buff, false, TimeSpan.Zero, null, action);

            repoMoq.Setup(repo => repo.GetSessionStateItemAsync(SqlSessionStateProviderAsync.AppendAppIdHash(TestSessionId), false)).Returns(Task.FromResult(ssItem));
            provider.SqlSessionStateRepository = repoMoq.Object;
            var httpContext = CreateMoqHttpContextBase();

            var getItemResult = await provider.GetItemAsync(httpContext, TestSessionId, CancellationToken.None);

            Assert.Equal(data.Items.Count, getItemResult.Item.Items.Count);
            Assert.Equal(data.Items["test1"], getItemResult.Item.Items["test1"]);
            Assert.Equal(now, (DateTime)getItemResult.Item.Items["test2"]);
            Assert.NotNull(getItemResult.Item.StaticObjects);
            Assert.Equal(data.Timeout, getItemResult.Item.Timeout);
            Assert.Equal(ssItem.Actions, getItemResult.Actions);
            Assert.Equal(ssItem.LockAge, getItemResult.LockAge);
            Assert.Equal(ssItem.Locked, getItemResult.Locked);
            Assert.Equal(ssItem.LockId, getItemResult.LockId);
            Assert.NotEqual(0, provider.OrigStreamLen);
        }
        public async void GetItemExclusiveAsync_Should_Return_SessionItem_With_Null_IF_SessionItem_Is_Locked()
        {
            var provider = CreateProvider();

            provider.Initialize(DefaultProviderName, CreateSessionStateProviderConfig(), CreateSessionStateSection(),
                                createConnectionStringSettings());

            var repoMoq = new Mock <ISqlSessionStateRepository>();
            var ssItem  = new SessionItem(null, true, TimeSpan.FromSeconds(2), 1, SessionStateActions.None);

            repoMoq.Setup(repo => repo.GetSessionStateItemAsync(SqlSessionStateProviderAsync.AppendAppIdHash(TestSessionId), true)).Returns(Task.FromResult(ssItem));
            provider.SqlSessionStateRepository = repoMoq.Object;
            var httpContext = CreateMoqHttpContextBase();

            var getItemResult = await provider.GetItemExclusiveAsync(httpContext, TestSessionId, CancellationToken.None);

            Assert.Null(getItemResult.Item);
            Assert.Equal(ssItem.Actions, getItemResult.Actions);
            Assert.Equal(ssItem.LockAge, getItemResult.LockAge);
            Assert.Equal(ssItem.Locked, getItemResult.Locked);
            Assert.Equal(ssItem.LockId, getItemResult.LockId);
            Assert.Equal(0, provider.OrigStreamLen);
        }