Skip to content

Commit

Permalink
Make HalibutRuntime implement IDisposable (#572)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwoctopusdeploy authored Dec 4, 2023
1 parent 4c88549 commit 97df1d6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
50 changes: 50 additions & 0 deletions source/Halibut.Tests/HalibutRuntimeDisposal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Threading.Tasks;
using FluentAssertions;
using Halibut.Tests.Support;
using Halibut.Tests.Support.TestAttributes;
using Halibut.Tests.Support.TestCases;
using Halibut.Tests.TestServices.Async;
using Halibut.TestUtils.Contracts;
using NUnit.Framework;

namespace Halibut.Tests
{
public class HalibutRuntimeDisposal : BaseTest
{
[Test]
[LatestClientAndLatestServiceTestCases(testNetworkConditions: false)]
public async Task CanDisposeOfHalibutClientAndService(ClientAndServiceTestCase clientAndServiceTestCase)
{
var timeoutsAndLimits = new HalibutTimeoutsAndLimitsForTestsBuilder().Build();
timeoutsAndLimits.PollingRequestQueueTimeout = TimeSpan.FromSeconds(15);

var clientAndService = await clientAndServiceTestCase.CreateTestCaseBuilder()
.WithStandardServices()
.AsLatestClientAndLatestServiceBuilder()
.WithHalibutTimeoutsAndLimits(timeoutsAndLimits)
.Build(CancellationToken);

var echoServiceClient = clientAndService.CreateAsyncClient<IEchoService, IAsyncClientEchoService>();

await echoServiceClient.SayHelloAsync("Hi");

#pragma warning disable VSTHRD103
#pragma warning disable CS0618 // Type or member is obsolete
clientAndService.Service.Dispose();
#pragma warning restore CS0618 // Type or member is obsolete
#pragma warning restore VSTHRD103

// The Service and Client are both HalibutRuntimes so ensuring that the Service is Dispose works should also ensure that the Client Dispose works.
// This test does not test specifics of the Dispose to ensure that all resources are cleaned up, but rather that the HalibutRuntime stops working.
// Future improvements could ensure that all the specifics of Dispose work as expected e.g. active connections are disconnected, listening ports freed up etc.
await AssertionExtensions.Should(() => echoServiceClient.SayHelloAsync("Hey")).ThrowAsync<HalibutClientException>("The Service should have been shutdown on Dispose");

#pragma warning disable VSTHRD103
#pragma warning disable CS0618 // Type or member is obsolete
clientAndService.Client.Dispose();
#pragma warning restore CS0618 // Type or member is obsolete
#pragma warning restore VSTHRD103
}
}
}
11 changes: 11 additions & 0 deletions source/Halibut/HalibutRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,17 @@ public async Task DisconnectAsync(ServiceEndPoint endpoint, CancellationToken ca
await connectionManager.DisconnectAsync(endpoint, log, cancellationToken);
}

/// <summary>
/// Dispose has been left in the API for backwards compatibility, but it is recommended to use DisposeAsync instead.
/// At the time this change was made, Tentacle (Tentacle Version 7.1.0) still uses this method.
/// Care must be taken to ensure all code using HalibutRuntime support and calls DisposeAsync.
/// </summary>
[Obsolete("Dispose has been left in the API for backwards compatibility, but it is recommended to use DisposeAsync instead.")]
public void Dispose()
{
DisposeAsync().GetAwaiter().GetResult();
}

public async ValueTask DisposeAsync()
{
pollingClients.Dispose();
Expand Down
2 changes: 1 addition & 1 deletion source/Halibut/IHalibutRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Halibut
{
public interface IHalibutRuntime : IAsyncDisposable
public interface IHalibutRuntime : IAsyncDisposable, IDisposable
{
ILogFactory Logs { get; }
int Listen();
Expand Down

0 comments on commit 97df1d6

Please sign in to comment.