• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Threading.Tasks;
21 using Grpc.Core;
22 using Grpc.Core.Internal;
23 using Grpc.Core.Utils;
24 using NUnit.Framework;
25 
26 namespace Grpc.Core.Tests
27 {
28     public class ChannelTest
29     {
30         [Test]
Constructor_RejectsInvalidParams()31         public void Constructor_RejectsInvalidParams()
32         {
33             Assert.Throws(typeof(ArgumentNullException), () => new Channel(null, ChannelCredentials.Insecure));
34         }
35 
36         [Test]
Constructor_RejectsDuplicateOptions()37         public void Constructor_RejectsDuplicateOptions()
38         {
39             var options = new ChannelOption[]
40             {
41                 new ChannelOption(ChannelOptions.PrimaryUserAgentString, "ABC"),
42                 new ChannelOption(ChannelOptions.PrimaryUserAgentString, "XYZ")
43             };
44             Assert.Throws(typeof(ArgumentException), () => new Channel("127.0.0.1", ChannelCredentials.Insecure, options));
45         }
46 
47         [Test]
State_IdleAfterCreation()48         public void State_IdleAfterCreation()
49         {
50             var channel = new Channel("localhost", ChannelCredentials.Insecure);
51             Assert.AreEqual(ChannelState.Idle, channel.State);
52             channel.ShutdownAsync().Wait();
53         }
54 
55         [Test]
WaitForStateChangedAsync_InvalidArgument()56         public void WaitForStateChangedAsync_InvalidArgument()
57         {
58             var channel = new Channel("localhost", ChannelCredentials.Insecure);
59             Assert.ThrowsAsync(typeof(ArgumentException), async () => await channel.WaitForStateChangedAsync(ChannelState.Shutdown));
60             channel.ShutdownAsync().Wait();
61         }
62 
63         [Test]
ResolvedTarget()64         public void ResolvedTarget()
65         {
66             var channel = new Channel("127.0.0.1", ChannelCredentials.Insecure);
67             Assert.IsTrue(channel.ResolvedTarget.Contains("127.0.0.1"));
68             channel.ShutdownAsync().Wait();
69         }
70 
71         [Test]
Shutdown_AllowedOnlyOnce()72         public void Shutdown_AllowedOnlyOnce()
73         {
74             var channel = new Channel("localhost", ChannelCredentials.Insecure);
75             channel.ShutdownAsync().Wait();
76             Assert.ThrowsAsync(typeof(InvalidOperationException), async () => await channel.ShutdownAsync());
77         }
78 
79         [Test]
ShutdownTokenCancelledAfterShutdown()80         public async Task ShutdownTokenCancelledAfterShutdown()
81         {
82             var channel = new Channel("localhost", ChannelCredentials.Insecure);
83             Assert.IsFalse(channel.ShutdownToken.IsCancellationRequested);
84             var shutdownTask = channel.ShutdownAsync();
85             Assert.IsTrue(channel.ShutdownToken.IsCancellationRequested);
86             await shutdownTask;
87         }
88 
89         [Test]
StateIsShutdownAfterShutdown()90         public async Task StateIsShutdownAfterShutdown()
91         {
92             var channel = new Channel("localhost", ChannelCredentials.Insecure);
93             await channel.ShutdownAsync();
94             Assert.AreEqual(ChannelState.Shutdown, channel.State);
95         }
96 
97         [Test]
ShutdownFinishesWaitForStateChangedAsync()98         public async Task ShutdownFinishesWaitForStateChangedAsync()
99         {
100             var channel = new Channel("localhost", ChannelCredentials.Insecure);
101             var stateChangedTask = channel.WaitForStateChangedAsync(ChannelState.Idle);
102             var shutdownTask = channel.ShutdownAsync();
103             await stateChangedTask;
104             await shutdownTask;
105         }
106 
107         [Test]
OperationsThrowAfterShutdown()108         public async Task OperationsThrowAfterShutdown()
109         {
110             var channel = new Channel("localhost", ChannelCredentials.Insecure);
111             await channel.ShutdownAsync();
112             Assert.ThrowsAsync(typeof(ObjectDisposedException), async () => await channel.WaitForStateChangedAsync(ChannelState.Idle));
113             Assert.Throws(typeof(ObjectDisposedException), () => { var x = channel.ResolvedTarget; });
114             Assert.ThrowsAsync(typeof(TaskCanceledException), async () => await channel.ConnectAsync());
115         }
116     }
117 }
118