示例#1
0
 private static void UseConnection(Action <IRedisClient> action)
 {
     using (var redis = RedisUtils.CreateClient())
     {
         action(redis);
     }
 }
 private void UseConnection(Action <RedisConnection> action)
 {
     using (var connection = new RedisConnection(RedisUtils.CreateClient()))
     {
         action(connection);
     }
 }
        public override void Before(MethodInfo methodUnderTest)
        {
            Monitor.Enter(GlobalLock);
            var client = RedisUtils.CreateClient();

            client.Multiplexer.GetServer(RedisUtils.GetHostAndPort()).FlushDatabase(RedisUtils.GetDb());
        }
        public void Execute_LeavesPingedJob_IfPingFlagSetLongerThanInvisibilityPeriod()
        {
            var redis = RedisUtils.CreateClient();

            // Arrange
            redis.SetAdd("{hangfire}:queues", "my-queue");
            redis.ListRightPush("{hangfire}:queue:my-queue:dequeued", "my-job");
            redis.HashSet("{hangfire}:job:my-job", "Checked",
                          JobHelper.SerializeDateTime(DateTime.UtcNow.AddDays(-1)));
            redis.HashSet("{hangfire}:job:my-job", "Ping",
                          JobHelper.SerializeDateTime(DateTime.UtcNow));

            var watcher = CreateWatcher();

            // Act
            watcher.Execute(_cts.Token);

            // Assert
            Assert.Equal(1, redis.ListLength("{hangfire}:queue:my-queue:dequeued"));
            Assert.Equal(0, redis.ListLength("{hangfire}:queue:my-queue"));

            var job = redis.HashGetAll("{hangfire}:job:my-job");

            Assert.Contains(job, x => x.Name == "Ping");
        }
        public void TwoContendingLocks()
        {
            var db = RedisUtils.CreateClient();

            using (var testLock1 = new RedisLock(db, "testLock", "owner", TimeSpan.FromMilliseconds(100)))
                Assert.Throws <TimeoutException>(() => new RedisLock(db, "testLock", "otherOwner", TimeSpan.FromMilliseconds(10)));
        }
示例#6
0
        public void Execute_EnqueuesTimedOutJobs_AndDeletesThemFromFetchedList()
        {
            var redis = RedisUtils.CreateClient();

            // Arrange
            redis.SetAdd("{hangfire}:queues", "my-queue");
            redis.ListRightPush("{hangfire}:queue:my-queue:dequeued", "my-job");
            redis.HashSet("{hangfire}:job:my-job", "Fetched",
                          JobHelper.SerializeDateTime(DateTime.UtcNow.AddDays(-1)));

            var watcher = CreateWatcher();

            // Act
            watcher.Execute(_cts.Token);

            // Assert
            Assert.Equal(0, redis.ListLength("{hangfire}:queue:my-queue:dequeued"));

            var listEntry = (string)redis.ListRightPop("{hangfire}:queue:my-queue");

            Assert.Equal("my-job", listEntry);

            var job = redis.HashGetAll("{hangfire}:job:my-job");

            Assert.False(job.Any(x => x.Name == "Fetched"));
        }
        public void Execute_EnqueuesTimedOutJobs_AndDeletesThemFromFetchedList()
        {
            using (var redis = RedisUtils.CreateClient())
            {
                // Arrange
                redis.AddItemToSet("hangfire:queues", "my-queue");
                redis.AddItemToList("hangfire:queue:my-queue:dequeued", "my-job");
                redis.SetEntryInHash("hangfire:job:my-job", "Fetched",
                                     JobHelper.SerializeDateTime(DateTime.UtcNow.AddDays(-1)));

                var watcher = CreateWatcher();

                // Act
                watcher.Execute(_cts.Token);

                // Assert
                Assert.Equal(0, redis.GetListCount("hangfire:queue:my-queue:dequeued"));

                var listEntry = redis.DequeueItemFromList("hangfire:queue:my-queue");
                Assert.Equal("my-job", listEntry);

                var job = redis.GetAllEntriesFromHash("hangfire:job:my-job");
                Assert.False(job.ContainsKey("Fetched"));
            }
        }
        public void AcquireFromMultipleThreads()
        {
            var db = RedisUtils.CreateClient();

            var sync = new ManualResetEventSlim();

            var thread1 = new Thread(state =>
            {
                using (var testLock1 = RedisLock.Acquire(db, "test", TimeSpan.FromMilliseconds(50)))
                {
                    // ensure nested lock release doesn't release parent lock
                    using (var testLock2 = RedisLock.Acquire(db, "test", TimeSpan.FromMilliseconds(50)))
                    { }

                    sync.Set();
                    Thread.Sleep(200);
                }
            });

            var thread2 = new Thread(state =>
            {
                Assert.True(sync.Wait(1000));

                Assert.Throws <DistributedLockTimeoutException>(() =>
                {
                    using (var testLock2 = RedisLock.Acquire(db, "test", TimeSpan.FromMilliseconds(50)))
                    { }
                });
            });

            thread1.Start();
            thread2.Start();
            thread1.Join();
            thread2.Join();
        }
示例#9
0
 private void UseConnection(Action <RedisConnection> action)
 {
     using (var connection = new RedisConnection(RedisUtils.CreateClient(), RedisUtils.CreateSubscriber(), Guid.NewGuid().ToString()))
     {
         action(connection);
     }
 }
示例#10
0
 private void UseConnections(Action <IRedisClient, RedisConnection> action)
 {
     using (var redis = RedisUtils.CreateClient())
         using (var connection = new RedisConnection(redis))
         {
             action(redis, connection);
         }
 }
示例#11
0
        public override void Before(MethodInfo methodUnderTest)
        {
            Monitor.Enter(GlobalLock);

            using (var client = RedisUtils.CreateClient())
            {
                client.FlushDb();
            }
        }
        private void UseConnections(Action <IDatabase, RedisConnection> action)
        {
            var redis = RedisUtils.CreateClient();

            using (var connection = new RedisConnection(redis, RedisUtils.CreateSubscriber()))
            {
                action(redis, connection);
            }
        }
        public void AcquireAndDisposeLock()
        {
            var db = RedisUtils.CreateClient();

            using (var testLock = new RedisLock(db, "testLock", "owner", TimeSpan.FromMilliseconds(1)))
                Assert.NotNull(testLock);
            using (var testLock = new RedisLock(db, "testLock", "owner", TimeSpan.FromMilliseconds(1)))
                Assert.NotNull(testLock);
        }
        public void AcquireInSequence()
        {
            var db = RedisUtils.CreateClient();

            using (var testLock = RedisLock.Acquire(db, "testLock", TimeSpan.FromMilliseconds(1)))
                Assert.NotNull(testLock);
            using (var testLock = RedisLock.Acquire(db, "testLock", TimeSpan.FromMilliseconds(1)))
                Assert.NotNull(testLock);
        }
        public void NestLock()
        {
            var db = RedisUtils.CreateClient();

            using (var testLock1 = new RedisLock(db, "testLock", "owner", TimeSpan.FromMilliseconds(100)))
            {
                using (var testLock2 = new RedisLock(db, "testLock", "owner", TimeSpan.FromMilliseconds(100)))
                { }
            }
        }
        private void UseConnection(Action <RedisConnection> action)
        {
            var redis        = RedisUtils.CreateClient();
            var subscription = new RedisSubscription(RedisUtils.CreateSubscriber());

            using (var connection = new RedisConnection(redis, subscription, Guid.NewGuid().ToString(), new RedisStorageOptions().FetchTimeout))
            {
                action(connection);
            }
        }
示例#17
0
        private void UseConnections(Action <IDatabase, RedisConnection> action)
        {
            var redis        = RedisUtils.CreateClient();
            var subscription = new RedisSubscription(_storage, RedisUtils.CreateSubscriber());

            using (var connection = new RedisConnection(_storage, redis, subscription, new RedisStorageOptions().FetchTimeout))
            {
                action(redis, connection);
            }
        }
        public void GetLockAfterAnotherTimeout()
        {
            var db = RedisUtils.CreateClient();

            using (var testLock = new RedisLock(db, "testLock", "owner", TimeSpan.FromMilliseconds(50)))
            {
                Thread.Sleep(60);
                using (var testLock2 = new RedisLock(db, "testLock", "owner", TimeSpan.FromMilliseconds(10)))
                    Assert.NotNull(testLock2);
            }
        }
        public void ExtendsALock()
        {
            var db = RedisUtils.CreateClient();

            using (var testLock = new RedisLock(db, "testLock", "owner", TimeSpan.FromMilliseconds(100)))
            {
                Assert.NotNull(testLock);
                using (var testLock2 = new RedisLock(db, "testLock", "owner", TimeSpan.FromMilliseconds(10)))
                    Assert.NotNull(testLock2);
            }
        }
示例#20
0
        public void MultiThreadLock()
        {
            var db = RedisUtils.CreateClient();

            int concurrentCount = 0;

            Task[] tasks = new Task[2];
            tasks[0] = Task.Factory.StartNew(() => MultiThreadLockWorker.DoWork(db, "testLock", TimeSpan.FromMilliseconds(300), TimeSpan.FromMilliseconds(300), ref concurrentCount));
            tasks[1] = Task.Factory.StartNew(() => MultiThreadLockWorker.DoWork(db, "testLock", TimeSpan.FromMilliseconds(5), TimeSpan.FromMilliseconds(10), ref concurrentCount));
            Task.WaitAll(tasks);
            Assert.Equal <int>(1, concurrentCount);
        }
        public async Task AcquireFromNestedTask()
        {
            var db = RedisUtils.CreateClient();

            using (var lock1 = RedisLock.Acquire(db, "test", TimeSpan.FromMilliseconds(50)))
            {
                Assert.NotNull(lock1);

                await Task.Delay(100);

                await Task.Run(() => NestedTask(db));
            }
        }
        public void AcquireNested()
        {
            var db = RedisUtils.CreateClient();

            using (var testLock1 = RedisLock.Acquire(db, "testLock", TimeSpan.FromMilliseconds(100)))
            {
                Assert.NotNull(testLock1);

                using (var testLock2 = RedisLock.Acquire(db, "testLock", TimeSpan.FromMilliseconds(100)))
                {
                    Assert.NotNull(testLock2);
                }
            }
        }
        public void ReleaseNestedAfterTimeout()
        {
            var db = RedisUtils.CreateClient();

            using (var testLock1 = new RedisLock(db, "testLock", "owner", TimeSpan.FromMilliseconds(50)))
            {
                using (var testLock2 = new RedisLock(db, "testLock", "owner", TimeSpan.FromMilliseconds(1)))
                { }

                Thread.Sleep(60);
                using (var testLock2 = new RedisLock(db, "testLock", "otherOwner", TimeSpan.FromMilliseconds(1))) { };
                Assert.True(true);
            }
        }
        public void NestLockDisposePreservesRoot()
        {
            var db = RedisUtils.CreateClient();

            using (var testLock1 = new RedisLock(db, "testLock", "owner", TimeSpan.FromMilliseconds(1000)))
            {
                using (var testLock2 = new RedisLock(db, "testLock", "owner", TimeSpan.FromMilliseconds(1000)))
                { }

                Assert.Throws <TimeoutException>(() =>
                {
                    using (var testLock2 = new RedisLock(db, "testLock", "otherOwner", TimeSpan.FromMilliseconds(1))) { };
                });
            }
        }
示例#25
0
        public void Execute_MarksDequeuedJobAsChecked_IfItHasNoFetchedFlagSet()
        {
            var redis = RedisUtils.CreateClient();

            // Arrange
            redis.SetAdd("{hangfire}:queues", "my-queue");
            redis.ListRightPush("{hangfire}:queue:my-queue:dequeued", "my-job");

            var watcher = CreateWatcher();

            // Act
            watcher.Execute(_cts.Token);

            Assert.NotNull(JobHelper.DeserializeNullableDateTime(
                               redis.HashGet("{hangfire}:job:my-job", "Checked")));
        }
        public void Execute_MarksDequeuedJobAsChecked_IfItHasNoFetchedFlagSet()
        {
            using (var redis = RedisUtils.CreateClient())
            {
                // Arrange
                redis.AddItemToSet("hangfire:queues", "my-queue");
                redis.AddItemToList("hangfire:queue:my-queue:dequeued", "my-job");

                var watcher = CreateWatcher();

                // Act
                watcher.Execute(_cts.Token);

                Assert.NotNull(JobHelper.DeserializeNullableDateTime(
                                   redis.GetValueFromHash("hangfire:job:my-job", "Checked")));
            }
        }
        public void Execute_DeletesNonExistingJobs()
        {
            var redis = RedisUtils.CreateClient();

            Assert.Equal(0, redis.ListLength("{hangfire}:succeeded"));
            Assert.Equal(0, redis.ListLength("{hangfire}:deleted"));

            // Arrange
            redis.ListRightPush("{hangfire}:succeded", "my-job");
            redis.ListRightPush("{hangfire}:deleted", "other-job");

            var watcher = CreateWatcher();

            // Act
            watcher.Execute(_cts.Token);

            // Assert
            Assert.Equal(0, redis.ListLength("{hangfire}:succeeded"));
            Assert.Equal(0, redis.ListLength("{hangfire}:deleted"));
        }
示例#28
0
        public void Execute_DoesNotEnqueueTimedOutByCheckedFlagJob_IfFetchedFlagSet()
        {
            var redis = RedisUtils.CreateClient();

            // Arrange
            redis.SetAdd("{hangfire}:queues", "my-queue");
            redis.ListRightPush("{hangfire}:queue:my-queue:dequeued", "my-job");
            redis.HashSet("{hangfire}:job:my-job", "Checked",
                          JobHelper.SerializeDateTime(DateTime.UtcNow.AddDays(-1)));
            redis.HashSet("{hangfire}:job:my-job", "Fetched",
                          JobHelper.SerializeDateTime(DateTime.UtcNow));

            var watcher = CreateWatcher();

            // Act
            watcher.Execute(_cts.Token);

            // Assert
            Assert.Equal(1, redis.ListLength("{hangfire}:queue:my-queue:dequeued"));
        }
        public void Execute_DoesNotEnqueueTimedOutByCheckedFlagJob_IfFetchedFlagSet()
        {
            using (var redis = RedisUtils.CreateClient())
            {
                // Arrange
                redis.AddItemToSet("hangfire:queues", "my-queue");
                redis.AddItemToList("hangfire:queue:my-queue:dequeued", "my-job");
                redis.SetEntryInHash("hangfire:job:my-job", "Checked",
                                     JobHelper.SerializeDateTime(DateTime.UtcNow.AddDays(-1)));
                redis.SetEntryInHash("hangfire:job:my-job", "Fetched",
                                     JobHelper.SerializeDateTime(DateTime.UtcNow));

                var watcher = CreateWatcher();

                // Act
                watcher.Execute(_cts.Token);

                // Assert
                Assert.Equal(1, redis.GetListCount("hangfire:queue:my-queue:dequeued"));
            }
        }
        public void Execute_DoesNotDeleteExistingJobs()
        {
            var redis = RedisUtils.CreateClient();

            // Arrange
            redis.ListRightPush("{hangfire}:succeeded", "my-job");
            redis.HashSet("{hangfire}:job:my-job", "Fetched",
                          JobHelper.SerializeDateTime(DateTime.UtcNow.AddDays(-1)));

            redis.ListRightPush("{hangfire}:deleted", "other-job");
            redis.HashSet("{hangfire}:job:other-job", "Fetched",
                          JobHelper.SerializeDateTime(DateTime.UtcNow.AddDays(-1)));

            var watcher = CreateWatcher();

            // Act
            watcher.Execute(_cts.Token);

            // Assert
            Assert.Equal(1, redis.ListLength("{hangfire}:succeeded"));
            Assert.Equal(1, redis.ListLength("{hangfire}:deleted"));
        }