acdream/tests/AcDream.Core.Net.Tests/SubscriptionSetTests.cs

47 lines
1.4 KiB
C#

namespace AcDream.Core.Net.Tests;
public sealed class SubscriptionSetTests
{
[Fact]
public void Dispose_ReleasesInReverseOrderAndIsIdempotent()
{
var order = new List<int>();
var subscriptions = new SubscriptionSet();
subscriptions.Add(() => order.Add(1));
subscriptions.Add(() => order.Add(2));
subscriptions.Add(() => order.Add(3));
subscriptions.Dispose();
subscriptions.Dispose();
Assert.Equal([3, 2, 1], order);
}
[Fact]
public void Dispose_AttemptsEveryReleaseAndAggregatesFailures()
{
var order = new List<int>();
var subscriptions = new SubscriptionSet();
subscriptions.Add(() => order.Add(1));
subscriptions.Add(() =>
{
order.Add(2);
throw new InvalidOperationException("second failed");
});
subscriptions.Add(() =>
{
order.Add(3);
throw new IOException("third failed");
});
AggregateException error = Assert.Throws<AggregateException>(
subscriptions.Dispose);
Assert.Equal([3, 2, 1], order);
Assert.Collection(
error.InnerExceptions,
exception => Assert.IsType<IOException>(exception),
exception => Assert.IsType<InvalidOperationException>(exception));
subscriptions.Dispose();
}
}