public void TestTailWithExampleLog()
        {
            File.Delete("test.log");

            var tailfiber = new PoolFiber();
            var reset = new AutoResetEvent(false);

            using (var tail = new Tail())
            {
                var tailchannel = new Channel<string>();
                tailchannel.Subscribe(tailfiber, str =>
                                                     {
                                                         tail.OutputRecievedEvent += OnOutputRecievedEvent;
                                                         tail.Start("test.log");
                                                     });

                tailfiber.Start();

                using (var testlog = new CreateTestLog("test.log"))
                {
                    tailchannel.Publish("");
                    testlog.StartWritingToTestLog(10);
                    testlog.Stop(); //don't really need to call this, but heh, what the...
                }

                tail.Stop();
                while(tail.State != TailState.Stopped){/*wait for tail to stop*/}
            }

            File.Delete("test.log");
        }
        public void SynchronousRequestWithMultipleReplies()
        {
            IFiber responder = new PoolFiber();
            responder.Start();
            var countChannel = new RequestReplyChannel<string, int>();

            var allSent = new AutoResetEvent(false);
            Action<IRequest<string, int>> onRequest =
                delegate(IRequest<string, int> req)
                {
                    for (var i = 0; i <= 5; i++)
                        req.SendReply(i);
                    allSent.Set();
                };
            countChannel.Subscribe(responder, onRequest);
            var response = countChannel.SendRequest("hello");
            int result;
            using (response)
            {
                for (var i = 0; i < 5; i++)
                {
                    Assert.IsTrue(response.Receive(10000, out result));
                    Assert.AreEqual(result, i);
                }
                allSent.WaitOne(10000, false);
            }
            Assert.IsTrue(response.Receive(30000, out result));
            Assert.AreEqual(5, result);
            Assert.IsFalse(response.Receive(30000, out result));
        }
Exemple #3
0
        public void Multiple()
        {
            var queues = new List<IFiber>();
            var receiveCount = 0;
            var reset = new AutoResetEvent(false);
            var channel = new QueueChannel<int>();

            var messageCount = 100;
            var updateLock = new object();
            for (var i = 0; i < 5; i++)
            {
                Action<int> onReceive = delegate
                                            {
                                                Thread.Sleep(15);
                                                lock (updateLock)
                                                {
                                                    receiveCount++;
                                                    if (receiveCount == messageCount)
                                                    {
                                                        reset.Set();
                                                    }
                                                }
                                            };
                var fiber = new PoolFiber();
                fiber.Start();
                queues.Add(fiber);
                channel.Subscribe(fiber, onReceive);
            }
            for (var i = 0; i < messageCount; i++)
            {
                channel.Publish(i);
            }
            Assert.IsTrue(reset.WaitOne(10000, false));
            queues.ForEach(delegate(IFiber q) { q.Dispose(); });
        }
Exemple #4
0
 public void SingleConsumer()
 {
     var one = new PoolFiber();
     one.Start();
     var oneConsumed = 0;
     var reset = new AutoResetEvent(false);
     using (one)
     {
         var channel = new QueueChannel<int>();
         Action<int> onMessage = delegate
         {
             oneConsumed++;
             if (oneConsumed == 20)
             {
                 reset.Set();
             }
         };
         channel.Subscribe(one, onMessage);
         for (var i = 0; i < 20; i++)
         {
             channel.Publish(i);
         }
         Assert.IsTrue(reset.WaitOne(10000, false));
     }
 }
        public IFiber GetContext(ContextType contextType)
        {
            IFiber fiber = null;
            switch (contextType)
            {
                case ContextType.New:
                    fiber = new PoolFiber();
                    fiber.Start();
                    //poolfibers.Add(fiber);
                    break;
                case ContextType.Gui:
                    //if(formFiber == null)
                    //{
                    //    if (marshaler == null) marshaler = new MarshalingControl();
                    //    formFiber = new FormFiber(marshaler, new BatchAndSingleExecutor());
                    //    formFiber.Start();
                    //}

                    //fiber = formFiber;

                    fiber = GetStartedGuiFiber();
                    break;
            }

            return fiber;
        }
 public void ExecuteOnlyAfterStart()
 {
     PoolFiber fiber = new PoolFiber();
     AutoResetEvent reset = new AutoResetEvent(false);
     fiber.Enqueue(delegate { reset.Set(); });
     Assert.IsFalse(reset.WaitOne(1, false));
     fiber.Start();
     Assert.IsTrue(reset.WaitOne(1000, false));
     fiber.Stop();
 }
 public void SynchronousRequestReply()
 {
     var responder = new PoolFiber();
     responder.Start();
     var timeCheck = new RequestReplyChannel<string, DateTime>();
     var now = DateTime.Now;
     Action<IRequest<string, DateTime>> onRequest = req => req.SendReply(now);
     timeCheck.Subscribe(responder, onRequest);
     var response = timeCheck.SendRequest("hello");
     DateTime result;
     Assert.IsTrue(response.Receive(10000, out result));
     Assert.AreEqual(result, now);
 }
Exemple #8
0
        public void TestFromRetlangSitePoolFiber()
        {
            //the test from retlang basic example
            using (var fiber = new PoolFiber())
            {
                fiber.Start();
                var channel = new Channel<string>();

                var reset = new AutoResetEvent(false);
                channel.Subscribe(fiber, delegate { reset.Set(); });
                channel.Publish("hello");

                Assert.IsTrue(reset.WaitOne(5000, false));

            }
        }
Exemple #9
0
        protected override void OnStartup(StartupEventArgs e)
        {
            RedirectDebug();

            Debug.WriteLine("------------------------------------");
            Debug.WriteLine("Application started");
            Debug.WriteLine("------------------------------------");

            var settings = Settings.Default;
            Thread.Sleep(settings.StartupDelaySecs * 1000);

            var poolFiber = new PoolFiber();
            poolFiber.Start();

            var mailChannel      = new Channel<MailMessage>();
            var foundClipChannel = new Channel<SimpleYouTubeClip>();
            var filteredChannel  = new Channel<SimpleYouTubeClip>();
            var richChannel      = new Channel<RichYouTubeClip>();

            var finishedChannel  = new Channel<RichYouTubeClip>();
            var commandChannel   = new Channel<PlayerCommand>();

            var extractor   = new LinkExtractor(poolFiber, mailChannel, foundClipChannel);
            var lunchFilter = new LunchFilter(poolFiber, foundClipChannel, filteredChannel);
            var downloader  = new YouTubeInfoDownloader(poolFiber, filteredChannel, richChannel);
            var playlist    = new PlaylistController(poolFiber, richChannel, finishedChannel, commandChannel);

            var main = new MainWindow(commandChannel, finishedChannel);

            lunchFilter.Enabled = settings.UseLunchFilter;

            // ----- FAKE INPUT ---------------------
            if (settings.DisplayFakeWindow)
            {
                var fake = new FakeMailWindow(mailChannel);
                fake.Show();
            }

            // ----- MAIL INPUT ---------------------
            if (settings.MailHost != "")
            {
                var server = new ServerAddress(settings.MailHost, settings.MailUseSsl ? 993 : 143, settings.MailUseSsl, settings.MailUser, settings.MailPassword);
                var mail = new MailReader(server, settings.MailCheckIntervalSecs * 1000, settings.DeleteHandledMail, poolFiber, mailChannel);
            }

            base.OnStartup(e);
        }
        public void BasicPubSubWithPoolQueue()
        {
            IFiber queue = new PoolFiber();
            queue.Start();
            Channel<string> hello = new Channel<string>();
            Channel<string> hello2 = new Channel<string>();

            AutoResetEvent reset = new AutoResetEvent(false);
            Action<string> receiveHello = delegate(string str)
                                              {
                                                  Assert.AreEqual("hello", str);
                                                  reset.Set();
                                              };
            hello.Subscribe(queue, receiveHello);
            hello2.Subscribe(queue, receiveHello);
            Assert.IsTrue(hello.Publish("hello"));
            Assert.IsTrue(reset.WaitOne(10000, false));
            queue.Dispose();
        }
Exemple #11
0
        public static void Register(Container container)
        {
            var executor = new Executor();

            container.Register<IExecutor>(c =>
                executor);
            container.Register<IFiber>(c =>
            {
                var fiber = new PoolFiber(executor);
                fiber.Start();

                return fiber;
            }).ReusedWithin(ReuseScope.None);

            container.Register<IWebClient>(c =>
                new WebClient()).
                ReusedWithin(ReuseScope.None);
            container.Register<ILauncher>(c =>
                new Launcher());
            container.Register<IBurritoDayModel>(c =>
            {
                var fiber = c.Resolve<IFiber>();
                var client = c.Resolve<IWebClient>();

                return new BurritoDayModel(fiber, client);
            });

            container.Register<BurritoDayPresenter, IBurritoDayView>((c, view) =>
            {
                var model = c.Resolve<IBurritoDayModel>();
                var fiber = c.ResolveNamed<IFiber>("GuiFiber");

                return new BurritoDayPresenter(model, view, fiber);
            });

            container.Register<UrlActivationPresenter, IUrlActivationView>((c, view) =>
            {
                var model = c.Resolve<ILauncher>();

                return new UrlActivationPresenter(model, view);
            });
        }
Exemple #12
0
        public void MyOwnBatchingTestToMakeSureIUnderstandRetlangsBatchingStuff()
        {
            using (var fiber = new PoolFiber())
            {
                fiber.Start();
                var reset = new ManualResetEvent(false);

                var channel = new Channel<BatchTestMessage>();
                channel.SubscribeToBatch(fiber, BatchSubscriberMethod, 1);

                for(var i=0; i < 10; i++) // <---  increase to 1000000 or so to see it make 3 or 4 calls to BatchSubscriber!
                {
                    channel.Publish(new BatchTestMessage("loop " + i));
                }

                //Assert.IsTrue(reset.WaitOne(10000, false));

                reset.WaitOne(1000, false);
            }
        }
Exemple #13
0
 public void SingleConsumerWithException()
 {
     var exec = new StubExecutor();
     var one = new PoolFiber(new DefaultThreadPool(), exec);
     one.Start();
     var reset = new AutoResetEvent(false);
     using (one)
     {
         var channel = new QueueChannel<int>();
         Action<int> onMessage = delegate(int num)
         {
             if (num == 0)
             {
                 throw new Exception();
             }
             reset.Set();
         };
         channel.Subscribe(one, onMessage);
         channel.Publish(0);
         channel.Publish(1);
         Assert.IsTrue(reset.WaitOne(10000, false));
         Assert.AreEqual(1, exec.failed.Count);
     }
 }
        public void InOrderExecution()
        {
            PoolFiber fiber = new PoolFiber(new DefaultThreadPool(), new BatchExecutor());
            fiber.Start();

            int count = 0;
            AutoResetEvent reset = new AutoResetEvent(false);
            List<int> result = new List<int>();
            Command command = delegate
                                  {
                                      result.Add(count++);
                                      if (count == 100)
                                      {
                                          reset.Set();
                                      }
                                  };
            for (int i = 0; i < 100; i++)
            {
                fiber.Enqueue(command);
            }

            Assert.IsTrue(reset.WaitOne(10000, false));
            Assert.AreEqual(100, count);
        }
 public void PointToPointPerfTest()
 {
     Channel<int> channel = new Channel<int>();
     IFiber bus = new PoolFiber();
     bus.Start();
     int max = 5000000;
     AutoResetEvent reset = new AutoResetEvent(false);
     Action<int> onMsg = delegate(int count)
                             {
                                 if (count == max)
                                 {
                                     reset.Set();
                                 }
                             };
     channel.Subscribe(bus, onMsg);
     using (new PerfTimer(max))
     {
         for (int i = 0; i <= max; i++)
         {
             channel.Publish(i);
         }
         Assert.IsTrue(reset.WaitOne(30000, false));
     }
 }
        public void TestLongRunningConceptWithRetlang()
        {
            var longrunningContext = new PoolFiber();
            longrunningContext.Start();
            var regularContext = new PoolFiber();
            regularContext.Start();

            var startFollowingFileChannel = new Channel<StartFollowingFileMessage>();
            var stopFollowingFileChannel = new Channel<StopFollowingFileMessage>();
            var followerStartedChannel = new Channel<FollowerStartedMessage>();
            var followerStoppedChannel = new Channel<FollowerStoppedMessage>();
            var longrunningTest = new LongRunningTest(followerStartedChannel, followerStoppedChannel);

            //this one runs in it's own context cause it won't end until a flag is set.
            startFollowingFileChannel.Subscribe(longrunningContext, longrunningTest.Handle);
            //this is registered on another context because if it's on the longrunning one, it won't ever run.
            stopFollowingFileChannel.Subscribe(regularContext, longrunningTest.Handle);

            var reset = new AutoResetEvent(false);
            Guid identifier;

            followerStartedChannel.Subscribe(regularContext, message =>
                                                                 {
                                                                     Console.WriteLine("Follower Started" + message.Identifier);
                                                                     identifier = message.Identifier;
                                                                     stopFollowingFileChannel.Publish(new StopFollowingFileMessage(identifier));
                                                                 });
            followerStoppedChannel.Subscribe(regularContext, message =>
                                                                 {
                                                                     Console.WriteLine("Follower stopped" + message.Identifier);
                                                                     reset.Set();
                                                                 });

            startFollowingFileChannel.Publish(new StartFollowingFileMessage(@"ExampleFiles/ExampleLogFiles/LotroLog.txt"));

            Assert.IsTrue(reset.WaitOne(5000, false));
        }
        public void TestFileFollowerwithEventAggregator()
        {
            var broker = new EventAggregator(new ChannelManager(), new ContextFactory());
            var publisher = new EventPublisher(broker);
            var filefollower = new FileFollower(publisher);
            Guid identifier = Guid.Empty;

            var longrunningcontext = new PoolFiber();
            longrunningcontext.Start();

            //register with longrunning context.
            broker.AddListener<StartFollowingFileMessage>(longrunningcontext, filefollower.Handle);

            //register with normal context.
            broker.AddListener<StopFollowingFileMessage>(filefollower.Handle);

            var reset = new AutoResetEvent(false);
            broker.AddListener<FollowerStartedMessage>(message =>
                                                           {
                                                               Console.WriteLine("Follower Started" + message.Identifier);
                                                               identifier = message.Identifier;
                                                               broker.SendMessage(new StopFollowingFileMessage(identifier));
                                                           });
            broker.AddListener<FollowerStoppedMessage>(message =>
                                                           {
                                                               if (message.Identifier == identifier)
                                                               {
                                                                   Console.WriteLine("Follower stopped" + message.Identifier);
                                                                   reset.Set();
                                                               }
                                                           });

            publisher.SendMessage(new StartFollowingFileMessage(@"ExampleFiles/ExampleLogFiles/LotroLog.txt"));
            Assert.IsTrue(reset.WaitOne(5000, false));
        }
Exemple #18
0
 protected override void SetUp()
 {
     _fiber = new PoolFiber();
     _fiber.Start();
 }
Exemple #19
0
        public void UnsubscriptionShouldRemoveSubscriber()
        {
            var fiber = new PoolFiber();
            fiber.Start();
            var channel = new Channel<int>();

            Assert.AreEqual(0, fiber.SubscriptionsCount);
            Assert.AreEqual(0, channel.SubscribersCount);
            var unsubscriber = channel.Subscribe(fiber, x => { });

            Assert.AreEqual(1, fiber.SubscriptionsCount);
            Assert.AreEqual(1, channel.SubscribersCount);
            unsubscriber.Dispose();

            Assert.AreEqual(0, fiber.SubscriptionsCount);
            Assert.AreEqual(0, channel.SubscribersCount);
        }
Exemple #20
0
        public void ShouldIncreasePoolFiberSubscriberCountByOne()
        {
            var fiber = new PoolFiber();
            fiber.Start();
            var channel = new Channel<int>();

            Assert.AreEqual(0, fiber.SubscriptionsCount);
            Assert.AreEqual(0, channel.SubscribersCount);
            channel.Subscribe(fiber, x => { });

            Assert.AreEqual(1, fiber.SubscriptionsCount);
            Assert.AreEqual(1, channel.SubscribersCount);
            fiber.Dispose();

            Assert.AreEqual(0, fiber.SubscriptionsCount);
            Assert.AreEqual(0, channel.SubscribersCount);
        }
Exemple #21
0
        public void RequestReply()
        {
            using (var fiber = new PoolFiber())
            {
                fiber.Start();
                var channel = new RequestReplyChannel<string, string>();
                channel.Subscribe(fiber, req => req.SendReply("bye"));
                var reply = channel.SendRequest("hello");

                string result;
                Assert.IsTrue(reply.Receive(10000, out result));
                Assert.AreEqual("bye", result);
            }
        }
Exemple #22
0
        public void PubSubWithPool()
        {
            //PoolFiber uses the .NET thread pool by default
            using (var fiber = new PoolFiber())
            {
                fiber.Start();
                var channel = new Channel<string>();

                var reset = new AutoResetEvent(false);
                channel.Subscribe(fiber, delegate { reset.Set(); });
                channel.Publish("hello");

                Assert.IsTrue(reset.WaitOne(5000, false));
            }
        }