blob: fc9d5599f21efa39f19ebb5ee077521569856b96 [file] [log] [blame]
Jan Tattermuscha7fff862015-02-13 11:08:08 -08001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015 gRPC authors.
Craig Tiller190d3602015-02-18 09:23:38 -08004//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005// 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
Craig Tiller190d3602015-02-18 09:23:38 -08008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller190d3602015-02-18 09:23:38 -080010//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011// 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.
Jan Tattermuscha7fff862015-02-13 11:08:08 -080016
17#endregion
18
Jan Tattermuscha7608b02015-02-03 17:54:38 -080019using System;
Jan Tattermusch5ee8e772016-05-24 16:17:10 -040020using System.Linq;
Jan Tattermuscha7ca8662017-08-08 13:58:59 +020021using System.Threading;
Jan Tattermusch30868622015-02-19 09:22:33 -080022using Grpc.Core;
23using NUnit.Framework;
Jan Tattermuscha7608b02015-02-03 17:54:38 -080024
Jan Tattermusch30868622015-02-19 09:22:33 -080025namespace Grpc.Core.Tests
Jan Tattermuscha7608b02015-02-03 17:54:38 -080026{
27 public class GrpcEnvironmentTest
28 {
29 [Test]
Jan Tattermuscha0fcfcc2015-02-23 20:26:50 -080030 public void InitializeAndShutdownGrpcEnvironment()
31 {
Jan Tattermusch2b357952015-08-20 14:54:33 -070032 var env = GrpcEnvironment.AddRef();
Jan Tattermusch71702b12016-05-25 20:48:38 -070033 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 }
Jan Tattermusch58584412016-05-31 14:32:27 -070038 GrpcEnvironment.ReleaseAsync().Wait();
Jan Tattermuscha7608b02015-02-03 17:54:38 -080039 }
Jan Tattermusch23821ce2015-02-13 10:46:02 -080040
41 [Test]
Jan Tattermuscha0fcfcc2015-02-23 20:26:50 -080042 public void SubsequentInvocations()
43 {
Jan Tattermusch2b357952015-08-20 14:54:33 -070044 var env1 = GrpcEnvironment.AddRef();
45 var env2 = GrpcEnvironment.AddRef();
Jan Tattermuschea02eb62015-08-19 17:26:53 -070046 Assert.AreSame(env1, env2);
Jan Tattermusch58584412016-05-31 14:32:27 -070047 GrpcEnvironment.ReleaseAsync().Wait();
48 GrpcEnvironment.ReleaseAsync().Wait();
Jan Tattermusch23821ce2015-02-13 10:46:02 -080049 }
50
51 [Test]
Jan Tattermuscha0fcfcc2015-02-23 20:26:50 -080052 public void InitializeAfterShutdown()
53 {
Jan Tattermuschea02eb62015-08-19 17:26:53 -070054 Assert.AreEqual(0, GrpcEnvironment.GetRefCount());
55
Jan Tattermusch2b357952015-08-20 14:54:33 -070056 var env1 = GrpcEnvironment.AddRef();
Jan Tattermusch58584412016-05-31 14:32:27 -070057 GrpcEnvironment.ReleaseAsync().Wait();
Jan Tattermusch23821ce2015-02-13 10:46:02 -080058
Jan Tattermusch2b357952015-08-20 14:54:33 -070059 var env2 = GrpcEnvironment.AddRef();
Jan Tattermusch58584412016-05-31 14:32:27 -070060 GrpcEnvironment.ReleaseAsync().Wait();
Jan Tattermusch23821ce2015-02-13 10:46:02 -080061
Jan Tattermuschea02eb62015-08-19 17:26:53 -070062 Assert.AreNotSame(env1, env2);
Jan Tattermusch23821ce2015-02-13 10:46:02 -080063 }
Jan Tattermusch13387982015-08-10 11:13:28 -070064
65 [Test]
Jan Tattermusch2b357952015-08-20 14:54:33 -070066 public void ReleaseWithoutAddRef()
67 {
Jan Tattermuschea02eb62015-08-19 17:26:53 -070068 Assert.AreEqual(0, GrpcEnvironment.GetRefCount());
Jan Tattermusch58584412016-05-31 14:32:27 -070069 Assert.ThrowsAsync(typeof(InvalidOperationException), async () => await GrpcEnvironment.ReleaseAsync());
Jan Tattermusch2b357952015-08-20 14:54:33 -070070 }
71
72 [Test]
Jan Tattermusch13387982015-08-10 11:13:28 -070073 public void GetCoreVersionString()
74 {
75 var coreVersion = GrpcEnvironment.GetCoreVersionString();
76 var parts = coreVersion.Split('.');
Jan Tattermusch6819ee72016-02-08 16:28:32 -080077 Assert.AreEqual(3, parts.Length);
Jan Tattermusch13387982015-08-10 11:13:28 -070078 }
Jan Tattermuscha7ca8662017-08-08 13:58:59 +020079
80 [Test]
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 }
Jan Tattermuscha7608b02015-02-03 17:54:38 -080093 }
94}