Пример #1
0
        public void Test_TaskEnqueueAndExecuting()
        {
            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
            var m = new RedisTaskQueue(redis);

            m.Clear();
            var mult = new MultiplyTask(new Inp {
                x = 4, y = 5
            });
            var add    = new AddTask(new (5, 6));
            var concat = new ConcatTask(new Inp2 {
                x = new List <string>()
                {
                    "Hello", " ", "World", "!"
                }
            });
            var listSum = new ListSum(new List <int> {
                1, 2, 3, 4, 5
            });

            m.Enqueue(mult);
            m.Enqueue(add);
            m.Enqueue(concat);
            m.Enqueue(listSum);

            var registry = (ITaskRegistry)TaskRegistry.Instance;

            registry.Clear();
            registry.DiscoverTasks();

            for (long i = 0; i < m.Length(); i++)
            {
                var task = m.Dequeue();
                task.Execute();
                if (i == 0)
                {
                    var res = (int)task.GetObjectResult();
                    Assert.Equal(20, res);
                }
                if (i == 1)
                {
                    var res = (int)task.GetObjectResult();
                    Assert.Equal(11, res);
                }
                if (i == 2)
                {
                    var res = (string)task.GetObjectResult();
                    Assert.Equal("Hello World!", res);
                }
                if (i == 3)
                {
                    var res = (int)task.GetObjectResult();
                    Assert.Equal(5 * 6 / 2, res);
                }
            }
        }
Пример #2
0
        public void Test_TaskEnqueueAndExecutingParallel()
        {
            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
            var m = new RedisTaskQueue(redis);

            m.Clear();
            var mult = new MultiplyTask(new Inp {
                x = 4, y = 5
            });
            var add    = new AddTask(new (5, 6));
            var concat = new ConcatTask(new Inp2 {
                x = new List <string>()
                {
                    "Hello", " ", "World", "!"
                }
            });

            for (int i = 0; i < 1000; i++)
            {
                m.Enqueue(mult);
                m.Enqueue(add);
                m.Enqueue(concat);
            }

            Action worker = () =>
            {
                try
                {
                    var task     = m.Dequeue();
                    var taskType = TaskRegistry.Instance.GetTaskByName(task.GetIdentifier());

                    task.Execute();
                    if (taskType == typeof(MultiplyTask))
                    {
                        var res = (int)task.GetObjectResult();
                        Assert.Equal(20, res);
                    }
                    if (taskType == typeof(AddTask))
                    {
                        var res = (int)task.GetObjectResult();
                        Assert.Equal(11, res);
                    }
                    if (taskType == typeof(ConcatTask))
                    {
                        var res = (string)task.GetObjectResult();
                        Assert.Equal("Hello World!", res);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            };

            var registry = (ITaskRegistry)TaskRegistry.Instance;

            registry.Clear();
            registry.DiscoverTasks();

            Parallel.Invoke(worker, worker, worker, worker, worker, worker);
        }