-
Notifications
You must be signed in to change notification settings - Fork 1
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
ef72f05
commit 3983461
Showing
5 changed files
with
187 additions
and
1 deletion.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
messaging/Squidex.Messaging.Tests/Internal/ExpectationHandler.cs
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,52 @@ | ||
// ========================================================================== | ||
// Squidex Headless CMS | ||
// ========================================================================== | ||
// Copyright (c) Squidex UG (haftungsbeschraenkt) | ||
// All rights reserved. Licensed under the MIT license. | ||
// ========================================================================== | ||
|
||
using System.Collections.Concurrent; | ||
|
||
namespace Squidex.Messaging.Internal; | ||
|
||
internal sealed class ExpectationHandler : IMessageHandler<TestMessage> | ||
{ | ||
private readonly int expectCount; | ||
private readonly Guid expectedId; | ||
private readonly TaskCompletionSource tcs = new TaskCompletionSource(); | ||
private readonly ConcurrentBag<int> messagesReceives = []; | ||
private readonly CancellationTokenSource cts; | ||
|
||
public Task Completion => tcs.Task; | ||
|
||
public IEnumerable<int> MessagesReceives => messagesReceives.OrderBy(x => x); | ||
|
||
public ExpectationHandler(int expectCount, Guid expectedId) | ||
{ | ||
this.expectCount = expectCount; | ||
this.expectedId = expectedId; | ||
|
||
cts = new CancellationTokenSource(30 * 1000); | ||
|
||
cts.Token.Register(() => | ||
{ | ||
_ = tcs.TrySetResult(); | ||
}); | ||
} | ||
|
||
public Task HandleAsync(TestMessage message, | ||
CancellationToken ct) | ||
{ | ||
if (message.TestId == expectedId) | ||
{ | ||
messagesReceives.Add(message.Value); | ||
} | ||
|
||
if (expectCount == messagesReceives.Count) | ||
{ | ||
tcs.TrySetResult(); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
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,83 @@ | ||
// ========================================================================== | ||
// Squidex Headless CMS | ||
// ========================================================================== | ||
// Copyright (c) Squidex UG (haftungsbeschraenkt) | ||
// All rights reserved. Licensed under the MIT license. | ||
// ========================================================================== | ||
|
||
using Squidex.Hosting; | ||
|
||
#pragma warning disable MA0048 // File name must match type name | ||
|
||
namespace Squidex.Messaging.Internal; | ||
|
||
internal static class Extensions | ||
{ | ||
public static async Task<Provider<T>> CreateAsync<T>(this IServiceProvider serviceProvider) where T : class | ||
{ | ||
var provider = new Provider<T>(serviceProvider); | ||
|
||
await provider.StartAsync(); | ||
|
||
return provider; | ||
} | ||
|
||
public static MessagingBuilder Configure<T>(this MessagingBuilder builder, Action<T>? configure) where T : class | ||
{ | ||
if (configure != null) | ||
{ | ||
builder.Services.ConfigureOptional(configure); | ||
} | ||
|
||
return builder; | ||
} | ||
|
||
public static MessagingBuilder AddHandler(this MessagingBuilder builder, IMessageHandler? handler) | ||
{ | ||
if (handler != null) | ||
{ | ||
builder.Services.AddSingleton(handler); | ||
} | ||
|
||
return builder; | ||
} | ||
|
||
public static MessagingBuilder AddOverride(this MessagingBuilder builder, Action<MessagingBuilder>? configure) | ||
{ | ||
configure?.Invoke(builder); | ||
return builder; | ||
} | ||
} | ||
|
||
public sealed class Provider<T>(IServiceProvider serviceProvider) : IAsyncDisposable where T : class | ||
{ | ||
public T Sut => serviceProvider.GetRequiredService<T>(); | ||
|
||
public async Task StartAsync() | ||
{ | ||
foreach (var initializable in serviceProvider.GetRequiredService<IEnumerable<IInitializable>>()) | ||
{ | ||
await initializable.InitializeAsync(default); | ||
} | ||
|
||
foreach (var process in serviceProvider.GetRequiredService<IEnumerable<IBackgroundProcess>>()) | ||
{ | ||
await process.StartAsync(default); | ||
} | ||
} | ||
|
||
public async ValueTask DisposeAsync() | ||
{ | ||
foreach (var process in serviceProvider.GetRequiredService<IEnumerable<IBackgroundProcess>>()) | ||
{ | ||
await process.StopAsync(default); | ||
} | ||
|
||
foreach (var initializable in serviceProvider.GetRequiredService<IEnumerable<IInitializable>>()) | ||
{ | ||
await initializable.ReleaseAsync(default); | ||
} | ||
|
||
(serviceProvider as IDisposable)?.Dispose(); | ||
} | ||
} |
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,36 @@ | ||
// ========================================================================== | ||
// Squidex Headless CMS | ||
// ========================================================================== | ||
// Copyright (c) Squidex UG (haftungsbeschraenkt) | ||
// All rights reserved. Licensed under the MIT license. | ||
// ========================================================================== | ||
|
||
namespace Squidex.Messaging.Internal; | ||
|
||
#pragma warning disable MA0048 // File name must match type name | ||
|
||
public sealed class TestValue(string value) | ||
{ | ||
public string Value { get; } = value; | ||
} | ||
|
||
public sealed class TestMessage : BaseMessage | ||
{ | ||
public int Value { get; } | ||
|
||
public TestMessage(Guid testId, int value) | ||
: base(testId) | ||
{ | ||
Value = value; | ||
} | ||
} | ||
|
||
public abstract class BaseMessage | ||
{ | ||
public Guid TestId { get; } | ||
|
||
protected BaseMessage(Guid testId) | ||
{ | ||
TestId = testId; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// ========================================================================== | ||
// Squidex Headless CMS | ||
// ========================================================================== | ||
// Copyright (c) Squidex UG (haftungsbeschraenkt) | ||
// All rights reserved. Licensed under the MIT license. | ||
// ========================================================================== | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Squidex.Messaging; | ||
|
||
public sealed class MessagingBuilder(IServiceCollection services) | ||
{ | ||
public IServiceCollection Services { get; } = services; | ||
} |