public void PostActsAsProxy()
        {
            int receivedValue = 0;
            var ts            = new TaskSchedulerSingleThreaded();
            var ab            = new ActionBlock <int>(i => receivedValue = i, new ExecutionDataflowBlockOptions {
                TaskScheduler = ts
            });

            new TargetBlockWrapper <int>(ab).Post(42);

            ts.ExecuteAllTasksInActualThread();
            Assert.That(receivedValue, Is.EqualTo(42));
        }
Esempio n. 2
0
        public void CheckIfTplDataflowBlocksRunIASingleThread()
        {
            // Arrange
            // We inject the scheduler to the action blocks. Each block inserts the calling thread id to a dictionary.
            ConcurrentDictionary <int, int> threadIdCounts = new ConcurrentDictionary <int, int>();
            TaskSchedulerSingleThreaded     ts             = new TaskSchedulerSingleThreaded();
            var abList = new List <ActionBlock <string> >
            {
                new ActionBlock <string>(s => IncreaseThreadIdCount(), new ExecutionDataflowBlockOptions {
                    TaskScheduler = ts
                }),
                new ActionBlock <string>(s => IncreaseThreadIdCount(), new ExecutionDataflowBlockOptions {
                    TaskScheduler = ts
                }),
                new ActionBlock <string>(s => IncreaseThreadIdCount(), new ExecutionDataflowBlockOptions {
                    TaskScheduler = ts
                }),
                new ActionBlock <string>(s => IncreaseThreadIdCount(), new ExecutionDataflowBlockOptions {
                    TaskScheduler = ts
                }),
            };

            abList.ForEach(b => b.Post("test"));

            // Act -> call queued action block calls via a single thread
            ts.ExecuteAllTasksInActualThread();

            // Assert -> threadIdCounts should only contain one entry with the actual thread id.
            Assert.Multiple(() =>
            {
                var currentThreadId = Thread.CurrentThread.ManagedThreadId;
                Assert.That(threadIdCounts.ContainsKey(currentThreadId),
                            Is.True,
                            $"Current thread id {currentThreadId} is not included in {nameof(threadIdCounts)}");
                Assert.That(threadIdCounts.Count,
                            Is.EqualTo(1),
                            $"{nameof(threadIdCounts)} doesnt' contain 1 element");
                Assert.That(threadIdCounts[currentThreadId], Is.EqualTo(abList.Count));
            });

            // Locals
            void IncreaseThreadIdCount()
            {
                var tId = Thread.CurrentThread.ManagedThreadId;

                threadIdCounts[tId] = threadIdCounts.ContainsKey(tId) ? ++threadIdCounts[tId] : 1;
            }
        }
        public void ReceiveActsAsProxy()
        {
            var ts          = new TaskSchedulerSingleThreaded();
            var sourceBlock = new BufferBlock <int>(new DataflowBlockOptions {
                TaskScheduler = ts
            });
            var targetBlock = new BufferBlock <int>(new DataflowBlockOptions {
                TaskScheduler = ts
            });

            targetBlock.LinkTo(sourceBlock, new DataflowLinkOptions {
                PropagateCompletion = true
            });
            targetBlock.Post(42);
            ts.ExecuteAllTasksInActualThread();
            var v = new SourceBlockWrapper <int>(sourceBlock).Receive();

            Assert.That(v, Is.EqualTo(42));
        }