Exemplo n.º 1
0
        public AsyncReqReplyServiceSpecs()
        {
            Reply   = string.Empty;
            Replied = new ManualResetEvent(false);
            Console.WriteLine("Start client fiber");
            Func <byte[], string> unmarshaller = x => Encoding.Unicode.GetString(x);
            Func <string, byte[]> marshaller   = x => Encoding.Unicode.GetBytes(x);

            ServerFiber   = PoolFiber.StartNew();
            ServerContext = NetMQContext.Create();
            Service       = new AsyncRequestHandlerSocket <string, string>(ServerContext,
                                                                           "tcp://localhost:9997",
                                                                           unmarshaller,
                                                                           marshaller);
            Service.SetRequestHandler(ServerFiber, request => request.Reply(request.Request.ToUpper()));
            Console.WriteLine("Start service");
            ClientFiber = StubFiber.StartNew();

            ClientContext = NetMQContext.Create();
            Client        = new AsyncRequestSocket <string, string>(ClientContext,
                                                                    "tcp://localhost:9997",
                                                                    marshaller,
                                                                    unmarshaller);
            Console.WriteLine("Start client");
        }
Exemplo n.º 2
0
 public void TestTwoFibers()
 {
     FiberTester.TestPubSubWExtraFiber(ThreadFiber.StartNew(), ThreadFiber.StartNew());
     FiberTester.TestPubSubWExtraFiber(PoolFiber.StartNew(), ThreadFiber.StartNew());
     FiberTester.TestPubSubWExtraFiber(PoolFiber.StartNew(), PoolFiber.StartNew());
     FiberTester.TestPubSubWExtraFiber(PoolFiber.StartNew(), StubFiber.StartNew());
 }
Exemplo n.º 3
0
 public void TestBatching()
 {
     FiberTester.TestBatching(ThreadFiber.StartNew());
     FiberTester.TestBatching(PoolFiber.StartNew());
     FiberTester.TestBatching(StubFiber.StartNew());
     FiberTester.TestBatchingWithKey(ThreadFiber.StartNew());
     FiberTester.TestBatchingWithKey(PoolFiber.StartNew());
     FiberTester.TestBatchingWithKey(StubFiber.StartNew());
 }
Exemplo n.º 4
0
 public RequestSocket(NetMQContext context,
                      string address,
                      Func <TRequest, byte[]> requestMarshaller,
                      Func <byte[], TReply> replyUnmarshaller)
 {
     _requestMarshaller = requestMarshaller;
     _replyUnmarshaller = replyUnmarshaller;
     _socket            = context.CreateSocket(ZmqSocketType.Req);
     _socket.Connect(address);
     _internalChannel.SetRequestHandler(StubFiber.StartNew(), InternalSendRequest);
 }
Exemplo n.º 5
0
        public void Cancel()
        {
            int    executionCount = 0;
            Action action         = () => executionCount++;
            var    timer          = new TimerAction(StubFiber.StartNew(), action, TimeSpan.FromMilliseconds(2));

            Thread.Sleep(100);
            Assert.AreEqual(1, executionCount);
            timer.Dispose();
            Thread.Sleep(150);
            Assert.AreEqual(1, executionCount);
        }
        public void CanSubscribeToEvent()
        {
            bool triggered = false;
            var  stub      = StubFiber.StartNew();
            var  evt       = new EventTester();
            var  dispose   = stub.SubscribeToEvent <object>(evt, "Event", x => triggered = true);

            Assert.IsTrue(evt.IsAttached);
            evt.Invoke();
            Assert.IsTrue(triggered);
            dispose.Dispose();
            Assert.IsFalse(evt.IsAttached);
        }
Exemplo n.º 7
0
        public void ChannelSubscription()
        {
            var fiber   = StubFiber.StartNew();
            var channel = new Channel <int>();

            fiber.Subscribe(channel, i => { });
            Assert.AreEqual(true, channel.HasSubscriptions());
            fiber.Dispose();
            Assert.AreEqual(false, channel.HasSubscriptions());
            var sub = fiber.Subscribe(channel, i => { });

            Assert.AreEqual(true, channel.HasSubscriptions());
            sub.Dispose();
            Assert.AreEqual(false, channel.HasSubscriptions());
        }
Exemplo n.º 8
0
        public void PipelineTest1()
        {
            string value    = null;
            int    tvalue   = 0;
            var    stage1   = new Stage <double, int>(d => (int)d);
            var    stage2   = new Tee <int>(i => tvalue = i);
            var    stage3   = new Stage <int, string>(i => (i * 10).ToString());
            var    stub     = StubFiber.StartNew();
            var    pipeline = stage1.To(stage2).To(stage3);

            pipeline.Subscribe(stub, s => value = s);
            stage1.Publish(1.3);
            Thread.Sleep(10);
            Assert.AreEqual("10", value);
            Assert.AreEqual(1, tvalue);
        }
Exemplo n.º 9
0
        public void CallbackFromIntervalTimerWithCancel()
        {
            int    executionCount = 0;
            Action action         = () => executionCount++;

            using (IFiber stubFiber = StubFiber.StartNew())
            {
                var timer = new TimerAction(stubFiber,
                                            action,
                                            TimeSpan.FromMilliseconds(2),
                                            TimeSpan.FromMilliseconds(150));
                Thread.Sleep(100);
                Assert.AreEqual(1, executionCount);
                timer.Dispose();
                Thread.Sleep(150);
                Assert.AreEqual(1, executionCount);
            }
        }
Exemplo n.º 10
0
        static void Main(string[] args)
        {
            //This example uses mutable messages, which require care.  Normally you want to
            //default to using immutable messages for each stage.
            //Its possible to fan out certain stages to deal with the issue with pipelines only being
            //as fast as the slowest stage.  You can use a Queue channel say between stage1 and stage2 and then
            //instantiate more than one stage2 processor.
            var channels = new Channels();

            using (var stage1 = new ConcurrentComponent <Payload, Payload>(new Stage1(new SomeService()), channels.Input, channels.Stage1To2, channels.Errors))
                using (var stage2 = new ConcurrentComponent <Payload, Payload>(new Stage2(new SomeDataAccess()), channels.Stage1To2, channels.Output, channels.Errors))
                    using (var stub = StubFiber.StartNew())
                    {
                        channels.Output.Subscribe(stub, payload => Console.WriteLine("Got output"));
                        channels.Stage1To2.Subscribe(stub, payload => Console.WriteLine("Monitoring Stage1to2 channel saw a message"));
                        using (var timer = new ThreadSafeTimer())
                        {
                            timer.Schedule(() => channels.Input.Publish(new Payload()), TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(2));
                            Console.WriteLine("Hit any key to stop");
                            Console.ReadKey();
                        }
                    }
        }
Exemplo n.º 11
0
 public void TestReqReply()
 {
     FiberTester.TestReqReply1(ThreadFiber.StartNew());
     FiberTester.TestReqReply1(PoolFiber.StartNew());
     FiberTester.TestReqReply1(StubFiber.StartNew());
 }
Exemplo n.º 12
0
 public void TestPubSubWithFilter()
 {
     FiberTester.TestPubSubWithFilter(ThreadFiber.StartNew());
     FiberTester.TestPubSubWithFilter(PoolFiber.StartNew());
     FiberTester.TestPubSubWithFilter(StubFiber.StartNew());
 }
Exemplo n.º 13
0
 public void TestPubSubSimple()
 {
     FiberTester.TestPubSubSimple(ThreadFiber.StartNew());
     FiberTester.TestPubSubSimple(PoolFiber.StartNew());
     FiberTester.TestPubSubSimple(StubFiber.StartNew());
 }
Exemplo n.º 14
0
 public void InOrderExecution()
 {
     FiberTester.InOrderExecution(ThreadFiber.StartNew());
     FiberTester.InOrderExecution(PoolFiber.StartNew());
     FiberTester.InOrderExecution(StubFiber.StartNew());
 }