Example #1
0
        public void TestNetworkServiceOneWayCommunication()
        {
            int networkServicePort1 = NetworkUtils.GenerateRandomPort(6000, 7000);
            int networkServicePort2 = NetworkUtils.GenerateRandomPort(7001, 8000);

            BlockingCollection <string> queue = new BlockingCollection <string>();

            using (var nameServer = NameServerTests.BuildNameServer())
            {
                IPEndPoint endpoint       = nameServer.LocalEndpoint;
                int        nameServerPort = endpoint.Port;
                string     nameServerAddr = endpoint.Address.ToString();
                using (INetworkService <string> networkService1 = BuildNetworkService(networkServicePort1, nameServerPort, nameServerAddr, null))
                    using (INetworkService <string> networkService2 = BuildNetworkService(networkServicePort2, nameServerPort, nameServerAddr, new MessageHandler(queue)))
                    {
                        IIdentifier id1 = new StringIdentifier("service1");
                        IIdentifier id2 = new StringIdentifier("service2");
                        networkService1.Register(id1);
                        networkService2.Register(id2);

                        using (IConnection <string> connection = networkService1.NewConnection(id2))
                        {
                            connection.Open();
                            connection.Write("abc");
                            connection.Write("def");
                            connection.Write("ghi");

                            Assert.Equal("abc", queue.Take());
                            Assert.Equal("def", queue.Take());
                            Assert.Equal("ghi", queue.Take());
                        }
                    }
            }
        }
        public void TestBroadcastOperatorWithDefaultCodec()
        {
            INameServer nameServer = NameServerTests.BuildNameServer();

            string groupName    = "group1";
            string operatorName = "broadcast";
            string masterTaskId = "task0";
            string driverId     = "Driver Id";
            int    numTasks     = 10;
            int    value        = 1337;
            int    fanOut       = 3;

            IGroupCommDriver groupCommDriver = GetInstanceOfGroupCommDriver(driverId, masterTaskId, groupName, fanOut, numTasks);

            var commGroup = groupCommDriver.DefaultGroup
                            .AddBroadcast(operatorName, masterTaskId)
                            .Build();

            List <ICommunicationGroupClient> commGroups = CommGroupClients(groupName, numTasks, groupCommDriver, commGroup, GetDefaultCodecConfig());

            IBroadcastSender <int>   sender    = commGroups[0].GetBroadcastSender <int>(operatorName);
            IBroadcastReceiver <int> receiver1 = commGroups[1].GetBroadcastReceiver <int>(operatorName);
            IBroadcastReceiver <int> receiver2 = commGroups[2].GetBroadcastReceiver <int>(operatorName);

            Assert.NotNull(sender);
            Assert.NotNull(receiver1);
            Assert.NotNull(receiver2);

            sender.Send(value);
            Assert.Equal(value, receiver1.Receive());
            Assert.Equal(value, receiver2.Receive());
        }
        public void TestStreamingNetworkServiceTwoWayCommunication()
        {
            int networkServicePort1 = NetworkUtils.GenerateRandomPort(6000, 7000);
            int networkServicePort2 = NetworkUtils.GenerateRandomPort(7001, 8000);

            BlockingCollection <string> queue1;
            BlockingCollection <string> queue2;

            using (var nameServer = NameServerTests.BuildNameServer())
            {
                IPEndPoint endpoint       = nameServer.LocalEndpoint;
                int        nameServerPort = endpoint.Port;
                string     nameServerAddr = endpoint.Address.ToString();

                var handlerConf =
                    TangFactory.GetTang()
                    .NewConfigurationBuilder()
                    .BindImplementation(GenericType <IObserver <NsMessage <string> > > .Class,
                                        GenericType <MessageHandler> .Class)
                    .Build();

                var networkServiceInjection1 = BuildNetworkService(networkServicePort1, nameServerPort, nameServerAddr,
                                                                   handlerConf);

                var networkServiceInjection2 = BuildNetworkService(networkServicePort2, nameServerPort, nameServerAddr,
                                                                   handlerConf);

                using (INetworkService <string> networkService1 = networkServiceInjection1.GetInstance <StreamingNetworkService <string> >())
                    using (INetworkService <string> networkService2 = networkServiceInjection2.GetInstance <StreamingNetworkService <string> >())
                    {
                        queue1 = networkServiceInjection1.GetInstance <MessageHandler>().Queue;
                        queue2 = networkServiceInjection2.GetInstance <MessageHandler>().Queue;

                        IIdentifier id1 = new StringIdentifier("service1");
                        IIdentifier id2 = new StringIdentifier("service2");
                        networkService1.Register(id1);
                        networkService2.Register(id2);

                        using (IConnection <string> connection1 = networkService1.NewConnection(id2))
                            using (IConnection <string> connection2 = networkService2.NewConnection(id1))
                            {
                                connection1.Open();
                                connection1.Write("abc");
                                connection1.Write("def");
                                connection1.Write("ghi");

                                connection2.Open();
                                connection2.Write("jkl");
                                connection2.Write("nop");

                                Assert.Equal("abc", queue2.Take());
                                Assert.Equal("def", queue2.Take());
                                Assert.Equal("ghi", queue2.Take());

                                Assert.Equal("jkl", queue1.Take());
                                Assert.Equal("nop", queue1.Take());
                            }
                    }
            }
        }
        public void TestSender()
        {
            using (var nameServer = NameServerTests.BuildNameServer())
            {
                IPEndPoint endpoint = nameServer.LocalEndpoint;

                BlockingCollection <GeneralGroupCommunicationMessage> messages1 =
                    new BlockingCollection <GeneralGroupCommunicationMessage>();
                BlockingCollection <GeneralGroupCommunicationMessage> messages2 =
                    new BlockingCollection <GeneralGroupCommunicationMessage>();

                var handler1 =
                    Observer.Create <NsMessage <GeneralGroupCommunicationMessage> >(msg => messages1.Add(msg.Data.First()));
                var handler2 =
                    Observer.Create <NsMessage <GeneralGroupCommunicationMessage> >(msg => messages2.Add(msg.Data.First()));

                var networkServiceInjector1 = BuildNetworkServiceInjector(endpoint, handler1);
                var networkServiceInjector2 = BuildNetworkServiceInjector(endpoint, handler2);

                var networkService1 =
                    networkServiceInjector1.GetInstance <StreamingNetworkService <GeneralGroupCommunicationMessage> >();
                var networkService2 =
                    networkServiceInjector2.GetInstance <StreamingNetworkService <GeneralGroupCommunicationMessage> >();
                networkService1.Register(new StringIdentifier("id1"));
                networkService2.Register(new StringIdentifier("id2"));

                Sender sender1 = networkServiceInjector1.GetInstance <Sender>();
                Sender sender2 = networkServiceInjector2.GetInstance <Sender>();

                sender1.Send(CreateGcmStringType("abc", "id1", "id2"));
                sender1.Send(CreateGcmStringType("def", "id1", "id2"));
                sender2.Send(CreateGcmStringType("ghi", "id2", "id1"));

                string msg1 = (messages2.Take() as GroupCommunicationMessage <string>).Data[0];
                string msg2 = (messages2.Take() as GroupCommunicationMessage <string>).Data[0];

                Assert.Equal("abc", msg1);
                Assert.Equal("def", msg2);

                string msg3 = (messages1.Take() as GroupCommunicationMessage <string>).Data[0];
                Assert.Equal("ghi", msg3);
            }
        }