Skip to content
Merged
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
59 changes: 59 additions & 0 deletions PowerKit.Tests/ProgressMuxerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using FluentAssertions;
using Gress;
using PowerKit;
using Xunit;

namespace PowerKit.Tests;

public class ProgressMuxerTests
{
[Fact]
public void CreateInput_Test()
{
// Arrange
var progress = new ProgressCollector<double>();
var muxer = new ProgressMuxer(progress);
var input = muxer.CreateInput();

// Act
input.Report(0.5);
input.Report(1.0);

// Assert
progress.GetValues().Should().Equal(0.5, 1.0);
}

[Fact]
public void CreateInput_Weight_Test()
{
// Arrange
var progress = new ProgressCollector<double>();
var muxer = new ProgressMuxer(progress);
var input = muxer.CreateInput(weight: 0.5);

// Act
input.Report(0.5);
input.Report(1.0);

// Assert
progress.GetValues().Should().Equal(0.25, 0.5);
}

[Fact]
public void CreateInput_MultipleInputs_Test()
{
// Arrange
var progress = new ProgressCollector<double>();
var muxer = new ProgressMuxer(progress);
var input1 = muxer.CreateInput(weight: 0.6);
var input2 = muxer.CreateInput(weight: 0.4);

// Act
input1.Report(1.0);
input2.Report(1.0);

// Assert
var values = progress.GetValues();
values[^1].Should().BeApproximately(1.0, 1e-10);
}
}
20 changes: 20 additions & 0 deletions PowerKit/DelegateProgress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#if NET40_OR_GREATER || NETSTANDARD || NET
#nullable enable
using System;
using System.Diagnostics.CodeAnalysis;

namespace PowerKit;

/// <summary>
/// Provides a lightweight <see cref="IProgress{T}" /> implementation that delegates
/// progress reporting to an action.
/// </summary>
#if !POWERKIT_INCLUDE_COVERAGE
[ExcludeFromCodeCoverage]
#endif
internal sealed class DelegateProgress<T>(Action<T> report) : IProgress<T>
{
/// <inheritdoc />
public void Report(T value) => report(value);
}
#endif
43 changes: 43 additions & 0 deletions PowerKit/ProgressMuxer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#if NET40_OR_GREATER || NETSTANDARD || NET
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;

namespace PowerKit;

/// <summary>
/// Multiplexes multiple <see cref="IProgress{T}" /> reporters into a single output reporter,
/// combining weighted progress values from multiple sources.
/// </summary>
#if !POWERKIT_INCLUDE_COVERAGE
[ExcludeFromCodeCoverage]
#endif
internal class ProgressMuxer(IProgress<double> output)
{
private readonly Lock _lock = new();
private readonly Dictionary<int, double> _splitTotals = new();

private int _splitCount;

/// <summary>
/// Creates a new progress input with the specified weight.
/// Progress reported to this input is multiplied by <paramref name="weight" />
/// and combined with all other inputs before being forwarded to the output.
/// </summary>
public IProgress<double> CreateInput(double weight = 1)
{
var index = Interlocked.Increment(ref _splitCount) - 1;
return new DelegateProgress<double>(p =>
{
using (_lock.EnterScope())
{
_splitTotals[index] = weight * p;
output.Report(_splitTotals.Values.Sum());
}
Comment thread
Tyrrrz marked this conversation as resolved.
});
}
}
#endif