public void GetWithoutSetTest()
 {
     using (RedisServer Server = new RedisServer())
     {
         RedisOutputCacheProvider provider = new RedisOutputCacheProvider();
         NameValueCollection config = new NameValueCollection();
         config.Add("ssl", "false");
         DateTime utxExpiry = DateTime.UtcNow.AddMinutes(3);
         provider.Initialize("name", config);
         Assert.Equal(null, provider.Get("key1"));
     }
 }
        public void AddWithExistingSetTest()
        {
            using (RedisServer Server = new RedisServer())
            {
                RedisOutputCacheProvider provider = new RedisOutputCacheProvider();
                NameValueCollection config = new NameValueCollection();
                config.Add("ssl", "false");
                provider.Initialize("name", config);

                DateTime utxExpiry = DateTime.UtcNow.AddMinutes(3);
                provider.Set("key3", "data3", utxExpiry);
                provider.Add("key3", "data3.1", utxExpiry);
                object data = provider.Get("key3");
                Assert.Equal("data3", data);
            }
        }
        public void AddScriptFixForExpiryTest()
        {
            using (RedisServer Server = new RedisServer())
            {
                RedisOutputCacheProvider provider = new RedisOutputCacheProvider();
                NameValueCollection config = new NameValueCollection();
                config.Add("ssl", "false");
                provider.Initialize("name", config);

                DateTime utxExpiry = DateTime.UtcNow.AddSeconds(1);
                provider.Add("key9", "data9", utxExpiry);
                object data = provider.Get("key9");
                Assert.Equal("data9", data);
                // Wait for 1.1 seconds so that data will expire
                System.Threading.Thread.Sleep(1100);
                data = provider.Get("key9");
                Assert.Equal(null, data);
            }
        }
Exemplo n.º 4
0
        public void TryTakeWriteLockAndGetData_WriteLockWithOtherWriteLock()
        {
            using (RedisServer redisServer = new RedisServer())
            {
                RedisConnectionWrapper redisConn = GetRedisConnectionWrapperWithUniqueSession();

                // Inserting data into redis server
                ChangeTrackingSessionStateItemCollection data = new ChangeTrackingSessionStateItemCollection();
                data["key"] = "value";
                redisConn.Set(data, 900);

                int lockTimeout = 900;

                // Takewrite lock successfully first time
                DateTime lockTime_1 = DateTime.Now;
                object   lockId_1;
                ISessionStateItemCollection dataFromRedis_1;
                int sessionTimeout;
                Assert.True(redisConn.TryTakeWriteLockAndGetData(lockTime_1, lockTimeout, out lockId_1, out dataFromRedis_1, out sessionTimeout));
                Assert.Equal(lockTime_1.Ticks.ToString(), lockId_1.ToString());
                Assert.Equal(1, dataFromRedis_1.Count);

                // try to take write lock and fail and get earlier lock id
                DateTime lockTime_2 = lockTime_1.AddSeconds(1);
                object   lockId_2;
                ISessionStateItemCollection dataFromRedis_2;
                Assert.False(redisConn.TryTakeWriteLockAndGetData(lockTime_2, lockTimeout, out lockId_2, out dataFromRedis_2, out sessionTimeout));
                Assert.Equal(lockTime_1.Ticks.ToString(), lockId_2.ToString());
                Assert.Equal(null, dataFromRedis_2);

                // Get actual connection
                IDatabase actualConnection = GetRealRedisConnection(redisConn);
                // remove data and lock from redis
                actualConnection.KeyDelete(redisConn.Keys.DataKey);
                actualConnection.KeyDelete(redisConn.Keys.LockKey);
                DisposeRedisConnectionWrapper(redisConn);
            }
        }
Exemplo n.º 5
0
        public void TryUpdateIfLockIdMatch_ExpiryTime_OnValidData()
        {
            ProviderConfiguration pc = Utility.GetDefaultConfigUtility();

            using (RedisServer redisServer = new RedisServer())
            {
                RedisConnectionWrapper redisConn = GetRedisConnectionWrapperWithUniqueSession();

                // Inserting data into redis server
                ChangeTrackingSessionStateItemCollection data = new ChangeTrackingSessionStateItemCollection(new RedisUtility(pc));
                data["key"]  = "value";
                data["key1"] = "value1";
                redisConn.Set(data, 900);

                // Check that data shoud exists
                int      lockTimeout = 90;
                DateTime lockTime    = DateTime.Now;
                object   lockId;
                ISessionStateItemCollection dataFromRedis;
                int sessionTimeout;
                redisConn.TryTakeWriteLockAndGetData(lockTime, lockTimeout, out lockId, out dataFromRedis, out sessionTimeout);
                Assert.Equal(2, dataFromRedis.Count);

                // Update expiry time to only 1 sec and than verify that.
                redisConn.TryUpdateAndReleaseLock(lockId, dataFromRedis, 1);

                // Wait for 1.1 seconds so that data will expire
                System.Threading.Thread.Sleep(1100);

                // Get data blob from redis
                IDatabase   actualConnection = GetRealRedisConnection(redisConn);
                HashEntry[] sessionDataFromRedisAfterExpire = actualConnection.HashGetAll(redisConn.Keys.DataKey);

                // Check that data shoud not be there
                Assert.Empty(sessionDataFromRedisAfterExpire);
                DisposeRedisConnectionWrapper(redisConn);
            }
        }
        public void Set_ValidData()
        {
            // this also tests host:port config part
            ProviderConfiguration pc = Utility.GetDefaultConfigUtility();

            pc.ApplicationName = "APPTEST";
            pc.Port            = 6379;

            using (RedisServer redisServer = new RedisServer())
            {
                RedisConnectionWrapper redisConn = GetRedisConnectionWrapperWithUniqueSession(pc);

                // Inserting data into redis server
                ChangeTrackingSessionStateItemCollection data = new ChangeTrackingSessionStateItemCollection(new RedisUtility(pc));
                data["key"]  = "value";
                data["key1"] = "value1";
                redisConn.Set(data, 900);

                // Get actual connection and get data blob from redis
                IDatabase   actualConnection     = GetRealRedisConnection(redisConn);
                HashEntry[] sessionDataFromRedis = actualConnection.HashGetAll(redisConn.Keys.DataKey);

                // Check that data shoud be same as what inserted
                Assert.Equal(2, sessionDataFromRedis.Length);
                ChangeTrackingSessionStateItemCollection dataFromRedis = new ChangeTrackingSessionStateItemCollection(new RedisUtility(pc));
                foreach (HashEntry entry in sessionDataFromRedis)
                {
                    dataFromRedis[entry.Name] = RedisUtility.GetObjectFromBytes(entry.Value).ToString();
                }
                Assert.Equal("value", dataFromRedis["key"]);
                Assert.Equal("value1", dataFromRedis["key1"]);

                // remove data from redis
                actualConnection.KeyDelete(redisConn.Keys.DataKey);
                DisposeRedisConnectionWrapper(redisConn);
            }
        }
        public void TryReleaseLockIfLockIdMatch_InvalidWriteLockRelease()
        {
            ProviderConfiguration pc = Utility.GetDefaultConfigUtility();

            using (RedisServer redisServer = new RedisServer())
            {
                RedisConnectionWrapper redisConn = GetRedisConnectionWrapperWithUniqueSession();

                // Inserting data into redis server
                ChangeTrackingSessionStateItemCollection data = new ChangeTrackingSessionStateItemCollection(new RedisUtility(pc));
                data["key"] = "value";
                redisConn.Set(data, 900);

                int lockTimeout = 900;

                DateTime lockTime = DateTime.Now;
                object   lockId;
                ISessionStateItemCollection dataFromRedis;
                int sessionTimeout;
                Assert.True(redisConn.TryTakeWriteLockAndGetData(lockTime, lockTimeout, out lockId, out dataFromRedis, out sessionTimeout));
                Assert.Equal(lockTime.Ticks.ToString(), lockId.ToString());
                Assert.Equal(1, dataFromRedis.Count);

                object wrongLockId = lockTime.AddSeconds(1).Ticks.ToString();
                redisConn.TryReleaseLockIfLockIdMatch(wrongLockId, 900);

                // Get actual connection and check that lock do not exists
                IDatabase actualConnection   = GetRealRedisConnection(redisConn);
                string    lockValueFromRedis = actualConnection.StringGet(redisConn.Keys.LockKey);
                Assert.Equal(lockId, lockValueFromRedis);

                // remove data from redis
                actualConnection.KeyDelete(redisConn.Keys.DataKey);
                actualConnection.KeyDelete(redisConn.Keys.LockKey);
                DisposeRedisConnectionWrapper(redisConn);
            }
        }
        public void TryTakeReadLockAndGetData_WithOtherWriteLock()
        {
            ProviderConfiguration pc = Utility.GetDefaultConfigUtility();

            using (RedisServer redisServer = new RedisServer())
            {
                RedisConnectionWrapper redisConn = GetRedisConnectionWrapperWithUniqueSession();

                // Inserting data into redis server
                ChangeTrackingSessionStateItemCollection data = new ChangeTrackingSessionStateItemCollection(new RedisUtility(pc));
                data["key"] = "value";
                redisConn.Set(data, 900);

                int lockTimeout = 900;

                DateTime lockTime_1 = DateTime.Now;
                object   lockId_1;
                ISessionStateItemCollection dataFromRedis_1;
                int sessionTimeout;
                Assert.True(redisConn.TryTakeWriteLockAndGetData(lockTime_1, lockTimeout, out lockId_1, out dataFromRedis_1, out sessionTimeout));
                Assert.Equal(lockTime_1.Ticks.ToString(), lockId_1.ToString());
                Assert.Equal(1, dataFromRedis_1.Count);

                object lockId_2;
                ISessionStateItemCollection dataFromRedis_2;
                Assert.False(redisConn.TryCheckWriteLockAndGetData(out lockId_2, out dataFromRedis_2, out sessionTimeout));
                Assert.Equal(lockTime_1.Ticks.ToString(), lockId_2.ToString());
                Assert.Equal(null, dataFromRedis_2);

                // Get actual connection
                // remove data and lock from redis
                IDatabase actualConnection = GetRealRedisConnection(redisConn);
                actualConnection.KeyDelete(redisConn.Keys.DataKey);
                actualConnection.KeyDelete(redisConn.Keys.LockKey);
                DisposeRedisConnectionWrapper(redisConn);
            }
        }
        public void TryTakeWriteLockAndGetData_ExpireWriteLock()
        {
            ProviderConfiguration pc = Utility.GetDefaultConfigUtility();

            using (RedisServer redisServer = new RedisServer())
            {
                RedisConnectionWrapper redisConn = GetRedisConnectionWrapperWithUniqueSession();

                // Inserting data into redis server
                ChangeTrackingSessionStateItemCollection data = new ChangeTrackingSessionStateItemCollection(new RedisUtility(pc));
                data["key"] = "value";
                redisConn.Set(data, 900);

                int lockTimeout = 1;

                DateTime lockTime = DateTime.Now;
                object   lockId;
                ISessionStateItemCollection dataFromRedis;
                int sessionTimeout;
                Assert.True(redisConn.TryTakeWriteLockAndGetData(lockTime, lockTimeout, out lockId, out dataFromRedis, out sessionTimeout));
                Assert.Equal(lockTime.Ticks.ToString(), lockId.ToString());
                Assert.Equal(1, dataFromRedis.Count);

                // Wait for 2 seconds so that lock will expire
                System.Threading.Thread.Sleep(1100);

                // Get actual connection and check that lock do not exists
                IDatabase actualConnection   = GetRealRedisConnection(redisConn);
                string    lockValueFromRedis = actualConnection.StringGet(redisConn.Keys.LockKey);
                Assert.Equal(null, lockValueFromRedis);

                // remove data from redis
                actualConnection.KeyDelete(redisConn.Keys.DataKey);
                DisposeRedisConnectionWrapper(redisConn);
            }
        }
        public void Set_ExpireData()
        {
            ProviderConfiguration pc = Utility.GetDefaultConfigUtility();

            using (RedisServer redisServer = new RedisServer())
            {
                RedisConnectionWrapper redisConn = GetRedisConnectionWrapperWithUniqueSession();
                // Inserting data into redis server that expires after 1 second
                ChangeTrackingSessionStateItemCollection data = new ChangeTrackingSessionStateItemCollection(new RedisUtility(pc));
                data["key"] = "value";
                redisConn.Set(data, 1);

                // Wait for 2 seconds so that data will expire
                System.Threading.Thread.Sleep(1100);

                // Get actual connection and get data blob from redis
                IDatabase   actualConnection     = GetRealRedisConnection(redisConn);
                HashEntry[] sessionDataFromRedis = actualConnection.HashGetAll(redisConn.Keys.DataKey);

                // Check that data shoud not be there
                Assert.Equal(0, sessionDataFromRedis.Length);
                DisposeRedisConnectionWrapper(redisConn);
            }
        }
Exemplo n.º 11
0
        public async Task SessionWriteCycle_Valid()
        {
            using (RedisServer redisServer = new RedisServer())
            {
                string sessionId = ResetRedisConnectionWrapperAndConfiguration();

                // Inserting empty session with "SessionStateActions.InitializeItem" flag into redis server
                RedisSessionStateProvider ssp = new RedisSessionStateProvider();
                await ssp.CreateUninitializedItemAsync(null, sessionId, (int)RedisSessionStateProvider.configuration.SessionTimeout.TotalMinutes, CancellationToken.None);

                // Get write lock and session from cache
                GetItemResult data = await ssp.GetItemExclusiveAsync(null, sessionId, CancellationToken.None);

                // Get actual connection and varify lock and session timeout
                IDatabase actualConnection = GetRealRedisConnection();
                Assert.Equal(data.LockId.ToString(), actualConnection.StringGet(ssp.cache.Keys.LockKey).ToString());
                Assert.Equal(((int)RedisSessionStateProvider.configuration.SessionTimeout.TotalSeconds).ToString(), actualConnection.HashGet(ssp.cache.Keys.InternalKey, "SessionTimeout").ToString());

                // setting data as done by any normal session operation
                data.Item.Items["key"] = "value";

                // session update
                await ssp.SetAndReleaseItemExclusiveAsync(null, sessionId, data.Item, data.LockId, false, CancellationToken.None);

                Assert.Single(actualConnection.HashGetAll(ssp.cache.Keys.DataKey));

                // reset sessions timoue
                await ssp.ResetItemTimeoutAsync(null, sessionId, CancellationToken.None);

                // End request
                await ssp.EndRequestAsync(null);

                // remove data and lock from redis
                DisposeRedisConnectionWrapper();
            }
        }
 public void RemoveWithoutSetTest()
 {
     using (RedisServer Server = new RedisServer())
     {
         RedisOutputCacheProvider provider = new RedisOutputCacheProvider();
         NameValueCollection config = new NameValueCollection();
         config.Add("ssl", "false");
         provider.Initialize("name", config);
         provider.Remove("key6");
         object data = provider.Get("key6");
         Assert.Equal(null, data);
     }
 }