Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
flcl42 committed Dec 11, 2024
1 parent 9e6fac5 commit 6997b37
Show file tree
Hide file tree
Showing 57 changed files with 627 additions and 710 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ jobs:
TEST_OPTS: -c ${{ env.BUILD_CONFIG }} --no-restore
run: |
dotnet test Libp2p.Core.Tests ${{ env.PACK_OPTS }}
#dotnet test Libp2p.Protocols.Multistream.Tests ${{ env.PACK_OPTS }}
dotnet test Libp2p.Protocols.Multistream.Tests ${{ env.PACK_OPTS }}
dotnet test Libp2p.Protocols.Noise.Tests ${{ env.PACK_OPTS }}
dotnet test Libp2p.Protocols.Pubsub.Tests ${{ env.PACK_OPTS }}
dotnet test Libp2p.Protocols.Quic.Tests ${{ env.PACK_OPTS }}
dotnet test Libp2p.Protocols.Yamux.Tests ${{ env.PACK_OPTS }}
dotnet test Libp2p.E2eTests ${{ env.PACK_OPTS }}
dotnet test Libp2p.Protocols.Pubsub.E2eTests ${{ env.PACK_OPTS }}
dotnet test Libp2p.Protocols.PubsubPeerDiscovery.E2eTests ${{ env.PACK_OPTS }}
4 changes: 2 additions & 2 deletions src/libp2p/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageVersion Include="Nethermind.Multiformats.Address" Version="1.1.5" />
<PackageVersion Include="Nethermind.Multiformats.Address" Version="1.1.7" />
<PackageVersion Include="NLog.Extensions.Logging" Version="5.3.12" />
<PackageVersion Include="Noise.NET" Version="1.0.0" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
Expand All @@ -34,4 +34,4 @@
<PackageVersion Include="SimpleBase" Version="4.0.0" />
<PackageVersion Include="System.Runtime.Caching" Version="7.0.0" />
</ItemGroup>
</Project>
</Project>
221 changes: 0 additions & 221 deletions src/libp2p/Libp2p.Core.Tests/ContextTests.cs

This file was deleted.

12 changes: 10 additions & 2 deletions src/libp2p/Libp2p.Core.Tests/TaskHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ public async Task Test_AllExceptions_RaiseAggregateException()
tcs2.SetException(new Exception());
tcs3.SetException(new Exception());


Task r = await t;
await t.ContinueWith((t) =>
{
Assert.Multiple(() =>
{
Assert.That(t.IsFaulted, Is.True);
Assert.That(t.Exception?.InnerException, Is.TypeOf<AggregateException>());
Assert.That((t.Exception?.InnerException as AggregateException)?.InnerExceptions, Has.Count.EqualTo(3));
});
});
}

[Test]
Expand All @@ -40,5 +47,6 @@ public async Task Test_SingleSuccess_ReturnsCompletedTask()
Task result = await t;

Assert.That(result, Is.EqualTo(tcs3.Task));
Assert.That((result as Task<bool>)!.Result, Is.EqualTo(true));
}
}
9 changes: 1 addition & 8 deletions src/libp2p/Libp2p.Core.TestsBase/E2e/TestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ internal class TestLocalPeer(Identity id, IProtocolStackSettings protocolStackSe
{
protected override async Task ConnectedTo(ISession session, bool isDialer)
{
try
{
await session.DialAsync<IdentifyProtocol>();
}
catch
{

}
await session.DialAsync<IdentifyProtocol>();
}
}
6 changes: 4 additions & 2 deletions src/libp2p/Libp2p.Core.TestsBase/E2e/TestMuxerProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class TestMuxerProtocol(ChannelBus bus, ILoggerFactory? loggerFactory = null) :
private readonly ILogger? logger = loggerFactory?.CreateLogger(id);

public string Id => id;
public static Multiaddress[] GetDefaultAddresses(PeerId peerId) => [$"/p2p/{peerId}"];
public static bool IsAddressMatch(Multiaddress addr) => true;

public async Task DialAsync(ITransportContext context, Multiaddress remoteAddr, CancellationToken token)
{
Expand Down Expand Up @@ -144,7 +146,7 @@ private async Task HandleRemote(IChannel downChannel, INewConnectionContext conn

UpgradeOptions req = new() { SelectedProtocol = selected, ModeOverride = UpgradeModeOverride.Listen };

IChannel upChannel = session.Upgrade(req);
IChannel upChannel = session.Upgrade(selected, req);
chans[packet.ChannelId] = new MuxerChannel { UpChannel = upChannel };
_ = HandleUpchannelData(downChannel, chans, packet.ChannelId, upChannel, logPrefix);

Expand All @@ -171,7 +173,7 @@ private async Task HandleRemote(IChannel downChannel, INewConnectionContext conn
if (packet.Protocols.Any())
{
UpgradeOptions req = new() { SelectedProtocol = session.SubProtocols.FirstOrDefault(x => x.Id == packet.Protocols.First()), CompletionSource = chans[packet.ChannelId].Tcs, Argument = chans[packet.ChannelId].Argument, ModeOverride = UpgradeModeOverride.Dial };
IChannel upChannel = session.Upgrade(req);
IChannel upChannel = session.Upgrade(session.SubProtocols.FirstOrDefault(x => x.Id == packet.Protocols.First())!, req);
chans[packet.ChannelId].UpChannel = upChannel;
logger?.LogDebug($"{logPrefix}({packet.ChannelId}): Start upchanel with {req.SelectedProtocol}");
_ = HandleUpchannelData(downChannel, chans, packet.ChannelId, upChannel, logPrefix);
Expand Down
19 changes: 10 additions & 9 deletions src/libp2p/Libp2p.Core.TestsBase/E2e/TestMuxerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.Extensions.DependencyInjection;
using Nethermind.Libp2p.Core.Discovery;
using Nethermind.Libp2p.Stack;
using NUnit.Framework;

namespace Nethermind.Libp2p.Core.TestsBase.E2e;
Expand All @@ -11,21 +12,21 @@ internal class TestMuxerTests
[Test]
public async Task Test_ConnectionEstablished_AfterHandshake()
{
ServiceProvider sp = new ServiceCollection()
ChannelBus channelBus = new();
ServiceProvider MakeServiceProvider() => new ServiceCollection()
.AddSingleton<IPeerFactoryBuilder>(sp => new TestBuilder(sp))
.AddSingleton<IProtocolStackSettings, ProtocolStackSettings>()
.AddSingleton<PeerStore>()
.AddSingleton<ChannelBus>()
.AddSingleton(channelBus)
.AddSingleton(sp => sp.GetService<IPeerFactoryBuilder>()!.Build())
.BuildServiceProvider();

IPeerFactory peerFactory = sp.GetService<IPeerFactory>()!;
IPeer peerA = MakeServiceProvider().GetRequiredService<IPeerFactory>().Create(TestPeers.Identity(1));
await peerA.StartListenAsync();
IPeer peerB = MakeServiceProvider().GetRequiredService<IPeerFactory>().Create(TestPeers.Identity(2));
await peerB.StartListenAsync();

IPeer peerA = peerFactory.Create(TestPeers.Identity(1));
await peerA.StartListenAsync([TestPeers.Multiaddr(1)]);
IPeer peerB = peerFactory.Create(TestPeers.Identity(2));
await peerB.StartListenAsync([TestPeers.Multiaddr(2)]);

ISession remotePeerB = await peerA.DialAsync(TestPeers.Multiaddr(1));
ISession remotePeerB = await peerA.DialAsync(TestPeers.Multiaddr(2));
await remotePeerB.DialAsync<TestPingProtocol>();
}
}
12 changes: 0 additions & 12 deletions src/libp2p/Libp2p.Core.TestsBase/E2e/TestSuite.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/libp2p/Libp2p.Core/Exceptions/Libp2pException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public Libp2pException() : base()
}
}

public class ChannelClosedException : Libp2pException;
public class ChannelClosedException() : Libp2pException("Channel closed");

/// <summary>
/// Appears when libp2p is not set up properly in part of protocol tack, IoC, etc.
Expand Down
2 changes: 1 addition & 1 deletion src/libp2p/Libp2p.Core/Extensions/TaskHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static async Task<Task> FirstSuccess(params Task[] tasks)
Task result = await Task.WhenAny(tcs.Task, all);
if (result == all)
{
throw new AggregateException(tasks.Select(t => t.Exception).Where(ex => ex is not null)!);
throw new AggregateException(tasks.Select(t => t.Exception?.InnerException).Where(ex => ex is not null)!);
}
return tcs.Task.Result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libp2p/Libp2p.Core/IPeer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface IPeer
/// </summary>
Task<ISession> DialAsync(PeerId peerId, CancellationToken token = default);

Task StartListenAsync(Multiaddress[] addrs, CancellationToken token = default);
Task StartListenAsync(Multiaddress[]? addrs = default, CancellationToken token = default);

Task DisconnectAsync();

Expand Down
Loading

0 comments on commit 6997b37

Please sign in to comment.