1 #region Copyright notice and license 2 3 // Copyright 2015 gRPC authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 #endregion 18 19 using System; 20 using System.Linq; 21 using System.Threading; 22 using Grpc.Core; 23 using NUnit.Framework; 24 25 namespace Grpc.Core.Tests 26 { 27 public class GrpcEnvironmentTest 28 { 29 [Test] InitializeAndShutdownGrpcEnvironment()30 public void InitializeAndShutdownGrpcEnvironment() 31 { 32 var env = GrpcEnvironment.AddRef(); 33 Assert.IsTrue(env.CompletionQueues.Count > 0); 34 for (int i = 0; i < env.CompletionQueues.Count; i++) 35 { 36 Assert.IsNotNull(env.CompletionQueues.ElementAt(i)); 37 } 38 GrpcEnvironment.ReleaseAsync().Wait(); 39 } 40 41 [Test] SubsequentInvocations()42 public void SubsequentInvocations() 43 { 44 var env1 = GrpcEnvironment.AddRef(); 45 var env2 = GrpcEnvironment.AddRef(); 46 Assert.AreSame(env1, env2); 47 GrpcEnvironment.ReleaseAsync().Wait(); 48 GrpcEnvironment.ReleaseAsync().Wait(); 49 } 50 51 [Test] InitializeAfterShutdown()52 public void InitializeAfterShutdown() 53 { 54 Assert.AreEqual(0, GrpcEnvironment.GetRefCount()); 55 56 var env1 = GrpcEnvironment.AddRef(); 57 GrpcEnvironment.ReleaseAsync().Wait(); 58 59 var env2 = GrpcEnvironment.AddRef(); 60 GrpcEnvironment.ReleaseAsync().Wait(); 61 62 Assert.AreNotSame(env1, env2); 63 } 64 65 [Test] ReleaseWithoutAddRef()66 public void ReleaseWithoutAddRef() 67 { 68 Assert.AreEqual(0, GrpcEnvironment.GetRefCount()); 69 Assert.ThrowsAsync(typeof(InvalidOperationException), async () => await GrpcEnvironment.ReleaseAsync()); 70 } 71 72 [Test] GetCoreVersionString()73 public void GetCoreVersionString() 74 { 75 var coreVersion = GrpcEnvironment.GetCoreVersionString(); 76 var parts = coreVersion.Split('.'); 77 Assert.AreEqual(3, parts.Length); 78 } 79 80 [Test] ShuttingDownEventIsFired()81 public void ShuttingDownEventIsFired() 82 { 83 var cts = new CancellationTokenSource(); 84 var handler = new EventHandler((sender, args) => { cts.Cancel(); }); 85 86 GrpcEnvironment.ShuttingDown += handler; 87 var env = GrpcEnvironment.AddRef(); 88 GrpcEnvironment.ReleaseAsync().Wait(); 89 GrpcEnvironment.ShuttingDown -= handler; 90 91 Assert.IsTrue(cts.Token.IsCancellationRequested); 92 } 93 } 94 } 95