-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c95761
commit 040467d
Showing
18 changed files
with
184 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="xunit" Version="2.5.3" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Core.ServiceMesh\Core.ServiceMesh.csproj" /> | ||
<ProjectReference Include="..\SampleInterfaces\SampleInterfaces.csproj" /> | ||
<ProjectReference Include="..\Core.ServiceMesh.SourceGen\Core.ServiceMesh.SourceGen.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" /> | ||
</ItemGroup> | ||
|
||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System.Diagnostics; | ||
using System.Numerics; | ||
using Core.ServiceMesh.Abstractions; | ||
using Microsoft.Extensions.Logging; | ||
using SampleInterfaces; | ||
|
||
namespace Core.ServiceMesh.Tests; | ||
|
||
[ServiceMesh("someservice")] | ||
public class SomeService(ILogger<SomeService> logger) : ISomeService | ||
{ | ||
public async ValueTask<string> GetSomeString(int a, string b) | ||
{ | ||
logger.LogInformation("test log"); | ||
Activity.Current?.AddEvent(new ActivityEvent("test 1")); | ||
await Task.Delay(100); | ||
Activity.Current?.AddEvent(new ActivityEvent("test 2")); | ||
return b + " " + a; | ||
} | ||
|
||
public async ValueTask CreateSomeObject() | ||
{ | ||
await Task.Delay(100); | ||
logger.LogInformation(nameof(CreateSomeObject)); | ||
} | ||
|
||
public async ValueTask<T> GenericAdd<T>(T a, T b) where T : INumber<T> | ||
{ | ||
await Task.Delay(100); | ||
return a + b; | ||
} | ||
|
||
public ValueTask<SampleResponse> Sample(SampleRequest request) | ||
{ | ||
return ValueTask.FromResult(new SampleResponse(request.Name + " " + request.Amount)); | ||
} | ||
|
||
public async IAsyncEnumerable<SampleResponse> StreamingResponse(SampleRequest request) | ||
{ | ||
await Task.Delay(100); | ||
yield return new SampleResponse("a"); | ||
await Task.Delay(100); | ||
yield return new SampleResponse("b"); | ||
await Task.Delay(100); | ||
yield return new SampleResponse("c"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using Core.ServiceMesh.Abstractions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using SampleInterfaces; | ||
using Xunit.Abstractions; | ||
|
||
namespace Core.ServiceMesh.Tests; | ||
|
||
[CollectionDefinition("UnitTest1", DisableParallelization = true)] | ||
public class UnitTest1(ITestOutputHelper logger) : IAsyncLifetime | ||
{ | ||
private readonly CancellationTokenSource _cancellation = new(); | ||
private IServiceMesh _mesh; | ||
private IServiceProvider _serviceProvider; | ||
private BackgroundService _worker; | ||
|
||
public async Task InitializeAsync() | ||
{ | ||
var serviceCollection = new ServiceCollection(); | ||
serviceCollection.AddLogging(); | ||
serviceCollection.AddServiceMesh(options => | ||
{ | ||
options.Prefix = Guid.NewGuid().ToString("N"); | ||
options.ConfigureNats = opts => opts with | ||
{ | ||
Url = "nats://localhost:4222" | ||
}; | ||
options.ConfigureStream = (name, config) => { config.MaxAge = TimeSpan.FromMinutes(10); }; | ||
options.InterfaceMode = ServiceInterfaceMode.None; | ||
options.Assemblies = [typeof(ISomeService).Assembly, typeof(SomeService).Assembly]; | ||
}); | ||
|
||
_serviceProvider = serviceCollection.BuildServiceProvider(true); | ||
_mesh = _serviceProvider.GetRequiredService<IServiceMesh>(); | ||
|
||
_worker = (BackgroundService)_mesh; | ||
await _worker.StartAsync(_cancellation.Token); | ||
} | ||
|
||
public async Task DisposeAsync() | ||
{ | ||
await _cancellation.CancelAsync(); | ||
|
||
try | ||
{ | ||
await _worker.ExecuteTask!; | ||
} | ||
catch | ||
{ | ||
// NOP | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task Test1() | ||
{ | ||
var someService = _mesh.CreateProxy<ISomeService>(); | ||
|
||
var res = await someService.GenericAdd(2, 4); | ||
|
||
Assert.Equal(6, res); | ||
} | ||
|
||
[Fact] | ||
public async Task Test2() | ||
{ | ||
var someService = _mesh.CreateProxy<ISomeService>(); | ||
|
||
var res = await someService.GenericAdd(6m, 6m); | ||
|
||
Assert.Equal(12m, res); | ||
} | ||
|
||
[Fact] | ||
public async Task Test3() | ||
{ | ||
var someService = _mesh.CreateProxy<ISomeService>(); | ||
|
||
var list = new List<SampleResponse>(); | ||
|
||
await foreach (var r in someService.StreamingResponse(new SampleRequest("Bla", 133.7m))) | ||
list.Add(r); | ||
|
||
Assert.Equal(3, list.Count); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.