Beispiel #1
0
        public static void Run()
        {
            Chain<int, double> chain = new Chain<int, double>()
            .Link<int>(x =>
            {
                // Generate random array of ints
                int[] nums = new int[x];
                Random rand = new Random();
                for (int i = 0; i < x; i++)
                {
                    nums[i] = rand.Next(100);
                }
                return nums;
            })
            .Link<int[]>(x =>
            {
                // order the numbers (pointless but why not)
                return x.OrderBy(y => y).ToArray();
            })
            .Link<int[]>(x =>
            {
                // compute the average and return it
                double average = 0.0;
                for (int i = 0; i < x.Length; i++)
                {
                    average += x[i];
                }
                return average / x.Length;
            });

            int count = 0;
            int started = 0;

            chain.ExecuteFinished += (sender, e) =>
            {
                Interlocked.Increment(ref count);
            };

            int iters = 1000000;

            Queue<int> diff = new Queue<int>(iters);

            Stopwatch sw = Stopwatch.StartNew();
            for (int i = 0; i < iters; i++)
            {
                started++;
                chain.ExecuteAsync(10);

                diff.Enqueue(started - count);
            }
            sw.Stop();

            Console.WriteLine("Took: " + sw.Elapsed.ToString());
        }
Beispiel #2
0
        public void TestAsyncExecution()
        {
            Chain<int, int> chain = new Chain<int, int>()
            .Link<int>(x => x + 1)
            .Link<int>(x => x + 1)
            .Link<int>(x => x + 1)
            .Link<int>(x => x + 1);

            int output = -1;
            chain.ExecuteFinished += (sender, e) => output = e.Output;

            chain.ExecuteAsync(0);

            SpinWait.SpinUntil(() => output != -1, 250);

            Assert.AreEqual<int>(4, output);
        }
Beispiel #3
0
        public static void Run()
        {
            Chain<int, double> chain = new Chain<int, double>()
                .Link<int>(x =>
                {
                    // Generate random array of ints
                    int[] nums = new int[x];
                    Random rand = new Random();
                    for (int i = 0; i < x; i++)
                    {
                        nums[i] = rand.Next(100);
                    }
                    return nums;
                })
                .Link<int[]>(x =>
                {
                    // order the numbers (pointless but why not)
                    return x.OrderBy(y => y).ToArray();
                })
                .Link<int[]>(x =>
                {
                    // compute the average and return it
                    double average = 0.0;
                    for (int i = 0; i < x.Length; i++)
                    {
                        average += x[i];
                    }
                    return average / x.Length;
                });

            chain.ExecuteFinished += (sender, e) =>
            {
                Console.WriteLine("Async execute finished, result: " + e.Output);
            };

            // Start execution of our chain, passing in a large initial argument so it takes some time
            chain.ExecuteAsync(10000);
        }
Beispiel #4
0
        public void TestAsyncExecutionError()
        {
            Chain<int, int> chain = new Chain<int, int>()
            .Link<int>(x => x + 1)
            .Link<int>(x =>
            {
                if (x == 1)
                    throw new Exception(x.ToString());
                else
                    return x + 1;
            })
            .Link<int>(x => x + 1)
            .Link<int>(x => x + 1);

            Exception ex = null;
            chain.ExecuteFinished += (sender, e) =>
            {
                ex = e.Exception;
            };

            chain.ExecuteAsync(0);

            SpinWait.SpinUntil(() => ex != null, 250);

            Assert.IsNotNull(ex);
            Assert.AreEqual<bool>(true, ex is ChainExecutionException);
        }
Beispiel #5
0
        public void TestAsyncTypeError()
        {
            Chain<int, int> chain = new Chain<int, int>()
            .Link<int>(x => x + 1)
            .Link<int>(x => x + "1")
            .Link<int>(x => x + 1)
            .Link<int>(x => x + 1);

            Exception ex = null;
            chain.ExecuteFinished += (sender, e) =>
            {
                ex = e.Exception;
            };

            chain.ExecuteAsync(0);

            SpinWait.SpinUntil(() => ex != null, 250);

            Assert.IsNotNull(ex);
            Assert.AreEqual<bool>(true, ex is LinkArgumentException);
        }