1 #region Copyright notice and license 2 3 // Copyright 2017 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.Collections.Generic; 21 using System.Diagnostics; 22 using System.Linq; 23 using System.Threading; 24 using System.Threading.Tasks; 25 using Grpc.Core; 26 using Grpc.Core.Internal; 27 using Grpc.Core.Profiling; 28 using Grpc.Core.Utils; 29 using NUnit.Framework; 30 31 namespace Grpc.Core.Tests 32 { 33 public class ChannelConnectivityTest 34 { 35 const string Host = "127.0.0.1"; 36 37 MockServiceHelper helper; 38 Server server; 39 Channel channel; 40 41 [SetUp] Init()42 public void Init() 43 { 44 helper = new MockServiceHelper(Host); 45 server = helper.GetServer(); 46 server.Start(); 47 channel = helper.GetChannel(); 48 } 49 50 [TearDown] Cleanup()51 public void Cleanup() 52 { 53 channel.ShutdownAsync().Wait(); 54 server.ShutdownAsync().Wait(); 55 } 56 57 [Test] Channel_WaitForStateChangedAsync()58 public async Task Channel_WaitForStateChangedAsync() 59 { 60 Assert.ThrowsAsync(typeof(TaskCanceledException), 61 async () => await channel.WaitForStateChangedAsync(channel.State, DateTime.UtcNow.AddMilliseconds(0))); 62 63 var stateChangedTask = channel.WaitForStateChangedAsync(channel.State); 64 await channel.ConnectAsync(DateTime.UtcNow.AddMilliseconds(5000)); 65 await stateChangedTask; 66 Assert.AreEqual(ChannelState.Ready, channel.State); 67 } 68 69 [Test] Channel_TryWaitForStateChangedAsync()70 public async Task Channel_TryWaitForStateChangedAsync() 71 { 72 Assert.IsFalse(await channel.TryWaitForStateChangedAsync(channel.State, DateTime.UtcNow.AddMilliseconds(0))); 73 74 var stateChangedTask = channel.TryWaitForStateChangedAsync(channel.State); 75 await channel.ConnectAsync(DateTime.UtcNow.AddMilliseconds(5000)); 76 Assert.IsTrue(await stateChangedTask); 77 Assert.AreEqual(ChannelState.Ready, channel.State); 78 } 79 80 [Test] Channel_ConnectAsync()81 public async Task Channel_ConnectAsync() 82 { 83 await channel.ConnectAsync(); 84 Assert.AreEqual(ChannelState.Ready, channel.State); 85 86 await channel.ConnectAsync(DateTime.UtcNow.AddMilliseconds(1000)); 87 Assert.AreEqual(ChannelState.Ready, channel.State); 88 } 89 } 90 } 91