Пример #1
0
        public async void TestGracefulHalt()
        {
            Peer           sender = null;
            Peer           recv1  = null;
            ChannelCreator cc     = null;

            try
            {
                sender = new PeerBuilder(new Number160("0x9876"))
                         .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(2424, 2424))
                         .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask())
                         .SetP2PId(55)
                         .SetPorts(2424)
                         .Start();
                recv1 = new PeerBuilder(new Number160("0x1234"))
                        .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(7777, 7777))
                        .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask())
                        .SetP2PId(55)
                        .SetPorts(7777)
                        .Start();

                // bootstrap first
                await sender.Bootstrap().SetPeerAddress(recv1.PeerAddress).StartAsync();

                Assert.IsTrue(sender.PeerBean.PeerMap.All.Count == 1);
                Assert.IsTrue(recv1.PeerBean.PeerMap.AllOverflow.Count == 1);

                // graceful shutdown
                cc = await recv1.ConnectionBean.Reservation.CreateAsync(1, 0);

                var shutdownBuilder = new ShutdownBuilder(sender);

                await sender.QuitRpc.QuitAsync(recv1.PeerAddress, shutdownBuilder, cc);

                await sender.ShutdownAsync();

                sender = null; // ignore finally-block shutdown

                Assert.IsTrue(recv1.PeerBean.PeerMap.All.Count == 0);
            }
            finally
            {
                if (cc != null)
                {
                    cc.ShutdownAsync().Wait();
                }
                if (sender != null)
                {
                    sender.ShutdownAsync().Wait();
                }
                if (recv1 != null)
                {
                    recv1.ShutdownAsync().Wait();
                }
            }
        }
Пример #2
0
        public async void TestOrder()
        {
            Peer           sender = null;
            Peer           recv1  = null;
            ChannelCreator cc     = null;

            try
            {
                sender = new PeerBuilder(new Number160("0x50"))
                         .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask())
                         .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(2525, 2525))
                         .SetP2PId(55)
                         .SetPorts(2525)
                         .Start();
                recv1 = new PeerBuilder(new Number160("0x20"))
                        .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask())
                        .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(9099, 9099))
                        .SetP2PId(55)
                        .SetPorts(8088)
                        .Start();
                recv1.RawDataReply(new TestOrderRawDataReply());

                for (int i = 0; i < 500; i++)
                {
                    cc = await sender.ConnectionBean.Reservation.CreateAsync(0, 1);

                    var sendDirectBuilder = new SendDirectBuilder(sender, (PeerAddress)null);
                    var buffer            = AlternativeCompositeByteBuf.CompBuffer().WriteInt(i);
                    sendDirectBuilder.SetBuffer(new Buffer(buffer));
                    sendDirectBuilder.SetIsStreaming();

                    var tr = sender.DirectDataRpc.SendAsync(recv1.PeerAddress, sendDirectBuilder, cc);
                    Core.Utils.Utils.AddReleaseListener(cc, tr);
                    tr.ContinueWith(t =>
                    {
                        int j = t.Result.Buffer(0).BackingBuffer.ReadInt();
                        Console.WriteLine("Received {0}.", j);
                    });
                }
            }
            finally
            {
                if (sender != null)
                {
                    sender.ShutdownAsync().Wait();
                }
                if (recv1 != null)
                {
                    recv1.ShutdownAsync().Wait();
                }
                if (cc != null)
                {
                    cc.ShutdownAsync().Wait();
                }
            }
        }
Пример #3
0
        public async void TestDirect1()
        {
            Peer           sender = null;
            Peer           recv1  = null;
            ChannelCreator cc     = null;

            try
            {
                sender = new PeerBuilder(new Number160("0x50"))
                         .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask())
                         .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(2525, 2525))
                         .SetP2PId(55)
                         .SetPorts(2525)
                         .Start();
                recv1 = new PeerBuilder(new Number160("0x20"))
                        .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask())
                        .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(9099, 9099))
                        .SetP2PId(55)
                        .SetPorts(8088)
                        .Start();
                recv1.RawDataReply(new TestRawDataReply());

                cc = await sender.ConnectionBean.Reservation.CreateAsync(0, 1);

                var sendDirectBuilder = new SendDirectBuilder(sender, (PeerAddress)null);
                sendDirectBuilder.SetIsStreaming();
                sendDirectBuilder.SetIdleTcpSeconds(Int32.MaxValue);
                var buffer = CreateTestBuffer();
                sendDirectBuilder.SetBuffer(buffer);

                var   tr1 = sender.DirectDataRpc.SendAsync(recv1.PeerAddress, sendDirectBuilder, cc);
                await tr1;
                Assert.IsTrue(!tr1.IsFaulted);

                var ret = tr1.Result.Buffer(0);
                Assert.AreEqual(buffer, ret);
            }
            finally
            {
                if (sender != null)
                {
                    sender.ShutdownAsync().Wait();
                }
                if (recv1 != null)
                {
                    recv1.ShutdownAsync().Wait();
                }
                if (cc != null)
                {
                    cc.ShutdownAsync().Wait();
                }
            }
        }
Пример #4
0
        public async void TestBootstrapFail()
        {
            var  rnd    = new Random(42);
            Peer master = null;
            Peer slave  = null;

            try
            {
                master = new PeerBuilder(new Number160(rnd))
                         .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask())
                         .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(4001, 4001))
                         .SetPorts(4001)
                         .Start();
                slave = new PeerBuilder(new Number160(rnd))
                        .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask())
                        .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(4002, 4002))
                        .SetPorts(4002)
                        .Start();

                // bootstrap to another address/port
                var taskBootstrap = master.Bootstrap()
                                    .SetInetAddress(IPAddress.Loopback)
                                    .SetPorts(3000)
                                    .StartAsync();
                try
                {
                    await taskBootstrap;
                    Assert.Fail("The bootstrapping task should fail.");
                }
                catch (Exception)
                {
                    Assert.IsTrue(taskBootstrap.IsFaulted);
                }

                // bootstrap to correct address
                taskBootstrap = master.Bootstrap()
                                .SetPeerAddress(slave.PeerAddress)
                                .StartAsync();
                await taskBootstrap;
                Assert.IsTrue(!taskBootstrap.IsFaulted);
            }
            finally
            {
                if (master != null)
                {
                    master.ShutdownAsync().Wait();
                }
                if (slave != null)
                {
                    slave.ShutdownAsync().Wait();
                }
            }
        }
Пример #5
0
        public async void TestNeighborFail()
        {
            Peer sender = null;
            Peer recv1  = null;

            try
            {
                sender = new PeerBuilder(new Number160("0x50"))
                         .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask())
                         .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(2424, 2424))
                         .SetP2PId(55)
                         .SetPorts(2424)
                         .Start();
                recv1 = new PeerBuilder(new Number160("0x20"))
                        .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask())
                        .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(8088, 8088))
                        .SetP2PId(55)
                        .SetPorts(2424)
                        .Start();

                var neighbors1 = new NeighborRpc(sender.PeerBean, sender.ConnectionBean);
                var neighbors2 = new NeighborRpc(recv1.PeerBean, recv1.ConnectionBean);
                var cc         = await recv1.ConnectionBean.Reservation.CreateAsync(1, 0);

                try
                {
                    var sv        = new SearchValues(new Number160("0x30"), null);
                    var infConfig = Utils2.CreateInfiniteConfiguration();
                    await
                    neighbors2.CloseNeighborsAsync(sender.PeerAddress, sv, Message.MessageType.Exception, cc,
                                                   infConfig);

                    Assert.Fail("ArgumentException should have been thrown.");
                }
                catch (ArgumentException)
                {
                    cc.ShutdownAsync().Wait();
                }
            }
            finally
            {
                if (sender != null)
                {
                    sender.ShutdownAsync().Wait();
                }
                if (recv1 != null)
                {
                    recv1.ShutdownAsync().Wait();
                }
            }
        }
Пример #6
0
        public async void TestNeighbor2()
        {
            Peer sender = null;
            Peer recv1  = null;

            try
            {
                sender = new PeerBuilder(new Number160("0x50"))
                         .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask())
                         .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(2424, 2424))
                         .SetP2PId(55)
                         .SetPorts(2424)
                         .Start();
                recv1 = new PeerBuilder(new Number160("0x20"))
                        .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask())
                        .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(8088, 8088))
                        .SetP2PId(55)
                        .SetPorts(2424)
                        .Start();

                var neighbors1 = new NeighborRpc(sender.PeerBean, sender.ConnectionBean);
                var neighbors2 = new NeighborRpc(recv1.PeerBean, recv1.ConnectionBean);
                var cc         = await recv1.ConnectionBean.Reservation.CreateAsync(1, 0);

                var sv        = new SearchValues(new Number160("0x30"), null);
                var infConfig = Utils2.CreateInfiniteConfiguration();
                var tr        = neighbors2.CloseNeighborsAsync(sender.PeerAddress, sv, Message.MessageType.Request2, cc,
                                                               infConfig);
                await tr;

                Assert.IsTrue(!tr.IsFaulted);

                var addresses = tr.Result.NeighborsSet(0);

                // I see noone, not even myself. My peer was added in the overflow map.
                Assert.AreEqual(0, addresses.Size);
                await cc.ShutdownAsync();
            }
            finally
            {
                if (sender != null)
                {
                    sender.ShutdownAsync().Wait();
                }
                if (recv1 != null)
                {
                    recv1.ShutdownAsync().Wait();
                }
            }
        }
Пример #7
0
        public async void TestPingTcp2()
        {
            Peer           sender = null;
            Peer           recv1  = null;
            ChannelCreator cc     = null;

            try
            {
                sender = new PeerBuilder(new Number160("0x9876"))
                         .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask())
                         .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(2424, 2424))
                         .SetP2PId(55)
                         .SetPorts(2424)
                         .Start();
                recv1 = new PeerBuilder(new Number160("0x1234"))
                        .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask())
                        .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(8088, 8088))
                        .SetP2PId(55)
                        .SetPorts(8088)
                        .Start();

                // TODO check release in cc for first ping
                cc = await recv1.ConnectionBean.Reservation.CreateAsync(0, 1);

                var   tr = sender.PingRpc.PingTcpAsync(recv1.PeerAddress, cc, new DefaultConnectionConfiguration());
                await tr;
                var   tr2 = recv1.PingRpc.PingTcpAsync(sender.PeerAddress, cc, new DefaultConnectionConfiguration());
                await tr2;

                Assert.IsTrue(!tr.IsFaulted);
                Assert.IsTrue(!tr2.IsFaulted);
            }
            finally
            {
                if (cc != null)
                {
                    cc.ShutdownAsync().Wait();
                }
                if (sender != null)
                {
                    sender.ShutdownAsync().Wait();
                }
                if (recv1 != null)
                {
                    recv1.ShutdownAsync().Wait();
                }
            }
        }
Пример #8
0
        public async void TestBootstrapDiscover()
        {
            var  rnd    = new Random(42);
            Peer master = null;
            Peer slave  = null;

            try
            {
                master = new PeerBuilder(new Number160(rnd))
                         .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask())
                         .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(4001, 4001))
                         .SetPorts(4001)
                         .Start();
                slave = new PeerBuilder(new Number160(rnd))
                        .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask())
                        .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(4002, 4002))
                        .SetPorts(4002)
                        .Start();

                var tcsDiscover = master.Discover().SetPeerAddress(slave.PeerAddress).Start();
                await tcsDiscover.Task;

                Assert.IsTrue(!tcsDiscover.Task.IsFaulted);
            }
            finally
            {
                if (master != null)
                {
                    master.ShutdownAsync().Wait();
                }
                if (slave != null)
                {
                    slave.ShutdownAsync().Wait();
                }
            }
        }
Пример #9
0
        public async void TestNeighborTcp()
        {
            Peer sender = null;
            Peer recv1  = null;

            try
            {
                sender = new PeerBuilder(new Number160("0x50"))
                         .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask())
                         .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(2424, 2424))
                         .SetP2PId(55)
                         .SetPorts(2424)
                         .Start();

                // add neighbors to the sender's peer map
                var addresses = Utils2.CreateDummyAddresses(300, PortTcp, PortUdp);
                for (int i = 0; i < addresses.Length; i++)
                {
                    sender.PeerBean.PeerMap.PeerFound(addresses[i], null, null);
                }

                // register neighbor RPC handlers
                var neighbors1 = new NeighborRpc(sender.PeerBean, sender.ConnectionBean); // TODO needed? registering?

                recv1 = new PeerBuilder(new Number160("0x20"))
                        .SetMaintenanceTask(Utils2.CreateInfiniteIntervalMaintenanceTask())
                        .SetChannelServerConfiguration(Utils2.CreateInfiniteTimeoutChannelServerConfiguration(8088, 8088))
                        .SetP2PId(55)
                        .SetPorts(8088)
                        .Start();
                var neighbors2 = new NeighborRpc(recv1.PeerBean, recv1.ConnectionBean);

                // ask sender for his neighbors
                var cc = await recv1.ConnectionBean.Reservation.CreateAsync(0, 1);

                var sv        = new SearchValues(new Number160("0x1"), null);
                var infConfig = Utils2.CreateInfiniteConfiguration()
                                .SetIsForceTcp();
                var tr = neighbors2.CloseNeighborsAsync(sender.PeerAddress, sv, Message.MessageType.Request2, cc,
                                                        infConfig);
                await tr;

                Assert.IsTrue(!tr.IsFaulted);

                // check if receiver got the neighbors
                var neighborSet = tr.Result.NeighborsSet(0);

                // 33 peer addresses can be stored in 1000 bytes (NeighborsLimit)
                Assert.IsTrue(neighborSet.Size == 33);
                var neighbors = neighborSet.Neighbors.ToList();
                Assert.AreEqual(new Number160("0x1"), neighbors[0].PeerId);
                Assert.AreEqual(PortTcp, neighbors[1].TcpPort);
                Assert.AreEqual(PortUdp, neighbors[2].UdpPort);
                await cc.ShutdownAsync();
            }
            finally
            {
                if (sender != null)
                {
                    sender.ShutdownAsync().Wait();
                }
                if (recv1 != null)
                {
                    recv1.ShutdownAsync().Wait();
                }
            }
        }