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); } } }
static void TestTaskExecutingRedisQueue() { Console.WriteLine("Hello World!"); ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost"); var m = new RedisTaskQueue(redis); var mult = new MultiplyTask(new Inp { x = 4, y = 5 }); var mult2 = new MultiplyTask(new Inp { x = 4, y = 56 }); var add = new AddTask(new (5, 6)); var swap = new SwapTask(new ("abi", "naber")); var concat = new ConcatTask(new Inp2 { x = new List <string> { "f", "u", "r", "k", "a", "n" } }); var matrix = new Matrix(); matrix.nums = new int[100, 100]; var matrixSum = new MatrixSum(matrix); //TestClient(); m.Enqueue(mult); m.Enqueue(mult2); m.Enqueue(add); m.Enqueue(swap); m.Enqueue(concat); m.Enqueue(matrixSum); // normally these will happen in workers. workers will execute tasks and store their results in // result store. Client will have an TaskResult handle which will update its content when result is ready. // below res1 will be all client have. This program.cs is only for testing purposes normally workers and // client will be different processes. /* Normally it will look like this; * * For Client: * * TaskResult mult=new MultiplyTask(new Inp{x=4,y=5}); this will enqueue task, will get the handle from store(or somewhere else) * TaskResult.result == null => will be true until result is executed by some worker * * For workers: each worker will dequeue from TaskStore and push results to ResultStore like; * * ITask task = queue.Dequeue(); * task.Execute() * resultStore.SetResultOfTask(task); */ Console.WriteLine("tasks queued"); Console.WriteLine("task will be dequed. Now cleaning task registry to simulate a server..."); var registry = (ITaskRegistry)TaskRegistry.Instance; registry.Clear(); registry.DiscoverTasks(); for (long i = m.Length(); i > 0; i--) { var task = m.Dequeue(); Console.WriteLine("task is executing..."); task.Execute(); Console.WriteLine($"time elapsed: {task.GetTimeElapsed().Value.TotalMilliseconds} ms"); } var x = "x"; }
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); }