GetStartpointCounter() статический приватный Метод

static private GetStartpointCounter ( int from, int to ) : StartPoint
from int
to int
Результат StartPoint
Пример #1
0
        public void IllegalAsciiArt1()
        {
            List <string> results = new List <string>();

            StartPoint <int>       s      = Helpers.GetStartpointCounter(1, 15);
            TaskNode <int, string> filter = Helpers.GetFilter();
            EndPoint <string>      n      = Helpers.GetEndpoint(results);

            try
            {
                Flow flow = Flow.FromAsciiArt(@"
   a----->b->c
     <--
",
                                              new Dictionary <char, TaskNode>()
                {
                    { 'a', s },
                    { 'b', filter },
                    { 'c', n }
                }
                                              );
            }
            catch (InvalidOperationException)
            {
                return;
            }
            Assert.Fail("loose arrows should throw exception");
        }
Пример #2
0
        public void ThreeStepWithFilter()
        {
            List <string>    results = new List <string>();
            StartPoint <int> s       = Helpers.GetStartpointCounter(1, 100);
            // pass only numbers divisible by three with no 2 in them
            // Output in
            TaskNode <int, string> filter = Helpers.GetFilter();

            filter.ItemProcessed += new EventHandler <TaskNode.ItemEventArgs>(EndItemProcessed);

            EndPoint <string> n = Helpers.GetEndpoint(results);

            Flow flow = Helpers.ConnectStartFilterEnd(s, filter, n);

            flow.Start();
            flow.RunToCompletion();

            Assert.Contains(flow.Status, new List <RunStatus>()
            {
                RunStatus.Stopped, RunStatus.Stopping
            });

            Assert.AreEqual(27, results.Count);
            Assert.AreEqual(results.Count, n.ItemsProcessed);
            Assert.AreEqual(1, s.ItemsProcessed);
            Console.WriteLine(flow.GetStateSnapshot());
            Assert.Greater(s.TotalSecondsProcessing, 0);
            Assert.Greater(filter.TotalSecondsProcessing, 0);
            Assert.Greater(filter.TotalSecondsBlocked, 0);

            // Each node works with one thread, so order is maintained, so:
            Assert.AreEqual('3', results[0][0]);
        }
Пример #3
0
        public void StartingAndStopping()
        {
            StartPoint <int> s = Helpers.GetStartpointCounter(1, 15);
            // pass only numbers divisible by three with no 2 in them
            // Output in
            TaskNode <int, string> filter = Helpers.GetFilter();

            filter.ThreadNumber = 2;

            Collector <string> collect = new Collector <string>();

            filter.ItemProcessed += new EventHandler <TaskNode.ItemEventArgs>(StopFlowItemProcessed);
            Flow flow = Flow.FromAsciiArt("c->d--->e", s, filter, collect);

            flow.Start();
            // Flow will be stopped from evnt handler
            // Give it time to do it wrong:
            Thread.Sleep(100);
            Assert.Greater(collect.Items.Count, 0);
            Assert.Less(collect.Items.Count, 4);
            flow.RunToCompletion();
            try
            {
                Console.WriteLine(flow.Status);
                //flow.Start();
                //Assert.Fail("An exception should be thrown when starting a stopped flow");
            }
            catch (InvalidOperationException)
            {
                //noop
            }
        }
Пример #4
0
        public void KeepOrder2()
        {
            List <int>          results = new List <int>();
            Random              r       = new Random();
            StartPoint <int>    s       = Helpers.GetStartpointCounter(1, 50);
            TaskNode <int, int> process = new TaskNode <int, int>(
                (int input, IWritableQueue <int> output) =>
            {
                int sleeptime = r.Next(50);
                Thread.Sleep(sleeptime);
                output.Send(input);
            }
                )
            {
                ThreadNumber = 5, KeepOrder = true
            };
            Collector <int> n = new Collector <int>();
            Flow            f = Flow.FromAsciiArt("c<----b<---0a", new Dictionary <char, TaskNode>()
            {
                { 'a', s }, { 'b', process }, { 'c', n }
            });

            f.Start();
            f.RunToCompletion();
            int vorig = 0;

            foreach (var item in n.Items)
            {
                Assert.AreEqual(vorig + 1, item);
                vorig = item;
            }
        }
Пример #5
0
        public void ThreeStepWithMultilineAsciiArt()
        {
            List <string> results = new List <string>();

            StartPoint <int>       s      = Helpers.GetStartpointCounter(1, 15);
            TaskNode <int, string> filter = Helpers.GetFilter();
            EndPoint <string>      n      = Helpers.GetEndpoint(results);

            Flow flow = Flow.FromAsciiArt(@"
   a
   |
   V 
   b
   |  
   +->c",
                                          new Dictionary <char, TaskNode>()
            {
                { 'a', s },
                { 'b', filter },
                { 'c', n }
            }
                                          );

            flow.Start();
            flow.RunToCompletion();
            Assert.AreEqual(4, results.Count);

            string outputArt = flow.GetStateSnapshot().ToStringAsciiArt();

            Assert.IsFalse(string.IsNullOrEmpty(outputArt));
            Console.WriteLine(outputArt);
        }
Пример #6
0
        public void LegalAsciiArtParallel()
        {
            List <string> results1 = new List <string>();
            List <string> results2 = new List <string>();

            StartPoint <int>       s1      = Helpers.GetStartpointCounter(1, 15);
            StartPoint <int>       s2      = Helpers.GetStartpointCounter(5, 35);
            TaskNode <int, string> filter1 = Helpers.GetFilter();
            TaskNode <int, string> filter2 = Helpers.GetFilter();
            EndPoint <string>      n1      = Helpers.GetEndpoint(results1);
            EndPoint <string>      n2      = Helpers.GetEndpoint(results2);

            n2.ThreadNumber = 3;

            Flow flow = Flow.FromAsciiArt(@"
a-->b->c
d->e--->f
",
                                          new Dictionary <char, TaskNode>()
            {
                { 'a', s1 },
                { 'b', filter1 },
                { 'c', n1 },
                { 'd', s2 },
                { 'e', filter2 },
                { 'f', n2 }
            }
                                          );

            flow.Start();
            flow.RunToCompletion();

            Assert.AreEqual(4, results1.Count);
            Assert.AreEqual(6, results2.Count);
        }
Пример #7
0
        public void KeepOrder3()
        {
            List <int>               results = new List <int>();
            Random                   r       = new Random();
            StartPoint <int>         s       = Helpers.GetStartpointCounter(1, 50);
            TaskNode <int, int, int> process = new TaskNode <int, int, int>(
                (input, output1, output2) =>
            {
                int sleeptime = r.Next(50);
                Thread.Sleep(sleeptime);
                if (input % 2 == 0)
                {
                    output1.Send(input);
                }
                else
                {
                    output2.Send(input);
                }
            }
                )
            {
                ThreadNumber = 2, KeepOrder = true
            };
            Collector <int> n  = new Collector <int>();
            Collector <int> n2 = new Collector <int>();
            Flow            f  = Flow.FromAsciiArt(@"
a-->b0-->c
    1
    |
    V
    d
", new Dictionary <char, TaskNode>()
            {
                { 'a', s }, { 'b', process }, { 'c', n }, { 'd', n2 }
            });

            f.Start();
            f.RunToCompletion();
            int vorig = 0;

            foreach (var item in n.Items)
            {
                Assert.AreEqual(vorig + 2, item);
                vorig = item;
            }
            vorig = -1;
            foreach (var item in n2.Items)
            {
                Assert.AreEqual(vorig + 2, item);
                vorig = item;
            }
        }
Пример #8
0
        public void ThreeStepWithMultilineAsciiArt()
        {
            List <string> results1 = new List <string>();
            List <string> results2 = new List <string>();

            StartPoint <int> s = Helpers.GetStartpointCounter(1, 50);
            TaskNode <int, string, string> splitter =
                new TaskNode <int, string, string>(
                    (int i, IWritableQueue <string> o1, IWritableQueue <string> o2) =>
            {
                if (i % 3 == 0 || i % 4 == 0 || i % 5 == 0)
                {
                    // divisible by 3, 4 or 5, go to stream 1
                    o1.Send(i.ToString(CultureInfo.InvariantCulture));
                }
                else
                {
                    // if not, send i and i+1 to stream 2
                    o2.Send(i.ToString());
                    o2.Send((i + 1).ToString(CultureInfo.InvariantCulture));
                }
            }
                    );
            EndPoint <string> end1 = Helpers.GetEndpoint(results1);
            EndPoint <string> end2 = Helpers.GetEndpoint(results2);

            Flow flow = Flow.FromAsciiArt(@"
s--+
   |
   V
   t0->n
   1
   | 
   +---->m
",
                                          new Dictionary <char, TaskNode>()
            {
                { 's', s },
                { 't', splitter },
                { 'n', end1 },
                { 'm', end2 }
            }
                                          );

            flow.Start();
            flow.RunToCompletion();
            Assert.AreEqual(29, results1.Count);
            Assert.AreEqual("50", results1[28]);
            Assert.AreEqual(42, results2.Count);
        }
Пример #9
0
        public void ThreeStepWithAsciiArt()
        {
            List <string> results = new List <string>();

            StartPoint <int>       s      = Helpers.GetStartpointCounter(1, 15);
            TaskNode <int, string> filter = Helpers.GetFilter();
            EndPoint <string>      n      = Helpers.GetEndpoint(results);

            Flow flow = Flow.FromAsciiArt(@"a-->b-->c", s, filter, n);

            flow.Start();
            flow.RunToCompletion();
            Assert.AreEqual(4, results.Count);
        }
Пример #10
0
        public void MultithreadedStartpointThrows()
        {
            List <string> results = new List <string>();

            try
            {
                StartPoint <int> s = Helpers.GetStartpointCounter(1, 15);
                s.ThreadNumber = 2;
                Assert.Fail("Library should throw invalid operstion exception when StartPoint is run multithreaded");
            }
            catch (InvalidOperationException)
            {
                //OK
            }
        }
Пример #11
0
        public void ConnectingWrongTypesThrows()
        {
            List <string>     results = new List <string>();
            StartPoint <int>  s       = Helpers.GetStartpointCounter(1, 15);
            EndPoint <string> n       = Helpers.GetEndpoint(results);

            Flow flow = new Flow();

            flow.AddNode(s);
            flow.AddNode(n);
            try
            {
                flow.ConnectNodes(s, n, 0);
                Assert.Fail("Connecting nodes of different types should throw invalid operation exception");
            }
            catch (InvalidOperationException)
            { }
        }
Пример #12
0
        public void IllegalAsciiArtList()
        {
            List <string> results = new List <string>();

            StartPoint <int>       s      = Helpers.GetStartpointCounter(1, 15);
            TaskNode <int, string> filter = Helpers.GetFilter();
            EndPoint <string>      n      = Helpers.GetEndpoint(results);

            try
            {
                Flow flow = Flow.FromAsciiArt(@"a-->b-->c", s, filter);
                Assert.Fail("Flow should raise an exception when the art uses more or less letters than tasks");
            }
            catch (Exception)
            {
                // noop
            }
        }
Пример #13
0
        public void KeepOrder()
        {
            List <string>    results = new List <string>();
            StartPoint <int> s       = Helpers.GetStartpointCounter(1, 15);
            // pass only numbers divisible by three with no 2 in them
            // Output in
            TaskNode <int, string> filter = Helpers.GetFilter();

            filter.ThreadNumber = 5;
            filter.KeepOrder    = true;

            EndPoint <string> n = Helpers.GetEndpoint(results);

            Flow flow = Helpers.ConnectStartFilterEnd(s, filter, n);

            flow.Start();
            flow.RunToCompletion();
            Assert.AreEqual(4, results.Count);
            Assert.AreEqual("3.00", results[0]);
        }
Пример #14
0
        public void ThreeStepWithMultilineAsciiArt2()
        {
            List <string> results = new List <string>();

            StartPoint <int>       s      = Helpers.GetStartpointCounter(1, 15);
            TaskNode <int, string> filter = Helpers.GetFilter();
            EndPoint <string>      n      = Helpers.GetEndpoint(results);

            Flow flow = Flow.FromAsciiArt(@"
   +-----+
   |     |
   V     |
   b     a
   |         c
   |         ^
   |         |
   +---#-----+", s, filter, n);

            flow.Start();
            flow.RunToCompletion();
            Assert.AreEqual(4, results.Count);
            Console.WriteLine(flow.GetStateSnapshot().ToStringAsciiArt());
        }