Jan Tattermusch | 153c32a | 2016-04-01 14:38:32 -0700 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | // Copyright 2016 gRPC authors. |
Jan Tattermusch | 153c32a | 2016-04-01 14:38:32 -0700 | [diff] [blame] | 4 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Jan Tattermusch | 153c32a | 2016-04-01 14:38:32 -0700 | [diff] [blame] | 8 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
Jan Tattermusch | 153c32a | 2016-04-01 14:38:32 -0700 | [diff] [blame] | 10 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Jan Tattermusch | 153c32a | 2016-04-01 14:38:32 -0700 | [diff] [blame] | 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] |
| 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] |
| 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> |
Jan Tattermusch | 153c32a | 2016-04-01 14:38:32 -0700 | [diff] [blame] | 62 | [Test] |
| 63 | public async Task HalfcloseAfterFullclose_ClientStreamingCall() |
| 64 | { |
Jan Tattermusch | 2fb313c | 2017-08-09 09:41:25 +0200 | [diff] [blame] | 65 | helper.ClientStreamingHandler = new ClientStreamingServerMethod<string, string>((requestStream, context) => |
Jan Tattermusch | 153c32a | 2016-04-01 14:38:32 -0700 | [diff] [blame] | 66 | { |
Jan Tattermusch | 2fb313c | 2017-08-09 09:41:25 +0200 | [diff] [blame] | 67 | return Task.FromResult("PASS"); |
Jan Tattermusch | 153c32a | 2016-04-01 14:38:32 -0700 | [diff] [blame] | 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. |
Jan Tattermusch | e331da2 | 2016-04-08 11:05:39 -0700 | [diff] [blame] | 79 | Assert.ThrowsAsync(typeof(InvalidOperationException), async () => await call.RequestStream.CompleteAsync()); |
Jan Tattermusch | 153c32a | 2016-04-01 14:38:32 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | } |