public async Task SubscribedPeers_AllTopics()
        {
            var topic1 = Guid.NewGuid().ToString();
            var topic2 = Guid.NewGuid().ToString();
            var ns     = new NotificationService {
                LocalPeer = self
            };
            var router = new FloodRouter {
                Swarm = new Swarm()
            };

            router.RemoteTopics.AddInterest(topic1, other1);
            router.RemoteTopics.AddInterest(topic2, other2);
            ns.Routers.Add(router);
            await ns.StartAsync();

            try
            {
                var peers = (await ns.PeersAsync(null)).ToArray();
                Assert.AreEqual(2, peers.Length);
            }
            finally
            {
                await ns.StopAsync();
            }
        }
Пример #2
0
        public void RemoteSubscriptions()
        {
            var router = new FloodRouter();

            var sub = new Subscription {
                Topic = "topic", Subscribe = true
            };

            router.ProcessSubscription(sub, other);
            Assert.AreEqual(1, router.RemoteTopics.GetPeers("topic").Count());

            var can = new Subscription {
                Topic = "topic", Subscribe = false
            };

            router.ProcessSubscription(can, other);
            Assert.AreEqual(0, router.RemoteTopics.GetPeers("topic").Count());
        }
Пример #3
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();
            }
        }
Пример #4
0
        public void Defaults()
        {
            var router = new FloodRouter();

            Assert.AreEqual("/floodsub/1.0.0", router.ToString());
        }
Пример #5
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();
            }
        }