1 // Licensed to the Apache Software Foundation(ASF) under one
2 // or more contributor license agreements.See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership.The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
21 using System.Threading.Tasks;
22 using KellermanSoftware.CompareNetObjects;
23 using Microsoft.VisualStudio.TestTools.UnitTesting;
24 using Thrift.Protocols;
25 using Thrift.Protocols.Entities;
26 using Thrift.Transports.Client;
28 namespace Thrift.IntegrationTests.Protocols
31 public class ProtocolsOperationsTests
33 private readonly CompareLogic _compareLogic = new CompareLogic();
36 [DataRow(typeof(TBinaryProtocol), TMessageType.Call)]
37 [DataRow(typeof(TBinaryProtocol), TMessageType.Exception)]
38 [DataRow(typeof(TBinaryProtocol), TMessageType.Oneway)]
39 [DataRow(typeof(TBinaryProtocol), TMessageType.Reply)]
40 [DataRow(typeof(TCompactProtocol), TMessageType.Call)]
41 [DataRow(typeof(TCompactProtocol), TMessageType.Exception)]
42 [DataRow(typeof(TCompactProtocol), TMessageType.Oneway)]
43 [DataRow(typeof(TCompactProtocol), TMessageType.Reply)]
44 [DataRow(typeof(TJsonProtocol), TMessageType.Call)]
45 [DataRow(typeof(TJsonProtocol), TMessageType.Exception)]
46 [DataRow(typeof(TJsonProtocol), TMessageType.Oneway)]
47 [DataRow(typeof(TJsonProtocol), TMessageType.Reply)]
48 public async Task WriteReadMessage_Test(Type protocolType, TMessageType messageType)
50 var expected = new TMessage(nameof(TMessage), messageType, 1);
54 var tuple = GetProtocolInstance(protocolType);
55 using (var stream = tuple.Item1)
57 var protocol = tuple.Item2;
59 await protocol.WriteMessageBeginAsync(expected);
60 await protocol.WriteMessageEndAsync();
62 stream.Seek(0, SeekOrigin.Begin);
64 var actualMessage = await protocol.ReadMessageBeginAsync();
65 await protocol.ReadMessageEndAsync();
67 var result = _compareLogic.Compare(expected, actualMessage);
68 Assert.IsTrue(result.AreEqual, result.DifferencesString);
73 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
78 [DataRow(typeof(TBinaryProtocol))]
79 [DataRow(typeof(TCompactProtocol))]
80 [DataRow(typeof(TJsonProtocol))]
81 [ExpectedException(typeof(Exception))]
82 public async Task WriteReadStruct_Test(Type protocolType)
84 var expected = new TStruct(nameof(TStruct));
88 var tuple = GetProtocolInstance(protocolType);
89 using (var stream = tuple.Item1)
91 var protocol = tuple.Item2;
93 await protocol.WriteStructBeginAsync(expected);
94 await protocol.WriteStructEndAsync();
96 stream?.Seek(0, SeekOrigin.Begin);
98 var actual = await protocol.ReadStructBeginAsync();
99 await protocol.ReadStructEndAsync();
101 var result = _compareLogic.Compare(expected, actual);
102 Assert.IsTrue(result.AreEqual, result.DifferencesString);
108 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
113 [DataRow(typeof(TBinaryProtocol))]
114 [DataRow(typeof(TCompactProtocol))]
115 [DataRow(typeof(TJsonProtocol))]
116 [ExpectedException(typeof(Exception))]
117 public async Task WriteReadField_Test(Type protocolType)
119 var expected = new TField(nameof(TField), TType.String, 1);
123 var tuple = GetProtocolInstance(protocolType);
124 using (var stream = tuple.Item1)
126 var protocol = tuple.Item2;
128 await protocol.WriteFieldBeginAsync(expected);
129 await protocol.WriteFieldEndAsync();
131 stream?.Seek(0, SeekOrigin.Begin);
133 var actual = await protocol.ReadFieldBeginAsync();
134 await protocol.ReadFieldEndAsync();
136 var result = _compareLogic.Compare(expected, actual);
137 Assert.IsTrue(result.AreEqual, result.DifferencesString);
142 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
147 [DataRow(typeof(TBinaryProtocol))]
148 [DataRow(typeof(TCompactProtocol))]
149 [DataRow(typeof(TJsonProtocol))]
150 public async Task WriteReadMap_Test(Type protocolType)
152 var expected = new TMap(TType.String, TType.String, 1);
156 var tuple = GetProtocolInstance(protocolType);
157 using (var stream = tuple.Item1)
159 var protocol = tuple.Item2;
161 await protocol.WriteMapBeginAsync(expected);
162 await protocol.WriteMapEndAsync();
164 stream?.Seek(0, SeekOrigin.Begin);
166 var actual = await protocol.ReadMapBeginAsync();
167 await protocol.ReadMapEndAsync();
169 var result = _compareLogic.Compare(expected, actual);
170 Assert.IsTrue(result.AreEqual, result.DifferencesString);
175 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
181 [DataRow(typeof(TBinaryProtocol))]
182 [DataRow(typeof(TCompactProtocol))]
183 [DataRow(typeof(TJsonProtocol))]
184 public async Task WriteReadList_Test(Type protocolType)
186 var expected = new TList(TType.String, 1);
190 var tuple = GetProtocolInstance(protocolType);
191 using (var stream = tuple.Item1)
193 var protocol = tuple.Item2;
195 await protocol.WriteListBeginAsync(expected);
196 await protocol.WriteListEndAsync();
198 stream?.Seek(0, SeekOrigin.Begin);
200 var actual = await protocol.ReadListBeginAsync();
201 await protocol.ReadListEndAsync();
203 var result = _compareLogic.Compare(expected, actual);
204 Assert.IsTrue(result.AreEqual, result.DifferencesString);
209 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
214 [DataRow(typeof(TBinaryProtocol))]
215 [DataRow(typeof(TCompactProtocol))]
216 [DataRow(typeof(TJsonProtocol))]
217 public async Task WriteReadSet_Test(Type protocolType)
219 var expected = new TSet(TType.String, 1);
223 var tuple = GetProtocolInstance(protocolType);
224 using (var stream = tuple.Item1)
226 var protocol = tuple.Item2;
228 await protocol.WriteSetBeginAsync(expected);
229 await protocol.WriteSetEndAsync();
231 stream?.Seek(0, SeekOrigin.Begin);
233 var actual = await protocol.ReadSetBeginAsync();
234 await protocol.ReadSetEndAsync();
236 var result = _compareLogic.Compare(expected, actual);
237 Assert.IsTrue(result.AreEqual, result.DifferencesString);
242 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
247 [DataRow(typeof(TBinaryProtocol))]
248 [DataRow(typeof(TCompactProtocol))]
249 [DataRow(typeof(TJsonProtocol))]
250 public async Task WriteReadBool_Test(Type protocolType)
256 var tuple = GetProtocolInstance(protocolType);
257 using (var stream = tuple.Item1)
259 var protocol = tuple.Item2;
261 await protocol.WriteBoolAsync(expected);
263 stream?.Seek(0, SeekOrigin.Begin);
265 var actual = await protocol.ReadBoolAsync();
267 var result = _compareLogic.Compare(expected, actual);
268 Assert.IsTrue(result.AreEqual, result.DifferencesString);
273 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
278 [DataRow(typeof(TBinaryProtocol))]
279 [DataRow(typeof(TCompactProtocol))]
280 [DataRow(typeof(TJsonProtocol))]
281 public async Task WriteReadByte_Test(Type protocolType)
283 var expected = sbyte.MaxValue;
287 var tuple = GetProtocolInstance(protocolType);
288 using (var stream = tuple.Item1)
290 var protocol = tuple.Item2;
292 await protocol.WriteByteAsync(expected);
294 stream?.Seek(0, SeekOrigin.Begin);
296 var actual = await protocol.ReadByteAsync();
298 var result = _compareLogic.Compare(expected, actual);
299 Assert.IsTrue(result.AreEqual, result.DifferencesString);
304 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
309 [DataRow(typeof(TBinaryProtocol))]
310 [DataRow(typeof(TCompactProtocol))]
311 [DataRow(typeof(TJsonProtocol))]
312 public async Task WriteReadI16_Test(Type protocolType)
314 var expected = short.MaxValue;
318 var tuple = GetProtocolInstance(protocolType);
319 using (var stream = tuple.Item1)
321 var protocol = tuple.Item2;
323 await protocol.WriteI16Async(expected);
325 stream?.Seek(0, SeekOrigin.Begin);
327 var actual = await protocol.ReadI16Async();
329 var result = _compareLogic.Compare(expected, actual);
330 Assert.IsTrue(result.AreEqual, result.DifferencesString);
335 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
340 [DataRow(typeof(TBinaryProtocol))]
341 [DataRow(typeof(TCompactProtocol))]
342 [DataRow(typeof(TJsonProtocol))]
343 public async Task WriteReadI32_Test(Type protocolType)
345 var expected = int.MaxValue;
349 var tuple = GetProtocolInstance(protocolType);
350 using (var stream = tuple.Item1)
352 var protocol = tuple.Item2;
354 await protocol.WriteI32Async(expected);
356 stream?.Seek(0, SeekOrigin.Begin);
358 var actual = await protocol.ReadI32Async();
360 var result = _compareLogic.Compare(expected, actual);
361 Assert.IsTrue(result.AreEqual, result.DifferencesString);
366 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
371 [DataRow(typeof(TBinaryProtocol))]
372 [DataRow(typeof(TCompactProtocol))]
373 [DataRow(typeof(TJsonProtocol))]
374 public async Task WriteReadI64_Test(Type protocolType)
376 var expected = long.MaxValue;
380 var tuple = GetProtocolInstance(protocolType);
381 using (var stream = tuple.Item1)
383 var protocol = tuple.Item2;
385 await protocol.WriteI64Async(expected);
387 stream?.Seek(0, SeekOrigin.Begin);
389 var actual = await protocol.ReadI64Async();
391 var result = _compareLogic.Compare(expected, actual);
392 Assert.IsTrue(result.AreEqual, result.DifferencesString);
397 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
402 [DataRow(typeof(TBinaryProtocol))]
403 [DataRow(typeof(TCompactProtocol))]
404 [DataRow(typeof(TJsonProtocol))]
405 public async Task WriteReadDouble_Test(Type protocolType)
407 var expected = double.MaxValue;
411 var tuple = GetProtocolInstance(protocolType);
412 using (var stream = tuple.Item1)
414 var protocol = tuple.Item2;
416 await protocol.WriteDoubleAsync(expected);
418 stream?.Seek(0, SeekOrigin.Begin);
420 var actual = await protocol.ReadDoubleAsync();
422 var result = _compareLogic.Compare(expected, actual);
423 Assert.IsTrue(result.AreEqual, result.DifferencesString);
428 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
433 [DataRow(typeof(TBinaryProtocol))]
434 [DataRow(typeof(TCompactProtocol))]
435 [DataRow(typeof(TJsonProtocol))]
436 public async Task WriteReadString_Test(Type protocolType)
438 var expected = nameof(String);
442 var tuple = GetProtocolInstance(protocolType);
443 using (var stream = tuple.Item1)
445 var protocol = tuple.Item2;
447 await protocol.WriteStringAsync(expected);
449 stream?.Seek(0, SeekOrigin.Begin);
451 var actual = await protocol.ReadStringAsync();
453 var result = _compareLogic.Compare(expected, actual);
454 Assert.IsTrue(result.AreEqual, result.DifferencesString);
459 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
464 [DataRow(typeof(TBinaryProtocol))]
465 [DataRow(typeof(TCompactProtocol))]
466 [DataRow(typeof(TJsonProtocol))]
467 public async Task WriteReadBinary_Test(Type protocolType)
469 var expected = Encoding.UTF8.GetBytes(nameof(String));
473 var tuple = GetProtocolInstance(protocolType);
474 using (var stream = tuple.Item1)
476 var protocol = tuple.Item2;
478 await protocol.WriteBinaryAsync(expected);
480 stream?.Seek(0, SeekOrigin.Begin);
482 var actual = await protocol.ReadBinaryAsync();
484 var result = _compareLogic.Compare(expected, actual);
485 Assert.IsTrue(result.AreEqual, result.DifferencesString);
490 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
494 private static Tuple<Stream, TProtocol> GetProtocolInstance(Type protocolType)
496 var memoryStream = new MemoryStream();
497 var streamClientTransport = new TStreamClientTransport(memoryStream, memoryStream);
498 var protocol = (TProtocol) Activator.CreateInstance(protocolType, streamClientTransport);
499 return new Tuple<Stream, TProtocol>(memoryStream, protocol);