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")));
        }
Exemple #2
0
        public void WaitForJob_WaitForTheTimeout()
        {
            //Arrange
            Stopwatch sw           = new Stopwatch();
            var       subscription = new RedisSubscription(_storage, RedisUtils.CreateSubscriber());
            var       timeout      = TimeSpan.FromMilliseconds(100);

            sw.Start();

            //Act
            subscription.WaitForJob(timeout, _cts.Token);

            //Assert
            sw.Stop();
            Assert.InRange(sw.ElapsedMilliseconds, 99, 120);
        }
        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")));
            }
        }
Exemple #4
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_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"));
        }
        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"));
        }
Exemple #8
0
        public void Execute_EnqueuesCheckedAndTimedOutJob_IfNoFetchedFlagSet()
        {
            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)));

            var watcher = CreateWatcher();

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

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

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

            Assert.False(job.Any(x => x.Name == "Checked"));
        }
        public void Execute_EnqueuesCheckedAndTimedOutJob_IfNoFetchedFlagSet()
        {
            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)));

                var watcher = CreateWatcher();

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

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

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

            var sync1 = new ManualResetEventSlim();
            var sync2 = new ManualResetEventSlim();

            var thread1 = new Thread(state =>
            {
                using (var testLock1 = RedisLock.Acquire(db, "testLock", TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(110)))
                {
                    Assert.NotNull(testLock1);

                    // sleep a bit more than holdDuration
                    Thread.Sleep(250);
                    sync1.Set();
                    sync2.Wait();
                }
            });

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

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

            thread1.Start();
            thread2.Start();
            thread2.Join();
            sync2.Set();
            thread1.Join();
        }
        private static void UseRedis(Action <IDatabase> action)
        {
            var redis = RedisUtils.CreateClient();

            action(redis);
        }
 public RedisTest()
 {
     _redis = RedisUtils.CreateClient();
 }
 public FetchedJobsWatcherFacts()
 {
     _storage = new RedisStorage(RedisUtils.GetHostAndPort(), RedisUtils.GetDb());
     _cts     = new CancellationTokenSource();
     _cts.Cancel();
 }
 private RedisStorage CreateStorage()
 {
     return(new RedisStorage(RedisUtils.GetHostAndPort(), RedisUtils.GetDb()));
 }
Exemple #15
0
        public void DbFromConnectionStringIsUsed()
        {
            var storage = new RedisStorage(String.Format("{0},defaultDatabase=5", RedisUtils.GetHostAndPort()));

            Assert.Equal(storage.Db, 5);
        }