Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.NET 6 and Nats support #76

Merged
merged 6 commits into from
Apr 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/build-debug.yml
Original file line number Diff line number Diff line change
@@ -27,6 +27,10 @@ jobs:
--health-retries 5
ports:
- 6379:6379
nats:
image: nats
ports:
- 4222:4222
steps:
- uses: actions/checkout@v2
- run: dotnet build ./tools/PostBuildUtility/ -c Debug
5 changes: 5 additions & 0 deletions .github/workflows/build-release.yml
Original file line number Diff line number Diff line change
@@ -103,6 +103,10 @@ jobs:
--health-retries 5
ports:
- 6379:6379
nats:
image: nats
ports:
- 4222:4222
steps:
- run: echo ${{ needs.update-packagejson.outputs.sha }}
- uses: actions/checkout@v2
@@ -116,6 +120,7 @@ jobs:
- run: dotnet pack ./src/MessagePipe.Analyzer/MessagePipe.Analyzer.csproj -c Release --no-build -p:Version=${{ env.GIT_TAG }} -o ./publish
- run: dotnet pack ./src/MessagePipe.Redis/MessagePipe.Redis.csproj -c Release --no-build -p:Version=${{ env.GIT_TAG }} -o ./publish
- run: dotnet pack ./src/MessagePipe.Interprocess/MessagePipe.Interprocess.csproj -c Release --no-build -p:Version=${{ env.GIT_TAG }} -o ./publish
- run: dotnet pack ./src/MessagePipe.Nats/MessagePipe.Nats.csproj -c Release --no-build -p:Version=${{ env.GIT_TAG }} -o ./publish
# Store artifacts.
- uses: actions/upload-artifact@v2
with:
20 changes: 19 additions & 1 deletion MessagePipe.sln
Original file line number Diff line number Diff line change
@@ -50,7 +50,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MessagePipe.Interprocess.Te
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "InterprocessServer", "sandbox\InterprocessServer\InterprocessServer.csproj", "{0E645BF9-3464-4856-A624-FEFCF0050220}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MessagePipe.Interprocess.Benchmark", "tests\MessagePipe.Interprocess.Benchmark\MessagePipe.Interprocess.Benchmark.csproj", "{AF421C72-0DDA-4568-BD7E-B3CE90B3B31C}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MessagePipe.Interprocess.Benchmark", "tests\MessagePipe.Interprocess.Benchmark\MessagePipe.Interprocess.Benchmark.csproj", "{AF421C72-0DDA-4568-BD7E-B3CE90B3B31C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MessagePipe.Nats", "src\MessagePipe.Nats\MessagePipe.Nats.csproj", "{80D9A03B-CDF0-4D52-BF07-2C198707E65C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MessagePipe.Nats.Tests", "tests\MessagePipe.Nats.Tests\MessagePipe.Nats.Tests.csproj", "{8BE1C4C7-9584-4F87-9773-6E7A3B3C71E2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -143,6 +147,18 @@ Global
{AF421C72-0DDA-4568-BD7E-B3CE90B3B31C}.Release|Any CPU.Build.0 = Release|Any CPU
{AF421C72-0DDA-4568-BD7E-B3CE90B3B31C}.WinBenchmark|Any CPU.ActiveCfg = Debug|Any CPU
{AF421C72-0DDA-4568-BD7E-B3CE90B3B31C}.WinBenchmark|Any CPU.Build.0 = Debug|Any CPU
{80D9A03B-CDF0-4D52-BF07-2C198707E65C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{80D9A03B-CDF0-4D52-BF07-2C198707E65C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{80D9A03B-CDF0-4D52-BF07-2C198707E65C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{80D9A03B-CDF0-4D52-BF07-2C198707E65C}.Release|Any CPU.Build.0 = Release|Any CPU
{80D9A03B-CDF0-4D52-BF07-2C198707E65C}.WinBenchmark|Any CPU.ActiveCfg = Debug|Any CPU
{80D9A03B-CDF0-4D52-BF07-2C198707E65C}.WinBenchmark|Any CPU.Build.0 = Debug|Any CPU
{8BE1C4C7-9584-4F87-9773-6E7A3B3C71E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8BE1C4C7-9584-4F87-9773-6E7A3B3C71E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8BE1C4C7-9584-4F87-9773-6E7A3B3C71E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8BE1C4C7-9584-4F87-9773-6E7A3B3C71E2}.Release|Any CPU.Build.0 = Release|Any CPU
{8BE1C4C7-9584-4F87-9773-6E7A3B3C71E2}.WinBenchmark|Any CPU.ActiveCfg = Debug|Any CPU
{8BE1C4C7-9584-4F87-9773-6E7A3B3C71E2}.WinBenchmark|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@@ -163,6 +179,8 @@ Global
{3B2C3908-E9EB-43F3-9340-3CF5EFBA2F09} = {36546FD6-866F-4809-AFCE-87F7F4201361}
{0E645BF9-3464-4856-A624-FEFCF0050220} = {9813BFC3-7860-4697-A3AF-118BDF710BD0}
{AF421C72-0DDA-4568-BD7E-B3CE90B3B31C} = {36546FD6-866F-4809-AFCE-87F7F4201361}
{80D9A03B-CDF0-4D52-BF07-2C198707E65C} = {381F6F79-110B-4CE4-9A49-583046D8C164}
{8BE1C4C7-9584-4F87-9773-6E7A3B3C71E2} = {36546FD6-866F-4809-AFCE-87F7F4201361}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {138B7AA8-E4C0-46A3-A48B-0D373CAC365D}
4 changes: 4 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -5,3 +5,7 @@ services:
image: "redis:6.2.1"
ports:
- "6379:6379"
nats:
image: nats
ports:
- 4222:4222
4 changes: 2 additions & 2 deletions sandbox/InterprocessServer/InterprocessServer.csproj
Original file line number Diff line number Diff line change
@@ -2,11 +2,11 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ConsoleAppFramework" Version="3.3.0" />
<PackageReference Include="ConsoleAppFramework" Version="4.0.6" />
</ItemGroup>

<ItemGroup>
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Original file line number Diff line number Diff line change
@@ -2,15 +2,15 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<Configurations>Debug;Release;WinBenchmark</Configurations>
<SignAssembly>true</SignAssembly>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ConsoleAppFramework" Version="3.3.0" />
<PackageReference Include="StackExchange.Redis" Version="2.2.4" />
<PackageReference Include="ZLogger" Version="1.5.0" />
<PackageReference Include="ConsoleAppFramework" Version="4.0.6" />
<PackageReference Include="StackExchange.Redis" Version="2.5.61" />
<PackageReference Include="ZLogger" Version="1.6.1" />
</ItemGroup>

<ItemGroup>
4 changes: 2 additions & 2 deletions src/MessagePipe.Analyzer/MessagePipe.Analyzer.csproj
Original file line number Diff line number Diff line change
@@ -20,11 +20,11 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2">
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" />
</ItemGroup>

<Target Name="PackBuildOutputs" DependsOnTargets="SatelliteDllsProjectOutputGroup;DebugSymbolsProjectOutputGroup">
10 changes: 5 additions & 5 deletions src/MessagePipe.Interprocess/MessagePipe.Interprocess.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net5.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net5.0;net6.0</TargetFrameworks>
<WarningsAsErrors>true</WarningsAsErrors>
<Configurations>Debug;Release;</Configurations>

@@ -16,14 +16,14 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MessagePack" Version="2.2.85" />
<PackageReference Include="System.IO.Pipelines" Version="5.0.1" />
<PackageReference Include="MessagePack" Version="2.3.85" />
<PackageReference Include="System.IO.Pipelines" Version="6.0.2" />
<ProjectReference Include="..\MessagePipe\MessagePipe.csproj" />
</ItemGroup>

<ItemGroup Condition="$(TargetFramework) == 'netstandard2.0'">
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
<PackageReference Include="System.Threading.Channels" Version="5.0.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="6.0.0" />
<PackageReference Include="System.Threading.Channels" Version="6.0.0" />
</ItemGroup>

<!-- Copy files for Unity -->
4 changes: 2 additions & 2 deletions src/MessagePipe.Interprocess/Workers/NamedPipeWorker.cs
Original file line number Diff line number Diff line change
@@ -249,9 +249,9 @@ async void RunReceiveLoop(Stream pipeStream, Func<CancellationToken, Task>? wait
{
var t = AsyncRequestHandlerRegistory.Get(reqTypeName, resTypeName);
var interfaceType = t.GetInterfaces().Where(x => x.IsGenericType && x.Name.StartsWith("IAsyncRequestHandler"))
.First(x => x.GetGenericArguments().Any(x => x.FullName == header.RequestType));
.First(x => x.GetGenericArguments().Any(y => y.FullName == header.RequestType));
var coreInterfaceType = t.GetInterfaces().Where(x => x.IsGenericType && x.Name.StartsWith("IAsyncRequestHandlerCore"))
.First(x => x.GetGenericArguments().Any(x => x.FullName == header.RequestType));
.First(x => x.GetGenericArguments().Any(y => y.FullName == header.RequestType));
var service = provider.GetRequiredService(interfaceType); // IAsyncRequestHandler<TRequest,TResponse>
var genericArgs = interfaceType.GetGenericArguments(); // [TRequest, TResponse]
var request = MessagePackSerializer.Deserialize(genericArgs[0], message.ValueMemory, options.MessagePackSerializerOptions);
4 changes: 2 additions & 2 deletions src/MessagePipe.Interprocess/Workers/TcpWorker.cs
Original file line number Diff line number Diff line change
@@ -264,9 +264,9 @@ async void RunReceiveLoop(SocketTcpClient client)
{
var t = AsyncRequestHandlerRegistory.Get(reqTypeName, resTypeName);
var interfaceType = t.GetInterfaces().Where(x => x.IsGenericType && x.Name.StartsWith("IAsyncRequestHandler"))
.First(x => x.GetGenericArguments().Any(x => x.FullName == header.RequestType));
.First(x => x.GetGenericArguments().Any(y => y.FullName == header.RequestType));
var coreInterfaceType = t.GetInterfaces().Where(x => x.IsGenericType && x.Name.StartsWith("IAsyncRequestHandlerCore"))
.First(x => x.GetGenericArguments().Any(x => x.FullName == header.RequestType));
.First(x => x.GetGenericArguments().Any(y => y.FullName == header.RequestType));
var service = provider.GetRequiredService(interfaceType); // IAsyncRequestHandler<TRequest,TResponse>
var genericArgs = interfaceType.GetGenericArguments(); // [TRequest, TResponse]
// Unity IL2CPP does not work(can not invoke nongenerics MessagePackSerializer)
Binary file added src/MessagePipe.Nats/Icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions src/MessagePipe.Nats/MessagePipe.Nats.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<!-- NuGet Packaging -->
<PackageTags>pubsub;eventaggregator</PackageTags>
<Description>Nats IDistributedPublisher/Subscriber provider for MessagePipe.</Description>
<SignAssembly>true</SignAssembly>
</PropertyGroup>

<ItemGroup>
<None Include="Icon.png" Pack="true" PackagePath="/" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="AlterNats" Version="0.0.2" />
<PackageReference Include="MessagePack" Version="2.3.85" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MessagePipe\MessagePipe.csproj" />
</ItemGroup>

</Project>
11 changes: 11 additions & 0 deletions src/MessagePipe.Nats/MessagePipeNatsOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace MessagePipe.Nats;

public sealed class MessagePipeNatsOptions
{
public NatsConnectionFactory NatsConnectionFactory { get; }

public MessagePipeNatsOptions(NatsConnectionFactory connectionFactory)
{
NatsConnectionFactory = connectionFactory;
}
}
28 changes: 28 additions & 0 deletions src/MessagePipe.Nats/NatsConnectionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using AlterNats;

namespace MessagePipe.Nats;

public class NatsConnectionFactory
{
readonly NatsOptions options;
NatsConnection? connection;

public NatsConnectionFactory() : this(NatsOptions.Default) { }

public NatsConnectionFactory(NatsOptions options)
{
this.options = options;
}

public async ValueTask<NatsConnection> GetConnectionAsync()
{
connection ??= new NatsConnection(options);

if (connection.ConnectionState == NatsConnectionState.Closed)
{
await connection.ConnectAsync();
}

return connection;
}
}
38 changes: 38 additions & 0 deletions src/MessagePipe.Nats/NatsPublisher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using AlterNats;

namespace MessagePipe.Nats;

public sealed class NatsPublisher<TKey, TMessage> : IDistributedPublisher<TKey, TMessage>
{
readonly NatsConnectionFactory connectionFactory;

public NatsPublisher(
NatsConnectionFactory connectionFactory)
{
this.connectionFactory = connectionFactory;
}

public async ValueTask PublishAsync(TKey key, TMessage message, CancellationToken cancellationToken = new CancellationToken())
{
var natsKey = GetNatsKey(key);

if (natsKey == null) throw new ArgumentNullException(nameof(key));

var connection = await connectionFactory.GetConnectionAsync();
await connection.PublishAsync(natsKey.Value, message);
}

NatsKey? GetNatsKey(TKey key)
{
switch (key)
{
case NatsKey natsKey:
return natsKey;
case string s:
return new NatsKey(s);
default:
var k = key?.ToString();
return k != null ? new NatsKey(k) : null;
}
}
}
110 changes: 110 additions & 0 deletions src/MessagePipe.Nats/NatsSubscriber.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using AlterNats;

namespace MessagePipe.Nats;

public sealed class NatsSubscriber<TKey, TMessage> : IDistributedSubscriber<TKey, TMessage>
{
readonly NatsConnectionFactory connectionFactory;
readonly FilterAttachedMessageHandlerFactory messageHandlerFactory;
readonly FilterAttachedAsyncMessageHandlerFactory asyncMessageHandlerFactory;

public NatsSubscriber(
NatsConnectionFactory connectionFactory,
FilterAttachedMessageHandlerFactory messageHandlerFactory,
FilterAttachedAsyncMessageHandlerFactory asyncMessageHandlerFactory)
{
this.connectionFactory = connectionFactory;
this.messageHandlerFactory = messageHandlerFactory;
this.asyncMessageHandlerFactory = asyncMessageHandlerFactory;
}

public ValueTask<IAsyncDisposable> SubscribeAsync(
TKey key,
IMessageHandler<TMessage> handler,
CancellationToken cancellationToken = new())
{
return SubscribeAsync(key, handler, Array.Empty<MessageHandlerFilter<TMessage>>(), cancellationToken);
}

public async ValueTask<IAsyncDisposable> SubscribeAsync(
TKey key,
IMessageHandler<TMessage> handler,
MessageHandlerFilter<TMessage>[] filters,
CancellationToken cancellationToken = new())
{
var subject = GetSubjectString(key);

if (subject == null) throw new ArgumentException(nameof(key));

handler = messageHandlerFactory.CreateMessageHandler(handler, filters); // with filter

var connection = await connectionFactory.GetConnectionAsync();

var s = await connection.SubscribeAsync<TMessage>(subject, data =>
{
handler.Handle(data);
});

return new Subscription(s);
}

public ValueTask<IAsyncDisposable> SubscribeAsync(
TKey key,
IAsyncMessageHandler<TMessage> handler,
CancellationToken cancellationToken = new())
{
return SubscribeAsync(key, handler, Array.Empty<AsyncMessageHandlerFilter<TMessage>>(), cancellationToken);
}

public async ValueTask<IAsyncDisposable> SubscribeAsync(
TKey key,
IAsyncMessageHandler<TMessage> handler,
AsyncMessageHandlerFilter<TMessage>[] filters,
CancellationToken cancellationToken = new())
{
var subject = GetSubjectString(key);

if (subject == null) throw new ArgumentException(nameof(key));

handler = asyncMessageHandlerFactory.CreateAsyncMessageHandler(handler, filters); // with filter

var connection = await connectionFactory.GetConnectionAsync();

var s = await connection.SubscribeAsync<TMessage>(subject, async data =>
{
await handler.HandleAsync(data, CancellationToken.None).ConfigureAwait(false);
});

return new Subscription(s);
}

sealed class Subscription : IAsyncDisposable
{
readonly IDisposable disposable;

public Subscription(IDisposable disposable)
{
this.disposable = disposable;
}

public ValueTask DisposeAsync()
{
disposable.Dispose();

return ValueTask.CompletedTask;
}
}

string? GetSubjectString(TKey key)
{
switch (key)
{
case NatsKey natsKey:
return natsKey.Key;
case string s:
return s;
default:
return key?.ToString();
}
}
}
32 changes: 32 additions & 0 deletions src/MessagePipe.Nats/ServiceCollectionNatsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.Extensions.DependencyInjection;

namespace MessagePipe.Nats;

public static class ServiceCollectionNatsExtensions
{
public static IServiceCollection AddMessagePipeNats(this IServiceCollection services, NatsConnectionFactory connectionFactory)
{
return AddMessagePipeNats(services, connectionFactory, _ => { });
}

public static IServiceCollection AddMessagePipeNats(this IServiceCollection services, NatsConnectionFactory connectionFactory, Action<MessagePipeNatsOptions> configure)
{
var options = new MessagePipeNatsOptions(connectionFactory);
configure(options);
services.AddSingleton(options);
services.AddSingleton(options.NatsConnectionFactory);

services.Add(typeof(IDistributedPublisher<,>), typeof(NatsPublisher<,>), InstanceLifetime.Singleton);
services.Add(typeof(IDistributedSubscriber<,>), typeof(NatsSubscriber<,>), InstanceLifetime.Singleton);

return services;
}

static void Add(this IServiceCollection services, Type serviceType, Type implementationType, InstanceLifetime scope)
{
var lifetime = (scope == InstanceLifetime.Scoped) ? ServiceLifetime.Scoped
: (scope == InstanceLifetime.Singleton) ? ServiceLifetime.Singleton
: ServiceLifetime.Transient;
services.Add(new ServiceDescriptor(serviceType, implementationType, lifetime));
}
}
6 changes: 3 additions & 3 deletions src/MessagePipe.Redis/MessagePipe.Redis.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net5.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net5.0;net6.0</TargetFrameworks>
<Configurations>Debug;Release;WinBenchmark</Configurations>

<!-- NuGet Packaging -->
@@ -15,8 +15,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MessagePack" Version="2.2.85" />
<PackageReference Include="StackExchange.Redis" Version="2.2.4" />
<PackageReference Include="MessagePack" Version="2.3.85" />
<PackageReference Include="StackExchange.Redis" Version="2.5.61" />
</ItemGroup>

<ItemGroup>
Original file line number Diff line number Diff line change
@@ -168,7 +168,7 @@ public void Handle(TMessage message)

void IUniTaskSource.GetResult(short token) => GetResult(token);
public UniTaskStatus UnsafeGetStatus() => core.UnsafeGetStatus();
public UniTaskStatus GetStatus(short token)
public /*replaced*/ UniTaskStatus GetStatus(short token)
{
return core.GetStatus(token);
}
@@ -249,7 +249,7 @@ public void Handle(TMessage message)

void IUniTaskSource.GetResult(short token) => GetResult(token);
public UniTaskStatus UnsafeGetStatus() => core.UnsafeGetStatus();
public UniTaskStatus GetStatus(short token)
public /*replaced*/ UniTaskStatus GetStatus(short token)
{
return core.GetStatus(token);
}
@@ -331,7 +331,7 @@ public void Handle(TMessage message)

void IUniTaskSource.GetResult(short token) => GetResult(token);
public UniTaskStatus UnsafeGetStatus() => core.UnsafeGetStatus();
public UniTaskStatus GetStatus(short token)
public /*replaced*/ UniTaskStatus GetStatus(short token)
{
return core.GetStatus(token);
}
@@ -422,7 +422,7 @@ public UniTask HandleAsync(TMessage message, CancellationToken cancellationToken

void IUniTaskSource.GetResult(short token) => GetResult(token);
public UniTaskStatus UnsafeGetStatus() => core.UnsafeGetStatus();
public UniTaskStatus GetStatus(short token)
public /*replaced*/ UniTaskStatus GetStatus(short token)
{
return core.GetStatus(token);
}
@@ -512,7 +512,7 @@ public UniTask HandleAsync(TMessage message, CancellationToken cancellationToken

void IUniTaskSource.GetResult(short token) => GetResult(token);
public UniTaskStatus UnsafeGetStatus() => core.UnsafeGetStatus();
public UniTaskStatus GetStatus(short token)
public /*replaced*/ UniTaskStatus GetStatus(short token)
{
return core.GetStatus(token);
}
@@ -604,7 +604,7 @@ public UniTask HandleAsync(TMessage message, CancellationToken cancellationToken

void IUniTaskSource.GetResult(short token) => GetResult(token);
public UniTaskStatus UnsafeGetStatus() => core.UnsafeGetStatus();
public UniTaskStatus GetStatus(short token)
public /*replaced*/ UniTaskStatus GetStatus(short token)
{
return core.GetStatus(token);
}
8 changes: 4 additions & 4 deletions src/MessagePipe/MessagePipe.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net5.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net5.0;net6.0</TargetFrameworks>
<WarningsAsErrors>true</WarningsAsErrors>
<Configurations>Debug;Release;WinBenchmark</Configurations>

@@ -16,11 +16,11 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
</ItemGroup>
<ItemGroup Condition="$(TargetFramework) == 'netstandard2.0'">
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
<PackageReference Include="System.Threading.Channels" Version="5.0.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="6.0.0" />
<PackageReference Include="System.Threading.Channels" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
Original file line number Diff line number Diff line change
@@ -6,16 +6,16 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2">
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.XUnit" Version="1.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.XUnit" Version="1.1.1" />

<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="FluentAssertions" Version="6.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
16 changes: 8 additions & 8 deletions tests/MessagePipe.Benchmark/MessagePipe.Benchmark.csproj
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<Configurations>Debug;Release;WinBenchmark</Configurations>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
@@ -14,14 +14,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
<PackageReference Include="Easy.MessageHub" Version="5.0.0" />
<PackageReference Include="MediatR" Version="9.0.0" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
<PackageReference Include="Microsoft.Toolkit.Mvvm" Version="7.0.1" />
<PackageReference Include="Prism.Core" Version="8.0.0.1909" />
<PackageReference Include="PubSub" Version="4.0.1" />
<PackageReference Include="MediatR" Version="10.0.1" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="10.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Toolkit.Mvvm" Version="7.1.2" />
<PackageReference Include="Prism.Core" Version="8.1.97" />
<PackageReference Include="PubSub" Version="4.0.2" />
<PackageReference Include="System.Reactive" Version="5.0.0" />
</ItemGroup>
<ItemGroup Condition="$(Configuration) == 'WinBenchmark'">
Original file line number Diff line number Diff line change
@@ -2,12 +2,12 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
<Configurations>Debug;Release;WinBenchmark</Configurations>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="FluentAssertions" Version="6.6.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
30 changes: 30 additions & 0 deletions tests/MessagePipe.Nats.Tests/MessagePipe.Nats.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AlterNats" Version="0.0.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\MessagePipe.Nats\MessagePipe.Nats.csproj" />
<ProjectReference Include="..\..\src\MessagePipe\MessagePipe.csproj" />
</ItemGroup>

</Project>
138 changes: 138 additions & 0 deletions tests/MessagePipe.Nats.Tests/NatsPubSub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AlterNats;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace MessagePipe.Nats.Tests;

public class NatsPubSub
{
[Theory]
[MemberData(nameof(BasicTestData))]
public async Task Basic<T>(IEnumerable<T> items)
{
var provider = TestHelper.BuildNatsServiceProvider();
var p = provider.GetRequiredService<IDistributedPublisher<NatsKey, T>>();
var s = provider.GetRequiredService<IDistributedSubscriber<NatsKey, T>>();

AutoResetEvent autoResetEvent = new AutoResetEvent(false);
autoResetEvent.Reset();
List<T> results = new();
var natsKey = new NatsKey(Guid.NewGuid().ToString("N"));

await using var d = await s.SubscribeAsync(natsKey, x =>
{
results.Add(x);

if (results.Count == items.Count())
autoResetEvent.Set();
});

foreach (var item in items)
{
await p.PublishAsync(natsKey, item);
}

var waitResult = autoResetEvent.WaitOne(5000);

Assert.True(waitResult, "Timeout");
Assert.Equal(items.ToArray(), results.ToArray());
}

static readonly int[] seed1 = { 24, 45, 99, 41, 98, 7, 81, 8, 26, 56 };

static object[][] BasicTestData()
{
return new[]
{
new object[] { seed1 },
new object[] { seed1.Select(x => $"Test:{x}") },
new object[] { seed1.Select(x => new SampleClass(x, $"Name{x}")) }
};
}
}

public class SampleClass : IEquatable<SampleClass>
{
public int Id { get; set; }
public string Name { get; set; }

public SampleClass(int id, string name)
{
Id = id;
Name = name;
}

public bool Equals(SampleClass? other)
{
if (ReferenceEquals(null, other))
{
return false;
}

if (ReferenceEquals(this, other))
{
return true;
}

return Id == other.Id && Name == other.Name;
}

public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}

if (ReferenceEquals(this, obj))
{
return true;
}

if (obj.GetType() != GetType())
{
return false;
}

return Equals((SampleClass)obj);
}

public override int GetHashCode()
{
return HashCode.Combine(Id, Name);
}

public override string ToString()
{
return $"{Id}-{Name}";
}
}

public static class TestHelper
{
public static IServiceProvider BuildNatsServiceProvider(INatsSerializer? serializer = default)
{
var sc = new ServiceCollection();
sc.AddMessagePipe();

if (serializer == default)
{
sc.AddMessagePipeNats(new NatsConnectionFactory());
}
else
{
sc.AddMessagePipeNats(new NatsConnectionFactory(NatsOptions.Default with
{
Serializer = serializer
}));
}

return sc.BuildServiceProvider();
}
}
4 changes: 4 additions & 0 deletions tests/MessagePipe.Nats.Tests/ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MessagePipe.Nats.Tests
===
This test uses local nats-server.
You should up `docker-compose up`(on sln directory) before test run.
14 changes: 7 additions & 7 deletions tests/MessagePipe.Redis.Tests/MessagePipe.Redis.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
<Configurations>Debug;Release;WinBenchmark</Configurations>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="System.Linq.Async" Version="5.0.0" />
<PackageReference Include="StackExchange.Redis" Version="2.2.4" />
<PackageReference Include="FluentAssertions" Version="6.6.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
<PackageReference Include="StackExchange.Redis" Version="2.5.61" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.3">
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
12 changes: 6 additions & 6 deletions tests/MessagePipe.Tests/MessagePipe.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<IsPackable>false</IsPackable>
<Configurations>Debug;Release;WinBenchmark</Configurations>
<SignAssembly>true</SignAssembly>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="System.Linq.Async" Version="5.0.0" />
<PackageReference Include="FluentAssertions" Version="6.6.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.3">
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
2 changes: 1 addition & 1 deletion tests/MessagePipe.Tests/RequestAllHandlerAsyncTest.cs
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@ public void TestCancellation()

source.Cancel();
var pongs = pingHandler.Awaiting(x => x.InvokeAllAsync(new Ping("hoge"), token)).Should()
.Throw<OperationCanceledException>();
.ThrowAsync<OperationCanceledException>();

}

4 changes: 2 additions & 2 deletions tests/MessagePipe.Tests/RequestAllHandlerTest.cs
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ public void TestHandling()

var pongs = pingHandler.InvokeAll(new Ping("myon!"));

pongs.Should().BeEquivalentTo(new Pong("myon!"), new Pong("myon!myon!"));
pongs.Should().BeEquivalentTo(new[] { new Pong("myon!"), new Pong("myon!myon!")});

}
[Fact]
@@ -30,7 +30,7 @@ public void TestLazyHandling()

var pongs = pingHandler.InvokeAllLazy(new Ping("myon!"));

pongs.Should().BeEquivalentTo(new Pong("myon!"), new Pong("myon!myon!"));
pongs.Should().BeEquivalentTo(new[] { new Pong("myon!"), new Pong("myon!myon!")});

}
[Fact]
2 changes: 1 addition & 1 deletion tests/MessagePipe.Tests/RequestHandlerAsyncTest.cs
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ public void TestCancellation()
var token = source.Token;

source.Cancel();
pingHandler.Awaiting(x => x.InvokeAsync(new Ping("hoge"), token)).Should().Throw<OperationCanceledException>();
pingHandler.Awaiting(x => x.InvokeAsync(new Ping("hoge"), token)).Should().ThrowAsync<OperationCanceledException>();

}

4 changes: 2 additions & 2 deletions tools/PostBuildUtility/PostBuildUtility.csproj
Original file line number Diff line number Diff line change
@@ -2,12 +2,12 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<Configurations>Debug;Release;WinBenchmark</Configurations>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ConsoleAppFramework" Version="3.3.0" />
<PackageReference Include="ConsoleAppFramework" Version="4.0.6" />
</ItemGroup>

</Project>