コード例 #1
0
        public void Adding_operations_to_a_fiber()
        {
            _count  = 10000;
            _values = new int[_count];
            Fiber fiber = new PoolFiber();

            int index     = 0;
            var completed = new Future <int>();

            var go = new Future <bool>();

            fiber.Add(() => { go.WaitUntilCompleted(10.Seconds()); });

            for (int i = 0; i < _count; i++)
            {
                int offset = i;
                fiber.Add(() =>
                {
                    _values[offset] = index++;

                    if (offset == _count - 1)
                    {
                        completed.Complete(offset);
                    }
                });
            }

            go.Complete(true);

            completed.WaitUntilCompleted(10.Seconds()).ShouldBeTrue();
        }
コード例 #2
0
		public void Adding_operations_to_a_fiber()
		{
			_count = 10000;
			_values = new int[_count];
			Fiber fiber = new PoolFiber();

			int index = 0;
			var completed = new Future<int>();

			var go = new Future<bool>();

			fiber.Add(() => { go.WaitUntilCompleted(10.Seconds()); });

			for (int i = 0; i < _count; i++)
			{
				int offset = i;
				fiber.Add(() =>
					{
						_values[offset] = index++;

						if (offset == _count - 1)
							completed.Complete(offset);
					});
			}

			go.Complete(true);

			completed.WaitUntilCompleted(10.Seconds()).ShouldBeTrue();
		}
コード例 #3
0
        public void Should_properly_wrap_the_channel_as_synchronized()
        {
            Assert.IsNull(SynchronizationContext.Current);

            var fiber = new PoolFiber();

            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();
            }
        }
コード例 #4
0
ファイル: ActionQueue_Specs.cs プロジェクト: Nangal/Stact
		public void Should_result_in_no_waiting_actions_in_the_queue()
		{
			Fiber fiber = new PoolFiber();

			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);
		}
コード例 #5
0
ファイル: ActionQueue_Specs.cs プロジェクト: yonglehou/Stact
        public void Should_result_in_no_waiting_actions_in_the_queue()
        {
            Fiber fiber = new PoolFiber();

            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);
        }
コード例 #6
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 PoolFiber();
                    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();
            }
        }
コード例 #7
0
ファイル: ActionQueue_Specs.cs プロジェクト: Nangal/Stact
		public void Should_prevent_new_actions_from_being_queued()
		{
			Fiber fiber = new PoolFiber();

			var called = new Future<bool>();

			fiber.Stop();

			fiber.Add(() => called.Complete(true));

			fiber.Shutdown(10.Seconds());

			called.IsCompleted.ShouldBeFalse();
		}
コード例 #8
0
ファイル: ActionQueue_Specs.cs プロジェクト: yonglehou/Stact
        public void Should_prevent_new_actions_from_being_queued()
        {
            Fiber fiber = new PoolFiber();

            var called = new Future <bool>();

            fiber.Stop();

            fiber.Add(() => called.Complete(true));

            fiber.Shutdown(10.Seconds());

            called.IsCompleted.ShouldBeFalse();
        }
コード例 #9
0
ファイル: LoopbackAction_Specs.cs プロジェクト: Nangal/Stact
		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 PoolFiber();
					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();
			}
		}