< Summary

Information
Class: Elsa.Testing.Shared.Services.SignalManager
Assembly: Elsa.Testing.Shared.Component
File(s): /home/runner/work/elsa-core/elsa-core/src/common/Elsa.Testing.Shared.Component/Services/SignalManager.cs
Line coverage
85%
Covered lines: 18
Uncovered lines: 3
Coverable lines: 21
Total lines: 50
Line coverage: 85.7%
Branch coverage
75%
Covered branches: 3
Total branches: 4
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
WaitAsync()50%2280%
WaitAsync()100%1177.77%
Trigger(...)100%22100%
GetOrCreate(...)100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/common/Elsa.Testing.Shared.Component/Services/SignalManager.cs

#LineLine coverage
 1using System.Collections.Concurrent;
 2
 3namespace Elsa.Testing.Shared.Services;
 4
 5public class SignalManager
 6{
 17    private readonly ConcurrentDictionary<object, TaskCompletionSource<object?>> _signals = new();
 8
 9    public async Task<T> WaitAsync<T>(object signal, int millisecondsTimeout = 60000)
 10    {
 2611        var result = await WaitAsync(signal, millisecondsTimeout);
 12
 2613        if(result is not T typedResult)
 014            throw new InvalidCastException($"Signal '{signal}' was not of type '{typeof(T).Name}'.");
 15
 2616        return typedResult;
 2617    }
 18
 19    public async Task<object?> WaitAsync(object signal, int millisecondsTimeout = 60000)
 20    {
 2621        var taskCompletionSource = GetOrCreate(signal);
 2622        using var cancellationTokenSource = new CancellationTokenSource(millisecondsTimeout);
 23        try
 24        {
 2625            await Task.WhenAny(taskCompletionSource.Task, Task.Delay(millisecondsTimeout, cancellationTokenSource.Token)
 2626            cancellationTokenSource.Token.ThrowIfCancellationRequested();
 2627            _signals.TryRemove(signal, out _);
 2628            return await taskCompletionSource.Task;
 29        }
 030        catch (OperationCanceledException)
 31        {
 032            throw new TimeoutException($"Signal '{signal}' timed out after {millisecondsTimeout} milliseconds.");
 33        }
 2634    }
 35
 36    public void Trigger(object signal, object? result = null)
 37    {
 553938        var taskCompletionSource = GetOrCreate(signal);
 39
 553940        if (taskCompletionSource.Task.IsCompleted)
 525241            return;
 42
 28743        taskCompletionSource.SetResult(result);
 28744    }
 45
 46    private TaskCompletionSource<object?> GetOrCreate(object eventName)
 47    {
 585248        return _signals.GetOrAdd(eventName, _ => new TaskCompletionSource<object?>(TaskCreationOptions.RunContinuationsA
 49    }
 50}