Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Fix tests; short-circuit StartAsync on first sequential failure
  • Loading branch information
jkrmo committed Sep 21, 2022
commit 946a3d4c6f50cfdfe3e75c5767f6c047f42d2f0a
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public async Task StartAsync(CancellationToken cancellationToken = default)
catch (Exception ex)
{
exceptions.Add(ex);
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,27 +217,46 @@ public void HostedServiceRegisteredWithFactory()
}
}

[Fact]
public async Task AppCrashesOnStartWhenFirstHostedServiceThrows()
[Theory]
[InlineData(1, true), InlineData(1, false)]
[InlineData(2, true), InlineData(2, false)]
[InlineData(10, true), InlineData(10, false)]
public async Task AppCrashesOnStartWhenFirstHostedServiceThrows(int eventCount, bool concurrentStartup)
{
bool[] events1 = null;
bool[] events2 = null;
bool[][] events = new bool[eventCount][];

using (var host = CreateBuilder()
.ConfigureServices((services) =>
{
events1 = RegisterCallbacksThatThrow(services);
events2 = RegisterCallbacksThatThrow(services);
services.Configure<HostOptions>(i => i.ServicesStartConcurrently = concurrentStartup);

for (int i = 0; i < eventCount; i++)
{
events[i] = RegisterCallbacksThatThrow(services);
}
})
.Build())
{
await Assert.ThrowsAsync<InvalidOperationException>(() => host.StartAsync());
Assert.True(events1[0]);
Assert.False(events2[0]);
if (concurrentStartup && eventCount > 1)
await Assert.ThrowsAsync<AggregateException>(() => host.StartAsync());
else
await Assert.ThrowsAsync<InvalidOperationException>(() => host.StartAsync());

for (int i = 0; i < eventCount; i++)
{
if (i == 0 || concurrentStartup)
Assert.True(events[i][0]);
else
Assert.False(events[i][0]);
}

host.Dispose();

// Stopping
Assert.False(events1[1]);
Assert.False(events2[1]);
for (int i = 0; i < eventCount; i++)
{
Assert.False(events[i][1]);
}
}
}

Expand Down Expand Up @@ -1108,17 +1127,23 @@ public async Task HostDisposeApplicationDoesNotFireStopOnHostedService()
}
}

[Fact]
public async Task HostDoesNotNotifyIHostApplicationLifetimeCallbacksIfIHostedServicesThrow()
[Theory]
[InlineData(1, true), InlineData(1, false)]
[InlineData(2, true), InlineData(2, false)]
[InlineData(10, true), InlineData(10, false)]
public async Task HostDoesNotNotifyIHostApplicationLifetimeCallbacksIfIHostedServicesThrow(int eventCount, bool concurrentStartup)
{
bool[] events1 = null;
bool[] events2 = null;
bool[][] events = new bool[eventCount][];

using (var host = CreateBuilder()
.ConfigureServices((services) =>
{
events1 = RegisterCallbacksThatThrow(services);
events2 = RegisterCallbacksThatThrow(services);
services.Configure<HostOptions>(i => i.ServicesStartConcurrently = concurrentStartup);

for (int i = 0; i < eventCount; i++)
{
events[i] = RegisterCallbacksThatThrow(services);
}
})
.Build())
{
Expand All @@ -1127,13 +1152,28 @@ public async Task HostDoesNotNotifyIHostApplicationLifetimeCallbacksIfIHostedSer
var started = RegisterCallbacksThatThrow(applicationLifetime.ApplicationStarted);
var stopping = RegisterCallbacksThatThrow(applicationLifetime.ApplicationStopping);

await Assert.ThrowsAsync<InvalidOperationException>(() => host.StartAsync());
Assert.True(events1[0]);
Assert.False(events2[0]);
if (concurrentStartup && eventCount > 1)
await Assert.ThrowsAsync<AggregateException>(() => host.StartAsync());
else
await Assert.ThrowsAsync<InvalidOperationException>(() => host.StartAsync());

for (int i = 0; i < eventCount; i++)
{
if (i == 0 || concurrentStartup)
Assert.True(events[i][0]);
else
Assert.False(events[i][0]);
}

Assert.False(started.All(s => s));

host.Dispose();
Assert.False(events1[1]);
Assert.False(events2[1]);

for (int i = 0; i < eventCount; i++)
{
Assert.False(events[i][1]);
}

Assert.False(stopping.All(s => s));
}
}
Expand Down