diff options
author | Jan Tattermusch <jtattermusch@users.noreply.github.com> | 2017-08-10 10:15:26 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-10 10:15:26 +0200 |
commit | 593a22a95cf6759f717efceb9ac0f1efe75c4ef6 (patch) | |
tree | c94815e61c930c0abc9a8955f23af98b81e42b74 /src | |
parent | cf875b7fd904f9ebf791edb3e71ca4214238429e (diff) | |
parent | f602869c9719d7de1cd09ef1f9c140df1ef15eef (diff) |
Merge pull request #12099 from jtattermusch/csharp_shutdownevent
Add GrpcEnvironment.ShuttingDown event
Diffstat (limited to 'src')
-rw-r--r-- | src/csharp/Grpc.Core.Tests/GrpcEnvironmentTest.cs | 15 | ||||
-rw-r--r-- | src/csharp/Grpc.Core/GrpcEnvironment.cs | 17 |
2 files changed, 28 insertions, 4 deletions
diff --git a/src/csharp/Grpc.Core.Tests/GrpcEnvironmentTest.cs b/src/csharp/Grpc.Core.Tests/GrpcEnvironmentTest.cs index 0f3a82c605..fc9d5599f2 100644 --- a/src/csharp/Grpc.Core.Tests/GrpcEnvironmentTest.cs +++ b/src/csharp/Grpc.Core.Tests/GrpcEnvironmentTest.cs @@ -18,6 +18,7 @@ using System; using System.Linq; +using System.Threading; using Grpc.Core; using NUnit.Framework; @@ -75,5 +76,19 @@ namespace Grpc.Core.Tests var parts = coreVersion.Split('.'); Assert.AreEqual(3, parts.Length); } + + [Test] + public void ShuttingDownEventIsFired() + { + var cts = new CancellationTokenSource(); + var handler = new EventHandler((sender, args) => { cts.Cancel(); }); + + GrpcEnvironment.ShuttingDown += handler; + var env = GrpcEnvironment.AddRef(); + GrpcEnvironment.ReleaseAsync().Wait(); + GrpcEnvironment.ShuttingDown -= handler; + + Assert.IsTrue(cts.Token.IsCancellationRequested); + } } } diff --git a/src/csharp/Grpc.Core/GrpcEnvironment.cs b/src/csharp/Grpc.Core/GrpcEnvironment.cs index 0663ee9215..cbc7d2078c 100644 --- a/src/csharp/Grpc.Core/GrpcEnvironment.cs +++ b/src/csharp/Grpc.Core/GrpcEnvironment.cs @@ -49,7 +49,7 @@ namespace Grpc.Core readonly DebugStats debugStats = new DebugStats(); readonly AtomicCounter cqPickerCounter = new AtomicCounter(); - bool isClosed; + bool isShutdown; /// <summary> /// Returns a reference-counted instance of initialized gRPC environment. @@ -238,6 +238,12 @@ namespace Grpc.Core } /// <summary> + /// Occurs when <c>GrpcEnvironment</c> is about the start the shutdown logic. + /// If <c>GrpcEnvironment</c> is later initialized and shutdown, the event will be fired again (unless unregistered first). + /// </summary> + public static event EventHandler ShuttingDown; + + /// <summary> /// Creates gRPC environment. /// </summary> private GrpcEnvironment() @@ -311,13 +317,16 @@ namespace Grpc.Core /// </summary> private async Task ShutdownAsync() { - if (isClosed) + if (isShutdown) { - throw new InvalidOperationException("Close has already been called"); + throw new InvalidOperationException("ShutdownAsync has already been called"); } + + await Task.Run(() => ShuttingDown?.Invoke(this, null)).ConfigureAwait(false); + await threadPool.StopAsync().ConfigureAwait(false); GrpcNativeShutdown(); - isClosed = true; + isShutdown = true; debugStats.CheckOK(); } |