示例#1
0
        public void TestMethod()
        {
            var source = new Source();

            var intQueue         = new ItemsQueueContainer <int>();
            var doubleQueue      = new ItemsQueueContainer <double>();
            var exceptionHandler = Mock.Create <IExceptionHandler>();

            var myWork = new MyWork();

            var counter = new MyCounter();

            var dataflow = DataflowBuilder
                           .StartFrom(source)
                           .TransmitWith(o => o.Custom(new MyTransmitterFactory()))
                           .SplitBatch()
                           .BuildTransformation(o => o.Transform(int.Parse)
                                                .Transform(x => x.ToString())
                                                .Transform(myWork))
                           .SendToPipe <int>(new InfinityPipe <int>(intQueue))
                           .TransmitWith(o => o.BalancedTransmitter(exceptionHandler, 0, 10)
                                         .SetCounter(counters => counters.InWorkItemsCount = counter))
                           .RouteTo(condition => condition.Case(o => o % 3 == 0)
                                    .Transform(o => o.ToString())
                                    .SendToTarget(new LoggingTarget <string>("0) ", 100)),

                                    condition => condition.Case(o => o % 3 == 1)
                                    .BroadcastTo(target2 => target2.SendToTarget(new LoggingTarget <int>("1) ", 200)),

                                                 target2 => target2.ToBatches(4, TimeSpan.FromMilliseconds(500), exceptionHandler)
                                                 .SplitBatch()
                                                 .Transform(o => (double)o)
                                                 .Transform(o => (int)o)
                                                 .Transform(o => (double)o)
                                                 .SendToPipe <double>(new InfinityPipe <double>(doubleQueue))
                                                 .TransmitWith(o => o.BalancedTransmitter(exceptionHandler, 0, 10))
                                                 .BuildTransformation(o => o.ToBatches(4, TimeSpan.FromMilliseconds(500), exceptionHandler)
                                                                      .SplitBatch())
                                                 .SendToTarget(new LoggingTarget <double>("2) "))),

                                    condition => condition.Default()
                                    .SendToTarget(new LoggingTarget <int>("3) ")))
                           .Build();

            dataflow.Start();

            Console.WriteLine("Started!");

            Task.Delay(4000).Wait();

            Console.WriteLine("I={0}, J={1}", counter.I, counter.J);
        }
        public void TestMethod2()
        {
            var exceptionHandler = Mock.Create <IExceptionHandler>();

            Mock.Arrange(() => exceptionHandler.Handle(null)).IgnoreArguments().Returns <Exception>(o =>
            {
                Console.WriteLine(o);
                return(true);
            });

            var itemStore    = new ItemStore();
            var uniqueQueue  = new ItemsQueueContainer <ItemWithDate>();
            var infinityPipe = new InfinityPipe <ItemWithDate>(uniqueQueue);

            var dataflow = DataflowBuilder
                           .StartFrom(infinityPipe)
                           .ConvertItemsTo(o => new ScheduledTransmitterItem <int>(o.DateTime, o.Num, stopSchedule: o.Count == 4))
                           .TransmitWith(o => o.ScheduledTransmitter(new DateTimeProvider(), exceptionHandler))
                           .Transform(itemStore.GetItem)
                           .BroadcastTo(t1 => t1.SendToTarget(new LoggingTarget <ItemWithDate>()),
                                        t2 => t2.Transform(o =>
            {
                o.Count++;
                o.DateTime = DateTime.Now.AddSeconds(o.Num + 1);
                return(o);
            })
                                        .RouteTo(condition => condition.Case(o => o.Count == 4)
                                                 .BroadcastTo(t21 => t21.SendToTarget(new DeleteTarget(itemStore)),
                                                              t22 => t22.SendToTarget(infinityPipe)),

                                                 condition => condition.Default()
                                                 .BroadcastTo(t21 => t21.SendToTarget(new UpdateTarget(itemStore)),
                                                              t22 => t22.SendToTarget(infinityPipe))))
                           .Build();

            dataflow.Start();

            var items = itemStore.Load();

            foreach (var item in items)
            {
                infinityPipe.SendAsync(item).Wait();
            }

            Task.Delay(10000).Wait();
        }