AppDomainUnloadTest.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #region Copyright notice and license
  2. // Copyright 2015 gRPC authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.Threading.Tasks;
  18. using Grpc.Core.Utils;
  19. using NUnit.Framework;
  20. namespace Grpc.Core.Tests
  21. {
  22. public class AppDomainUnloadTest
  23. {
  24. #if NETCOREAPP1_1 || NETCOREAPP2_1
  25. [Test]
  26. [Ignore("Not supported for CoreCLR")]
  27. public void AppDomainUnloadHookCanCleanupAbandonedCall()
  28. {
  29. }
  30. #else
  31. [Test]
  32. public void AppDomainUnloadHookCanCleanupAbandonedCall()
  33. {
  34. var setup = new AppDomainSetup
  35. {
  36. ApplicationBase = AppDomain.CurrentDomain.BaseDirectory
  37. };
  38. var childDomain = AppDomain.CreateDomain("test", null, setup);
  39. var remoteObj = childDomain.CreateInstance(typeof(AppDomainTestClass).Assembly.GetName().Name, typeof(AppDomainTestClass).FullName);
  40. // Try to unload the appdomain once we've created a server and a channel inside the appdomain.
  41. AppDomain.Unload(childDomain);
  42. }
  43. public class AppDomainTestClass
  44. {
  45. const string Host = "127.0.0.1";
  46. /// <summary>
  47. /// Creates a server and a channel and initiates a call. The code is invoked from inside of an AppDomain
  48. /// to test if AppDomain.Unload() work if Grpc is being used.
  49. /// </summary>
  50. public AppDomainTestClass()
  51. {
  52. var helper = new MockServiceHelper(Host);
  53. var readyToShutdown = new TaskCompletionSource<object>();
  54. helper.DuplexStreamingHandler = new DuplexStreamingServerMethod<string, string>(async (requestStream, responseStream, context) =>
  55. {
  56. readyToShutdown.SetResult(null);
  57. await requestStream.ToListAsync();
  58. });
  59. var server = helper.GetServer();
  60. server.Start();
  61. var channel = helper.GetChannel();
  62. var call = Calls.AsyncDuplexStreamingCall(helper.CreateDuplexStreamingCall());
  63. readyToShutdown.Task.Wait(); // make sure handler is running
  64. }
  65. }
  66. #endif
  67. }
  68. }