public void Should_properly_wrap_the_channel_as_synchronized()
        {
            Assert.IsNull(SynchronizationContext.Current);

            var fiber = new ThreadPoolFiber();

            var input = new ChannelAdapter();

            var context = new TestSynchronizationContext();

            var future = new Future <TestMessage>();

            SynchronizationContext.SetSynchronizationContext(context);

            Assert.IsNotNull(SynchronizationContext.Current);

            using (input.Connect(x =>
            {
                x.AddConsumerOf <TestMessage>()
                .OnCurrentSynchronizationContext()
                .UsingConsumer(message =>
                {
                    Trace.WriteLine("Received on Thread: " + Thread.CurrentThread.ManagedThreadId);

                    Assert.IsNotNull(SynchronizationContext.Current);
                    Assert.AreEqual(context, SynchronizationContext.Current);

                    future.Complete(message);
                });
            }))
            {
                Trace.WriteLine("Subscribed on Thread: " + Thread.CurrentThread.ManagedThreadId);

                SynchronizationContext.SetSynchronizationContext(null);

                input.Flatten().Select(c => c.GetType()).ShouldEqual(new[]
                {
                    typeof(ChannelAdapter),
                    typeof(BroadcastChannel),
                    typeof(TypedChannelAdapter <TestMessage>),
                    typeof(SynchronizedChannel <TestMessage>),
                    typeof(ConsumerChannel <TestMessage>),
                });

                fiber.Add(() =>
                {
                    Trace.WriteLine("Thread: " + Thread.CurrentThread.ManagedThreadId);
                    Assert.IsNull(SynchronizationContext.Current);

                    input.Send(new TestMessage());
                });

                Assert.IsNull(SynchronizationContext.Current);

                future.WaitUntilCompleted(2.Seconds()).ShouldBeTrue();
            }
        }
        public void Should_properly_wrap_the_channel_as_synchronized()
        {
            Assert.IsNull(SynchronizationContext.Current);

            var fiber = new ThreadPoolFiber();

            var input = new ChannelAdapter();

            var context = new TestSynchronizationContext();

            var future = new Future<TestMessage>();

            SynchronizationContext.SetSynchronizationContext(context);

            Assert.IsNotNull(SynchronizationContext.Current);

            using (input.Connect(x =>
                {
                    x.AddConsumerOf<TestMessage>()
                        .OnCurrentSynchronizationContext()
                        .UsingConsumer(message =>
                            {
                                Trace.WriteLine("Received on Thread: " + Thread.CurrentThread.ManagedThreadId);

                                Assert.IsNotNull(SynchronizationContext.Current);
                                Assert.AreEqual(context, SynchronizationContext.Current);

                                future.Complete(message);
                            });
                }))
            {
                Trace.WriteLine("Subscribed on Thread: " + Thread.CurrentThread.ManagedThreadId);

                SynchronizationContext.SetSynchronizationContext(null);

                input.Flatten().Select(c => c.GetType()).ShouldEqual(new[]
                    {
                        typeof(ChannelAdapter),
                        typeof(BroadcastChannel),
                        typeof(TypedChannelAdapter<TestMessage>),
                        typeof(SynchronizedChannel<TestMessage>),
                        typeof(ConsumerChannel<TestMessage>),
                    });

                fiber.Add(() =>
                    {
                        Trace.WriteLine("Thread: " + Thread.CurrentThread.ManagedThreadId);
                        Assert.IsNull(SynchronizationContext.Current);

                        input.Send(new TestMessage());
                    });

                Assert.IsNull(SynchronizationContext.Current);

                future.WaitUntilCompleted(2.Seconds()).ShouldBeTrue();
            }
        }
        public void Should_result_in_no_waiting_actions_in_the_queue()
        {
            TraceLogger.Configure(LogLevel.Debug);

            Fiber fiber = new ThreadPoolFiber();

            var called = new Future<bool>();

            10.Times(() => fiber.Add(() => Thread.Sleep(100)));
            fiber.Add(() => called.Complete(true));

            Stopwatch timer = Stopwatch.StartNew();

            fiber.Shutdown(8.Seconds());

            timer.Stop();

            called.IsCompleted.ShouldBeTrue();

            timer.ElapsedMilliseconds.ShouldBeLessThan(2000);
        }
Exemple #4
0
        public void Should_result_in_no_waiting_actions_in_the_queue()
        {
            TraceLogger.Configure(LogLevel.Debug);

            Fiber fiber = new ThreadPoolFiber();

            var called = new Future <bool>();

            10.Times(() => fiber.Add(() => Thread.Sleep(100)));
            fiber.Add(() => called.Complete(true));

            Stopwatch timer = Stopwatch.StartNew();

            fiber.Shutdown(8.Seconds());

            timer.Stop();

            called.IsCompleted.ShouldBeTrue();

            timer.ElapsedMilliseconds.ShouldBeLessThan(2000);
        }
Exemple #5
0
        public void Should_properly_release_one_waiting_writer()
        {
            const int writerCount  = 10;
            const int messageCount = 1000;

            var complete = new Future <bool>();
            int total    = 0;

            Fiber reader = new ThreadFiber();

            try
            {
                Thread.Sleep(100);

                Stopwatch timer = Stopwatch.StartNew();

                var writers = new List <Fiber>();
                for (int i = 0; i < writerCount; i++)
                {
                    Fiber fiber = new ThreadPoolFiber();
                    for (int j = 0; j < messageCount; j++)
                    {
                        fiber.Add(() =>
                        {
                            SuperSleeper.Wait(1);

                            reader.Add(() =>
                            {
                                total++;
                                if (total == writerCount * messageCount)
                                {
                                    complete.Complete(true);
                                }
                            });
                        });
                    }

                    writers.Add(fiber);
                }

                complete.WaitUntilCompleted(20.Seconds()).ShouldBeTrue();

                timer.Stop();

                Trace.WriteLine("Elapsed time: " + timer.ElapsedMilliseconds + "ms (expected " + writerCount * messageCount + ")");
            }
            finally
            {
                reader.Stop();
            }
        }
        public void Should_prevent_new_actions_from_being_queued()
        {
            Fiber fiber = new ThreadPoolFiber();

            var called = new Future<bool>();

            fiber.Stop();

            Assert.Throws<FiberException>(() => fiber.Add(() => called.Complete(true)));

            fiber.Shutdown(10.Seconds());

            called.IsCompleted.ShouldBeFalse();
        }
Exemple #7
0
        public void Should_prevent_new_actions_from_being_queued()
        {
            Fiber fiber = new ThreadPoolFiber();

            var called = new Future <bool>();

            fiber.Stop();

            Assert.Throws <FiberException>(() => fiber.Add(() => called.Complete(true)));

            fiber.Shutdown(10.Seconds());

            called.IsCompleted.ShouldBeFalse();
        }
        public void Should_properly_release_one_waiting_writer()
        {
            const int writerCount = 10;
            const int messageCount = 1000;

            var complete = new Future<bool>();
            int total = 0;

            Fiber reader = new ThreadFiber();

            try
            {
                Thread.Sleep(100);

                Stopwatch timer = Stopwatch.StartNew();

                var writers = new List<Fiber>();
                for (int i = 0; i < writerCount; i++)
                {
                    Fiber fiber = new ThreadPoolFiber();
                    for (int j = 0; j < messageCount; j++)
                    {
                        fiber.Add(() =>
                            {
                                SuperSleeper.Wait(1);

                                reader.Add(() =>
                                    {
                                        total++;
                                        if (total == writerCount*messageCount)
                                            complete.Complete(true);
                                    });
                            });
                    }

                    writers.Add(fiber);
                }

                complete.WaitUntilCompleted(20.Seconds()).ShouldBeTrue();

                timer.Stop();

                Trace.WriteLine("Elapsed time: " + timer.ElapsedMilliseconds + "ms (expected " + writerCount*messageCount + ")");
            }
            finally
            {
                reader.Stop();
            }
        }
        public void Should_be_passable_to_another_action_queue()
        {
            Fiber fiber = new ThreadPoolFiber();

            var timer = Stopwatch.StartNew();

            Stopwatch inner = new Stopwatch();
            Stopwatch dep = new Stopwatch();
            try
            {
                using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew))
                {
                    inner.Start();

                    DependentTransaction dependentClone = Transaction.Current.DependentClone(DependentCloneOption.BlockCommitUntilComplete);

                    dependentClone.TransactionCompleted += (sender,args) =>
                        {
                            Trace.WriteLine("Completed");
                        };

                    fiber.Add(() =>
                        {
                            dep.Start();
                            try
                            {
                                //ThreadUtil.Sleep(20.Milliseconds());

                                Trace.WriteLine("complieing");
                                dependentClone.Complete();
                                dep.Stop();
                                Trace.WriteLine("done");
                            }
                            catch (Exception ex)
                            {
                                dependentClone.Rollback(ex);
                            }
                            finally
                            {
                                dependentClone.Dispose();
                                Trace.WriteLine("clone disposed");
                            }
                        });

                    scope.Complete();
                    Trace.WriteLine("scope complete");
                }

                Trace.WriteLine("all done");
            }
            catch (Exception)
            {
            }

            inner.Stop();
            timer.Stop();

            Trace.WriteLine("Timer: " + (int)timer.ElapsedMilliseconds);
            Trace.WriteLine("Inner: " + (int)inner.ElapsedMilliseconds);
            Trace.WriteLine("Dep: " + (int)dep.ElapsedMilliseconds);
        }
Exemple #10
0
        public void Should_be_passable_to_another_action_queue()
        {
            Fiber fiber = new ThreadPoolFiber();


            var timer = Stopwatch.StartNew();

            Stopwatch inner = new Stopwatch();
            Stopwatch dep   = new Stopwatch();

            try
            {
                using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew))
                {
                    inner.Start();

                    DependentTransaction dependentClone = Transaction.Current.DependentClone(DependentCloneOption.BlockCommitUntilComplete);

                    dependentClone.TransactionCompleted += (sender, args) =>
                    {
                        Trace.WriteLine("Completed");
                    };

                    fiber.Add(() =>
                    {
                        dep.Start();
                        try
                        {
                            //ThreadUtil.Sleep(20.Milliseconds());

                            Trace.WriteLine("complieing");
                            dependentClone.Complete();
                            dep.Stop();
                            Trace.WriteLine("done");
                        }
                        catch (Exception ex)
                        {
                            dependentClone.Rollback(ex);
                        }
                        finally
                        {
                            dependentClone.Dispose();
                            Trace.WriteLine("clone disposed");
                        }
                    });

                    scope.Complete();
                    Trace.WriteLine("scope complete");
                }

                Trace.WriteLine("all done");
            }
            catch (Exception)
            {
            }

            inner.Stop();
            timer.Stop();

            Trace.WriteLine("Timer: " + (int)timer.ElapsedMilliseconds);
            Trace.WriteLine("Inner: " + (int)inner.ElapsedMilliseconds);
            Trace.WriteLine("Dep: " + (int)dep.ElapsedMilliseconds);
        }