Skip to content
Open
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions Api.Gateway/Api.Gateway.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Ocelot" Version="24.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AspireApp1\AspireApp1.ServiceDefaults\AspireApp.ServiceDefaults.csproj" />
</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions Api.Gateway/LoadBalancer/WeightedRandom.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Ocelot.LoadBalancer.Interfaces;
using Ocelot.Responses;
using Ocelot.Values;

namespace Api.Gateway.LoadBalancer;

/// <summary>
/// Балансировка случайным образом с весами
/// </summary>
public class WeightedRandom : ILoadBalancer
{
private readonly Func<Task<List<Service>>> _services = null!;
private static readonly object _locker = new();

private readonly int[] _values = null!;

public string Type => nameof(WeightedRandom);
public WeightedRandom(Func<Task<List<Service>>> services, IConfiguration configuration)
{
_services = services;
var frequencies = configuration.GetSection("LoadBalancer:WeightedRandom:Weights").Get<int[]>();
if (frequencies == null || frequencies.Length == 0)
throw new InvalidOperationException("LoadBalancer:WeightedRandom:Weights is empty or null. Add weights to configuration");
_values = [.. Enumerable.Range(0, frequencies.Length).Zip(frequencies, (val, freq) => Enumerable.Repeat(val, freq)).SelectMany(x => x)];
}
public async Task<Response<ServiceHostAndPort>> LeaseAsync(HttpContext httpContext)
{
var services = await _services.Invoke();
lock (_locker)
{
Random.Shared.Shuffle(_values);
return new OkResponse<ServiceHostAndPort>(services[_values.First()].HostAndPort);
}
}
public void Release(ServiceHostAndPort hostAndPort) { }
}
26 changes: 26 additions & 0 deletions Api.Gateway/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Api.Gateway.LoadBalancer;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;

var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();
builder.Services.AddServiceDiscovery();
builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);
builder.Services.AddOcelot()
.AddCustomLoadBalancer<WeightedRandom>((sp, _, discoveryProvider) =>
{
var configuration = sp.GetRequiredService<IConfiguration>();
return new WeightedRandom(discoveryProvider.GetAsync, configuration);
});
builder.Services.AddCors(options => options.AddDefaultPolicy(policy =>
{
policy.WithOrigins("http://localhost:5127")
.WithMethods("GET")
.AllowAnyHeader();
}));

var app = builder.Build();
app.UseCors();
await app.UseOcelot();
app.Run();
38 changes: 38 additions & 0 deletions Api.Gateway/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:25431",
"sslPort": 44359
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5190",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7034;http://localhost:5190",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
8 changes: 8 additions & 0 deletions Api.Gateway/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
14 changes: 14 additions & 0 deletions Api.Gateway/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"LoadBalancer": {
"WeightedRandom": {
"Weights": [1, 2, 3, 2, 1]
}
}
}
35 changes: 35 additions & 0 deletions Api.Gateway/ocelot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"Routes": [
{
"UpstreamPathTemplate": "/program-project",
"UpstreamHttpMethod": [ "GET" ],
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 4440
},
{
"Host": "localhost",
"Port": 4441
},
{
"Host": "localhost",
"Port": 4442
},
{
"Host": "localhost",
"Port": 4443
},
{
"Host": "localhost",
"Port": 4444
}
],
"DownstreamPathTemplate": "/program-project",
"DownstreamScheme": "https",
"LoadBalancerOptions": {
"Type": "WeightedRandom"
}
}
]
}
33 changes: 33 additions & 0 deletions AspireApp1/AspireApp1.AppHost.Tests/AspireApp.AppHost.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<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="Aspire.Hosting.Testing" Version="9.5.0" />
<PackageReference Include="coverlet.collector" Version="6.0.4" />
<PackageReference Include="MartinCostello.Logging.XUnit" Version="0.7.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AspireApp1.AppHost\AspireApp.AppHost.csproj" />
<ProjectReference Include="..\..\ServiceApi\ServiceApi.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="System.Net" />
<Using Include="Microsoft.Extensions.DependencyInjection" />
<Using Include="Aspire.Hosting.ApplicationModel" />
<Using Include="Aspire.Hosting.Testing" />
<Using Include="Xunit" />
</ItemGroup>

</Project>
118 changes: 118 additions & 0 deletions AspireApp1/AspireApp1.AppHost.Tests/IntegrationTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using Aspire.Hosting;
using Microsoft.Extensions.Logging;
using ServiceApi.Entities;
using System.Text.Json;
using Xunit.Abstractions;

namespace AspireApp.AppHost.Tests;

/// <summary>
/// Интеграционные тесты для проверки микросервисного пайплайна
/// </summary>
/// <param name="output">Служба журналирования юнит-тестов</param>
public class IntegrationTest(ITestOutputHelper output) : IAsyncLifetime
{
private DistributedApplication? _app;
private HttpClient? _gatewayClient;
private HttpClient? _fileClient;

/// <inheritdoc/>
public async Task InitializeAsync()
{
var cancellationToken = CancellationToken.None;
IDistributedApplicationTestingBuilder builder = await DistributedApplicationTestingBuilder.CreateAsync<Projects.AspireApp_AppHost>(cancellationToken);
builder.Configuration["DcpPublisher:RandomizePorts"] = "false";
builder.Services.AddLogging(logging =>
{
logging.AddXUnit(output);
logging.SetMinimumLevel(LogLevel.Debug);
logging.AddFilter("Aspire.Hosting.Dcp", LogLevel.Debug);
logging.AddFilter("Aspire.Hosting", LogLevel.Debug);
});
_app = await builder.BuildAsync(cancellationToken);
await _app.StartAsync(cancellationToken);
_gatewayClient = _app.CreateHttpClient("api-gateway", "http");
_fileClient = _app.CreateHttpClient("fileservice", "http");
}

/// <summary>
/// Проверяет, что вызов гейтвея:
/// <list type="bullet">
/// <item><description>В ответ отправляет сгенерированный программный проект</description></item>
/// <item><description>Сериализует программный проект в S3 хранилище</description></item>
/// <item><description>Данные в ответе гейтвея и в S3 идентичны</description></item>
/// </list>
/// </summary>
[Fact]
public async Task GatewayCall_StoresProgramProjectInS3()
{
var id = Random.Shared.Next(1, 1000);
using var gatewayResponse = await _gatewayClient!.GetAsync($"/program-project?id={id}");
var apiProject = JsonSerializer.Deserialize<ProgramProject>(await gatewayResponse.Content.ReadAsStringAsync());

await Task.Delay(5000);
using var listResponse = await _fileClient!.GetAsync("/api/s3");
var fileList = JsonSerializer.Deserialize<List<string>>(await listResponse.Content.ReadAsStringAsync());
using var s3Response = await _fileClient!.GetAsync($"/api/s3/programproject_{id}.json");
var s3Project = JsonSerializer.Deserialize<ProgramProject>(await s3Response.Content.ReadAsStringAsync());

Assert.NotNull(fileList);
Assert.Single(fileList);
Assert.NotNull(apiProject);
Assert.NotNull(s3Project);
Assert.Equal(id, s3Project.Id);
Assert.Equivalent(apiProject, s3Project);
}

/// <summary>
/// Проверяет, что после генерации двух разных программных проектов
/// в S3 хранилище появляются два разных файла
/// </summary>
[Fact]
public async Task TwoDifferentIds_ProduceTwoFilesInS3()
{
var firstId = Random.Shared.Next(1, 500);
var secondId = Random.Shared.Next(501, 1000);

await _gatewayClient!.GetAsync($"/program-project?id={firstId}");
await _gatewayClient!.GetAsync($"/program-project?id={secondId}");

await Task.Delay(5000);
using var listResponse = await _fileClient!.GetAsync("/api/s3");
var fileList = JsonSerializer.Deserialize<List<string>>(await listResponse.Content.ReadAsStringAsync());

Assert.NotNull(fileList);
Assert.Equal(2, fileList.Count);
Assert.Contains($"programproject_{firstId}.json", fileList);
Assert.Contains($"programproject_{secondId}.json", fileList);
}

/// <summary>
/// Проверяет, что повторный запрос того же программного проекта
/// обслуживается из кэша и не приводит к дублирующей отправке в S3
/// </summary>
[Fact]
public async Task RepeatedRequest_DoesNotDuplicateFileInS3()
Comment on lines +90 to +95
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вроде этим тестом мы можем утверждать только то, что в объектном хранилище в итоге находится один объект с ключом programproject_{id}.json. Но тест не доказывает, что при повторных запросах не происходит отправка в брокер сообщений и запись в объектное хранилище с перезаписью существующего объекта
Можно просто уточнить комментарий к тесту, чтобы он не вводил в заблуждение

{
var id = Random.Shared.Next(1, 1000);

await _gatewayClient!.GetAsync($"/program-project?id={id}");
await _gatewayClient!.GetAsync($"/program-project?id={id}");
await _gatewayClient!.GetAsync($"/program-project?id={id}");

await Task.Delay(5000);
using var listResponse = await _fileClient!.GetAsync("/api/s3");
var fileList = JsonSerializer.Deserialize<List<string>>(await listResponse.Content.ReadAsStringAsync());

Assert.NotNull(fileList);
Assert.Single(fileList);
Assert.Equal($"programproject_{id}.json", fileList[0]);
}

/// <inheritdoc/>
public async Task DisposeAsync()
{
await _app!.StopAsync();
await _app.DisposeAsync();
}
}
33 changes: 33 additions & 0 deletions AspireApp1/AspireApp1.AppHost/AspireApp.AppHost.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<Sdk Name="Aspire.AppHost.Sdk" Version="9.5.2" />

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
<UserSecretsId>af866732-ee76-4a9f-b28a-2e8d52f5e153</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.5.2" />
<PackageReference Include="Aspire.Hosting.Redis" Version="9.5.2" />
<PackageReference Include="LocalStack.Aspire.Hosting" Version="9.5.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Api.Gateway\Api.Gateway.csproj" />
<ProjectReference Include="..\..\Client.Wasm\Client.Wasm.csproj" />
<ProjectReference Include="..\..\FileService\FileService.csproj" />
<ProjectReference Include="..\..\ServiceApi\ServiceApi.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="CloudFormation\programproject-template-sqs-s3.yaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Loading