public async Task DoNotBroadcastToSourcePeer() { TestTransport transportA = CreateTestTransport(new PrivateKey()); TestTransport transportB = CreateTestTransport(new PrivateKey()); TestTransport transportC = CreateTestTransport(new PrivateKey()); await StartTestTransportAsync(transportA); await StartTestTransportAsync(transportB); await StartTestTransportAsync(transportC); try { await transportA.AddPeersAsync(new[] { transportB.AsPeer }, null); await transportB.AddPeersAsync(new[] { transportC.AsPeer }, null); transportA.BroadcastTestMessage(null, "foo"); await transportC.WaitForTestMessageWithData("foo"); await Task.Delay(100); Assert.True(transportC.ReceivedTestMessageOfData("foo")); Assert.False(transportA.ReceivedTestMessageOfData("foo")); } finally { await transportA.StopAsync(TimeSpan.Zero); await transportB.StopAsync(TimeSpan.Zero); await transportC.StopAsync(TimeSpan.Zero); } }
public async Task RefreshTable() { const int peersCount = 10; var privateKey = new PrivateKey(); var privateKeys = Enumerable.Range(0, peersCount).Select( i => TestUtils.GeneratePrivateKeyOfBucketIndex(privateKey.ToAddress(), i / 2)); TestTransport transport = CreateTestTransport(privateKey); TestTransport[] transports = privateKeys.Select(key => CreateTestTransport(key)).ToArray(); await StartTestTransportAsync(transport); foreach (var t in transports) { await StartTestTransportAsync(t); } try { foreach (var t in transports) { transport.Table.AddPeer( (BoundPeer)t.AsPeer, DateTimeOffset.UtcNow - TimeSpan.FromMinutes(2)); } BoundPeer[] refreshCandidates = transport.Table.PeersToRefresh(TimeSpan.FromMinutes(1)).ToArray(); Assert.Equal(peersCount, transport.Peers.Count()); Assert.Equal(peersCount / 2, refreshCandidates.Length); Assert.Equal(peersCount / 2, transport.Table.NonEmptyBuckets.Count()); await transport.Protocol.RefreshTableAsync(TimeSpan.FromMinutes(1), default); Assert.NotEqual( refreshCandidates.ToHashSet(), transport.Table.PeersToRefresh(TimeSpan.FromMinutes(1)).ToHashSet()); Assert.Equal( peersCount / 2, transport.Table.PeersToRefresh(TimeSpan.FromMinutes(1)).Count()); Assert.Equal(peersCount / 2, transport.Table.NonEmptyBuckets.Count()); await transport.Protocol.RefreshTableAsync(TimeSpan.FromMinutes(1), default); Assert.Empty(transport.Table.PeersToRefresh(TimeSpan.FromMinutes(1))); } finally { await transport.StopAsync(TimeSpan.Zero); foreach (var t in transports) { await t.StopAsync(TimeSpan.Zero); } } Assert.True(true); }
private async Task <Task> StartTestTransportAsync( TestTransport transport, CancellationToken cancellationToken = default(CancellationToken)) { await transport.StartAsync(cancellationToken); return(transport.RunAsync(cancellationToken)); }
public async Task BroadcastMessage(int count) { var seed = CreateTestTransport(); await StartTestTransportAsync(seed); var transports = new TestTransport[count]; for (var i = 0; i < count; i++) { transports[i] = CreateTestTransport(); await StartTestTransportAsync(transports[i]); } try { foreach (var transport in transports) { await transport.BootstrapAsync(new[] { seed.AsPeer }); } Log.Debug("Bootstrap completed."); var tasks = transports.Select(transport => transport.WaitForTestMessageWithData("foo")); seed.BroadcastTestMessage(null, "foo"); Log.Debug("Broadcast completed."); await Task.WhenAll(tasks); } finally { foreach (var transport in transports) { Assert.True(transport.ReceivedTestMessageOfData("foo")); await transport.StopAsync(TimeSpan.Zero); } } }