Example #1
0
        public async Task MultiAddress()
        {
            var swarmB = new Swarm {
                LocalPeer = other
            };
            await swarmB.StartAsync();

            var pingB = new Ping1 {
                Swarm = swarmB
            };
            await pingB.StartAsync();

            var peerBAddress = await swarmB.StartListeningAsync("/ip4/127.0.0.1/tcp/0");

            var swarm = new Swarm {
                LocalPeer = self
            };
            await swarm.StartAsync();

            var pingA = new Ping1 {
                Swarm = swarm
            };
            await pingA.StartAsync();

            try
            {
                await swarm.ConnectAsync(peerBAddress);

                var result = await pingA.PingAsync(other.Id, 4);

                Assert.IsTrue(result.All(r => r.Success));
            }
            finally
            {
                await swarm.StopAsync();

                await swarmB.StopAsync();

                await pingB.StopAsync();

                await pingA.StopAsync();
            }
        }
Example #2
0
        public async Task Sends_Hello_OnConnect()
        {
            var topic = Guid.NewGuid().ToString();

            var swarm1 = new Swarm {
                LocalPeer = self
            };
            var router1 = new FloodRouter {
                Swarm = swarm1
            };
            var ns1 = new NotificationService {
                LocalPeer = self
            };

            ns1.Routers.Add(router1);
            await swarm1.StartAsync();

            await ns1.StartAsync();

            var swarm2 = new Swarm {
                LocalPeer = other
            };
            var router2 = new FloodRouter {
                Swarm = swarm2
            };
            var ns2 = new NotificationService {
                LocalPeer = other
            };

            ns2.Routers.Add(router2);
            await swarm2.StartAsync();

            await ns2.StartAsync();

            try
            {
                await swarm1.StartListeningAsync("/ip4/127.0.0.1/tcp/0");

                await swarm2.StartListeningAsync("/ip4/127.0.0.1/tcp/0");

                var cs = new CancellationTokenSource();
                await ns1.SubscribeAsync(topic, msg => { }, cs.Token);

                await swarm1.ConnectAsync(other);

                Peer[] peers   = new Peer[0];
                var    endTime = DateTime.Now.AddSeconds(3);
                while (peers.Length == 0)
                {
                    if (DateTime.Now > endTime)
                    {
                        Assert.Fail("timeout");
                    }
                    await Task.Delay(100);

                    peers = (await ns2.PeersAsync(topic)).ToArray();
                }
                CollectionAssert.Contains(peers, self);
            }
            finally
            {
                await swarm1.StopAsync();

                await ns1.StopAsync();

                await swarm2.StopAsync();

                await ns2.StopAsync();
            }
        }
Example #3
0
        public async Task Relays_PublishedMessage()
        {
            var topic = Guid.NewGuid().ToString();

            var swarm1 = new Swarm {
                LocalPeer = self
            };
            var router1 = new FloodRouter {
                Swarm = swarm1
            };
            var ns1 = new NotificationService {
                LocalPeer = self
            };

            ns1.Routers.Add(router1);
            await swarm1.StartAsync();

            await ns1.StartAsync();

            var swarm2 = new Swarm {
                LocalPeer = other
            };
            var router2 = new FloodRouter {
                Swarm = swarm2
            };
            var ns2 = new NotificationService {
                LocalPeer = other
            };

            ns2.Routers.Add(router2);
            await swarm2.StartAsync();

            await ns2.StartAsync();

            var swarm3 = new Swarm {
                LocalPeer = other1
            };
            var router3 = new FloodRouter {
                Swarm = swarm3
            };
            var ns3 = new NotificationService {
                LocalPeer = other1
            };

            ns3.Routers.Add(router3);
            await swarm3.StartAsync();

            await ns3.StartAsync();

            try
            {
                IPublishedMessage lastMessage2 = null;
                IPublishedMessage lastMessage3 = null;
                await swarm1.StartListeningAsync("/ip4/127.0.0.1/tcp/0");

                await swarm2.StartListeningAsync("/ip4/127.0.0.1/tcp/0");

                await swarm3.StartListeningAsync("/ip4/127.0.0.1/tcp/0");

                var cs = new CancellationTokenSource();
                await ns2.SubscribeAsync(topic, msg => lastMessage2 = msg, cs.Token);

                await ns3.SubscribeAsync(topic, msg => lastMessage3 = msg, cs.Token);

                await swarm1.ConnectAsync(other);

                await swarm3.ConnectAsync(other);

                Peer[] peers   = new Peer[0];
                var    endTime = DateTime.Now.AddSeconds(3);
                while (peers.Length == 0)
                {
                    if (DateTime.Now > endTime)
                    {
                        Assert.Fail("timeout");
                    }
                    await Task.Delay(100);

                    peers = (await ns2.PeersAsync(topic)).ToArray();
                }
                CollectionAssert.Contains(peers, other1);

                await ns1.PublishAsync(topic, new byte[] { 1 });

                endTime = DateTime.Now.AddSeconds(3);
                while (lastMessage2 == null || lastMessage3 == null)
                {
                    if (DateTime.Now > endTime)
                    {
                        Assert.Fail("timeout");
                    }
                    await Task.Delay(100);
                }

                Assert.IsNotNull(lastMessage2);
                Assert.AreEqual(self, lastMessage2.Sender);
                CollectionAssert.AreEqual(new byte[] { 1 }, lastMessage2.DataBytes);
                CollectionAssert.Contains(lastMessage2.Topics.ToArray(), topic);

                Assert.IsNotNull(lastMessage3);
                Assert.AreEqual(self, lastMessage3.Sender);
                CollectionAssert.AreEqual(new byte[] { 1 }, lastMessage3.DataBytes);
                CollectionAssert.Contains(lastMessage3.Topics.ToArray(), topic);
            }
            finally
            {
                await swarm1.StopAsync();

                await ns1.StopAsync();

                await swarm2.StopAsync();

                await ns2.StopAsync();

                await swarm3.StopAsync();

                await ns3.StopAsync();
            }
        }