public void TestEnqueueAndDelete100Tasks()
        {
            var client = new QueueClient();

            for (var i = 0; i < 100; i++)
            {
                client.Enqueue(new TaskMessage
                {
                    Parameters = "params",
                    Queue      = "TestQueue"
                });
            }

            Assert.AreEqual(client.AllTasks("TestQueue").Count, 100);

            var deletedTasks = 0;

            while (client.AllTasks("TestQueue").Count > 0)
            {
                client.RemoveTask(client.AllTasks("TestQueue")[0]);
                deletedTasks++;
            }

            Assert.AreEqual(deletedTasks, 100);
        }
        public void TestHostDifferentPort()
        {
            var stopWatch = new Stopwatch();

            try
            {
                using (var client = new QueueClient("localhost", 6377, false))
                {
                    var homemadeTask = new TaskMessage
                    {
                        Parameters = "params",
                        Queue      = "TestQueue"
                    };

                    stopWatch.Start();
                    client.Enqueue(homemadeTask);
                    stopWatch.Stop();
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine("Elapsed time: {0} ms", stopWatch.ElapsedMilliseconds);
                Console.WriteLine(exception.ToString());
                Assert.Pass();
            }
        }
        public void TestModifyTaskOnCollectionThenAssertModificationDoesNotWork()
        {
            using (var client = new QueueClient())
            {
                // first enqueue a simple task.
                client.Enqueue(new TaskMessage
                {
                    Parameters = "Test Params",
                    Queue      = "TestQueue"
                });

                // now modify the task from within the collection.
                client.PendingTasks("TestQueue")[0].Parameters = "Test Params 2";
            }

            // Dispose of the previous client to ensure modifications dont persist in its memory.
            using (var client = new QueueClient())
            {
                // using a new client, read the task again.
                var task = client.PendingTasks("TestQueue")[0];

                Assert.AreEqual(task.Parameters, "Test Params");

                client.RemoveTask(client.AllTasks("TestQueue")[0]);
            }
        }
        public void TestHostUnreachableResponseTime()
        {
            RedisServer.Kill();

            var stopWatch = new Stopwatch();

            try
            {
                using (var client = new QueueClient())
                {
                    var homemadeTask = new TaskMessage
                    {
                        Parameters = "params",
                        Queue = "TestQueue"
                    };

                    stopWatch.Start();
                    client.Enqueue(homemadeTask);
                    stopWatch.Stop();
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine("Elapsed time: {0} ms", stopWatch.ElapsedMilliseconds);
                Console.WriteLine(exception.ToString());
                RedisServer.Start();
                Assert.Pass();
            }
        }
        public void TestHostUnreachableResponseTime()
        {
            RedisServer.Kill();

            var stopWatch = new Stopwatch();

            try
            {
                using (var client = new QueueClient())
                {
                    var homemadeTask = new TaskMessage
                    {
                        Parameters = "params",
                        Queue      = "TestQueue"
                    };

                    stopWatch.Start();
                    client.Enqueue(homemadeTask);
                    stopWatch.Stop();
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine("Elapsed time: {0} ms", stopWatch.ElapsedMilliseconds);
                Console.WriteLine(exception.ToString());
                RedisServer.Start();
                Assert.Pass();
            }
        }
 public void TestCantEnqueueWithoutSettingAQueueInTheTaskMessage()
 {
     using (var client = new QueueClient())
         Assert.Throws <NoQueueSpecifiedException>(
             () => client.Enqueue(new TaskMessage {
             Parameters = "Test"
         }));
 }
        public void TestWriteLatencyForDifferentPayloads()
        {
            var stopWatch = new Stopwatch();

            using (var client = new QueueClient())
            {
                // Small message
                var homemadeTask = new TaskMessage
                {
                    Parameters = RandomString(1024),
                    Queue      = "TestQueue"
                };

                stopWatch.Start();
                client.Enqueue(homemadeTask);
                stopWatch.Stop();

                Console.WriteLine("1K Message write latency (no network included): {0} ms", stopWatch.ElapsedMilliseconds);

                // Medium message
                homemadeTask = new TaskMessage
                {
                    Parameters = RandomString(1024 * 10),
                    Queue      = "TestQueue"
                };

                stopWatch.Start();
                client.Enqueue(homemadeTask);
                stopWatch.Stop();

                Console.WriteLine("10K Message write latency (no network included): {0} ms", stopWatch.ElapsedMilliseconds);

                // Large message
                homemadeTask = new TaskMessage
                {
                    Parameters = RandomString(1024 * 100),
                    Queue      = "TestQueue"
                };

                stopWatch.Start();
                client.Enqueue(homemadeTask);
                stopWatch.Stop();

                Console.WriteLine("100K Message write latency (no network included): {0} ms", stopWatch.ElapsedMilliseconds);
            }
        }
        public void TestMonitorCanRecoverFromRedisOutage()
        {
            var task = new TaskMessage
            {
                Parameters = "blah",
                Queue      = "TestQueue"
            };

            var task2 = new TaskMessage
            {
                Parameters = "blah",
                Queue      = "TestQueue"
            };

            var performerMock = new Mock <Performer>();

            performerMock.SetupGet(x => x.Status).Returns(new PerformResult
            {
                Data    = string.Empty,
                Outcome = Outcome.Success,
                Reason  = string.Empty
            });

            performerMock.Setup(x => x.Perform(task.Parameters));
            performerMock.Setup(x => x.Perform(task2.Parameters));

            var monitor = new RedisMonitor();

            monitor.Performer = performerMock.Object;

            monitor.Start();
            Assert.IsTrue(monitor.Running);

            using (var client = new QueueClient())
                client.Enqueue(task);

            Thread.Sleep(1500);
            RedisServer.Kill();

            Thread.Sleep(100);
            RedisServer.Start();

            Thread.Sleep(2000);
            using (var client = new QueueClient())
                client.Enqueue(task2);

            Thread.Sleep(10000);
            monitor.Stop();

            Assert.IsFalse(monitor.Running);
        }
        public void TestThrowsExceptionOnRedisDeath()
        {
            var client = new QueueClient();

            RedisServer.Kill();

            Assert.Throws <IOException>(() => client.Enqueue(new TaskMessage
            {
                Parameters = "params",
                Queue      = "TestQueue"
            }));

            RedisServer.Start();
        }
        public void TestCantReserveMultipleTasksAtOnce()
        {
            using (var client = new QueueClient())
            {
                client.Enqueue(new TaskMessage
                {
                    Parameters = "params",
                    Queue      = "TestQueue"
                });

                client.Enqueue(new TaskMessage
                {
                    Parameters = "params",
                    Queue      = "TestQueue"
                });

                client.Reserve("TestQueue");
                Assert.Throws <TaskAlreadyReservedException>(() => client.Reserve("TestQueue"));

                client.Fail(string.Empty);
                client.RemoveTask(client.AllTasks("TestQueue")[0]);
                client.RemoveTask(client.AllTasks("TestQueue")[0]);
            }
        }
        public void TestProcessPendingTasksForMultipleTasksMockingPerformerOnly()
        {
            #region Prepare Tasks and mock Performer for processing
            var monitor       = new RedisMonitor();
            var performerMock = new Mock <Performer>();

            using (var client = new QueueClient())
            {
                for (var i = 0; i < 10; i++)
                {
                    var task = new TaskMessage
                    {
                        Parameters = "Task " + i,
                        Queue      = "TestQueue"
                    };

                    client.Enqueue(task);
                    performerMock.Setup(x => x.Perform(task.Parameters));
                }
            }

            // ensure all tasks will be successful.
            performerMock.SetupGet(x => x.Status).Returns(new PerformResult
            {
                Data    = string.Empty,
                Outcome = Outcome.Success,
                Reason  = string.Empty
            });
            #endregion

            monitor.Performer = performerMock.Object;
            monitor.Start();
            Thread.Sleep(2000);
            monitor.Stop();


            for (var i = 0; i < 10; i++)
            {
                performerMock.Verify(x => x.Perform("Task " + i));
            }

            performerMock.VerifyGet(x => x.TaskStorage);
            performerMock.VerifyGet(x => x.Status);

            using (var client = new QueueClient())
                Assert.AreEqual(client.PendingTasks("TestQueue").Count, 0);
        }
		public void TestCachingCreatesAFile()
		{
			using (var client = new QueueClient())
			{
				var homemadeTask = new TaskMessage
				{
					Parameters = "params",
					Queue = "TestQueue"
				};

				client.Enqueue(homemadeTask);
				var retrievedTask = client.Reserve("TestQueue");

				Assert.That(File.Exists("redisCache.bin"));

				client.Fail(string.Empty);
				client.RemoveTask(client.AllTasks("TestQueue")[0]);
			}
		}
        public void TestCachingCreatesAFile()
        {
            using (var client = new QueueClient())
            {
                var homemadeTask = new TaskMessage
                {
                    Parameters = "params",
                    Queue      = "TestQueue"
                };

                client.Enqueue(homemadeTask);
                var retrievedTask = client.Reserve("TestQueue");

                Assert.That(File.Exists("redisCache.bin"));

                client.Fail(string.Empty);
                client.RemoveTask(client.AllTasks("TestQueue")[0]);
            }
        }
        public void TestCanEnqueueTaskInRedisAndReadItBack()
        {
            using (var client = new QueueClient())
            {
                var homemadeTask = new TaskMessage
                {
                    Parameters = "params",
                    Queue      = "TestQueue"
                };

                client.Enqueue(homemadeTask);
                var retrievedTask = client.Reserve("TestQueue");

                Assert.AreEqual(retrievedTask.Parameters, homemadeTask.Parameters);
                Assert.AreEqual(retrievedTask.Queue, homemadeTask.Queue);

                client.Fail(string.Empty);
                client.RemoveTask(client.AllTasks("TestQueue")[0]);
            }
        }
		public void TestCanEnqueueTaskInRedisAndReadItBack()
		{
			using(var client = new QueueClient())
			{
				var homemadeTask = new TaskMessage
				{
					Parameters = "params", 
					Queue = "TestQueue"
				};

				client.Enqueue(homemadeTask);
				var retrievedTask = client.Reserve("TestQueue");
			
				Assert.AreEqual(retrievedTask.Parameters, homemadeTask.Parameters);
				Assert.AreEqual(retrievedTask.Queue, homemadeTask.Queue);

				client.Fail(string.Empty);
				client.RemoveTask(client.AllTasks("TestQueue")[0]);
			}
		}
        public void TestCachingCanPreserveTask()
        {
            using (var client = new QueueClient())
            {
                var homemadeTask = new TaskMessage
                {
                    Parameters = "params",
                    Queue      = "TestQueue"
                };

                client.Enqueue(homemadeTask);
                var retrievedTask = client.Reserve("TestQueue");
            }

            using (var client = new QueueClient())
            {
                Assert.That(client.CurrentTask != null);
                client.Fail(string.Empty);
                client.RemoveTask(client.AllTasks("TestQueue")[0]);
            }
        }
        public void TestMonitorWakesUpOnMessageReceived()
        {
            var task = new TaskMessage
            {
                Parameters = "blah",
                Queue      = "TestQueue"
            };

            var performerMock = new Mock <Performer>();

            performerMock.SetupGet(x => x.Status).Returns(new PerformResult
            {
                Data    = string.Empty,
                Outcome = Outcome.Success,
                Reason  = string.Empty
            });

            performerMock.Setup(x => x.Perform(task.Parameters));

            var monitor = new RedisMonitor();

            monitor.Performer = performerMock.Object;

            var start = DateTime.Now;

            monitor.Start();
            Assert.IsTrue(monitor.Running);

            using (var client = new QueueClient())
                client.Enqueue(task);

            monitor.Stop();
            Assert.IsFalse(monitor.Running);

            var stop = DateTime.Now;

            Assert.Less((stop - start).TotalSeconds, 60);
        }
        public void TestHostDifferentPort()
        {
            var stopWatch = new Stopwatch();

            try
            {
                using (var client = new QueueClient("localhost", 6377, false))
                {
                    var homemadeTask = new TaskMessage
                    {
                        Parameters = "params",
                        Queue = "TestQueue"
                    };

                    stopWatch.Start();
                    client.Enqueue(homemadeTask);
                    stopWatch.Stop();
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine("Elapsed time: {0} ms", stopWatch.ElapsedMilliseconds);
                Console.WriteLine(exception.ToString());
                Assert.Pass();
            }
        }
        public void TestModifyTaskOnCollectionThenAssertModificationDoesNotWork()
        {
            using (var client = new QueueClient())
            {
                // first enqueue a simple task.
                client.Enqueue(new TaskMessage
                {
                    Parameters = "Test Params",
                    Queue = "TestQueue"
                });

                // now modify the task from within the collection.
                client.PendingTasks("TestQueue")[0].Parameters = "Test Params 2";
            }

            // Dispose of the previous client to ensure modifications dont persist in its memory.
            using (var client = new QueueClient())
            {
                // using a new client, read the task again.
                var task = client.PendingTasks("TestQueue")[0];

                Assert.AreEqual(task.Parameters, "Test Params");

                client.RemoveTask(client.AllTasks("TestQueue")[0]);
            }
        }
        public void TestCantReserveMultipleTasksAtOnce()
        {
            using (var client = new QueueClient())
            {
                client.Enqueue(new TaskMessage
                {
                    Parameters = "params",
                    Queue = "TestQueue"
                });

                client.Enqueue(new TaskMessage
                {
                    Parameters = "params",
                    Queue = "TestQueue"
                });

                client.Reserve("TestQueue");
                Assert.Throws<TaskAlreadyReservedException>(() => client.Reserve("TestQueue"));

                client.Fail(string.Empty);
                client.RemoveTask(client.AllTasks("TestQueue")[0]);
                client.RemoveTask(client.AllTasks("TestQueue")[0]);
            }
        }
 public void TestCantEnqueueWithoutSettingAQueueInTheTaskMessage()
 {
     using (var client = new QueueClient())
         Assert.Throws<NoQueueSpecifiedException>(
             () => client.Enqueue(new TaskMessage { Parameters = "Test" }));
 }
        public void TestThrowsExceptionOnRedisDeath()
        {
            var client = new QueueClient();
            RedisServer.Kill();

            Assert.Throws<IOException>(() => client.Enqueue(new TaskMessage
            {
                Parameters = "params",
                Queue = "TestQueue"
            }));

            RedisServer.Start();
        }
        public void TestEnqueueAndDelete100Tasks()
        {
            var client = new QueueClient();
            for (var i = 0; i < 100; i++)
            {
                client.Enqueue(new TaskMessage
                {
                    Parameters = "params",
                    Queue = "TestQueue"
                });
            }

            Assert.AreEqual(client.AllTasks("TestQueue").Count, 100);

            var deletedTasks = 0;
            while (client.AllTasks("TestQueue").Count > 0)
            {
                client.RemoveTask(client.AllTasks("TestQueue")[0]);
                deletedTasks++;
            }

            Assert.AreEqual(deletedTasks, 100);
        }
        public void TestCachingCanPreserveTask()
        {
            using (var client = new QueueClient())
            {
                var homemadeTask = new TaskMessage
                {
                    Parameters = "params",
                    Queue = "TestQueue"
                };

                client.Enqueue(homemadeTask);
                var retrievedTask = client.Reserve("TestQueue");
            }

            using (var client = new QueueClient())
            {
                Assert.That(client.CurrentTask != null);
                client.Fail(string.Empty);
                client.RemoveTask(client.AllTasks("TestQueue")[0]);
            }
        }
        public void TestWriteLatencyForDifferentPayloads()
        {
            var stopWatch = new Stopwatch();

            using (var client = new QueueClient())
            {
                // Small message
                var homemadeTask = new TaskMessage
                {
                    Parameters = RandomString(1024),
                    Queue = "TestQueue"
                };

                stopWatch.Start();
                client.Enqueue(homemadeTask);
                stopWatch.Stop();

                Console.WriteLine("1K Message write latency (no network included): {0} ms", stopWatch.ElapsedMilliseconds);

                // Medium message
                homemadeTask = new TaskMessage
                {
                    Parameters = RandomString(1024 * 10),
                    Queue = "TestQueue"
                };

                stopWatch.Start();
                client.Enqueue(homemadeTask);
                stopWatch.Stop();

                Console.WriteLine("10K Message write latency (no network included): {0} ms", stopWatch.ElapsedMilliseconds);

                // Large message
                homemadeTask = new TaskMessage
                {
                    Parameters = RandomString(1024 * 100),
                    Queue = "TestQueue"
                };

                stopWatch.Start();
                client.Enqueue(homemadeTask);
                stopWatch.Stop();

                Console.WriteLine("100K Message write latency (no network included): {0} ms", stopWatch.ElapsedMilliseconds);
            }
        }