1 #region Copyright notice and license 2 3 // Copyright 2016 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 26 using Grpc.Core; 27 using Grpc.Core.Internal; 28 using Grpc.Core.Utils; 29 30 using NUnit.Framework; 31 32 namespace Grpc.Core.Tests 33 { 34 public class HalfcloseTest 35 { 36 MockServiceHelper helper; 37 Server server; 38 Channel channel; 39 40 [SetUp] Init()41 public void Init() 42 { 43 helper = new MockServiceHelper(); 44 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 /// <summary> 58 /// For client streaming and duplex streaming calls, if server does a full close 59 /// before we halfclose the request stream, an attempt to halfclose 60 /// (complete the request stream) shouldn't be treated as an error. 61 /// </summary> 62 [Test] HalfcloseAfterFullclose_ClientStreamingCall()63 public async Task HalfcloseAfterFullclose_ClientStreamingCall() 64 { 65 helper.ClientStreamingHandler = new ClientStreamingServerMethod<string, string>((requestStream, context) => 66 { 67 return Task.FromResult("PASS"); 68 }); 69 70 var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreamingCall()); 71 // make sure server has fullclosed on us 72 Assert.AreEqual("PASS", await call.ResponseAsync); 73 74 // sending close from client should be still fine because server can finish 75 // the call anytime and we cannot do anything about it on the client side. 76 await call.RequestStream.CompleteAsync(); 77 78 // Second attempt to close from client is not allowed. 79 Assert.ThrowsAsync(typeof(InvalidOperationException), async () => await call.RequestStream.CompleteAsync()); 80 } 81 } 82 } 83