blob: b8df515dec00574897e5c8e5aecd2348632249a6 [file] [log] [blame]
Jens Geyeraa0c8b32019-01-28 23:27:45 +01001// 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
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
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
16// under the License.
17
18using System;
19using System.IO;
20using System.Text;
21using System.Threading.Tasks;
22using KellermanSoftware.CompareNetObjects;
23using Microsoft.VisualStudio.TestTools.UnitTesting;
24using Thrift.Protocol;
25using Thrift.Protocol.Entities;
26using Thrift.Transport.Client;
27
28namespace Thrift.IntegrationTests.Protocols
29{
30 [TestClass]
31 public class ProtocolsOperationsTests
32 {
33 private readonly CompareLogic _compareLogic = new CompareLogic();
Jens Geyereacd1d42019-11-20 19:03:14 +010034 private static readonly TConfiguration Configuration = null; // or new TConfiguration() if needed
Jens Geyeraa0c8b32019-01-28 23:27:45 +010035
36 [DataTestMethod]
37 [DataRow(typeof(TBinaryProtocol), TMessageType.Call)]
38 [DataRow(typeof(TBinaryProtocol), TMessageType.Exception)]
39 [DataRow(typeof(TBinaryProtocol), TMessageType.Oneway)]
40 [DataRow(typeof(TBinaryProtocol), TMessageType.Reply)]
41 [DataRow(typeof(TCompactProtocol), TMessageType.Call)]
42 [DataRow(typeof(TCompactProtocol), TMessageType.Exception)]
43 [DataRow(typeof(TCompactProtocol), TMessageType.Oneway)]
44 [DataRow(typeof(TCompactProtocol), TMessageType.Reply)]
45 [DataRow(typeof(TJsonProtocol), TMessageType.Call)]
46 [DataRow(typeof(TJsonProtocol), TMessageType.Exception)]
47 [DataRow(typeof(TJsonProtocol), TMessageType.Oneway)]
48 [DataRow(typeof(TJsonProtocol), TMessageType.Reply)]
49 public async Task WriteReadMessage_Test(Type protocolType, TMessageType messageType)
50 {
51 var expected = new TMessage(nameof(TMessage), messageType, 1);
52
53 try
54 {
55 var tuple = GetProtocolInstance(protocolType);
56 using (var stream = tuple.Item1)
57 {
58 var protocol = tuple.Item2;
59
60 await protocol.WriteMessageBeginAsync(expected);
61 await protocol.WriteMessageEndAsync();
62
63 stream.Seek(0, SeekOrigin.Begin);
64
65 var actualMessage = await protocol.ReadMessageBeginAsync();
66 await protocol.ReadMessageEndAsync();
67
68 var result = _compareLogic.Compare(expected, actualMessage);
69 Assert.IsTrue(result.AreEqual, result.DifferencesString);
70 }
71 }
72 catch (Exception e)
73 {
74 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
75 }
76 }
77
78 [DataTestMethod]
79 [DataRow(typeof(TBinaryProtocol))]
80 [DataRow(typeof(TCompactProtocol))]
81 [DataRow(typeof(TJsonProtocol))]
82 [ExpectedException(typeof(Exception))]
83 public async Task WriteReadStruct_Test(Type protocolType)
84 {
85 var expected = new TStruct(nameof(TStruct));
86
87 try
88 {
89 var tuple = GetProtocolInstance(protocolType);
90 using (var stream = tuple.Item1)
91 {
92 var protocol = tuple.Item2;
93
94 await protocol.WriteStructBeginAsync(expected);
95 await protocol.WriteStructEndAsync();
96
97 stream?.Seek(0, SeekOrigin.Begin);
98
99 var actual = await protocol.ReadStructBeginAsync();
100 await protocol.ReadStructEndAsync();
101
102 var result = _compareLogic.Compare(expected, actual);
103 Assert.IsTrue(result.AreEqual, result.DifferencesString);
104 }
105
106 }
107 catch (Exception e)
108 {
109 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
110 }
111 }
112
113 [DataTestMethod]
114 [DataRow(typeof(TBinaryProtocol))]
115 [DataRow(typeof(TCompactProtocol))]
116 [DataRow(typeof(TJsonProtocol))]
117 [ExpectedException(typeof(Exception))]
118 public async Task WriteReadField_Test(Type protocolType)
119 {
120 var expected = new TField(nameof(TField), TType.String, 1);
121
122 try
123 {
124 var tuple = GetProtocolInstance(protocolType);
125 using (var stream = tuple.Item1)
126 {
127 var protocol = tuple.Item2;
128
129 await protocol.WriteFieldBeginAsync(expected);
130 await protocol.WriteFieldEndAsync();
131
132 stream?.Seek(0, SeekOrigin.Begin);
133
134 var actual = await protocol.ReadFieldBeginAsync();
135 await protocol.ReadFieldEndAsync();
136
137 var result = _compareLogic.Compare(expected, actual);
138 Assert.IsTrue(result.AreEqual, result.DifferencesString);
139 }
140 }
141 catch (Exception e)
142 {
143 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
144 }
145 }
146
147 [DataTestMethod]
148 [DataRow(typeof(TBinaryProtocol))]
149 [DataRow(typeof(TCompactProtocol))]
150 [DataRow(typeof(TJsonProtocol))]
151 public async Task WriteReadMap_Test(Type protocolType)
152 {
153 var expected = new TMap(TType.String, TType.String, 1);
154
155 try
156 {
157 var tuple = GetProtocolInstance(protocolType);
158 using (var stream = tuple.Item1)
159 {
160 var protocol = tuple.Item2;
161
162 await protocol.WriteMapBeginAsync(expected);
163 await protocol.WriteMapEndAsync();
164
165 stream?.Seek(0, SeekOrigin.Begin);
166
167 var actual = await protocol.ReadMapBeginAsync();
168 await protocol.ReadMapEndAsync();
169
170 var result = _compareLogic.Compare(expected, actual);
171 Assert.IsTrue(result.AreEqual, result.DifferencesString);
172 }
173 }
174 catch (Exception e)
175 {
176 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
177 }
178
179 }
180
181 [DataTestMethod]
182 [DataRow(typeof(TBinaryProtocol))]
183 [DataRow(typeof(TCompactProtocol))]
184 [DataRow(typeof(TJsonProtocol))]
185 public async Task WriteReadList_Test(Type protocolType)
186 {
187 var expected = new TList(TType.String, 1);
188
189 try
190 {
191 var tuple = GetProtocolInstance(protocolType);
192 using (var stream = tuple.Item1)
193 {
194 var protocol = tuple.Item2;
195
196 await protocol.WriteListBeginAsync(expected);
197 await protocol.WriteListEndAsync();
198
199 stream?.Seek(0, SeekOrigin.Begin);
200
201 var actual = await protocol.ReadListBeginAsync();
202 await protocol.ReadListEndAsync();
203
204 var result = _compareLogic.Compare(expected, actual);
205 Assert.IsTrue(result.AreEqual, result.DifferencesString);
206 }
207 }
208 catch (Exception e)
209 {
210 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
211 }
212 }
213
214 [DataTestMethod]
215 [DataRow(typeof(TBinaryProtocol))]
216 [DataRow(typeof(TCompactProtocol))]
217 [DataRow(typeof(TJsonProtocol))]
218 public async Task WriteReadSet_Test(Type protocolType)
219 {
220 var expected = new TSet(TType.String, 1);
221
222 try
223 {
224 var tuple = GetProtocolInstance(protocolType);
225 using (var stream = tuple.Item1)
226 {
227 var protocol = tuple.Item2;
228
229 await protocol.WriteSetBeginAsync(expected);
230 await protocol.WriteSetEndAsync();
231
232 stream?.Seek(0, SeekOrigin.Begin);
233
234 var actual = await protocol.ReadSetBeginAsync();
235 await protocol.ReadSetEndAsync();
236
237 var result = _compareLogic.Compare(expected, actual);
238 Assert.IsTrue(result.AreEqual, result.DifferencesString);
239 }
240 }
241 catch (Exception e)
242 {
243 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
244 }
245 }
246
247 [DataTestMethod]
248 [DataRow(typeof(TBinaryProtocol))]
249 [DataRow(typeof(TCompactProtocol))]
250 [DataRow(typeof(TJsonProtocol))]
251 public async Task WriteReadBool_Test(Type protocolType)
252 {
253 var expected = true;
254
255 try
256 {
257 var tuple = GetProtocolInstance(protocolType);
258 using (var stream = tuple.Item1)
259 {
260 var protocol = tuple.Item2;
261
262 await protocol.WriteBoolAsync(expected);
263
264 stream?.Seek(0, SeekOrigin.Begin);
265
266 var actual = await protocol.ReadBoolAsync();
267
268 var result = _compareLogic.Compare(expected, actual);
269 Assert.IsTrue(result.AreEqual, result.DifferencesString);
270 }
271 }
272 catch (Exception e)
273 {
274 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
275 }
276 }
277
278 [DataTestMethod]
279 [DataRow(typeof(TBinaryProtocol))]
280 [DataRow(typeof(TCompactProtocol))]
281 [DataRow(typeof(TJsonProtocol))]
282 public async Task WriteReadByte_Test(Type protocolType)
283 {
284 var expected = sbyte.MaxValue;
285
286 try
287 {
288 var tuple = GetProtocolInstance(protocolType);
289 using (var stream = tuple.Item1)
290 {
291 var protocol = tuple.Item2;
292
293 await protocol.WriteByteAsync(expected);
294
295 stream?.Seek(0, SeekOrigin.Begin);
296
297 var actual = await protocol.ReadByteAsync();
298
299 var result = _compareLogic.Compare(expected, actual);
300 Assert.IsTrue(result.AreEqual, result.DifferencesString);
301 }
302 }
303 catch (Exception e)
304 {
305 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
306 }
307 }
308
309 [DataTestMethod]
310 [DataRow(typeof(TBinaryProtocol))]
311 [DataRow(typeof(TCompactProtocol))]
312 [DataRow(typeof(TJsonProtocol))]
313 public async Task WriteReadI16_Test(Type protocolType)
314 {
315 var expected = short.MaxValue;
316
317 try
318 {
319 var tuple = GetProtocolInstance(protocolType);
320 using (var stream = tuple.Item1)
321 {
322 var protocol = tuple.Item2;
323
324 await protocol.WriteI16Async(expected);
325
326 stream?.Seek(0, SeekOrigin.Begin);
327
328 var actual = await protocol.ReadI16Async();
329
330 var result = _compareLogic.Compare(expected, actual);
331 Assert.IsTrue(result.AreEqual, result.DifferencesString);
332 }
333 }
334 catch (Exception e)
335 {
336 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
337 }
338 }
339
340 [DataTestMethod]
341 [DataRow(typeof(TBinaryProtocol))]
342 [DataRow(typeof(TCompactProtocol))]
343 [DataRow(typeof(TJsonProtocol))]
344 public async Task WriteReadI32_Test(Type protocolType)
345 {
346 var expected = int.MaxValue;
347
348 try
349 {
350 var tuple = GetProtocolInstance(protocolType);
351 using (var stream = tuple.Item1)
352 {
353 var protocol = tuple.Item2;
354
355 await protocol.WriteI32Async(expected);
356
357 stream?.Seek(0, SeekOrigin.Begin);
358
359 var actual = await protocol.ReadI32Async();
360
361 var result = _compareLogic.Compare(expected, actual);
362 Assert.IsTrue(result.AreEqual, result.DifferencesString);
363 }
364 }
365 catch (Exception e)
366 {
367 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
368 }
369 }
370
371 [DataTestMethod]
372 [DataRow(typeof(TBinaryProtocol))]
373 [DataRow(typeof(TCompactProtocol))]
374 [DataRow(typeof(TJsonProtocol))]
375 public async Task WriteReadI64_Test(Type protocolType)
376 {
377 var expected = long.MaxValue;
378
379 try
380 {
381 var tuple = GetProtocolInstance(protocolType);
382 using (var stream = tuple.Item1)
383 {
384 var protocol = tuple.Item2;
385
386 await protocol.WriteI64Async(expected);
387
388 stream?.Seek(0, SeekOrigin.Begin);
389
390 var actual = await protocol.ReadI64Async();
391
392 var result = _compareLogic.Compare(expected, actual);
393 Assert.IsTrue(result.AreEqual, result.DifferencesString);
394 }
395 }
396 catch (Exception e)
397 {
398 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
399 }
400 }
401
402 [DataTestMethod]
403 [DataRow(typeof(TBinaryProtocol))]
404 [DataRow(typeof(TCompactProtocol))]
405 [DataRow(typeof(TJsonProtocol))]
406 public async Task WriteReadDouble_Test(Type protocolType)
407 {
408 var expected = double.MaxValue;
409
410 try
411 {
412 var tuple = GetProtocolInstance(protocolType);
413 using (var stream = tuple.Item1)
414 {
415 var protocol = tuple.Item2;
416
417 await protocol.WriteDoubleAsync(expected);
418
419 stream?.Seek(0, SeekOrigin.Begin);
420
421 var actual = await protocol.ReadDoubleAsync();
422
423 var result = _compareLogic.Compare(expected, actual);
424 Assert.IsTrue(result.AreEqual, result.DifferencesString);
425 }
426 }
427 catch (Exception e)
428 {
429 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
430 }
431 }
432
433 [DataTestMethod]
434 [DataRow(typeof(TBinaryProtocol))]
435 [DataRow(typeof(TCompactProtocol))]
436 [DataRow(typeof(TJsonProtocol))]
437 public async Task WriteReadString_Test(Type protocolType)
438 {
439 var expected = nameof(String);
440
441 try
442 {
443 var tuple = GetProtocolInstance(protocolType);
444 using (var stream = tuple.Item1)
445 {
446 var protocol = tuple.Item2;
447
448 await protocol.WriteStringAsync(expected);
449
450 stream?.Seek(0, SeekOrigin.Begin);
451
452 var actual = await protocol.ReadStringAsync();
453
454 var result = _compareLogic.Compare(expected, actual);
455 Assert.IsTrue(result.AreEqual, result.DifferencesString);
456 }
457 }
458 catch (Exception e)
459 {
460 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
461 }
462 }
463
464 [DataTestMethod]
465 [DataRow(typeof(TBinaryProtocol))]
466 [DataRow(typeof(TCompactProtocol))]
467 [DataRow(typeof(TJsonProtocol))]
468 public async Task WriteReadBinary_Test(Type protocolType)
469 {
470 var expected = Encoding.UTF8.GetBytes(nameof(String));
471
472 try
473 {
474 var tuple = GetProtocolInstance(protocolType);
475 using (var stream = tuple.Item1)
476 {
477 var protocol = tuple.Item2;
478
479 await protocol.WriteBinaryAsync(expected);
480
481 stream?.Seek(0, SeekOrigin.Begin);
482
483 var actual = await protocol.ReadBinaryAsync();
484
485 var result = _compareLogic.Compare(expected, actual);
486 Assert.IsTrue(result.AreEqual, result.DifferencesString);
487 }
488 }
489 catch (Exception e)
490 {
491 throw new Exception($"Exception during testing of protocol: {protocolType.FullName}", e);
492 }
493 }
494
495 private static Tuple<Stream, TProtocol> GetProtocolInstance(Type protocolType)
496 {
497 var memoryStream = new MemoryStream();
Jens Geyereacd1d42019-11-20 19:03:14 +0100498 var streamClientTransport = new TStreamTransport(memoryStream, memoryStream,Configuration);
Jens Geyeraa0c8b32019-01-28 23:27:45 +0100499 var protocol = (TProtocol) Activator.CreateInstance(protocolType, streamClientTransport);
500 return new Tuple<Stream, TProtocol>(memoryStream, protocol);
501 }
502 }
503}