OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015, Google Inc. |
| 4 // All rights reserved. |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 10 // * Redistributions of source code must retain the above copyright |
| 11 // notice, this list of conditions and the following disclaimer. |
| 12 // * Redistributions in binary form must reproduce the above |
| 13 // copyright notice, this list of conditions and the following disclaimer |
| 14 // in the documentation and/or other materials provided with the |
| 15 // distribution. |
| 16 // * Neither the name of Google Inc. nor the names of its |
| 17 // contributors may be used to endorse or promote products derived from |
| 18 // this software without specific prior written permission. |
| 19 // |
| 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
| 32 #endregion |
| 33 |
| 34 using System; |
| 35 using System.Diagnostics; |
| 36 using System.Linq; |
| 37 using System.Threading; |
| 38 using System.Threading.Tasks; |
| 39 using Grpc.Core; |
| 40 using Grpc.Core.Internal; |
| 41 using Grpc.Core.Utils; |
| 42 using NUnit.Framework; |
| 43 |
| 44 namespace Grpc.Core.Tests |
| 45 { |
| 46 public class CompressionTest |
| 47 { |
| 48 MockServiceHelper helper; |
| 49 Server server; |
| 50 Channel channel; |
| 51 |
| 52 [SetUp] |
| 53 public void Init() |
| 54 { |
| 55 helper = new MockServiceHelper(); |
| 56 |
| 57 server = helper.GetServer(); |
| 58 server.Start(); |
| 59 channel = helper.GetChannel(); |
| 60 } |
| 61 |
| 62 [TearDown] |
| 63 public void Cleanup() |
| 64 { |
| 65 channel.ShutdownAsync().Wait(); |
| 66 server.ShutdownAsync().Wait(); |
| 67 } |
| 68 |
| 69 [Test] |
| 70 public void WriteOptions_Unary() |
| 71 { |
| 72 helper.UnaryHandler = new UnaryServerMethod<string, string>(async (r
equest, context) => |
| 73 { |
| 74 context.WriteOptions = new WriteOptions(WriteFlags.NoCompress); |
| 75 return request; |
| 76 }); |
| 77 |
| 78 var callOptions = new CallOptions(writeOptions: new WriteOptions(Wri
teFlags.NoCompress)); |
| 79 Calls.BlockingUnaryCall(helper.CreateUnaryCall(callOptions), "abc"); |
| 80 } |
| 81 |
| 82 [Test] |
| 83 public async Task WriteOptions_DuplexStreaming() |
| 84 { |
| 85 helper.DuplexStreamingHandler = new DuplexStreamingServerMethod<stri
ng, string>(async (requestStream, responseStream, context) => |
| 86 { |
| 87 await requestStream.ToListAsync(); |
| 88 |
| 89 context.WriteOptions = new WriteOptions(WriteFlags.NoCompress); |
| 90 |
| 91 await context.WriteResponseHeadersAsync(new Metadata { { "ascii-
header", "abcdefg" } }); |
| 92 |
| 93 await responseStream.WriteAsync("X"); |
| 94 |
| 95 responseStream.WriteOptions = null; |
| 96 await responseStream.WriteAsync("Y"); |
| 97 |
| 98 responseStream.WriteOptions = new WriteOptions(WriteFlags.NoComp
ress); |
| 99 await responseStream.WriteAsync("Z"); |
| 100 }); |
| 101 |
| 102 var callOptions = new CallOptions(writeOptions: new WriteOptions(Wri
teFlags.NoCompress)); |
| 103 var call = Calls.AsyncDuplexStreamingCall(helper.CreateDuplexStreami
ngCall(callOptions)); |
| 104 |
| 105 // check that write options from call options are propagated to requ
est stream. |
| 106 Assert.IsTrue((call.RequestStream.WriteOptions.Flags & WriteFlags.No
Compress) != 0); |
| 107 |
| 108 call.RequestStream.WriteOptions = new WriteOptions(); |
| 109 await call.RequestStream.WriteAsync("A"); |
| 110 |
| 111 call.RequestStream.WriteOptions = null; |
| 112 await call.RequestStream.WriteAsync("B"); |
| 113 |
| 114 call.RequestStream.WriteOptions = new WriteOptions(WriteFlags.NoComp
ress); |
| 115 await call.RequestStream.WriteAsync("C"); |
| 116 |
| 117 await call.RequestStream.CompleteAsync(); |
| 118 |
| 119 await call.ResponseStream.ToListAsync(); |
| 120 } |
| 121 } |
| 122 } |
OLD | NEW |