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