Пример #1
0
        public void SendMessageReceiveResponse()
        {
            IMessagingSystemFactory aServiceMessaging = new TcpMessagingSystemFactory();
            IMessagingSystemFactory aLocalMessaging   = new SynchronousMessagingSystemFactory();

            // Create services.
            TService aService1 = new TService(aServiceMessaging, "tcp://127.0.0.1:8097/");
            TService aService2 = new TService(aServiceMessaging, "tcp://127.0.0.1:8098/");

            // Create the backup router.
            IBackupConnectionRouterFactory aBackupRouterFactory = new BackupConnectionRouterFactory(aServiceMessaging);
            IBackupConnectionRouter        aBackupRouter        = aBackupRouterFactory.CreateBackupConnectionRouter();
            List <RedirectEventArgs>       aRedirections        = new List <RedirectEventArgs>();
            AutoResetEvent aRedirectionEvent = new AutoResetEvent(false);

            aBackupRouter.ConnectionRedirected += (x, y) =>
            {
                lock (aRedirections)
                {
                    aRedirections.Add(y);
                    aRedirectionEvent.Set();
                }
            };
            aBackupRouter.AddReceivers(new string[] { "tcp://127.0.0.1:8097/", "tcp://127.0.0.1:8098/" });
            Assert.AreEqual(2, aBackupRouter.AvailableReceivers.Count());

            IDuplexInputChannel aBackupRouterInputChannel = aLocalMessaging.CreateDuplexInputChannel("BackupRouter");

            aBackupRouter.AttachDuplexInputChannel(aBackupRouterInputChannel);

            // Create clients connected to the backup router.
            TClient aClient1 = new TClient(aLocalMessaging, "BackupRouter");
            TClient aClient2 = new TClient(aLocalMessaging, "BackupRouter");

            try
            {
                // Start both services.
                aService1.myInputChannel.StartListening();
                aService2.myInputChannel.StartListening();


                // Connect client 1.
                aClient1.myOutputChannel.OpenConnection();
                Thread.Sleep(300);
                Assert.AreEqual(1, aService1.myConnectedClients.Count);

                // Disconnect client 1.
                aClient1.myOutputChannel.CloseConnection();
                Thread.Sleep(300);
                Assert.AreEqual(0, aService1.myConnectedClients.Count);

                // Connect client 1 and 2.
                EneterTrace.Info("Client1 opens connection.");
                aClient1.myOutputChannel.OpenConnection();
                EneterTrace.Info("Client2 opens connection.");
                aClient2.myOutputChannel.OpenConnection();
                Thread.Sleep(300);
                Assert.AreEqual(2, aService1.myConnectedClients.Count);

                // Stop service 1.
                aService1.myInputChannel.StopListening();
                aService1.myConnectedClients.Clear();
                aRedirectionEvent.WaitOne();
                aRedirectionEvent.WaitOne();
                // Give some time until the service has connections.
                Thread.Sleep(500);
                Assert.AreEqual(2, aService2.myConnectedClients.Count);

                // Start service 1 again and stop Service 2.
                aService1.myInputChannel.StartListening();
                aService2.myInputChannel.StopListening();
                aService2.myConnectedClients.Clear();
                aRedirectionEvent.WaitOne();
                aRedirectionEvent.WaitOne();
                // Give some time until the service has connections.
                Thread.Sleep(300);
                Assert.AreEqual(2, aService1.myConnectedClients.Count);

                aService2.myInputChannel.StartListening();

                // Send the request message.
                aClient1.myOutputChannel.SendMessage("Hello from 1");
                aClient1.myResponseMessageReceived.WaitOne();
                aClient2.myOutputChannel.SendMessage("Hello from 2");
                aClient2.myResponseMessageReceived.WaitOne();
                Assert.AreEqual(2, aService1.myReceivedMessages.Count);
                Assert.AreEqual(0, aService2.myReceivedMessages.Count);
                Assert.AreEqual("Hello from 1", aService1.myReceivedMessages[0]);
                Assert.AreEqual("Hello from 2", aService1.myReceivedMessages[1]);
                Assert.AreEqual(1, aClient1.myReceivedResponses.Count);
                Assert.AreEqual(1, aClient2.myReceivedResponses.Count);
                Assert.AreEqual("Response for Hello from 1", aClient1.myReceivedResponses[0]);
                Assert.AreEqual("Response for Hello from 2", aClient2.myReceivedResponses[0]);
            }
            finally
            {
                aBackupRouter.RemoveAllReceivers();
                aClient1.Dispose();
                aClient2.Dispose();
                aService1.Dispose();
                aService2.Dispose();
            }
        }
Пример #2
0
        public void CalculatePi()
        {
            //EneterTrace.DetailLevel = EneterTrace.EDetailLevel.Debug;
            //EneterTrace.TraceLog = new StreamWriter("d:/tracefile.txt");

            IMessagingSystemFactory aThreadMessaging = new ThreadMessagingSystemFactory();

            List <CalculatorService> aServices = new List <CalculatorService>();

            ILoadBalancer aDistributor = null;
            IDuplexTypedMessageSender <double, Interval> aSender = null;

            // Create 50 calculating services.
            try
            {
                for (int i = 0; i < 50; ++i)
                {
                    aServices.Add(new CalculatorService("a" + i.ToString(), aThreadMessaging));
                }

                // Create Distributor
                ILoadBalancerFactory aDistributorFactory = new RoundRobinBalancerFactory(aThreadMessaging);
                aDistributor = aDistributorFactory.CreateLoadBalancer();

                // Attach available services to the distributor.
                for (int i = 0; i < aServices.Count; ++i)
                {
                    aDistributor.AddDuplexOutputChannel("a" + i.ToString());
                }

                // Attach input channel to the distributor.
                IDuplexInputChannel anInputChannel = aThreadMessaging.CreateDuplexInputChannel("DistributorAddress");
                aDistributor.AttachDuplexInputChannel(anInputChannel);


                // Create client that needs to calculate PI.
                IDuplexTypedMessagesFactory aTypedMessagesFactory = new DuplexTypedMessagesFactory();
                aSender = aTypedMessagesFactory.CreateDuplexTypedMessageSender <double, Interval>();

                AutoResetEvent aCalculationCompletedEvent = new AutoResetEvent(false);
                int            aCount = 0;
                double         aPi    = 0.0;
                aSender.ResponseReceived += (x, y) =>
                {
                    ++aCount;
                    EneterTrace.Debug("Completed interval: " + aCount.ToString());

                    aPi += y.ResponseMessage;

                    if (aCount == 400)
                    {
                        aCalculationCompletedEvent.Set();
                    }
                };

                IDuplexOutputChannel anOutputChannel = aThreadMessaging.CreateDuplexOutputChannel("DistributorAddress");
                aSender.AttachDuplexOutputChannel(anOutputChannel);

                // Sender sends several parallel requests to calculate specified intervals.
                // 2 / 0.005 = 400 intervals.
                for (double i = -1.0; i <= 1.0; i += 0.005)
                {
                    Interval anInterval = new Interval(i, i + 0.005);
                    aSender.SendRequestMessage(anInterval);
                }

                // Wait until all requests are calculated.
                EneterTrace.Debug("Test waits until completion.");
                aCalculationCompletedEvent.WaitOne();

                EneterTrace.Info("Calculated PI = " + aPi.ToString());
            }
            catch (Exception err)
            {
                EneterTrace.Error("Test failed", err);
                throw;
            }
            finally
            {
                aSender.DetachDuplexOutputChannel();
                aDistributor.DetachDuplexInputChannel();
                aServices.ForEach(x => x.Dispose());
            }
        }
Пример #3
0
        public void A16_RequestResponse_100_ConstantlyInterrupted()
        {
            IDuplexOutputChannel aDuplexOutputChannel           = MessagingSystem.CreateDuplexOutputChannel(ChannelId);
            IDuplexInputChannel  aDuplexInputChannel            = MessagingSystem.CreateDuplexInputChannel(ChannelId);
            IDuplexInputChannel  anUnderlyingDuplexInputChannel = aDuplexInputChannel.GetField <IDuplexInputChannel>("myInputChannel");

            Assert.NotNull(anUnderlyingDuplexInputChannel);

            AutoResetEvent anAllMessagesProcessedEvent = new AutoResetEvent(false);

            // Received messages.
            List <int> aReceivedMessages = new List <int>();

            aDuplexInputChannel.MessageReceived += (x, y) =>
            {
                lock (aReceivedMessages)
                {
                    string aReceivedMessage = y.Message as string;

                    EneterTrace.Info("Received message: " + aReceivedMessage);

                    int k = int.Parse(aReceivedMessage);
                    aReceivedMessages.Add(k);
                    k += 1000;

                    EneterTrace.Info("Sent response message: " + k.ToString());
                    aDuplexInputChannel.SendResponseMessage(y.ResponseReceiverId, k.ToString());
                }
            };

            aDuplexOutputChannel.ConnectionClosed += (x, y) =>
            {
                EneterTrace.Info("ConnectionClosed invoked in duplex output channel");

                // The buffered duplex output channel exceeded the max offline time.
                anAllMessagesProcessedEvent.Set();
            };

            // Received response messages.
            List <int> aReceivedResponseMessages = new List <int>();

            aDuplexOutputChannel.ResponseMessageReceived += (x, y) =>
            {
                lock (aReceivedResponseMessages)
                {
                    string aReceivedMessage = y.Message as string;

                    EneterTrace.Info("Received response message: " + aReceivedMessage);

                    int k = int.Parse(aReceivedMessage);
                    aReceivedResponseMessages.Add(k);

                    if (aReceivedResponseMessages.Count == 100)
                    {
                        anAllMessagesProcessedEvent.Set();
                    }
                }
            };


            try
            {
                bool aTestFinishedFlag = false;

                aDuplexInputChannel.StartListening();
                aDuplexOutputChannel.OpenConnection();


                Thread anInteruptingThread = new Thread(() =>
                {
                    for (int i = 0; i < 100 && !aTestFinishedFlag; ++i)
                    {
                        anUnderlyingDuplexInputChannel.DisconnectResponseReceiver(aDuplexOutputChannel.ResponseReceiverId);
                        Thread.Sleep(ConnectionInterruptionFrequency);
                    }
                });

                // Start constant disconnecting.
                anInteruptingThread.Start();

                for (int i = 0; i < 100; ++i)
                {
                    aDuplexOutputChannel.SendMessage(i.ToString());
                }

                // Wait until all messages are processed.
                //anAllMessagesProcessedEvent.WaitOne();
                Assert.IsTrue(anAllMessagesProcessedEvent.WaitOne(20000), "The timeout occured.");

                aTestFinishedFlag = true;
                anInteruptingThread.Join();
            }
            finally
            {
                aDuplexOutputChannel.CloseConnection();
                aDuplexInputChannel.StopListening();
            }

            aReceivedMessages.Sort();
            Assert.AreEqual(100, aReceivedMessages.Count);
            for (int i = 0; i < 100; ++i)
            {
                Assert.AreEqual(i, aReceivedMessages[i]);
            }

            aReceivedResponseMessages.Sort();
            Assert.AreEqual(100, aReceivedResponseMessages.Count);
            for (int i = 0; i < 100; ++i)
            {
                Assert.AreEqual(i + 1000, aReceivedResponseMessages[i]);
            }
        }