コード例 #1
0
ファイル: AutoDialerTest.cs プロジェクト: sgryphon/peer-talk
 public void Defaults()
 {
     using (var dialer = new AutoDialer(new Swarm()))
     {
         Assert.AreEqual(AutoDialer.DefaultMinConnections, dialer.MinConnections);
     }
 }
コード例 #2
0
ファイル: AutoDialerTest.cs プロジェクト: sgryphon/peer-talk
        public async Task Noop_OnPeerDiscovered_When_NotBelow_MinConnections()
        {
            var swarmA = new Swarm {
                LocalPeer = peerA
            };
            await swarmA.StartAsync();

            var peerAAddress = await swarmA.StartListeningAsync("/ip4/127.0.0.1/tcp/0");

            var swarmB = new Swarm {
                LocalPeer = peerB
            };
            await swarmB.StartAsync();

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

            try
            {
                using (var dialer = new AutoDialer(swarmA)
                {
                    MinConnections = 0
                })
                {
                    var other = swarmA.RegisterPeerAddress(peerBAddress);

                    // wait for the connection.
                    var endTime = DateTime.Now.AddSeconds(3);
                    while (other.ConnectedAddress == null)
                    {
                        if (DateTime.Now > endTime)
                        {
                            return;
                        }
                        await Task.Delay(100);
                    }
                    Assert.Fail("Autodial should not happen");
                }
            }
            finally
            {
                await swarmA?.StopAsync();

                await swarmB?.StopAsync();
            }
        }
コード例 #3
0
ファイル: AutoDialerTest.cs プロジェクト: sgryphon/peer-talk
        public async Task Connects_OnPeerDisconnected_When_Below_MinConnections()
        {
            var swarmA = new Swarm {
                LocalPeer = peerA
            };
            await swarmA.StartAsync();

            var peerAAddress = await swarmA.StartListeningAsync("/ip4/127.0.0.1/tcp/0");

            var swarmB = new Swarm {
                LocalPeer = peerB
            };
            await swarmB.StartAsync();

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

            var swarmC = new Swarm {
                LocalPeer = peerC
            };
            await swarmC.StartAsync();

            var peerCAddress = await swarmC.StartListeningAsync("/ip4/127.0.0.1/tcp/0");

            bool isBConnected = false;

            swarmA.ConnectionEstablished += (s, conn) =>
            {
                if (conn.RemotePeer == peerB)
                {
                    isBConnected = true;
                }
            };

            try
            {
                using (var dialer = new AutoDialer(swarmA)
                {
                    MinConnections = 1
                })
                {
                    var b = swarmA.RegisterPeerAddress(peerBAddress);
                    var c = swarmA.RegisterPeerAddress(peerCAddress);

                    // wait for the peer B connection.
                    var endTime = DateTime.Now.AddSeconds(3);
                    while (!isBConnected)
                    {
                        if (DateTime.Now > endTime)
                        {
                            Assert.Fail("Did not do autodial on peer discovered");
                        }
                        await Task.Delay(100);
                    }
                    Assert.IsNull(c.ConnectedAddress);
                    await swarmA.DisconnectAsync(peerBAddress);

                    // wait for the peer C connection.
                    endTime = DateTime.Now.AddSeconds(3);
                    while (c.ConnectedAddress == null)
                    {
                        if (DateTime.Now > endTime)
                        {
                            Assert.Fail("Did not do autodial on peer disconnected");
                        }
                        await Task.Delay(100);
                    }
                }
            }
            finally
            {
                await swarmA?.StopAsync();

                await swarmB?.StopAsync();

                await swarmC?.StopAsync();
            }
        }