Inheritance: IMessage
        public void Process(IMessage message)
        {
            if (IsFlowInputMessage(message))
            {
                // parent/flow.port => flow/.port
                var output = new Message(Port.Build(message.Port.OperationName, "", message.Port.Name), message.Data, message.CorrelationId)
                {
                    Causalities = message.Causalities,
                    FlowStack = message.FlowStack
                };

                if (message.Port.Path != "") output.FlowStack.Push(message.Port.Path);

                Result(output);
            }
            else
            {
                // flow/.port => parent/flow.port
                var parentFlowname = "";
                if (!message.FlowStack.IsEmpty) parentFlowname = message.FlowStack.Pop();

                var output = new Message(Port.Build(parentFlowname, Create_operation_name(message), message.Port.Name), message.Data, message.CorrelationId)
                {
                    Causalities = message.Causalities,
                    FlowStack = message.FlowStack
                };

                Result(output);
            }
        }
        public void Stress_test_parallel_method()
        {
            var cache = new AsynchronizerCache();
            var sut = new EBCOperation("math", new MyParallelEbc(), null, cache);

            var are = new AutoResetEvent(false);
            var results = new List<int>();
            var threads = new Dictionary<long, int>();
            var exceptions = new List<string>();

            const int N = 2000;
            for (var i = 1; i <= N; i++)
            {
                var input = new Message("math.Inc", i);
                var methodOp = sut.Create_method_operation(input);
                Assert.IsInstanceOf<AsyncWrapperOperation>(methodOp);

                methodOp.Implementation(input, _ =>
                                                   {
                                                       lock(results)
                                                       {
                                                           results.Add((int)_.Data);

                                                           var thUsage = 0;
                                                           if (threads.TryGetValue(Thread.CurrentThread.GetHashCode(), out thUsage))
                                                               threads[Thread.CurrentThread.GetHashCode()] = thUsage + 1;
                                                           else
                                                               threads.Add(Thread.CurrentThread.GetHashCode(), 1);

                                                           if (results.Count == N) are.Set();
                                                       }
                                                   },
                                                   ex =>
                                                       {
                                                           lock(exceptions)
                                                           {
                                                               exceptions.Add(string.Format("data==[{0}] -> {1}", (int)input.Data, ex.ToString()));
                                                           }
                                                       });
            }

            var waitOne = are.WaitOne(2000);
            Console.WriteLine("count: {0}, thread count: {1}, ex count: {2}", results.Count, threads.Count, exceptions.Count);

            foreach(var th in threads)
                Console.WriteLine("{1} x thread #{0}", th.Key, th.Value);
            Console.WriteLine("thread usage total: {0}", threads.Values.Sum());

            for(var i = 0; i < Math.Min(5, exceptions.Count); i++)
                Console.WriteLine("*** {0}: {1}", i, exceptions[i]);
            Assert.IsTrue(waitOne);
        }
        public void Pop()
        {
            var sut = new PopCausality("pc");
            Assert.AreEqual("pc", sut.Name);

            var msg = new Message("x", "hello");
            msg.Causalities.Push(new Port("handler"));

            CausalityStack result = null;
            sut.Implementation(msg, _ => result = _.Causalities, null);

            Assert.IsTrue(result.IsEmpty);
        }
        public void Async_EBC_method_throwing_exception()
        {
            var cache = new AsynchronizerCache();
            var sut = new EBCOperation("math", new MyAsyncEbc(), null, cache);

            var are = new AutoResetEvent(false);
            FlowRuntimeException result = null;

            var input = new Message("math.ThrowException", 41);
            var methodOp = sut.Create_method_operation(input);

            methodOp.Implementation(input, _ => { }, ex => { result = ex; are.Set(); });

            Assert.IsTrue(are.WaitOne(1000));
            Assert.IsInstanceOf<ApplicationException>(result.InnerException);
        }
        public void Output_is_in_same_context_as_input()
        {
            var sut = new Execute_task();

            var msg = new Message("a/b/x.in", "hello");
            var task = new Task(msg,
                                new Operation("x", (input, outputCont, _) => {
                                                                          		outputCont(new Message("x.out", input.Data.ToString() + "x"));
                                                                          }));
            var results = new List<IMessage>();
            sut.Result += _ => results.Add(_);

            sut.Process(task);
            Assert.AreEqual("hellox", results[0].Data.ToString());
            Assert.AreEqual("a/b/x.out", results[0].Port.Fullname);
        }
        private void Put_output_in_same_context_as_input(Task task, IMessage output)
        {
            if (!(task.Operation is IFlow) && task.Message.Port.Path != output.Port.Path)
                    output = new Message(Port.Build(task.Message.Port.Path, output.Port.OperationName, output.Port.InstanceNumber, output.Port.Name),
                                         output.Data,
                                         task.Message.CorrelationId);

            if (task.Message.Port.InstanceNumber != "" && output.Port.InstanceNumber == "")
                output = new Message(Port.Build(output.Port.Path, output.Port.OperationName, task.Message.Port.InstanceNumber, output.Port.Name),
                                     output.Data,
                                     task.Message.CorrelationId);

            if (!(task.Operation is IFlow)) output.FlowStack = task.Message.FlowStack;
            output.Causalities = task.Message.Causalities;

            Result(output);
        }
        public void Operation_with_single_output()
        {
            var sut = new Execute_task();

            var msg = new Message("x.in", "hello");
            var task = new Task(msg,
                                new Operation("x", (input, outputCont, _) => {
                                                                          		outputCont(input);
                                                                          }));

            IMessage result = null;
            sut.Result += _ => result = _;

            sut.Process(task);

            Assert.AreSame(msg, result);
        }
        public void Operation_with_multiple_outputs()
        {
            var sut = new Execute_task();

            var msg = new Message("x.in", "hello");
            var task = new Task(msg,
                                new Operation("x", (input, outputCont, _) => {
                                                                          		outputCont(new Message("1", input.Data.ToString() + "x"));
                                                                                outputCont(new Message("2", input.Data.ToString() + "y"));
                                                                          }));

            var results = new List<IMessage>();
            sut.Result += _ => results.Add(_);

            sut.Process(task);

            Assert.AreEqual("1", results[0].Port.Fullname);
            Assert.AreEqual("hellox", results[0].Data.ToString());
            Assert.AreEqual("2", results[1].Port.Fullname);
            Assert.AreEqual("helloy", results[1].Data.ToString());
        }
Example #9
0
        protected override void Process(IMessage input, Action<IMessage> continueWith, Action<FlowRuntimeException> unhandledException)
        {
            if ("encode decode".IndexOf(input.Port.Name.ToLower()) < 0) throw new ArgumentException("Invalid input port! Valid input ports of Correlator are .encode and .decode.");

            IMessage msg = null;
            Correlation corr = null;
            switch(input.Port.Name.ToLower())
            {
                case "encode":
                    corr = (Correlation)input.Data;
                    msg = new Message(base.Name + ".encoded", corr.Data, corr.Id);
                    break;

                case "decode":
                    corr = new Correlation(input.CorrelationId, input.Data);
                    msg = new Message(base.Name + ".decoded", corr, corr.Id);
                    break;
            }

            msg.Causalities = input.Causalities;
            msg.FlowStack = input.FlowStack;
            continueWith(msg);
        }
        public void Wrap_async_EBC_method()
        {
            var cache = new AsynchronizerCache();
            var sut = new EBCOperation("math", new MyAsyncEbc(), null, cache);

            var are = new AutoResetEvent(false);
            IMessage result = null;
            Thread methodThread = null;

            var input = new Message("math.Inc", 41);
            var methodOp = sut.Create_method_operation(input);
            Assert.IsInstanceOf<AsyncWrapperOperation>(methodOp);

            methodOp.Implementation(input, _ => { result = _; methodThread = Thread.CurrentThread; are.Set(); }, _ => { });

            Assert.IsTrue(are.WaitOne(1000));
            Assert.AreEqual(42, (int)result.Data);
            Assert.AreNotSame(methodThread, Thread.CurrentThread);
        }
        public void Wrap_EBC()
        {
            var sut = new EBCOperation("math", new MyEbc(), null, null);

            IMessage result = null;

            var input = new Message("math.Inc", 41);
            var ebcOp = sut.Create_method_operation(input);
            ebcOp.Implementation(input, _ => result = _, _ => {});

            Assert.AreEqual("math.Result", result.Port.Fullname);
            Assert.AreEqual(42, (int)result.Data);

            input = new Message("math.Divide", new Tuple<int, int>(42, 7));
            ebcOp = sut.Create_method_operation(input);
            ebcOp.Implementation(input, _ => result = _, _ => { });

            Assert.AreEqual("math.Result", result.Port.Fullname);
            Assert.AreEqual(6, (int)result.Data);

            input = new Message("math.Divide", new Tuple<int, int>(42, 0));
            ebcOp = sut.Create_method_operation(input);
            ebcOp.Implementation(input, _ => result = _, _ => { });

            Assert.AreEqual("math.DivisionByZero", result.Port.Fullname);
            Assert.IsNull(result.Data);
        }
 private void Catch_exception_with_causality(Task task, FlowRuntimeException ex)
 {
     var c = task.Message.Causalities.Peek();
     var cMsg = new Message(c.Port, ex, task.Message.CorrelationId);
     HandledException(cMsg);
 }
 public void Process_remote_input(HostInput input)
 {
     _cache.Add(input.CorrelationId, input.StandInEndpointAddress);
     var msg = new Message(input.Portname.StandInPortnameToInputPortname(), input.Data.Deserialize(), input.CorrelationId);
     Translated_input(msg);
 }