Example #1
0
        public async Task OnConnect_Sends_WantList()
        {
            _dfsService.Options.Discovery.DisableMdns    = true;
            _dfsService.Options.Discovery.BootstrapPeers = new MultiAddress[0];
            await _dfsService.StartAsync();

            _dfsServiceOther.Options.Discovery.DisableMdns    = true;
            _dfsServiceOther.Options.Discovery.BootstrapPeers = new MultiAddress[0];
            await _dfsServiceOther.StartAsync();

            try
            {
                var local  = _dfsService.LocalPeer;
                var remote = _dfsServiceOther.LocalPeer;
                TestContext.WriteLine($"this at {local.Addresses.First()}");
                TestContext.WriteLine($"other at {remote.Addresses.First()}");

                var data = Guid.NewGuid().ToByteArray();
                var cid  = new Cid
                {
                    Hash = MultiHash.ComputeHash(data)
                };

                var _ = _dfsService.BlockApi.GetAsync(cid);
                await _dfsService.SwarmApi.ConnectAsync(remote.Addresses.First());

                var endTime = DateTime.Now.AddSeconds(10);
                while (DateTime.Now < endTime)
                {
                    var wants = await _dfsServiceOther.BitSwapApi.WantsAsync(local.Id);

                    if (wants.Contains(cid))
                    {
                        return;
                    }

                    await Task.Delay(200);
                }

                throw new Exception("want list not sent");
            }
            finally
            {
                await _dfsServiceOther.StopAsync();

                await _dfsService.StopAsync();

                _dfsService.Options.Discovery      = new DiscoveryOptions();
                _dfsServiceOther.Options.Discovery = new DiscoveryOptions();
            }
        }
Example #2
0
        public async Task Put_Informs_Bitswap()
        {
            _dfs = TestDfs.GetTestDfs(null, "sha2-256");
            await _dfs.StartAsync();

            try
            {
                var data = Guid.NewGuid().ToByteArray();
                var cid  = new Cid
                {
                    Hash = MultiHash.ComputeHash(data)
                };

                var cts = new CancellationTokenSource();
                cts.CancelAfter(20000);

                var wantTask = _dfs.BitSwapApi.GetAsync(cid, cts.Token);
                var cid1     = await _dfs.BlockApi.PutAsync(data, cancel : cts.Token);

                Assert.AreEqual(cid, cid1);
                Assert.AreEqual(cid, wantTask.Result.Id);
                Assert.AreEqual(data.Length, wantTask.Result.Size);
                Assert.AreEqual(data, wantTask.Result.DataBytes);
            }
            finally
            {
                await _dfs.StopAsync();
            }
        }
Example #3
0
        public async Task Can_Start_And_Stop()
        {
            var peer = _dfs1.LocalPeer;

            Assert.False(_dfs1.IsStarted);
            await _dfs1.StartAsync();

            Assert.True(_dfs1.IsStarted);
            Assert.AreNotEqual(0, peer.Addresses.Count());
            await _dfs1.StopAsync();

            Assert.False(_dfs1.IsStarted);
            Assert.AreEqual(0, peer.Addresses.Count());

            await _dfs1.StartAsync();

            Assert.AreNotEqual(0, peer.Addresses.Count());
            await _dfs1.StopAsync();

            Assert.AreEqual(0, peer.Addresses.Count());

            await _dfs1.StartAsync();

            Assert.AreNotEqual(0, peer.Addresses.Count());
            ExceptionAssert.Throws <Exception>(() => _dfs1.StartAsync().Wait());
            await _dfs1.StopAsync();

            Assert.AreEqual(0, peer.Addresses.Count());
        }
Example #4
0
        public async Task UnWant()
        {
            await _dfsService.StartAsync();

            try
            {
                var cts      = new CancellationTokenSource();
                var block    = new DagNode(Encoding.UTF8.GetBytes("BitSwapApiTest unknown block 2"));
                var wantTask = _dfsService.BitSwapApi.GetAsync(block.Id, cts.Token).ConfigureAwait(false);

                var endTime = DateTime.Now.AddSeconds(10);
                while (true)
                {
                    if (DateTime.Now > endTime)
                    {
                        throw new Exception("wanted block is missing");
                    }

                    await Task.Delay(100, cts.Token);

                    var w = await _dfsService.BitSwapApi.WantsAsync(cancel : cts.Token);

                    if (w.Contains(block.Id))
                    {
                        break;
                    }
                }

                cts.Cancel();
                _dfsService.BitSwapApi.UnWant(block.Id, cts.Token);
                var wants = await _dfsService.BitSwapApi.WantsAsync(cancel : cts.Token);

                wants.ToArray().Should().NotContain(block.Id);

                //Race condition as wantTask will be on another thread and could create unpredictable behaviour
                //Assert.True(wantTask.IsCanceled);
            }
            finally
            {
                await _dfsService.StopAsync();
            }
        }
Example #5
0
        public async Task Put_Informs_Dht()
        {
            var data = Guid.NewGuid().ToByteArray();
            await _dfs.StartAsync();

            try
            {
                var self = _dfs.LocalPeer;
                var cid  = await _dfs.BlockApi.PutAsync(data);

                var cts   = new CancellationTokenSource(TimeSpan.FromSeconds(3));
                var peers = await _dfs.DhtApi.FindProvidersAsync(cid, 1, cancel : cts.Token);

                Assert.AreEqual(self, peers.First());
            }
            finally
            {
                await _dfs.StopAsync();
            }
        }
Example #6
0
        public async Task Can_Start_And_Stop_MultipleEngines()
        {
            var peer1 = _dfs1.LocalPeer;
            var peer2 = _dfs2.LocalPeer;

            for (int n = 0; n < 3; ++n)
            {
                await _dfs1.StartAsync();

                Assert.AreNotEqual(0, peer1.Addresses.Count());
                await _dfs2.StartAsync();

                Assert.AreNotEqual(0, peer2.Addresses.Count());

                await _dfs2.StopAsync();

                Assert.AreEqual(0, peer2.Addresses.Count());
                await _dfs1.StopAsync();

                Assert.AreEqual(0, peer1.Addresses.Count());
            }
        }
Example #7
0
        public async Task Subscribed_Topics()
        {
            var topic = Guid.NewGuid().ToString();
            var cs    = new CancellationTokenSource();
            await ipfs.StartAsync();

            try
            {
                await ipfs.PubSubApi.SubscribeAsync(topic, msg => { }, cs.Token);

                var topics = ipfs.PubSubApi.SubscribedTopicsAsync(cs.Token).Result.ToArray();
                Assert.True(topics.Length > 0);
                topics.Should().Contain(topic);
            }
            finally
            {
                await ipfs.StopAsync();

                cs.Cancel();
            }
        }