Exemplo n.º 1
0
        private static void TestTransformBlock()
        {
            //var transformBlock = new TransformBlock<int, double>(n => Math.Sqrt(n));

            //transformBlock.Post(10);
            //transformBlock.Post(20);
            //transformBlock.Post(30);

            //for (int i = 0; i < 3; i++)
            //{
            //    Console.WriteLine(transformBlock.Receive());
            //}

            var tbUrl = new TransformBlock <string, string>((url) =>
            {
                WebClient webClient = new WebClient();
                string res          = webClient.DownloadString(new Uri(url));
                return(res);
            });

            tbUrl.Post("https://www.cnblogs.com/haoxinyue/archive/2013/03/01/2938959.html");
            tbUrl.Post("https://zhidao.baidu.com/question/570037232.html");

            Console.WriteLine(tbUrl.Receive());
            Console.WriteLine(tbUrl.Receive());
        }
Exemplo n.º 2
0
        protected void RunParseTest_NoTags(string metric, Type expectedType)
        {
            _systemMetrics.Setup(p => p.LogCount("parser.linesSeen", 1)).Verifiable();

            _block.Post(metric);
            var message = _block.Receive();

            Assert.IsInstanceOfType(message, expectedType);
            Assert.AreEqual(metric, message.ToString());
            _systemMetrics.VerifyAll();
        }
Exemplo n.º 3
0
        public void ProcessedRawLine_GotValidRawMessageInstance()
        {
            _systemMetrics.Setup(p => p.LogCount("parser.LinesSeen", 1)).Verifiable();

            var timestamp = DateTime.Now.Ticks;
            var metric    = "a.raw.metric:100|r|" + timestamp;

            _block.Post(metric);
            var message = _block.Receive();

            Assert.AreEqual(metric, message.ToString());
            _systemMetrics.VerifyAll();
        }
Exemplo n.º 4
0
        public void ProcessedRawLine_GotValidRawMessageInstance()
        {
            _systemMetrics.When(p => p.LogCount("parser.LinesSeen", 1));

            var timestamp = DateTime.Now.Ticks;
            var metric    = "a.raw.metric:100|r|" + timestamp;

            _block.Post(metric);
            var message = _block.Receive();

            message.ToString().ShouldBe(metric);
            _systemMetrics.ReceivedCalls();
        }
Exemplo n.º 5
0
        public async Task DataflowExampleWithTry()
        {
            var inputBlock = new TransformBlock <int, Try <int> >(value => Try.Create(() =>
            {
                if (value % 3 == 0)
                {
                    throw new InvalidOperationException($"Power of 3 found: {value}");
                }
                return(value);
            }));
            var outputBlock = new TransformBlock <Try <int>, Try <int> >(t => t.Map(async value =>
            {
                await Task.Yield();
                if (value % 2 == 0)
                {
                    throw new InvalidOperationException($"Power of 2 found: {value}");
                }
                return(value);
            }));

            inputBlock.LinkTo(outputBlock, new DataflowLinkOptions {
                PropagateCompletion = true
            });
            inputBlock.Post(1); // comes through as a value
            inputBlock.Post(2); // comes through as an exception
            inputBlock.Post(3); // comes through as an exception
            inputBlock.Post(5); // comes through as a value

            inputBlock.Complete();
            var result1 = outputBlock.Receive();

            Assert.Equal(1, result1.Value);
            var result2 = outputBlock.Receive();

            Assert.True(result2.IsException);
            var result2Exception = Assert.Throws <InvalidOperationException>(() => result2.Value);

            Assert.Equal("Power of 2 found: 2", result2Exception.Message);
            var result3 = outputBlock.Receive();

            Assert.True(result3.IsException);
            var result3Exception = Assert.Throws <InvalidOperationException>(() => result3.Value);

            Assert.Equal("Power of 3 found: 3", result3Exception.Message);
            var result5 = outputBlock.Receive();

            Assert.Equal(5, result5.Value);

            await outputBlock.Completion; // no faulted blocks
        }
        public static void Run()
        {
            Func <int, int> fn = n => { Thread.Sleep(1000);
                                        return(n * n); };

            var tfBlock = new TransformBlock <int, int>(fn);//pointer

            //Here you generate data and add it
            for (int i = 0; i < 10; i++)
            {
                //generate value, then post it in a transformation block
                //and in a code behind you will get the result of calculations
                //compiler does not run throw calculation code part directly
                tfBlock.Post(i);
            }

            //Then here you try to receive it
            for (int i = 0; i < 10; i++)
            {
                int result = tfBlock.Receive(); //To extract data here you need to receive it
                Console.WriteLine(result);      //Then you print it by one.
            }
            Console.WriteLine("Done");
            //Notice that for this example “Done” is only printed after all the output values have been printed.
            //This is because the Receive() method operates synchronously in the same thread as the for loops.
        }
        static void Main(string[] args)
        {
            var transformBlock = new TransformBlock <int, string>(i =>
            {
                Task.Delay(500).Wait();
                return(i.ToString());
            }, new ExecutionDataflowBlockOptions()
            {
                MaxDegreeOfParallelism = 2
            });

            for (int i = 0; i < 10; i++)
            {
                transformBlock.Post(i);
                Console.WriteLine($"Number of messages in the input queue: {transformBlock.InputCount}");
            }

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine($"Number of messages in the input queue: {transformBlock.OutputCount}");
                var result = transformBlock.Receive();
                Console.WriteLine($"Recived: {result}");
            }

            Console.WriteLine("done");
            Console.ReadKey();
        }
Exemplo n.º 8
0
        public void TransformParallel()
        {
            var block = new TransformBlock <int, string>(a =>
            {
                Task.Delay(500).Wait();
                return(a.ToString());
            }, new ExecutionDataflowBlockOptions()
            {
                MaxDegreeOfParallelism = 10
            });

            for (int i = 0; i < 100; i++)
            {
                block.Post(i);
                Console.WriteLine($"Transform input queue: {block.InputCount}");
            }

            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine($"Transform output queue: {block.OutputCount}");
                var result = block.Receive();
                Console.WriteLine($"Received: {result}");
            }

            Console.WriteLine("Done!");
        }
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            var transformBlock = new TransformBlock <int, string>(n => {
                Task.Delay(500).Wait();
                return(n.ToString());
            }, new ExecutionDataflowBlockOptions
            {
                MaxDegreeOfParallelism    = 2,
                SingleProducerConstrained = true // Assumes only a single producer
            });

            for (int i = 0; i < 10; i++)
            {
                transformBlock.Post(i);
                Console.WriteLine($"Number of messages in the input queue {transformBlock.InputCount}");
            }

            for (int i = 0; i < 10; i++)
            {
                Console.Write($"Input {transformBlock.OutputCount}, Output {transformBlock.OutputCount}, received ");
                var result = transformBlock.Receive();
                Console.WriteLine(result);
            }

            Console.WriteLine("Finished!");
            Console.ReadKey();
        }
Exemplo n.º 10
0
        public IEnumerable <IMetaData> TransformManyBlockUsage(string stringToSplit)
        {
            Console.WriteLine($"Inside {nameof(TplDataflow1ExecutionBlocksController)} - {nameof(TransformManyBlockUsage)}");

            // Create the members of the pipeline.
            var transformManyBlockSplitAnInputStringIntoArray = new TransformManyBlock <string, string>(input =>
                                                                                                        Functions.SplitAnInputStringIntoArray(input, SplitterSeparator)
                                                                                                        );
            var transformBlockCreateASingleMedatadataFromAString = new TransformBlock <string, IMetaData>(stringInput =>
                                                                                                          Functions.CreateASingleMedatadataFromAString(stringInput)
                                                                                                          );

            // Connect the dataflow blocks to form a pipeline.
            transformManyBlockSplitAnInputStringIntoArray.LinkTo(transformBlockCreateASingleMedatadataFromAString, DataflowOptions.LinkOptions);

            // Start TransformManyBlockUsage pipeline with the input values.
            transformManyBlockSplitAnInputStringIntoArray.Post(stringToSplit);

            // Mark the head of the pipeline as complete.
            transformManyBlockSplitAnInputStringIntoArray.Complete();

            // Equivalent of transformManyBlockSplitAnInputStringIntoArray.OutputCount
            var ouputCount = stringToSplit?.Split(SplitterSeparator, StringSplitOptions.RemoveEmptyEntries).Length ?? 0;

            for (var i = 0; i < ouputCount; i++)
            {
                yield return(transformBlockCreateASingleMedatadataFromAString.Receive());
            }
        }
Exemplo n.º 11
0
        public override void Run()
        {
            /* Unbounded 表示块使用支持的最大并行数
             * 块并行处理时,处理的数据顺序不一定与接收的顺序一致,但是输出时与接收的顺序一致
             */
            TransformBlock <RunModel, string> transformBlock = new TransformBlock <RunModel, string>(
                (model) =>
            {
                Helper.PrintLine($"开始处理 {model.Name}");
                return($"I'm {model.Name}");
            },
                new ExecutionDataflowBlockOptions()
            {
                MaxDegreeOfParallelism = DataflowBlockOptions.Unbounded
            });

            var models = this.CreateCollection();

            // TransformBlock 可以同时作为输入和输出
            Array.ForEach(models, model => transformBlock.Post(model));
            for (int index = 0; index < models.Length; index++)
            {
                Helper.PrintLine(transformBlock.Receive());
            }
        }
Exemplo n.º 12
0
        public void Run()
        {
            ConcurrentBag <int> values = new ConcurrentBag <int>();
            var transformBlock         = new TransformBlock <int, string>(n =>
            {
                Task.Delay(500).Wait();
                values.Add(n);
                return(n.ToString());
            }, new ExecutionDataflowBlockOptions()
            {
                MaxDegreeOfParallelism = 4
            });

            for (int i = 0; i < 10; i++)
            {
                transformBlock.Post(i);
                Console.WriteLine("Input count:" + transformBlock.InputCount);
            }

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("output count:" + transformBlock.OutputCount);
                var result     = transformBlock.Receive();
                var listResult = 0;
                values.TryTake(out listResult);
                Console.WriteLine($"Result:{result}  Output count:{transformBlock.OutputCount} input count:{transformBlock.InputCount} list item: {listResult}");
            }
        }
Exemplo n.º 13
0
        static void ShowTransformBlock()
        {
            // <snippet5>
            // Create a TransformBlock<int, double> object that
            // computes the square root of its input.
            var transformBlock = new TransformBlock <int, double>(n => Math.Sqrt(n));

            // Post several messages to the block.
            transformBlock.Post(10);
            transformBlock.Post(20);
            transformBlock.Post(30);

            // Read the output messages from the block.
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine(transformBlock.Receive());
            }

            /* Output:
             * 3.16227766016838
             * 4.47213595499958
             * 5.47722557505166
             */
            // </snippet5>
        }
Exemplo n.º 14
0
        public void LinkToTransformBlock()
        {
            var multiply = new TransformBlock <int, int>(item => item * 2);
            var subtract = new TransformBlock <int, int>(item => item - 2);

            multiply.LinkTo(subtract);

            // Send messages to the multiply block
            multiply.Post(10);
            multiply.Post(20);
            multiply.Post(50);

            // Read the output from the subtract block and test result
            Assert.That(subtract.Receive(), Is.EqualTo(18));
            Assert.That(subtract.Receive(), Is.EqualTo(38));
            Assert.That(subtract.Receive(), Is.EqualTo(98));
        }
Exemplo n.º 15
0
        public async Task BlockTest()
        {
            var block = new TransformBlock <int, int>(item =>
            {
                if (item == 1)
                {
                    throw new InvalidOperationException("Blech.");
                }
                return(item * 2);
            });

            block.Post(2);
            block.Post(4);
            block.Complete();

            Assert.AreEqual(4, block.Receive());
            Assert.AreEqual(8, block.Receive());
            await block.Completion;
        }
Exemplo n.º 16
0
        //画像が削除されて意味がなくなったツイートを消す
        //URL転載したやつの転載元ツイートが消された場合
        public int RemoveOrphanTweet()
        {
            const int    BulkUnit      = 100;
            const int    RangeSeconds  = 300;
            const string head          = @"DELETE FROM tweet WHERE tweet_id IN";
            string       BulkDeleteCmd = BulkCmdStrIn(BulkUnit, head);

            TransformBlock <long, DataTable> GetTweetBlock = new TransformBlock <long, DataTable>(async(long id) =>
            {
                using (MySqlCommand Cmd = new MySqlCommand(@"SELECT tweet_id
FROM tweet
WHERE retweet_id IS NULL
AND NOT EXISTS (SELECT * FROM tweet_media WHERE tweet_media.tweet_id = tweet.tweet_id)
AND tweet_id BETWEEN @begin AND @end
ORDER BY tweet_id DESC;"))
                {
                    Cmd.Parameters.AddWithValue("@begin", id);
                    Cmd.Parameters.AddWithValue("@end", id + SnowFlake.msinSnowFlake * RangeSeconds * 1000 - 1);
                    return(await SelectTable(Cmd, IsolationLevel.RepeatableRead));
                }
            }, new ExecutionDataflowBlockOptions {
                MaxDegreeOfParallelism = Environment.ProcessorCount
            });


            DateTimeOffset date = DateTimeOffset.UtcNow.AddHours(-1);

            for (int i = 0; i < 20; i++)
            {
                GetTweetBlock.Post(SnowFlake.SecondinSnowFlake(date, false));
                date = date.AddHours(-1);
            }
            while (true)
            {
                DataTable Table = GetTweetBlock.Receive();
                if (Table.Rows.Count > 0)
                {
                    using (MySqlCommand delcmd = new MySqlCommand(BulkCmdStrIn(Table.Rows.Count, head)))
                    {
                        for (int n = 0; n < Table.Rows.Count; n++)
                        {
                            delcmd.Parameters.AddWithValue("@" + n.ToString(), Table.Rows[n].Field <long>(0));
                        }
                        Console.WriteLine("{0} {1} Tweets removed", date, ExecuteNonQuery(delcmd));
                    }
                }
                GetTweetBlock.Post(SnowFlake.SecondinSnowFlake(date, false));
                date = date.AddSeconds(-RangeSeconds);
            }
        }
        public IEnumerable <ConsumerResult <TResultPayload> > Generate <TResultPayload>(ISourceCodeProvider dataProvider, IConsumer <TResultPayload> consumer)
        {
            if (dataProvider == null)
            {
                throw new ArgumentNullException("dataProvider");
            }

            if (consumer == null)
            {
                throw new ArgumentNullException("consumer");
            }

            var linkOptions = new DataflowLinkOptions {
                PropagateCompletion = true
            };
            var processingTaskRestriction = new ExecutionDataflowBlockOptions {
                MaxDegreeOfParallelism = _testsGeneratorRestrictions.MaxProcessingTasksCount
            };
            var outputTaskRestriction = new ExecutionDataflowBlockOptions {
                MaxDegreeOfParallelism = _testsGeneratorRestrictions.MaxWritingTasksCount
            };

            var producerBuffer       = new TransformBlock <string, TestClassInMemoryInfo>(new Func <string, TestClassInMemoryInfo>(Produce), processingTaskRestriction);
            var generatedTestsBuffer = new TransformBlock <TestClassInMemoryInfo, ConsumerResult <TResultPayload> >(
                new Func <TestClassInMemoryInfo, ConsumerResult <TResultPayload> >(consumer.Consume), outputTaskRestriction);

            producerBuffer.LinkTo(generatedTestsBuffer, linkOptions);
            _additionalProducerBuffer.LinkTo(generatedTestsBuffer, linkOptions);

            var consumerResults = Task.Run(async delegate {
                List <ConsumerResult <TResultPayload> > consumerResultsBuffer = new List <ConsumerResult <TResultPayload> >();

                while (await generatedTestsBuffer.OutputAvailableAsync())
                {
                    consumerResultsBuffer.Add(generatedTestsBuffer.Receive());
                }

                return(consumerResultsBuffer);
            });

            Parallel.ForEach(dataProvider.Provide(), async dataInMemory => {
                await producerBuffer.SendAsync(dataInMemory);
            });

            producerBuffer.Complete();
            consumerResults.Wait();

            return(consumerResults.Result);
        }
Exemplo n.º 18
0
        public static void TestDownloadHtml()
        {
            tbUrl.Post("http://www.baidu.com");
            tbUrl.Post("http://www.sina.com");

            for (var i = 0; i < 3; i++)
            {
                string result = tbUrl.Receive();
                Console.WriteLine(result);
            }
            tbUrl.Complete();
            Console.WriteLine("Post Finished");
            tbUrl.Completion.Wait();
            Console.WriteLine("Proccess Finished");
        }
Exemplo n.º 19
0
        private static void DataStructure_TransformBlock()
        {
            var transformBlock = new TransformBlock <int, double>(n => Math.Sqrt(n));

            // Post several messages to the block.
            transformBlock.Post(10);
            transformBlock.Post(20);
            transformBlock.Post(30);

            // Read the output messages from the block.
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine(transformBlock.Receive());
            }
        }
Exemplo n.º 20
0
        /// <summary>
        /// The following basic example creates a TransformBlock<TInput,TOutput> object
        /// that computes the square root of its input. The TransformBlock<TInput,TOutput>
        /// object takes Int32 values as input and produces Double values as output.
        /// </summary>
        public static void Run()
        {
            // Create a TransformBlock<int, double> object that
            // computes the square root of its input.
            TransformBlock <int, double> transformBlock = new TransformBlock <int, double>(n => Math.Sqrt(n));

            // Post several messages to the block.
            transformBlock.Post(10);
            transformBlock.Post(20);
            transformBlock.Post(30);
            // Read the output messages from the block.
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine(transformBlock.Receive());
            }
        }
Exemplo n.º 21
0
        private static DrillStats ExecuteConnections(DrillOptions drillOptions)
        {
            ThreadPool.SetMinThreads(drillOptions.ConnectionCount,
                                     drillOptions.ConnectionCount);

            ServicePointManager.DefaultConnectionLimit = drillOptions.ConnectionCount;

            var aggregatedResults = new List <RequestResult>();

            List <RequestResult> RequestFunc(DrillOptions options)
            {
                return(ExecuteConnection(options));
            }

            var executionDataflowBlockOptions = new ExecutionDataflowBlockOptions
            {
                MaxDegreeOfParallelism = drillOptions.ConnectionCount
            };

            var transformBlock = new TransformBlock <DrillOptions, List <RequestResult> >(
                RequestFunc, executionDataflowBlockOptions);

            for (var i = 0; i < drillOptions.ConnectionCount; i++)
            {
                transformBlock.Post(drillOptions);
            }

            for (var i = 0; i < drillOptions.ConnectionCount; i++)
            {
                var requestResults = transformBlock.Receive();

                aggregatedResults.AddRange(requestResults);
            }

            var drillStats = new DrillStats
            {
                AverageResponseTime = aggregatedResults.Average(a => a.ResponseMilliseconds),
                ConnectionCount     = drillOptions.ConnectionCount,
                TotalRequestCount   = aggregatedResults.Count,
                FailureCount        = aggregatedResults.Count(c => !c.IsSuccessful),
                RequestResults      = aggregatedResults
            };

            drillOptions.Recorder?.RecordDrill(drillOptions, drillStats);

            return(drillStats);
        }
Exemplo n.º 22
0
        public static async Task SquareBlock()
        {
            var squareRootBlock = new TransformBlock <double, double>(x => Math.Sqrt(x));
            var subtractBlock   = new TransformBlock <double, double>(item => item - 2.0);

            //var options = new DataflowLinkOptions { PropagateCompletion = true };
            //squareRootBlock.LinkTo(subtractBlock, options);
            //// The first block's completion is automatically propagated to the second block.
            //squareRootBlock.Complete();
            //await subtractBlock.Completion;

            await squareRootBlock.SendAsync(25.0);

            double result = squareRootBlock.Receive();

            double sqrt;

            squareRootBlock.TryReceive(out sqrt);
        }
Exemplo n.º 23
0
        public static void TestSync8()
        {
            tbSync.LinkTo(ab4);

            for (int i = 0; i < 10; i++)
            {
                tbSync.Post(i);
            }
            for (int i = 9; i >= 0; i--)
            {
                Console.WriteLine(tbSync.Receive());
            }

            tbSync.Complete();
            Console.WriteLine("Post finished");

            tbSync.Completion.Wait();
            Console.WriteLine("TransformBlock process finished");
        }
Exemplo n.º 24
0
        static public void Run()
        {
            Func <int, int> fn = n => {
                Thread.Sleep(1000);
                return(n * n);
            };

            var tfBlock = new TransformBlock <int, int>(fn);

            for (int i = 0; i < 10; i++)
            {
                tfBlock.Post(i);
            }

            for (int i = 0; i < 10; i++)
            {
                int result = tfBlock.Receive();
                Console.WriteLine(result);
            }

            Console.WriteLine("Done");
        }
Exemplo n.º 25
0
        private static void Example1() // NOTE: Синхронный прием данных
        {
            Func <int, int> fn = n =>
            {
                Thread.Sleep(TimeSpan.FromSeconds(1));
                return(n * n);
            };

            var transformBlock = new TransformBlock <int, int>(fn);

            for (var i = 0; i < 10; i++)
            {
                transformBlock.Post(i);
            }

            for (var i = 0; i < 10; i++)
            {
                var result = transformBlock.Receive();
                Console.WriteLine(result);
            }

            Console.WriteLine("Done");
        }
Exemplo n.º 26
0
        public void SimpleTransform()
        {
            var block = new TransformBlock <int, string>(a =>
            {
                Task.Delay(500).Wait();
                return(a.ToString());
            });

            for (int i = 0; i < 10; i++)
            {
                block.Post(i);
                Console.WriteLine($"Transform input queue: {block.InputCount}");
            }

            for (int i = 0; i < 10; i++)
            {
                var result = block.Receive();
                Console.WriteLine($"Received: {result}");
                Console.WriteLine($"Transform output queue: {block.OutputCount}");
            }

            Console.WriteLine("Done!");
        }
Exemplo n.º 27
0
        public IMetaData TransformBlockUsage(string stringToSplit)
        {
            Console.WriteLine($"Inside {nameof(TplDataflow1ExecutionBlocksController)} - {nameof(TransformBlockUsage)}");

            // Create the members of the pipeline.
            var transformBlockSplitAnInputStringIntoArray = new TransformBlock <string, string[]>(input =>
                                                                                                  Functions.SplitAnInputStringIntoArray(input, SplitterSeparator)
                                                                                                  );
            var transformBlockCreateASingleMedatadataFromStrings = new TransformBlock <string[], IMetaData>(stringArray =>
                                                                                                            Functions.CreateASingleMedatadataFromStrings(stringArray)
                                                                                                            );

            // Connect the dataflow blocks to form a pipeline.
            transformBlockSplitAnInputStringIntoArray.LinkTo(transformBlockCreateASingleMedatadataFromStrings, DataflowOptions.LinkOptions);

            // Start TransformBlockUsage pipeline with the input values.
            transformBlockSplitAnInputStringIntoArray.Post(stringToSplit);

            // Mark the head of the pipeline as complete.
            transformBlockSplitAnInputStringIntoArray.Complete();

            // Return the value transformed by the last TransformBlock.
            return(transformBlockCreateASingleMedatadataFromStrings.Receive());
        }
Exemplo n.º 28
0
        public void RunWriteOnceBlockConformanceTests()
        {
            bool passed = true, localPassed = true;

            {
                // Test posting then receiving
                localPassed = true;
                var wob             = new WriteOnceBlock <int>(i => i);
                int successfulPosts = 0;
                for (int i = 10; i <= 20; i++)
                {
                    successfulPosts += wob.Post(i) ? 1 : 0;
                }
                localPassed |= successfulPosts == 1;
                localPassed |= wob.Receive() == 10;
                Console.WriteLine("{0}: Posting then receiving", localPassed ? "Success" : "Failure");
                passed &= localPassed;
            }

            {
                // Test receiving then posting
                localPassed = true;
                var wob = new WriteOnceBlock <int>(i => i);
                Task.Factory.StartNew(() =>
                {
                    Task.Delay(1000).Wait();
                    wob.Post(42);
                });
                localPassed |= wob.Receive() == 42;
                localPassed |= wob.Post(43) == false;
                wob.Completion.Wait();
                Console.WriteLine("{0}: Receiving then posting", localPassed ? "Success" : "Failure");
                passed &= localPassed;
            }

            {
                // Test broadcasting
                localPassed = true;
                var wob = new WriteOnceBlock <int>(i => i + 1);
                var tb1 = new TransformBlock <int, int>(i => i);
                var tb2 = new TransformBlock <int, int>(i => i);
                var tb3 = new TransformBlock <int, int>(i => i);
                wob.LinkTo(tb1);
                wob.LinkTo(tb2);
                wob.LinkTo(tb3);
                wob.Post(42);
                localPassed |= tb1.Receive() == 43;
                localPassed |= tb2.Receive() == 43;
                localPassed |= tb3.Receive() == 43;
                Console.WriteLine("{0}: Broadcasting", localPassed ? "Success" : "Failure");
                passed &= localPassed;
            }

            {
                // Test using a precanceled token
                localPassed = true;
                try
                {
                    var cts = new CancellationTokenSource();
                    cts.Cancel();
                    var dbo = new DataflowBlockOptions {
                        CancellationToken = cts.Token
                    };
                    var wob = new WriteOnceBlock <int>(i => i, dbo);

                    int         ignoredValue;
                    IList <int> ignoredValues;
                    localPassed &= wob.LinkTo(new ActionBlock <int>(delegate { })) != null;
                    localPassed &= wob.SendAsync(42).Result == false;
                    localPassed &= ((IReceivableSourceBlock <int>)wob).TryReceiveAll(out ignoredValues) == false;
                    localPassed &= wob.Post(42) == false;
                    localPassed &= wob.TryReceive(out ignoredValue) == false;
                    localPassed &= wob.Completion != null;
                    wob.Complete();
                }
                catch (Exception)
                {
                    localPassed = false;
                }
                Console.WriteLine("{0}: Precanceled tokens work correctly", localPassed ? "Success" : "Failure");
                passed &= localPassed;
            }

            {
                // Test using token canceled after construction
                localPassed = true;
                try
                {
                    var cts = new CancellationTokenSource();
                    var dbo = new DataflowBlockOptions {
                        CancellationToken = cts.Token
                    };
                    var wob = new WriteOnceBlock <int>(i => i, dbo);
                    cts.Cancel();

                    int         ignoredValue;
                    IList <int> ignoredValues;
                    localPassed &= wob.LinkTo(new ActionBlock <int>(delegate { })) != null;
                    localPassed &= wob.SendAsync(42).Result == false;
                    localPassed &= ((IReceivableSourceBlock <int>)wob).TryReceiveAll(out ignoredValues) == false;
                    localPassed &= wob.Post(42) == false;
                    localPassed &= wob.TryReceive(out ignoredValue) == false;
                    localPassed &= wob.Completion != null;
                    wob.Complete();
                }
                catch (Exception)
                {
                    localPassed = false;
                }
                Console.WriteLine("{0}: Precanceled tokens work correctly", localPassed ? "Success" : "Failure");
                passed &= localPassed;
            }

            Assert.True(passed, "Test failed.");
        }
Exemplo n.º 29
0
        //[Fact(Skip = "Outerloop")]
        public void RunTransformBlockConformanceTests()
        {
            bool passed = true;

            // SYNC
            #region Sync
            {
                // Do everything twice - once through OfferMessage and Once through Post
                for (FeedMethod feedMethod = FeedMethod._First; passed & feedMethod < FeedMethod._Count; feedMethod++)
                {
                    Func<DataflowBlockOptions, TargetProperties<int>> transformBlockFactory =
                        options =>
                        {
                            TransformBlock<int, int> transformBlock = new TransformBlock<int, int>(i => i, (ExecutionDataflowBlockOptions)options);
                            ActionBlock<int> actionBlock = new ActionBlock<int>(i => TrackCaptures(i), (ExecutionDataflowBlockOptions)options);

                            transformBlock.LinkTo(actionBlock);

                            return new TargetProperties<int> { Target = transformBlock, Capturer = actionBlock, ErrorVerifyable = false };
                        };
                    CancellationTokenSource cancellationSource = new CancellationTokenSource();
                    var defaultOptions = new ExecutionDataflowBlockOptions();
                    var dopOptions = new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = Environment.ProcessorCount };
                    var mptOptions = new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = Environment.ProcessorCount, MaxMessagesPerTask = 2 };
                    var cancellationOptions = new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = Environment.ProcessorCount, MaxMessagesPerTask = 2, CancellationToken = cancellationSource.Token };

                    passed &= FeedTarget(transformBlockFactory, defaultOptions, 1, Intervention.None, null, feedMethod, true);
                    passed &= FeedTarget(transformBlockFactory, defaultOptions, 1, Intervention.None, null, feedMethod, true);
                    passed &= FeedTarget(transformBlockFactory, dopOptions, 1, Intervention.None, null, feedMethod, true);
                    passed &= FeedTarget(transformBlockFactory, mptOptions, 1, Intervention.None, null, feedMethod, true);
                    passed &= FeedTarget(transformBlockFactory, mptOptions, 1, Intervention.Complete, null, feedMethod, true);
                    passed &= FeedTarget(transformBlockFactory, cancellationOptions, 1, Intervention.Cancel, cancellationSource, feedMethod, true);
                }

                // Test chained Post/Receive
                {
                    bool localPassed = true;
                    const int ITERS = 2;
                    var network = Chain<TransformBlock<int, int>, int>(4, () => new TransformBlock<int, int>(i => i * 2));
                    for (int i = 0; i < ITERS; i++)
                    {
                        network.Post(i);
                        localPassed &= (((IReceivableSourceBlock<int>)network).Receive() == i * 16);
                    }
                    Console.WriteLine("{0}: Chained Post/Receive", localPassed ? "Success" : "Failure");
                    passed &= localPassed;
                }

                // Test chained SendAsync/Receive
                {
                    bool localPassed = true;
                    const int ITERS = 2;
                    var network = Chain<TransformBlock<int, int>, int>(4, () => new TransformBlock<int, int>(i => i * 2));
                    for (int i = 0; i < ITERS; i++)
                    {
                        network.SendAsync(i);
                        localPassed &= (((IReceivableSourceBlock<int>)network).Receive() == i * 16);
                    }
                    Console.WriteLine("{0}: Chained SendAsync/Receive", localPassed ? "Success" : "Failure");
                    passed &= localPassed;
                }

                // Test chained Post all then Receive
                {
                    bool localPassed = true;
                    const int ITERS = 2;
                    var network = Chain<TransformBlock<int, int>, int>(4, () => new TransformBlock<int, int>(i => i * 2));
                    for (int i = 0; i < ITERS; i++) localPassed &= network.Post(i) == true;
                    for (int i = 0; i < ITERS; i++) localPassed &= ((IReceivableSourceBlock<int>)network).Receive() == i * 16;
                    Console.WriteLine("{0}: Chained Post all then Receive", localPassed ? "Success" : "Failure");
                    passed &= localPassed;
                }

                // Test chained SendAsync all then Receive
                {
                    bool localPassed = true;
                    const int ITERS = 2;
                    var network = Chain<TransformBlock<int, int>, int>(4, () => new TransformBlock<int, int>(i => i * 2));
                    var tasks = new Task[ITERS];
                    for (int i = 1; i <= ITERS; i++) tasks[i - 1] = network.SendAsync(i);
                    Task.WaitAll(tasks);
                    int total = 0;
                    for (int i = 1; i <= ITERS; i++) total += ((IReceivableSourceBlock<int>)network).Receive();
                    localPassed &= (total == ((ITERS * (ITERS + 1)) / 2 * 16));
                    Console.WriteLine("{0}: Chained SendAsync all then Receive", localPassed ? "Success" : "Failure");
                    passed &= localPassed;
                }

                // Test that OperationCanceledExceptions are ignored
                {
                    bool localPassed = true;

                    var t = new TransformBlock<int, int>(i =>
                    {
                        if ((i % 2) == 0) throw new OperationCanceledException();
                        return i;
                    });
                    for (int i = 0; i < 2; i++) t.Post(i);
                    t.Complete();
                    for (int i = 0; i < 2; i++)
                    {
                        if ((i % 2) != 0) localPassed &= t.Receive() == i;
                    }
                    t.Completion.Wait();
                    Console.WriteLine("{0}: OperationCanceledExceptions are ignored", localPassed ? "Success" : "Failure");
                    passed &= localPassed;
                }

                // Test using a precanceled token
                {
                    bool localPassed = true;
                    try
                    {
                        var cts = new CancellationTokenSource();
                        cts.Cancel();
                        var dbo = new ExecutionDataflowBlockOptions { CancellationToken = cts.Token };
                        var t = new TransformBlock<int, int>(i => i, dbo);

                        int ignoredValue;
                        IList<int> ignoredValues;
                        localPassed &= t.LinkTo(new ActionBlock<int>(delegate { })) != null;
                        localPassed &= t.SendAsync(42).Result == false;
                        localPassed &= t.TryReceiveAll(out ignoredValues) == false;
                        localPassed &= t.Post(42) == false;
                        localPassed &= t.OutputCount == 0;
                        localPassed &= t.TryReceive(out ignoredValue) == false;
                        localPassed &= t.Completion != null;
                        t.Complete();
                    }
                    catch (Exception)
                    {
                        localPassed = false;
                    }
                    Console.WriteLine("    {0}: Precanceled tokens work correctly", localPassed ? "Success" : "Failure");
                    passed &= localPassed;
                }

                // Test faulting
                {
                    bool localPassed = true;
                    var t = new TransformBlock<int, int>(new Func<int, int>(i => { throw new InvalidOperationException(); }));
                    t.Post(42);
                    t.Post(1);
                    t.Post(2);
                    t.Post(3);
                    try { t.Completion.Wait(); }
                    catch { }
                    localPassed &= t.Completion.IsFaulted;
                    localPassed &= SpinWait.SpinUntil(() => t.InputCount == 0, 500);
                    localPassed &= SpinWait.SpinUntil(() => t.OutputCount == 0, 500);
                    localPassed &= t.Post(4) == false;
                    Console.WriteLine("    {0}: Faulted handled correctly", localPassed ? "Success" : "Failure");
                    passed &= localPassed;
                }
            }
            #endregion

            #region Async
            // ASYNC (a copy of the sync but with constructors returning Task<T> instead of T
            {
                // Do everything twice - once through OfferMessage and Once through Post
                for (FeedMethod feedMethod = FeedMethod._First; passed & feedMethod < FeedMethod._Count; feedMethod++)
                {
                    Func<DataflowBlockOptions, TargetProperties<int>> transformBlockFactory =
                        options =>
                        {
                            TransformBlock<int, int> transformBlock = new TransformBlock<int, int>(i => Task.Factory.StartNew(() => i), (ExecutionDataflowBlockOptions)options);
                            ActionBlock<int> actionBlock = new ActionBlock<int>(i => TrackCaptures(i), (ExecutionDataflowBlockOptions)options);

                            transformBlock.LinkTo(actionBlock);

                            return new TargetProperties<int> { Target = transformBlock, Capturer = actionBlock, ErrorVerifyable = false };
                        };
                    CancellationTokenSource cancellationSource = new CancellationTokenSource();
                    var defaultOptions = new ExecutionDataflowBlockOptions();
                    var dopOptions = new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = Environment.ProcessorCount };
                    var mptOptions = new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = Environment.ProcessorCount, MaxMessagesPerTask = 2 };
                    var cancellationOptions = new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = Environment.ProcessorCount, MaxMessagesPerTask = 2, CancellationToken = cancellationSource.Token };

                    passed &= FeedTarget(transformBlockFactory, defaultOptions, 1, Intervention.None, null, feedMethod, true);
                    passed &= FeedTarget(transformBlockFactory, dopOptions, 10, Intervention.None, null, feedMethod, true);
                    passed &= FeedTarget(transformBlockFactory, mptOptions, 10000, Intervention.None, null, feedMethod, true);
                    passed &= FeedTarget(transformBlockFactory, mptOptions, 10000, Intervention.Complete, null, feedMethod, true);
                    passed &= FeedTarget(transformBlockFactory, cancellationOptions, 10000, Intervention.Cancel, cancellationSource, feedMethod, true);
                }

                // Test chained Post/Receive
                {
                    bool localPassed = true;
                    const int ITERS = 2;
                    var network = Chain<TransformBlock<int, int>, int>(4, () => new TransformBlock<int, int>(i => Task.Factory.StartNew(() => i * 2)));
                    for (int i = 0; i < ITERS; i++)
                    {
                        network.Post(i);
                        localPassed &= (((IReceivableSourceBlock<int>)network).Receive() == i * 16);
                    }
                    Console.WriteLine("{0}: Chained Post/Receive", localPassed ? "Success" : "Failure");
                    passed &= localPassed;
                }

                // Test chained SendAsync/Receive
                {
                    bool localPassed = true;
                    const int ITERS = 2;
                    var network = Chain<TransformBlock<int, int>, int>(4, () => new TransformBlock<int, int>(i => Task.Factory.StartNew(() => i * 2)));
                    for (int i = 0; i < ITERS; i++)
                    {
                        network.SendAsync(i);
                        localPassed &= (((IReceivableSourceBlock<int>)network).Receive() == i * 16);
                    }
                    Console.WriteLine("{0}: Chained SendAsync/Receive", localPassed ? "Success" : "Failure");
                    passed &= localPassed;
                }

                // Test chained Post all then Receive
                {
                    bool localPassed = true;
                    const int ITERS = 2;
                    var network = Chain<TransformBlock<int, int>, int>(4, () => new TransformBlock<int, int>(i => Task.Factory.StartNew(() => i * 2)));
                    for (int i = 0; i < ITERS; i++) localPassed &= network.Post(i) == true;
                    for (int i = 0; i < ITERS; i++) localPassed &= ((IReceivableSourceBlock<int>)network).Receive() == i * 16;
                    Console.WriteLine("{0}: Chained Post all then Receive", localPassed ? "Success" : "Failure");
                    passed &= localPassed;
                }

                // Test chained SendAsync all then Receive
                {
                    bool localPassed = true;
                    const int ITERS = 2;
                    var network = Chain<TransformBlock<int, int>, int>(4, () => new TransformBlock<int, int>(i => Task.Factory.StartNew(() => i * 2)));
                    var tasks = new Task[ITERS];
                    for (int i = 1; i <= ITERS; i++) tasks[i - 1] = network.SendAsync(i);
                    Task.WaitAll(tasks);
                    int total = 0;
                    for (int i = 1; i <= ITERS; i++) total += ((IReceivableSourceBlock<int>)network).Receive();
                    localPassed &= (total == ((ITERS * (ITERS + 1)) / 2 * 16));
                    Console.WriteLine("{0}: Chained SendAsync all then Receive", localPassed ? "Success" : "Failure");
                    passed &= localPassed;
                }

                // Test that OperationCanceledExceptions are ignored
                {
                    bool localPassed = true;

                    var t = new TransformBlock<int, int>(i =>
                    {
                        if ((i % 2) == 0) throw new OperationCanceledException();
                        return Task.Factory.StartNew(() => i);
                    });
                    for (int i = 0; i < 2; i++) t.Post(i);
                    t.Complete();
                    for (int i = 0; i < 2; i++)
                    {
                        if ((i % 2) != 0) localPassed &= t.Receive() == i;
                    }
                    t.Completion.Wait();
                    Console.WriteLine("{0}: OperationCanceledExceptions are ignored", localPassed ? "Success" : "Failure");
                    passed &= localPassed;
                }

                // Test that null tasks are ignored
                {
                    bool localPassed = true;

                    var t = new TransformBlock<int, int>(i =>
                    {
                        if ((i % 2) == 0) return null;
                        return Task.Factory.StartNew(() => i);
                    });
                    for (int i = 0; i < 2; i++) t.Post(i);
                    t.Complete();
                    for (int i = 0; i < 2; i++)
                    {
                        if ((i % 2) != 0) localPassed &= t.Receive() == i;
                    }
                    t.Completion.Wait();
                    Console.WriteLine("{0}: null tasks are ignored", localPassed ? "Success" : "Failure");
                    passed &= localPassed;
                }

                // Test that null tasks are ignored when a reordering buffer is in place
                {
                    bool localPassed = true;

                    var t = new TransformBlock<int, int>(i =>
                    {
                        if (i == 0)
                        {
                            Task.Delay(10).Wait();
                            return null;
                        }
                        return Task.Factory.StartNew(() => i);
                    }, new ExecutionDataflowBlockOptions { MaxDegreeOfParallelism = 2 });
                    t.Post(0);
                    t.Post(1);
                    try
                    {
                        localPassed &= t.Receive(TimeSpan.FromSeconds(4)) == 1;
                    }
                    catch
                    {
                        localPassed = false;
                    }
                    Console.WriteLine("{0}: null tasks are ignored with reordering buffer", localPassed ? "Success" : "Failure");
                    passed &= localPassed;
                }

                // Test faulting from the delegate
                {
                    bool localPassed = true;
                    var t = new TransformBlock<int, int>(new Func<int, Task<int>>(i => { throw new InvalidOperationException(); }));
                    t.Post(42);
                    t.Post(1);
                    t.Post(2);
                    t.Post(3);
                    try { t.Completion.Wait(); }
                    catch { }
                    localPassed &= t.Completion.IsFaulted;
                    localPassed &= SpinWait.SpinUntil(() => t.InputCount == 0, 500);
                    localPassed &= SpinWait.SpinUntil(() => t.OutputCount == 0, 500);
                    localPassed &= t.Post(4) == false;
                    Console.WriteLine("    {0}: Faulted from delegate handled correctly", localPassed ? "Success" : "Failure");
                    passed &= localPassed;
                }

                // Test faulting from the task
                {
                    bool localPassed = true;
                    var t = new TransformBlock<int, int>(new Func<int, Task<int>>(i => Task<int>.Factory.StartNew(() => { throw new InvalidOperationException(); })));
                    t.Post(42);
                    t.Post(1);
                    t.Post(2);
                    t.Post(3);
                    try { t.Completion.Wait(); }
                    catch { }
                    localPassed &= t.Completion.IsFaulted;
                    localPassed &= SpinWait.SpinUntil(() => t.InputCount == 0, 500);
                    localPassed &= SpinWait.SpinUntil(() => t.OutputCount == 0, 500);
                    localPassed &= t.Post(4) == false;
                    Console.WriteLine("    {0}: Faulted from task handled correctly", localPassed ? "Success" : "Failure");
                    passed &= localPassed;
                }
            }
            #endregion

            Assert.True(passed, "Test failed.");
        }
Exemplo n.º 30
0
        public void RunBroadcastBlockConformanceTests()
        {
            bool localPassed = true;
            {
                // Test posting then receiving
                localPassed = true;
                var bb = new BroadcastBlock <int>(i => i);
                for (int i = 0; i < 2; i++)
                {
                    bb.Post(i);
                }
                Task.Delay(1).Wait();
                localPassed |= bb.Receive() == 1;
                Assert.True(localPassed, string.Format("{0}: Posting then receiving", localPassed ? "Success" : "Failure"));
            }

            {
                // Test receiving then posting
                localPassed = true;
                var bb = new BroadcastBlock <int>(i => i);
                Task.Run(() =>
                {
                    Task.Delay(1).Wait();
                    bb.Post(42);
                });

                localPassed |= bb.Receive() == 42;
                Assert.True(localPassed, string.Format("{0}: Receiving then posting", localPassed ? "Success" : "Failure"));
            }

            {
                // Test broadcasting
                localPassed = true;
                var bb  = new BroadcastBlock <int>(i => i + 1);
                var tb1 = new TransformBlock <int, int>(i => i);
                var tb2 = new TransformBlock <int, int>(i => i);
                var tb3 = new TransformBlock <int, int>(i => i);
                bb.LinkTo(tb1);
                bb.LinkTo(tb2);
                bb.LinkTo(tb3);
                for (int i = 0; i < 2; i++)
                {
                    bb.Post(i);
                }
                for (int i = 0; i < 2; i++)
                {
                    localPassed |= tb1.Receive() == i + 1;
                    localPassed |= tb2.Receive() == i + 1;
                    localPassed |= tb3.Receive() == i + 1;
                }

                Assert.True(localPassed, string.Format("{0}: Broadcasting", localPassed ? "Success" : "Failure"));
            }

            // Test using a precanceled token
            {
                localPassed = true;
                try
                {
                    var cts = new CancellationTokenSource();
                    cts.Cancel();
                    var dbo = new DataflowBlockOptions {
                        CancellationToken = cts.Token
                    };
                    var bb = new BroadcastBlock <int>(i => i, dbo);

                    int         ignoredValue;
                    IList <int> ignoredValues;
                    localPassed &= bb.LinkTo(new ActionBlock <int>(delegate { })) != null;
                    localPassed &= bb.SendAsync(42).Result == false;
                    localPassed &= ((IReceivableSourceBlock <int>)bb).TryReceiveAll(out ignoredValues) == false;
                    localPassed &= bb.Post(42) == false;
                    localPassed &= bb.TryReceive(out ignoredValue) == false;
                    localPassed &= bb.Completion != null;
                    bb.Complete();
                }
                catch (Exception)
                {
                    localPassed = false;
                }

                Assert.True(localPassed, string.Format("{0}: Precanceled tokens work correctly", localPassed ? "Success" : "Failure"));
            }
        }
Exemplo n.º 31
0
        public void RunWriteOnceBlockConformanceTests()
        {
            bool passed = true, localPassed = true;
            {
                // Test posting then receiving
                localPassed = true;
                var wob = new WriteOnceBlock<int>(i => i);
                int successfulPosts = 0;
                for (int i = 10; i <= 20; i++)
                {
                    successfulPosts += wob.Post(i) ? 1 : 0;
                }
                localPassed |= successfulPosts == 1;
                localPassed |= wob.Receive() == 10;
                Console.WriteLine("{0}: Posting then receiving", localPassed ? "Success" : "Failure");
                passed &= localPassed;
            }

            {
                // Test receiving then posting
                localPassed = true;
                var wob = new WriteOnceBlock<int>(i => i);
                Task.Factory.StartNew(() =>
                {
                    Task.Delay(1000).Wait();
                    wob.Post(42);
                });
                localPassed |= wob.Receive() == 42;
                localPassed |= wob.Post(43) == false;
                wob.Completion.Wait();
                Console.WriteLine("{0}: Receiving then posting", localPassed ? "Success" : "Failure");
                passed &= localPassed;
            }

            {
                // Test broadcasting
                localPassed = true;
                var wob = new WriteOnceBlock<int>(i => i + 1);
                var tb1 = new TransformBlock<int, int>(i => i);
                var tb2 = new TransformBlock<int, int>(i => i);
                var tb3 = new TransformBlock<int, int>(i => i);
                wob.LinkTo(tb1);
                wob.LinkTo(tb2);
                wob.LinkTo(tb3);
                wob.Post(42);
                localPassed |= tb1.Receive() == 43;
                localPassed |= tb2.Receive() == 43;
                localPassed |= tb3.Receive() == 43;
                Console.WriteLine("{0}: Broadcasting", localPassed ? "Success" : "Failure");
                passed &= localPassed;
            }

            {
                // Test using a precanceled token
                localPassed = true;
                try
                {
                    var cts = new CancellationTokenSource();
                    cts.Cancel();
                    var dbo = new DataflowBlockOptions { CancellationToken = cts.Token };
                    var wob = new WriteOnceBlock<int>(i => i, dbo);

                    int ignoredValue;
                    IList<int> ignoredValues;
                    localPassed &= wob.LinkTo(new ActionBlock<int>(delegate { })) != null;
                    localPassed &= wob.SendAsync(42).Result == false;
                    localPassed &= ((IReceivableSourceBlock<int>)wob).TryReceiveAll(out ignoredValues) == false;
                    localPassed &= wob.Post(42) == false;
                    localPassed &= wob.TryReceive(out ignoredValue) == false;
                    localPassed &= wob.Completion != null;
                    wob.Complete();
                }
                catch (Exception)
                {
                    localPassed = false;
                }
                Console.WriteLine("{0}: Precanceled tokens work correctly", localPassed ? "Success" : "Failure");
                passed &= localPassed;
            }

            {
                // Test using token canceled after construction
                localPassed = true;
                try
                {
                    var cts = new CancellationTokenSource();
                    var dbo = new DataflowBlockOptions { CancellationToken = cts.Token };
                    var wob = new WriteOnceBlock<int>(i => i, dbo);
                    cts.Cancel();

                    int ignoredValue;
                    IList<int> ignoredValues;
                    localPassed &= wob.LinkTo(new ActionBlock<int>(delegate { })) != null;
                    localPassed &= wob.SendAsync(42).Result == false;
                    localPassed &= ((IReceivableSourceBlock<int>)wob).TryReceiveAll(out ignoredValues) == false;
                    localPassed &= wob.Post(42) == false;
                    localPassed &= wob.TryReceive(out ignoredValue) == false;
                    localPassed &= wob.Completion != null;
                    wob.Complete();
                }
                catch (Exception)
                {
                    localPassed = false;
                }
                Console.WriteLine("{0}: Precanceled tokens work correctly", localPassed ? "Success" : "Failure");
                passed &= localPassed;
            }

            Assert.True(passed, "Test failed.");
        }
Exemplo n.º 32
0
 private Frame Dequeue()
 {
     FScheduledFrameInfos.RemoveFirst();
     return(FFramePreloader.Receive());
 }
Exemplo n.º 33
0
        //[Fact(Skip = "Outerloop")]
        public void RunBroadcastBlockConformanceTests()
        {
            bool localPassed = true;
            {
                // Test posting then receiving
                localPassed = true;
                var bb = new BroadcastBlock<int>(i => i);
                for (int i = 0; i < 2; i++) bb.Post(i);
                Task.Delay(1).Wait();
                localPassed |= bb.Receive() == 1;
                Assert.True(localPassed, string.Format("{0}: Posting then receiving", localPassed ? "Success" : "Failure"));
            }

            {
                // Test receiving then posting
                localPassed = true;
                var bb = new BroadcastBlock<int>(i => i);
                Task.Factory.StartNew(() =>
                {
                    Task.Delay(1).Wait();
                    bb.Post(42);
                });

                localPassed |= bb.Receive() == 42;
                Assert.True(localPassed, string.Format("{0}: Receiving then posting", localPassed ? "Success" : "Failure"));
            }

            {
                // Test broadcasting
                localPassed = true;
                var bb = new BroadcastBlock<int>(i => i + 1);
                var tb1 = new TransformBlock<int, int>(i => i);
                var tb2 = new TransformBlock<int, int>(i => i);
                var tb3 = new TransformBlock<int, int>(i => i);
                bb.LinkTo(tb1);
                bb.LinkTo(tb2);
                bb.LinkTo(tb3);
                for (int i = 0; i < 2; i++)
                {
                    bb.Post(i);
                }
                for (int i = 0; i < 2; i++)
                {
                    localPassed |= tb1.Receive() == i + 1;
                    localPassed |= tb2.Receive() == i + 1;
                    localPassed |= tb3.Receive() == i + 1;
                }

                Assert.True(localPassed, string.Format("{0}: Broadcasting", localPassed ? "Success" : "Failure"));
            }

            // Test using a precanceled token
            {
                localPassed = true;
                try
                {
                    var cts = new CancellationTokenSource();
                    cts.Cancel();
                    var dbo = new DataflowBlockOptions { CancellationToken = cts.Token };
                    var bb = new BroadcastBlock<int>(i => i, dbo);

                    int ignoredValue;
                    IList<int> ignoredValues;
                    localPassed &= bb.LinkTo(new ActionBlock<int>(delegate { })) != null;
                    localPassed &= bb.SendAsync(42).Result == false;
                    localPassed &= ((IReceivableSourceBlock<int>)bb).TryReceiveAll(out ignoredValues) == false;
                    localPassed &= bb.Post(42) == false;
                    localPassed &= bb.TryReceive(out ignoredValue) == false;
                    localPassed &= bb.Completion != null;
                    bb.Complete();
                }
                catch (Exception)
                {
                    localPassed = false;
                }

                Assert.True(localPassed, string.Format("{0}: Precanceled tokens work correctly", localPassed ? "Success" : "Failure"));
            }
        }