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