Skip to content

Commit 978cce6

Browse files
authored
Make IPublishingActivityProgressReporter mockable (#8697)
* Make IPublishingActivityProgressReporter mockable Also add a Null reporter to easily use in tests. * Add test * Add instance singleton * fixup test
1 parent 9cbf38d commit 978cce6

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Diagnostics.CodeAnalysis;
5+
6+
namespace Aspire.Hosting.Publishing;
7+
8+
/// <summary>
9+
/// Minimalistic reporter that does nothing.
10+
/// </summary>
11+
[Experimental("ASPIREPUBLISHERS001")]
12+
public sealed class NullPublishingActivityProgressReporter : IPublishingActivityProgressReporter
13+
{
14+
/// <summary>
15+
/// Gets the singleton instance of <see cref="NullPublishingActivityProgressReporter"/>.
16+
/// </summary>
17+
public static NullPublishingActivityProgressReporter Instance { get; } = new NullPublishingActivityProgressReporter();
18+
19+
private NullPublishingActivityProgressReporter()
20+
{
21+
}
22+
23+
/// <inheritdoc/>
24+
public Task<PublishingActivity> CreateActivityAsync(string id, string initialStatusText, bool isPrimary, CancellationToken cancellationToken)
25+
{
26+
var activity = new PublishingActivity(id, isPrimary);
27+
activity.LastStatus = new PublishingActivityStatus
28+
{
29+
Activity = activity,
30+
StatusText = initialStatusText,
31+
IsComplete = false,
32+
IsError = false
33+
};
34+
35+
return Task.FromResult(activity);
36+
}
37+
38+
/// <inheritdoc/>
39+
public Task UpdateActivityStatusAsync(PublishingActivity publishingActivity, Func<PublishingActivityStatus, PublishingActivityStatus> statusUpdate, CancellationToken cancellationToken)
40+
{
41+
return Task.CompletedTask;
42+
}
43+
}

src/Aspire.Hosting/Publishing/PublishingActivityProgressReporter.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ namespace Aspire.Hosting.Publishing;
1414
[Experimental("ASPIREPUBLISHERS001")]
1515
public sealed class PublishingActivity
1616
{
17-
internal PublishingActivity(string id, bool isPrimary = false)
17+
/// <summary>
18+
/// Initializes a new instance of the <see cref="PublishingActivity"/> class.
19+
/// </summary>
20+
/// <param name="id">The unique identifier for the publishing activity.</param>
21+
/// <param name="isPrimary">Indicates whether this activity is the primary activity.</param>
22+
public PublishingActivity(string id, bool isPrimary = false)
1823
{
1924
Id = id;
2025
IsPrimary = isPrimary;
@@ -33,7 +38,7 @@ internal PublishingActivity(string id, bool isPrimary = false)
3338
/// <summary>
3439
/// The status text of the publishing activity.
3540
/// </summary>
36-
public PublishingActivityStatus? LastStatus { get; internal set; }
41+
public PublishingActivityStatus? LastStatus { get; set; }
3742
}
3843

3944
/// <summary>
@@ -79,7 +84,7 @@ public interface IPublishingActivityProgressReporter
7984
/// <returns>The publishing activity</returns>
8085
/// <remarks>
8186
/// When an activity is created the <paramref name="isPrimary"/> flag indicates whether this
82-
/// activity is the primary activity. When the primary activity is completed any laumcher
87+
/// activity is the primary activity. When the primary activity is completed any launcher
8388
/// which is reading activities will stop listening for updates.
8489
/// </remarks>
8590
Task<PublishingActivity> CreateActivityAsync(string id, string initialStatusText, bool isPrimary, CancellationToken cancellationToken);
@@ -152,4 +157,4 @@ public async Task UpdateActivityStatusAsync(PublishingActivity publishingActivit
152157
}
153158

154159
internal Channel<PublishingActivityStatus> ActivityStatusUpdated { get; } = Channel.CreateUnbounded<PublishingActivityStatus>();
155-
}
160+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
#pragma warning disable ASPIREPUBLISHERS001
5+
6+
using Aspire.Hosting.Publishing;
7+
using Xunit;
8+
9+
namespace Aspire.Hosting.Tests.Publishing;
10+
11+
public class NullPublishingActivityProgressReporterTests
12+
{
13+
[Fact]
14+
public async Task CanUseNullReporter()
15+
{
16+
var reporter = NullPublishingActivityProgressReporter.Instance;
17+
var activity = await reporter.CreateActivityAsync("1", "initial", isPrimary: true, default);
18+
await reporter.UpdateActivityStatusAsync(activity, (status) => status with { IsComplete = true }, default);
19+
20+
Assert.NotNull(activity);
21+
}
22+
}

0 commit comments

Comments
 (0)