blob: 9254cb998dc9f2483f346b8dd5d79e99240a4582 [file] [log] [blame]
Jan Tattermusch5321d492015-08-07 23:21:27 -07001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015 gRPC authors.
Jan Tattermusch5321d492015-08-07 23:21:27 -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 Tattermusch5321d492015-08-07 23:21:27 -07008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermusch5321d492015-08-07 23:21:27 -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 Tattermusch5321d492015-08-07 23:21:27 -070016
17#endregion
18
19using System;
20using System.Diagnostics;
21using System.Linq;
Jan Tattermusch4047d5d2016-06-21 20:53:56 -070022using System.Text;
Jan Tattermusch5321d492015-08-07 23:21:27 -070023using System.Threading;
24using System.Threading.Tasks;
25using Grpc.Core;
26using Grpc.Core.Internal;
27using Grpc.Core.Utils;
28using NUnit.Framework;
29
30namespace Grpc.Core.Tests
31{
32 public class CompressionTest
33 {
34 MockServiceHelper helper;
35 Server server;
36 Channel channel;
37
38 [SetUp]
39 public void Init()
40 {
41 helper = new MockServiceHelper();
42
43 server = helper.GetServer();
44 server.Start();
45 channel = helper.GetChannel();
46 }
47
48 [TearDown]
49 public void Cleanup()
50 {
Jan Tattermusch2b357952015-08-20 14:54:33 -070051 channel.ShutdownAsync().Wait();
Jan Tattermusch5321d492015-08-07 23:21:27 -070052 server.ShutdownAsync().Wait();
53 }
54
Jan Tattermusch5321d492015-08-07 23:21:27 -070055 [Test]
56 public void WriteOptions_Unary()
57 {
Jan Tattermusch2fb313c2017-08-09 09:41:25 +020058 helper.UnaryHandler = new UnaryServerMethod<string, string>((request, context) =>
Jan Tattermusch5321d492015-08-07 23:21:27 -070059 {
60 context.WriteOptions = new WriteOptions(WriteFlags.NoCompress);
Jan Tattermusch2fb313c2017-08-09 09:41:25 +020061 return Task.FromResult(request);
Jan Tattermusch5321d492015-08-07 23:21:27 -070062 });
63
64 var callOptions = new CallOptions(writeOptions: new WriteOptions(WriteFlags.NoCompress));
65 Calls.BlockingUnaryCall(helper.CreateUnaryCall(callOptions), "abc");
66 }
67
68 [Test]
69 public async Task WriteOptions_DuplexStreaming()
70 {
71 helper.DuplexStreamingHandler = new DuplexStreamingServerMethod<string, string>(async (requestStream, responseStream, context) =>
72 {
Jan Tattermuschf22abfb2015-08-09 16:15:34 -070073 await requestStream.ToListAsync();
Jan Tattermusch5321d492015-08-07 23:21:27 -070074
75 context.WriteOptions = new WriteOptions(WriteFlags.NoCompress);
76
Jan Tattermusch410c4732015-08-08 00:02:07 -070077 await context.WriteResponseHeadersAsync(new Metadata { { "ascii-header", "abcdefg" } });
Jan Tattermusch5321d492015-08-07 23:21:27 -070078
79 await responseStream.WriteAsync("X");
80
81 responseStream.WriteOptions = null;
82 await responseStream.WriteAsync("Y");
83
84 responseStream.WriteOptions = new WriteOptions(WriteFlags.NoCompress);
85 await responseStream.WriteAsync("Z");
86 });
87
88 var callOptions = new CallOptions(writeOptions: new WriteOptions(WriteFlags.NoCompress));
89 var call = Calls.AsyncDuplexStreamingCall(helper.CreateDuplexStreamingCall(callOptions));
90
91 // check that write options from call options are propagated to request stream.
92 Assert.IsTrue((call.RequestStream.WriteOptions.Flags & WriteFlags.NoCompress) != 0);
93
94 call.RequestStream.WriteOptions = new WriteOptions();
95 await call.RequestStream.WriteAsync("A");
96
97 call.RequestStream.WriteOptions = null;
98 await call.RequestStream.WriteAsync("B");
99
100 call.RequestStream.WriteOptions = new WriteOptions(WriteFlags.NoCompress);
101 await call.RequestStream.WriteAsync("C");
102
103 await call.RequestStream.CompleteAsync();
104
Jan Tattermuschf22abfb2015-08-09 16:15:34 -0700105 await call.ResponseStream.ToListAsync();
Jan Tattermusch5321d492015-08-07 23:21:27 -0700106 }
Jan Tattermusch4047d5d2016-06-21 20:53:56 -0700107
108 [Test]
109 public void CanReadCompressedMessages()
110 {
111 var compressionMetadata = new Metadata
112 {
Jan Tattermusch606e35a2016-06-22 12:26:36 -0700113 { new Metadata.Entry(Metadata.CompressionRequestAlgorithmMetadataKey, "gzip") }
Jan Tattermusch4047d5d2016-06-21 20:53:56 -0700114 };
115
116 helper.UnaryHandler = new UnaryServerMethod<string, string>(async (req, context) =>
117 {
118 await context.WriteResponseHeadersAsync(compressionMetadata);
119 return req;
120 });
121
122 var stringBuilder = new StringBuilder();
123 for (int i = 0; i < 200000; i++)
124 {
125 stringBuilder.Append('a');
126 }
127 var request = stringBuilder.ToString();
128 var response = Calls.BlockingUnaryCall(helper.CreateUnaryCall(new CallOptions(compressionMetadata)), request);
129
130 Assert.AreEqual(request, response);
131 }
Jan Tattermusch5321d492015-08-07 23:21:27 -0700132 }
133}