blob: 5fc8fb694ab35008211aea8743438e759e9c2fb8 [file] [log] [blame]
Jan Tattermusch153c32a2016-04-01 14:38:32 -07001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2016 gRPC authors.
Jan Tattermusch153c32a2016-04-01 14:38:32 -07004//
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
Jan Tattermusch153c32a2016-04-01 14:38:32 -07008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermusch153c32a2016-04-01 14:38:32 -070010//
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 Tattermusch153c32a2016-04-01 14:38:32 -070016
17#endregion
18
19using System;
20using System.Collections.Generic;
21using System.Diagnostics;
22using System.Linq;
23using System.Threading;
24using System.Threading.Tasks;
25
26using Grpc.Core;
27using Grpc.Core.Internal;
28using Grpc.Core.Utils;
29
30using NUnit.Framework;
31
32namespace 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 Tattermusch153c32a2016-04-01 14:38:32 -070062 [Test]
63 public async Task HalfcloseAfterFullclose_ClientStreamingCall()
64 {
Jan Tattermusch2fb313c2017-08-09 09:41:25 +020065 helper.ClientStreamingHandler = new ClientStreamingServerMethod<string, string>((requestStream, context) =>
Jan Tattermusch153c32a2016-04-01 14:38:32 -070066 {
Jan Tattermusch2fb313c2017-08-09 09:41:25 +020067 return Task.FromResult("PASS");
Jan Tattermusch153c32a2016-04-01 14:38:32 -070068 });
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 Tattermusche331da22016-04-08 11:05:39 -070079 Assert.ThrowsAsync(typeof(InvalidOperationException), async () => await call.RequestStream.CompleteAsync());
Jan Tattermusch153c32a2016-04-01 14:38:32 -070080 }
81 }
82}