Exemple #1
0
        public void WorksAsCollection()
        {
            Swarm a = _swarms[0];
            Swarm b = _swarms[1];
            Swarm c = _swarms[2];

            Assert.Empty(a);
            Assert.Empty(b);
            Assert.Empty(c);

            a.Add(b.AsPeer);
            a.Add(c.AsPeer);
            Assert.Contains(b.AsPeer, a);
            Assert.Contains(c.AsPeer, a);

            Peer[] peers = null;
            Assert.Throws <ArgumentNullException>(() =>
            {
                a.CopyTo(peers, 0);
            });

            peers = new Peer[3];
            Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                a.CopyTo(peers, -1);
            });
            Assert.Throws <ArgumentException>(() =>
            {
                a.CopyTo(peers, 2);
            });

            a.CopyTo(peers, 1);

            Assert.Equal(new Peer[] { null, b.AsPeer, c.AsPeer }, peers);
        }
Exemple #2
0
        public void CanBeComparedProperly()
        {
            var pk1 = new PrivateKey();
            var pk2 = new PrivateKey();
            var a   = new Swarm(
                pk1,
                new Uri($"inproc://swarmtest.t"),
                3000);
            var b = new Swarm(
                pk1,
                new Uri($"inproc://swarmtest.t"),
                3000,
                a.LastDistributed);
            var c = new Swarm(
                pk2,
                new Uri($"inproc://swarmtest.t"),
                3000);
            var u1 = new Uri($"inproc://swarmtest.t");

            Assert.Equal(a, b);
            Assert.NotEqual(a, c);
            a.Add(c.AsPeer);

            Assert.NotEqual(a, b);
            Assert.True(a.KeyEquals(b));
        }
Exemple #3
0
        public async Task InitialBlockDownload()
        {
            Swarm minerSwarm    = _swarms[0];
            Swarm receiverSwarm = _swarms[1];

            BlockChain <DumbAction> minerChain    = _blockchains[0];
            BlockChain <DumbAction> receiverChain = _blockchains[1];

            foreach (int i in Enumerable.Range(0, 10))
            {
                minerChain.MineBlock(_fx1.Address1);
            }

            try
            {
                await StartAsync(minerSwarm, minerChain);

                receiverSwarm.Add(minerSwarm.AsPeer);

                await StartAsync(receiverSwarm, receiverChain);

                await Task.Delay(TimeSpan.FromSeconds(10));

                Assert.Equal(minerChain.AsEnumerable(), receiverChain.AsEnumerable());
            }
            finally
            {
                await Task.WhenAll(
                    minerSwarm.StopAsync(),
                    receiverSwarm.StopAsync());
            }
        }
Exemple #4
0
        public void BeComparedProperly()
        {
            var pk1 = new PrivateKey();
            var pk2 = new PrivateKey();
            var a   = new Swarm(
                pk1,
                1,
                host: "0.0.0.0",
                listenPort: 5555);
            var b = new Swarm(
                pk1,
                1,
                host: "0.0.0.0",
                listenPort: 5555,
                createdAt: a.LastDistributed);
            var c = new Swarm(
                pk2,
                1,
                host: "0.0.0.0",
                listenPort: 5555);

            Assert.Equal(a, b);
            Assert.NotEqual(a, c);
            a.Add(c.AsPeer);

            Assert.NotEqual(a, b);
            Assert.True(a.KeyEquals(b));
        }
Exemple #5
0
        public static void CreateSwarm(int count)
        {
            if (count > 5)
            {
                count = 5;
            }

            for (byte i = 0; i < count; i++)
            {
                switch (i % 4)
                {
                case 0:
                    Swarm.Add(new Ship(Buf.GreenStructure, R.Next(7, 15)));
                    break;

                case 1:
                    Swarm.Add(new Ship(Buf.YellowStructure, R.Next(7, 15)));
                    break;

                case 2:
                    Swarm.Add(new Ship(Buf.RedStructure, R.Next(7, 15)));
                    break;

                case 3:
                    Swarm.Add(new Ship(Buf.BlueStructure, R.Next(7, 15)));
                    break;
                }

                Swarm[i].Posx = 20 + i * 20;
                Swarm[i].Posy = 5;
                Ship.Move(Swarm[i], 0, 0);
            }
        }
Exemple #6
0
        public async Task WorksAsCollection()
        {
            Swarm a = _swarms[0];
            Swarm b = _swarms[1];
            Swarm c = _swarms[2];

            // Obtaining swarm's endpoint...
            await Task.WhenAll(
                StartAsync(a, _blockchains[0]),
                StartAsync(b, _blockchains[1]),
                StartAsync(c, _blockchains[2]));

            Assert.Empty(a);
            Assert.Empty(b);
            Assert.Empty(c);

            a.Add(b.AsPeer);
            a.Add(c.AsPeer);
            Assert.Contains(b.AsPeer, a);
            Assert.Contains(c.AsPeer, a);

            Peer[] peers = null;
            Assert.Throws <ArgumentNullException>(() =>
            {
                a.CopyTo(peers, 0);
            });

            peers = new Peer[3];
            Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                a.CopyTo(peers, -1);
            });
            Assert.Throws <ArgumentException>(() =>
            {
                a.CopyTo(peers, 2);
            });

            a.CopyTo(peers, 1);

            Assert.Equal(
                new HashSet <Peer> {
                null, b.AsPeer, c.AsPeer
            },
                peers.ToHashSet());
        }
Exemple #7
0
        public async Task Preload()
        {
            Swarm minerSwarm    = _swarms[0];
            Swarm receiverSwarm = _swarms[1];

            BlockChain <DumbAction> minerChain    = _blockchains[0];
            BlockChain <DumbAction> receiverChain = _blockchains[1];

            foreach (int i in Enumerable.Range(0, 10))
            {
                minerChain.MineBlock(_fx1.Address1);
            }

            var actualStates = new List <BlockDownloadState>();
            var progress     = new Progress <BlockDownloadState>(state =>
            {
                lock (actualStates)
                {
                    actualStates.Add(state);
                }
            });

            try
            {
                await StartAsync(minerSwarm, minerChain);

                receiverSwarm.Add(minerSwarm.AsPeer);

                await receiverSwarm.PreloadAsync(receiverChain, progress);

                Assert.Equal(minerChain.AsEnumerable(), receiverChain.AsEnumerable());

                IEnumerable <BlockDownloadState> expectedStates = minerChain.Select((b, i) =>
                {
                    return(new BlockDownloadState()
                    {
                        ReceivedBlockHash = b.Hash,
                        TotalBlockCount = 10,
                        ReceivedBlockCount = i + 1,
                    });
                });

                Assert.Equal(expectedStates, actualStates);
            }
            finally
            {
                await Task.WhenAll(
                    minerSwarm.StopAsync(),
                    receiverSwarm.StopAsync());
            }
        }
Exemple #8
0
        public async Task DetectAppProtocolVersion()
        {
            var a = new Swarm(
                new PrivateKey(),
                host: IPAddress.Loopback.ToString(),
                appProtocolVersion: 2);
            var b = new Swarm(
                new PrivateKey(),
                host: IPAddress.Loopback.ToString(),
                appProtocolVersion: 3);

            var c = new Swarm(
                new PrivateKey(),
                host: IPAddress.Loopback.ToString(),
                appProtocolVersion: 2);
            var d = new Swarm(
                new PrivateKey(),
                host: IPAddress.Loopback.ToString(),
                appProtocolVersion: 3);

            BlockChain <DumbAction> chain = _blockchains[0];

            try
            {
                await StartAsync(a, chain);
                await StartAsync(b, chain);
                await StartAsync(c, chain);
                await StartAsync(d, chain);

                var peers = new[] { c.AsPeer, d.AsPeer };

                foreach (var peer in peers)
                {
                    a.Add(peer);
                    b.Add(peer);
                }

                Assert.Equal(new[] { c.AsPeer }, a.ToArray());
                Assert.Equal(new[] { d.AsPeer }, b.ToArray());
            }
            finally
            {
                await a.StopAsync();

                await b.StopAsync();

                await c.StopAsync();

                await d.StopAsync();
            }
        }
Exemple #9
0
        public async Task HandleDifferentAppProtocolVersion()
        {
            var isCalled = false;

            void GameHandler(object sender, DifferentProtocolVersionEventArgs e)
            {
                isCalled = true;
            }

            var a = new Swarm(
                new PrivateKey(),
                host: IPAddress.Loopback.ToString(),
                appProtocolVersion: 2,
                differentVersionPeerEncountered: GameHandler);
            var b = new Swarm(
                new PrivateKey(),
                host: IPAddress.Loopback.ToString(),
                appProtocolVersion: 3);

            BlockChain <DumbAction> chain = _blockchains[0];

            try
            {
                await StartAsync(a, chain);
                await StartAsync(b, chain);

                a.Add(b.AsPeer);

                Assert.True(isCalled);
            }
            finally
            {
                await a.StopAsync();

                await b.StopAsync();
            }
        }