-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(client): don't access disposed resources (#738)
This PR is ensuring that the host does not stop with an exception. Those exceptions were occuring because the `CancellationTokenSource` inside the hosted services was already disposed when `StopAsync` got called. The reason for this is that the host invokes `DisposeAsync` **before** `StopAsync`.
- Loading branch information
1 parent
b94eb81
commit f524069
Showing
7 changed files
with
251 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
test/KubeOps.Operator.Test/HostedServices/LeaderResourceWatcher.Integration.Test.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using KubeOps.Abstractions.Controller; | ||
using KubeOps.Operator.Test.TestEntities; | ||
|
||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace KubeOps.Operator.Test.HostedServices; | ||
|
||
public class LeaderAwareHostedServiceDisposeIntegrationTest : HostedServiceDisposeIntegrationTest | ||
{ | ||
protected override void ConfigureHost(HostApplicationBuilder builder) | ||
{ | ||
builder.Services | ||
.AddKubernetesOperator(op => op.EnableLeaderElection = true) | ||
.AddController<TestController, V1OperatorIntegrationTestEntity>(); | ||
} | ||
|
||
private class TestController : IEntityController<V1OperatorIntegrationTestEntity> | ||
{ | ||
public Task ReconcileAsync(V1OperatorIntegrationTestEntity entity, CancellationToken cancellationToken) => | ||
Task.CompletedTask; | ||
|
||
public Task DeletedAsync(V1OperatorIntegrationTestEntity entity, CancellationToken cancellationToken) => | ||
Task.CompletedTask; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
test/KubeOps.Operator.Test/HostedServices/ResourceWatcher.Integration.Test.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using KubeOps.Abstractions.Controller; | ||
using KubeOps.Operator.Test.TestEntities; | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace KubeOps.Operator.Test.HostedServices; | ||
|
||
public class HostedServiceDisposeIntegrationTest : IntegrationTestBase | ||
{ | ||
[Fact] | ||
public async Task Should_Allow_DisposeAsync_Before_StopAsync() | ||
{ | ||
var hostedServices = Services.GetServices<IHostedService>() | ||
.Where(service => service.GetType().Namespace!.StartsWith("KubeOps")); | ||
|
||
// We need to test the inverse order, because the Host is usually disposing the resources in advance of | ||
// stopping them. | ||
foreach (IHostedService service in hostedServices) | ||
{ | ||
await Assert.IsAssignableFrom<IAsyncDisposable>(service).DisposeAsync(); | ||
await service.StopAsync(CancellationToken.None); | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task Should_Allow_StopAsync_Before_DisposeAsync() | ||
{ | ||
var hostedServices = Services.GetServices<IHostedService>() | ||
.Where(service => service.GetType().Namespace!.StartsWith("KubeOps")); | ||
|
||
foreach (IHostedService service in hostedServices) | ||
{ | ||
await service.StopAsync(CancellationToken.None); | ||
await Assert.IsAssignableFrom<IAsyncDisposable>(service).DisposeAsync(); | ||
} | ||
} | ||
|
||
protected override void ConfigureHost(HostApplicationBuilder builder) | ||
{ | ||
builder.Services | ||
.AddKubernetesOperator() | ||
.AddController<TestController, V1OperatorIntegrationTestEntity>(); | ||
} | ||
|
||
private class TestController : IEntityController<V1OperatorIntegrationTestEntity> | ||
{ | ||
public Task ReconcileAsync(V1OperatorIntegrationTestEntity entity, CancellationToken cancellationToken) => | ||
Task.CompletedTask; | ||
|
||
public Task DeletedAsync(V1OperatorIntegrationTestEntity entity, CancellationToken cancellationToken) => | ||
Task.CompletedTask; | ||
} | ||
} |