From 98a232577fa56bb703ea96b88cc6c5b9391178f0 Mon Sep 17 00:00:00 2001 From: Jens Geyer Date: Sun, 9 Jan 2022 16:50:52 +0100 Subject: THRIFT-5479 Add net 6 support --- .../cpp/src/thrift/generate/t_netstd_generator.cc | 5 ++- .../cpp/src/thrift/generate/t_netstd_generator.h | 12 +++++ .../Thrift.Benchmarks/CompactProtocolBenchmarks.cs | 14 +++--- .../Thrift.Benchmarks/Thrift.Benchmarks.csproj | 1 + .../Protocols/ProtocolsOperationsTests.cs | 11 +++-- .../Thrift.IntegrationTests.csproj | 1 + .../Thrift.PublicInterfaces.Compile.Tests.csproj | 1 + .../Thrift.Tests/Collections/TCollectionsTests.cs | 4 +- .../Tests/Thrift.Tests/DataModel/DeepCopy.cs | 34 +++++++------- lib/netstd/Tests/Thrift.Tests/Thrift.Tests.csproj | 1 + test/netstd/Client/Client.csproj | 1 + test/netstd/Client/Performance/PerformanceTests.cs | 52 +++++++++++----------- test/netstd/Client/Performance/TestDataFactory.cs | 25 ++++++----- test/netstd/Client/TestClient.cs | 11 +++-- test/netstd/Server/Server.csproj | 1 + test/netstd/Server/TestServer.cs | 15 ++++--- 16 files changed, 110 insertions(+), 79 deletions(-) diff --git a/compiler/cpp/src/thrift/generate/t_netstd_generator.cc b/compiler/cpp/src/thrift/generate/t_netstd_generator.cc index 88f8da417..1f8e169da 100644 --- a/compiler/cpp/src/thrift/generate/t_netstd_generator.cc +++ b/compiler/cpp/src/thrift/generate/t_netstd_generator.cc @@ -304,7 +304,8 @@ void t_netstd_generator::reset_indent() { void t_netstd_generator::start_netstd_namespace(ostream& out) { - out << "#pragma warning disable IDE0079 // remove unnecessary pragmas" << endl + out << "#nullable disable // suppress C# 8.0 nullable contexts (we still support earlier versions)" << endl + << "#pragma warning disable IDE0079 // remove unnecessary pragmas" << endl << "#pragma warning disable IDE1006 // parts of the code use IDL spelling" << endl << "#pragma warning disable IDE0083 // pattern matching \"that is not SomeType\" requires net5.0 but we still support earlier versions" << endl << endl; @@ -862,7 +863,7 @@ void t_netstd_generator::generate_netstd_struct(t_struct* tstruct, bool is_excep f_struct.open(f_struct_name.c_str()); - reset_indent(); + reset_indent(); f_struct << autogen_comment() << netstd_type_usings() << netstd_thrift_usings() << endl; generate_netstd_struct_definition(f_struct, tstruct, is_exception); diff --git a/compiler/cpp/src/thrift/generate/t_netstd_generator.h b/compiler/cpp/src/thrift/generate/t_netstd_generator.h index 47a704280..31226f245 100644 --- a/compiler/cpp/src/thrift/generate/t_netstd_generator.h +++ b/compiler/cpp/src/thrift/generate/t_netstd_generator.h @@ -150,6 +150,18 @@ public: string convert_to_pascal_case(const string& str); string get_enum_class_name(t_type* type); +protected: + std::string autogen_comment() override { + return std::string("/**\n") + + " * \n" + + " * " + autogen_summary() + "\n" + + " * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n" + + " * \n" + " */\n" + ; + } + + private: string namespace_name_; string namespace_dir_; diff --git a/lib/netstd/Benchmarks/Thrift.Benchmarks/CompactProtocolBenchmarks.cs b/lib/netstd/Benchmarks/Thrift.Benchmarks/CompactProtocolBenchmarks.cs index 16dcc766e..ae4d54537 100644 --- a/lib/netstd/Benchmarks/Thrift.Benchmarks/CompactProtocolBenchmarks.cs +++ b/lib/netstd/Benchmarks/Thrift.Benchmarks/CompactProtocolBenchmarks.cs @@ -28,8 +28,8 @@ namespace Thrift.Benchmarks [MemoryDiagnoser] public class CompactProtocolBenchmarks { - private MemoryStream _Stream; - private TProtocol _Protocol; + private MemoryStream? _Stream; + private TProtocol? _Protocol; [Params(10000)] public int NumberOfOperationsPerIteration { get; set; } @@ -45,16 +45,18 @@ namespace Thrift.Benchmarks [GlobalCleanup] public void GlobalCleanup() { - _Protocol.Dispose(); + _Protocol?.Dispose(); } [Benchmark] public async Task WriteString() { + if ((_Protocol is null) || (_Stream is null)) + throw new System.Exception("unexpected internal state"); + for (int i = 0; i < NumberOfOperationsPerIteration; i++) { await _Protocol.WriteStringAsync("Thrift String Benchmark"); - _Stream.Seek(0, SeekOrigin.Begin); } } @@ -62,12 +64,14 @@ namespace Thrift.Benchmarks [Benchmark] public async Task ReadString() { + if ((_Protocol is null) || (_Stream is null)) + throw new System.Exception("unexpected internal state"); + await _Protocol.WriteStringAsync("Thrift String Benchmark"); for (int i = 0; i < NumberOfOperationsPerIteration; i++) { _Stream.Seek(0, SeekOrigin.Begin); - await _Protocol.ReadStringAsync(); } } diff --git a/lib/netstd/Benchmarks/Thrift.Benchmarks/Thrift.Benchmarks.csproj b/lib/netstd/Benchmarks/Thrift.Benchmarks/Thrift.Benchmarks.csproj index 633850e0a..0e29b3b78 100644 --- a/lib/netstd/Benchmarks/Thrift.Benchmarks/Thrift.Benchmarks.csproj +++ b/lib/netstd/Benchmarks/Thrift.Benchmarks/Thrift.Benchmarks.csproj @@ -23,6 +23,7 @@ net6.0 false true + enable diff --git a/lib/netstd/Tests/Thrift.IntegrationTests/Protocols/ProtocolsOperationsTests.cs b/lib/netstd/Tests/Thrift.IntegrationTests/Protocols/ProtocolsOperationsTests.cs index 62ab31780..dd9646ad9 100644 --- a/lib/netstd/Tests/Thrift.IntegrationTests/Protocols/ProtocolsOperationsTests.cs +++ b/lib/netstd/Tests/Thrift.IntegrationTests/Protocols/ProtocolsOperationsTests.cs @@ -25,13 +25,15 @@ using Thrift.Protocol; using Thrift.Protocol.Entities; using Thrift.Transport.Client; +#pragma warning disable IDE0063 // using + namespace Thrift.IntegrationTests.Protocols { [TestClass] public class ProtocolsOperationsTests { - private readonly CompareLogic _compareLogic = new CompareLogic(); - private static readonly TConfiguration Configuration = null; // or new TConfiguration() if needed + private readonly CompareLogic _compareLogic = new(); + private static readonly TConfiguration Configuration = new(); [DataTestMethod] [DataRow(typeof(TBinaryProtocol), TMessageType.Call)] @@ -496,8 +498,9 @@ namespace Thrift.IntegrationTests.Protocols { var memoryStream = new MemoryStream(); var streamClientTransport = new TStreamTransport(memoryStream, memoryStream,Configuration); - var protocol = (TProtocol) Activator.CreateInstance(protocolType, streamClientTransport); - return new Tuple(memoryStream, protocol); + if( Activator.CreateInstance(protocolType, streamClientTransport) is TProtocol protocol) + return new Tuple(memoryStream, protocol); + throw new Exception("Unexpected"); } } } diff --git a/lib/netstd/Tests/Thrift.IntegrationTests/Thrift.IntegrationTests.csproj b/lib/netstd/Tests/Thrift.IntegrationTests/Thrift.IntegrationTests.csproj index 6d473e44f..56123dd84 100644 --- a/lib/netstd/Tests/Thrift.IntegrationTests/Thrift.IntegrationTests.csproj +++ b/lib/netstd/Tests/Thrift.IntegrationTests/Thrift.IntegrationTests.csproj @@ -30,6 +30,7 @@ false false false + enable diff --git a/lib/netstd/Tests/Thrift.PublicInterfaces.Compile.Tests/Thrift.PublicInterfaces.Compile.Tests.csproj b/lib/netstd/Tests/Thrift.PublicInterfaces.Compile.Tests/Thrift.PublicInterfaces.Compile.Tests.csproj index 15112747f..0d2770d36 100644 --- a/lib/netstd/Tests/Thrift.PublicInterfaces.Compile.Tests/Thrift.PublicInterfaces.Compile.Tests.csproj +++ b/lib/netstd/Tests/Thrift.PublicInterfaces.Compile.Tests/Thrift.PublicInterfaces.Compile.Tests.csproj @@ -29,6 +29,7 @@ false false false + enable diff --git a/lib/netstd/Tests/Thrift.Tests/Collections/TCollectionsTests.cs b/lib/netstd/Tests/Thrift.Tests/Collections/TCollectionsTests.cs index 061032ae0..778d24c80 100644 --- a/lib/netstd/Tests/Thrift.Tests/Collections/TCollectionsTests.cs +++ b/lib/netstd/Tests/Thrift.Tests/Collections/TCollectionsTests.cs @@ -216,9 +216,9 @@ namespace Thrift.Tests.Collections public int X { get; set; } // all Thrift-generated classes override Equals(), we do just the same - public override bool Equals(object that) + public override bool Equals(object? that) { - if (!(that is ExampleClass other)) return false; + if (that is not ExampleClass other) return false; if (ReferenceEquals(this, other)) return true; return this.X == other.X; diff --git a/lib/netstd/Tests/Thrift.Tests/DataModel/DeepCopy.cs b/lib/netstd/Tests/Thrift.Tests/DataModel/DeepCopy.cs index f717b4dda..84fcab85c 100644 --- a/lib/netstd/Tests/Thrift.Tests/DataModel/DeepCopy.cs +++ b/lib/netstd/Tests/Thrift.Tests/DataModel/DeepCopy.cs @@ -24,6 +24,8 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using OptReqDefTest; using Thrift.Collections; +#nullable disable // this is just test code, we leave it at that + namespace Thrift.Tests.DataModel { // ReSharper disable once InconsistentNaming @@ -289,7 +291,7 @@ namespace Thrift.Tests.DataModel return value; } - private List>> ModifyValue(List>> value) + private static List>> ModifyValue(List>> value) { if (value == null) value = new List>>(); @@ -325,7 +327,7 @@ namespace Thrift.Tests.DataModel return value; } - private THashSet> ModifyValue(THashSet> value) + private static THashSet> ModifyValue(THashSet> value) { if (value == null) value = new THashSet>(); @@ -342,7 +344,7 @@ namespace Thrift.Tests.DataModel return value; } - private Dictionary ModifyValue(Dictionary value) + private static Dictionary ModifyValue(Dictionary value) { if (value == null) value = new Dictionary(); @@ -352,7 +354,7 @@ namespace Thrift.Tests.DataModel return value; } - private THashSet ModifyValue(THashSet value) + private static THashSet ModifyValue(THashSet value) { if (value == null) value = new THashSet(); @@ -375,7 +377,7 @@ namespace Thrift.Tests.DataModel return value; } - private List ModifyValue(List value) + private static List ModifyValue(List value) { if (value == null) value = new List(); @@ -385,12 +387,12 @@ namespace Thrift.Tests.DataModel return value; } - private bool ModifyValue(bool value) + private static bool ModifyValue(bool value) { return !value; } - private Dictionary ModifyValue(Dictionary value) + private static Dictionary ModifyValue(Dictionary value) { if (value == null) value = new Dictionary(); @@ -398,7 +400,7 @@ namespace Thrift.Tests.DataModel return value; } - private THashSet ModifyValue(THashSet value) + private static THashSet ModifyValue(THashSet value) { if (value == null) value = new THashSet(); @@ -406,7 +408,7 @@ namespace Thrift.Tests.DataModel return value; } - private List ModifyValue(List value) + private static List ModifyValue(List value) { if (value == null) value = new List(); @@ -414,7 +416,7 @@ namespace Thrift.Tests.DataModel return value; } - private byte[] ModifyValue(byte[] value) + private static byte[] ModifyValue(byte[] value) { if (value == null) value = new byte[1] { 0 }; @@ -423,27 +425,27 @@ namespace Thrift.Tests.DataModel return value; } - private string ModifyValue(string value) + private static string ModifyValue(string value) { return value + "1"; } - private double ModifyValue(double value) + private static double ModifyValue(double value) { return value + 1.1; } - private short ModifyValue(short value) + private static short ModifyValue(short value) { return ++value; } - private Distance ModifyValue(Distance value) + private static Distance ModifyValue(Distance value) { return ++value; } - private void VerifyDifferentContent(RaceDetails first, RaceDetails second) + private static void VerifyDifferentContent(RaceDetails first, RaceDetails second) { Assert.AreNotEqual(first, second); @@ -521,7 +523,7 @@ namespace Thrift.Tests.DataModel Assert.AreNotEqual(first.Triplesix, second.Triplesix); } - private void VerifyIdenticalContent(RaceDetails first, RaceDetails second) + private static void VerifyIdenticalContent(RaceDetails first, RaceDetails second) { Assert.AreEqual(first, second); diff --git a/lib/netstd/Tests/Thrift.Tests/Thrift.Tests.csproj b/lib/netstd/Tests/Thrift.Tests/Thrift.Tests.csproj index 18fce0177..38a6a5c3a 100644 --- a/lib/netstd/Tests/Thrift.Tests/Thrift.Tests.csproj +++ b/lib/netstd/Tests/Thrift.Tests/Thrift.Tests.csproj @@ -21,6 +21,7 @@ net6.0 0.16.0.0 + enable diff --git a/test/netstd/Client/Client.csproj b/test/netstd/Client/Client.csproj index c7d53493e..859297f97 100644 --- a/test/netstd/Client/Client.csproj +++ b/test/netstd/Client/Client.csproj @@ -31,6 +31,7 @@ false false false + enable diff --git a/test/netstd/Client/Performance/PerformanceTests.cs b/test/netstd/Client/Performance/PerformanceTests.cs index f9eb9e4e4..c1f00ddf8 100644 --- a/test/netstd/Client/Performance/PerformanceTests.cs +++ b/test/netstd/Client/Performance/PerformanceTests.cs @@ -34,10 +34,10 @@ namespace Client.Tests { public class PerformanceTests { - private CancellationTokenSource Cancel; - private CrazyNesting Testdata; - private TMemoryBufferTransport MemBuffer; - private TTransport Transport; + private CancellationTokenSource Cancel = new(); + private CrazyNesting? Testdata; + private TMemoryBufferTransport? MemBuffer; + private TTransport? Transport; private LayeredChoice Layered; private readonly TConfiguration Configuration = new(); @@ -76,52 +76,49 @@ namespace Client.Tests } } - private Task GenericProtocolFactory(bool forWrite) + private async Task GenericProtocolFactory(bool forWrite) where T : TProtocol { var oldTrans = Transport; try { + Transport = null; + // read happens after write here, so let's take over the written bytes if (forWrite) MemBuffer = new TMemoryBufferTransport(Configuration); else - MemBuffer = new TMemoryBufferTransport(MemBuffer.GetBuffer(), Configuration); + MemBuffer = new TMemoryBufferTransport(MemBuffer?.GetBuffer(), Configuration); // layered transports anyone? - switch (Layered) + Transport = Layered switch { - case LayeredChoice.None: - Transport = MemBuffer; - break; - case LayeredChoice.Framed: - Transport = new TFramedTransport(MemBuffer); - break; - case LayeredChoice.Buffered: - Transport = new TBufferedTransport(MemBuffer); - break; - default: - Debug.Assert(false); - break; - } + LayeredChoice.None => MemBuffer, + LayeredChoice.Framed => new TFramedTransport(MemBuffer), + LayeredChoice.Buffered => new TBufferedTransport(MemBuffer), + _ => throw new Exception("Unhandled case " + Layered.ToString()), + }; + ; if (!Transport.IsOpen) Transport.OpenAsync().Wait(); - var instance = (T)Activator.CreateInstance(typeof(T), Transport); - return Task.FromResult(instance); + if (Activator.CreateInstance(typeof(T), Transport) is T instance) + return instance; + + throw new Exception("Unexpected."); } finally { - if (oldTrans is IDisposable) - (oldTrans as IDisposable).Dispose(); + oldTrans?.Dispose(); } } private string GetProtocolTransportName(TProtocol proto) { - var name = Transport.GetType().Name; - if (name.Equals(MemBuffer.GetType().Name)) + var name = Transport?.GetType().Name; + var bufnm = MemBuffer?.GetType().Name; + if ((name is null) || name.Equals(bufnm)) name = string.Empty; else name = " + " + name; @@ -135,6 +132,9 @@ namespace Client.Tests { var stop = new Stopwatch(); + if ((Testdata is null) || (Transport is null)) + throw new Exception("unexpected internal state"); + var proto = await factory(true); stop.Start(); await Testdata.WriteAsync(proto, Cancel.Token); diff --git a/test/netstd/Client/Performance/TestDataFactory.cs b/test/netstd/Client/Performance/TestDataFactory.cs index 98962857c..833947c25 100644 --- a/test/netstd/Client/Performance/TestDataFactory.cs +++ b/test/netstd/Client/Performance/TestDataFactory.cs @@ -26,7 +26,7 @@ namespace Client.Tests static class TestDataFactory { - public static CrazyNesting CreateCrazyNesting(int count = 10) + public static CrazyNesting? CreateCrazyNesting(int count = 10) { if (count <= 0) return null; @@ -78,13 +78,15 @@ namespace Client.Tests private static Dictionary CreateUserMap(int count) { - var retval = new Dictionary(); - retval.Add(Numberz.ONE, count); - retval.Add(Numberz.TWO, count); - retval.Add(Numberz.THREE, count); - retval.Add(Numberz.FIVE, count); - retval.Add(Numberz.SIX, count); - retval.Add(Numberz.EIGHT, count); + var retval = new Dictionary + { + { Numberz.ONE, count }, + { Numberz.TWO, count }, + { Numberz.THREE, count }, + { Numberz.FIVE, count }, + { Numberz.SIX, count }, + { Numberz.EIGHT, count } + }; return retval; } @@ -138,9 +140,10 @@ namespace Client.Tests private static Dictionary CreateListFieldDataDictValueListDict(int count) { - var retval = new Dictionary(); - retval.Add(CreateInsanity(count), string.Format("data level {0}", count)); - return retval; + return new Dictionary + { + { CreateInsanity(count), string.Format("data level {0}", count) } + }; } private static byte[] CreateBytesArray(int count) diff --git a/test/netstd/Client/TestClient.cs b/test/netstd/Client/TestClient.cs index 38bccf1f5..0a7fa003b 100644 --- a/test/netstd/Client/TestClient.cs +++ b/test/netstd/Client/TestClient.cs @@ -71,12 +71,12 @@ namespace ThriftTest public string host = "localhost"; public int port = 9090; public int numThreads = 1; - public string url; - public string pipe; + public string url = string.Empty; + public string pipe = string.Empty; public LayeredChoice layered = LayeredChoice.None; public ProtocolChoice protocol = ProtocolChoice.Binary; public TransportChoice transport = TransportChoice.Socket; - private readonly TConfiguration Configuration = null; // or new TConfiguration() if needed + private readonly TConfiguration Configuration = new(); internal void Parse(List args) { @@ -210,7 +210,7 @@ namespace ThriftTest "keys/", }; - string existingPath = null; + var existingPath = string.Empty; foreach (var possiblePath in possiblePaths) { var path = Path.GetFullPath(possiblePath + clientCertName); @@ -234,8 +234,7 @@ namespace ThriftTest public TTransport CreateTransport() { // endpoint transport - TTransport trans = null; - + TTransport trans; switch (transport) { case TransportChoice.Http: diff --git a/test/netstd/Server/Server.csproj b/test/netstd/Server/Server.csproj index 7d5eb1742..cda1dff9f 100644 --- a/test/netstd/Server/Server.csproj +++ b/test/netstd/Server/Server.csproj @@ -31,6 +31,7 @@ false false false + enable diff --git a/test/netstd/Server/TestServer.cs b/test/netstd/Server/TestServer.cs index 65458d60a..515a299f8 100644 --- a/test/netstd/Server/TestServer.cs +++ b/test/netstd/Server/TestServer.cs @@ -36,6 +36,7 @@ using Thrift.Transport.Server; #pragma warning disable IDE0063 // using can be simplified, we don't #pragma warning disable IDE0057 // substr can be simplified, we don't +#pragma warning disable CS1998 // await missing namespace ThriftTest { @@ -74,7 +75,7 @@ namespace ThriftTest internal TransportChoice transport = TransportChoice.Socket; internal ServerChoice server = ServerChoice.Simple; internal int port = 9090; - internal string pipe = null; + internal string pipe = string.Empty; internal void Parse(List args) { @@ -166,7 +167,7 @@ namespace ThriftTest public static int _clientID = -1; // use with Interlocked only! #pragma warning restore CA2211 - private static readonly TConfiguration Configuration = null; // or new TConfiguration() if needed + private static readonly TConfiguration Configuration = new(); public delegate void TestLogDelegate(string msg, params object[] values); @@ -180,10 +181,10 @@ namespace ThriftTest return Task.CompletedTask; } - public Task CreateContextAsync(TProtocol input, TProtocol output, CancellationToken cancellationToken) + public async Task CreateContextAsync(TProtocol input, TProtocol output, CancellationToken cancellationToken) { callCount++; - return Task.FromResult(null); + return null; } public Task DeleteContextAsync(object serverContext, TProtocol input, TProtocol output, CancellationToken cancellationToken) @@ -201,7 +202,7 @@ namespace ThriftTest public class TestHandlerAsync : ThriftTest.IAsync { - public TServer Server { get; set; } + //public TServer Server { get; set; } private readonly int handlerID; private readonly StringBuilder sb = new(); private readonly TestLogDelegate logger; @@ -520,7 +521,7 @@ namespace ThriftTest "keys/", }; - string existingPath = null; + var existingPath = string.Empty; foreach (var possiblePath in possiblePaths) { var path = Path.GetFullPath(possiblePath + serverCertName); @@ -595,7 +596,7 @@ namespace ThriftTest } // Layered transport (mandatory) - TTransportFactory transFactory = null; + TTransportFactory? transFactory; switch (param.buffering) { case BufferChoice.Framed: -- cgit v1.2.1