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