Skip to content

Commit

Permalink
Add missing types.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Mar 24, 2024
1 parent ef72f05 commit 3983461
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 1 deletion.
52 changes: 52 additions & 0 deletions messaging/Squidex.Messaging.Tests/Internal/ExpectationHandler.cs
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;
}
}
83 changes: 83 additions & 0 deletions messaging/Squidex.Messaging.Tests/Internal/Extensions.cs
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();
}
}
36 changes: 36 additions & 0 deletions messaging/Squidex.Messaging.Tests/Internal/Types.cs
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Task StartAsync(
}

public Task UpdateAliveAsync(
CancellationToken ct)
CancellationToken ct = default)
{
KeyValuePair<(string Group, string Key), (SerializedObject Value, TimeSpan Expires)>[] localEntries;

Expand Down
15 changes: 15 additions & 0 deletions messaging/Squidex.Messaging/MessagingBuilder.cs
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;
}

0 comments on commit 3983461

Please sign in to comment.