diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6a10cea504f..da69203b054 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -621,6 +621,13 @@ jobs: lib/netstd/Tests/codegen/run-NetStd-Codegen-Tests.ps1 ` -TargetVersions @("net${{ matrix.dotnet-version }}") + - name: Run netstd codegen tests with the isolated option + shell: pwsh + run: | + lib/netstd/Tests/codegen/run-NetStd-Codegen-Tests.ps1 ` + -TargetVersions @("net${{ matrix.dotnet-version }}") ` + -Isolated + lib-haxe-codegen: needs: [compiler] runs-on: ubuntu-24.04 diff --git a/compiler/cpp/src/thrift/generate/t_netstd_generator.cc b/compiler/cpp/src/thrift/generate/t_netstd_generator.cc index e9a179b5e37..6e6278ccb5d 100644 --- a/compiler/cpp/src/thrift/generate/t_netstd_generator.cc +++ b/compiler/cpp/src/thrift/generate/t_netstd_generator.cc @@ -59,6 +59,7 @@ t_netstd_generator::t_netstd_generator(t_program* program, const mapsecond; } + else if (iter->first.compare("isolated") == 0) { + isolated_ = true; + } else if (iter->first.compare("pascal") == 0) { use_pascal_case_properties = true; } @@ -125,6 +129,8 @@ bool t_netstd_generator::is_wcf_enabled() const { return wcf_; } bool t_netstd_generator::is_serialize_enabled() const { return serialize_; } +bool t_netstd_generator::is_isolated_enabled() const { return isolated_; } + bool t_netstd_generator::is_union_enabled() const { return union_; } void t_netstd_generator::init_generator() @@ -170,6 +176,7 @@ void t_netstd_generator::init_generator() pverbose("- target NET version ... %d\n", target_net_version); pverbose("- no_deepcopy .......... %s\n", (suppress_deepcopy ? "ON" : "off")); pverbose("- async_postfix ........ %s\n", (add_async_postfix ? "ON" : "off")); + pverbose("- isolated ............. %s\n", (isolated_ ? "ON" : "off")); } string t_netstd_generator::normalize_name(string name, bool is_arg_name) @@ -2454,15 +2461,80 @@ void t_netstd_generator::generate_service_client(ostream& out, t_service* tservi << indent() << "{" << '\n'; indent_up(); + // With the isolated option there is a choice between the constructors, so each one says when to use it. + // Default output has nothing to choose from and stays as it is. + const string shared_protocol_remark = + "Use this constructor when the application owns and configures the protocol and the calls of this client do not overlap. " + "The calls share the protocol and its transport, so they must not run at the same time. " + "For overlapping calls use the constructor that takes a call-isolated transport."; + + if (isolated_) + { + out << indent() << "/// Creates a client that uses one shared protocol for all of its calls." << '\n' + << indent() << "/// " << shared_protocol_remark << "" << '\n' + << indent() << "/// The protocol used for requests and replies. The client takes ownership of it." << '\n'; + } out << indent() << "public Client(TProtocol protocol) : this(protocol, protocol)" << '\n' << indent() << "{" << '\n' << indent() << "}" << '\n' - << '\n' - << indent() << "public Client(TProtocol inputProtocol, TProtocol outputProtocol) : base(inputProtocol, outputProtocol)" << '\n' + << '\n'; + + if (isolated_) + { + out << indent() << "/// Creates a client that uses shared input and output protocols for all of its calls." << '\n' + << indent() << "/// " << shared_protocol_remark << "" << '\n' + << indent() << "/// The protocol used for replies. The client takes ownership of it." << '\n' + << indent() << "/// The protocol used for requests. The client takes ownership of it." << '\n'; + } + out << indent() << "public Client(TProtocol inputProtocol, TProtocol outputProtocol) : base(inputProtocol, outputProtocol)" << '\n' << indent() << "{" << '\n' << indent() << "}" << '\n' << '\n'; + if (isolated_) + { + // dual mode: next to the legacy constructors, every call can get its own transport view and protocol, + // see TBaseClient.CreateCallContext() + out << indent() << "/// Creates a client that gives every call its own transport view and protocol." << '\n' + << indent() << "/// Use this constructor when calls on the same client can overlap, for example over a " + << "THttpTransport. The transport must be call-isolated; to add buffering or framing use " + << "TIsolatedBufferedTransport or TIsolatedFramedTransport, because a legacy buffered or framed " + << "transport is rejected at compile time. The transport applies its per-call timeout to every call, and a call can " + << "be given a shorter one through CreateCallContext. The client owns the transport and disposes it, so keep " + << "the client alive until all of its calls have completed. A client created this way has no shared protocols: " + << "InputProtocol and OutputProtocol throw." << '\n' + << indent() << "/// The call-isolated transport that creates a transport view for each call." << '\n' + << indent() << "/// Creates the protocol of each call over its transport view, " + << "for example a factory that returns a multiplexed protocol." << '\n' + << indent() << "public Client(global::Thrift.Transport.ICallIsolatedTransport transport, global::Thrift.Protocol.TProtocolFactory protocolFactory) : base(transport, protocolFactory)" << '\n' + << indent() << "{" << '\n' + << indent() << "}" << '\n' + << '\n'; + } + + // The operations that take a call context use the cancellation token of that context, which combines the caller's + // token with the timeouts. A failure caused by the timeout is reported as such, not as a plain cancellation. + // The names of the context variable and of the catch variable are chosen per method, so that they never collide + // with a parameter of the IDL; the parameters themselves are not renamed. + auto begin_ctx_body = [&](const string& ctx) + { + out << indent() << "var " << CANCELLATION_TOKEN_NAME << " = " << ctx << ".CancellationToken;" << '\n' + << indent() << "try" << '\n' + << indent() << "{" << '\n'; + indent_up(); + }; + auto end_ctx_body = [&](const string& ctx, const string& cause) + { + indent_down(); + out << indent() << "}" << '\n' + << indent() << "catch (global::System.Exception " << cause << ") when (" << ctx << ".IsTimeoutFailure(" << cause << "))" << '\n' + << indent() << "{" << '\n'; + indent_up(); + out << indent() << "throw " << ctx << ".CreateTimeoutException(" << cause << ");" << '\n'; + indent_down(); + out << indent() << "}" << '\n'; + }; + vector functions = tservice->get_functions(); vector::const_iterator functions_iterator; @@ -2470,14 +2542,47 @@ void t_netstd_generator::generate_service_client(ostream& out, t_service* tservi { string raw_func_name = (*functions_iterator)->get_name(); string function_name = raw_func_name + (add_async_postfix ? "Async" : ""); + const string ctx = context_variable_name(*functions_iterator); + const string cause = timeout_cause_name(*functions_iterator); // async generate_deprecation_attribute(out, (*functions_iterator)->annotations_); - out << indent() << "public async " << function_signature_async(*functions_iterator, "") << '\n' + out << indent() << "public " << (hides_base_client_member(*functions_iterator) ? "new " : "") << "async " + << function_signature_async(*functions_iterator, "") << '\n' << indent() << "{" << '\n'; indent_up(); - out << indent() << "await send_" << function_name << "("; string call_args = argument_list((*functions_iterator)->get_arglist(),false); + if (isolated_) + { + // a client built with a call-isolated transport uses a context for exactly the lifetime of this call; + // a client built with protocols keeps using the shared protocols below. + // The members of the client base class are reached through a cast to the client base class: the service, or an ancestor service (then "base" is its client), may have a method of the same name. + out << indent() << "if (((global::Thrift.TBaseClient)this).IsCallIsolated)" << '\n' + << indent() << "{" << '\n'; + indent_up(); + out << indent() << "using (var " << ctx << " = await ((global::Thrift.TBaseClient)this).CreateCallContextAsync(" << CANCELLATION_TOKEN_NAME << ").ConfigureAwait(false))" << '\n' + << indent() << "{" << '\n'; + indent_up(); + out << indent() << "await send_" << function_name << "(" << ctx; + if(! call_args.empty()) { + out << ", " << call_args; + } + out << ").ConfigureAwait(false);" << '\n'; + if(! (*functions_iterator)->is_oneway()) { + out << indent() << ((*functions_iterator)->get_returntype()->is_void() ? "" : "return ") + << "await recv_" << function_name << "(" << ctx << ").ConfigureAwait(false);" << '\n'; + } + indent_down(); + out << indent() << "}" << '\n'; + if ((*functions_iterator)->is_oneway() || (*functions_iterator)->get_returntype()->is_void()) + { + // nothing to return, but the shared-protocol path below must not run as well + out << indent() << "return;" << '\n'; + } + indent_down(); + out << indent() << "}" << '\n' << '\n'; + } + out << indent() << "await send_" << function_name << "("; if(! call_args.empty()) { out << call_args << ", "; } @@ -2489,114 +2594,157 @@ void t_netstd_generator::generate_service_client(ostream& out, t_service* tservi indent_down(); out << indent() << "}" << '\n' << '\n'; - // async send - generate_deprecation_attribute(out, (*functions_iterator)->annotations_); - out << indent() << "public async " << function_signature_async(*functions_iterator, "send_", MODE_NO_RETURN) << '\n' - << indent() << "{" << '\n'; - indent_up(); - - string tmpvar = tmp("tmp"); - string argsname = (*functions_iterator)->get_name() + "_args"; - - out << indent() << "await OutputProtocol.WriteMessageBeginAsync(new TMessage(\"" << raw_func_name - << "\", TMessageType." << ((*functions_iterator)->is_oneway() ? "Oneway" : "Call") - << ", SeqId), " << CANCELLATION_TOKEN_NAME << ").ConfigureAwait(false);" << '\n' - << indent() << '\n' - << indent() << "var " << tmpvar << " = new InternalStructs." << argsname << "() {" << '\n'; - indent_up(); - - t_struct* arg_struct = (*functions_iterator)->get_arglist(); - collect_extensions_types(arg_struct); - prepare_member_name_mapping(arg_struct); - const vector& fields = arg_struct->get_members(); - vector::const_iterator fld_iter; - - for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) + // the shared-protocol operations come first; an isolated client adds a second set that takes the call context + for (int pass = 0; pass < (isolated_ ? 2 : 1); ++pass) { - out << indent() << prop_name(*fld_iter) << " = " << normalize_name((*fld_iter)->get_name(),true) << "," << '\n'; - } + const bool ctx_pass = (pass == 1); + const string out_protocol = ctx_pass ? ctx + ".Protocol" : "OutputProtocol"; + const string in_protocol = ctx_pass ? ctx + ".Protocol" : "InputProtocol"; + const string seq_id = ctx_pass ? ctx + ".SeqId" : "SeqId"; + const int ctx_mode = ctx_pass ? (MODE_CALL_CONTEXT | MODE_NO_TOKEN) : MODE_FULL_DECL; + + // async send + if (ctx_pass) + { + out << indent() << "/// Sends the request of one call. The caller owns and must dispose it after the call has completed. The call's cancellation token and timeout" + << " are those of the context." << '\n'; + } + generate_deprecation_attribute(out, (*functions_iterator)->annotations_); + out << indent() << "public async " << function_signature_async(*functions_iterator, "send_", MODE_NO_RETURN | ctx_mode) << '\n' + << indent() << "{" << '\n'; + indent_up(); + if (ctx_pass) + { + begin_ctx_body(ctx); + } - indent_down(); - out << indent() << "};" << '\n'; + string tmpvar = tmp("tmp"); + string argsname = (*functions_iterator)->get_name() + "_args"; + out << indent() << "await " << out_protocol << ".WriteMessageBeginAsync(new TMessage(\"" << raw_func_name + << "\", TMessageType." << ((*functions_iterator)->is_oneway() ? "Oneway" : "Call") + << ", " << seq_id << "), " << CANCELLATION_TOKEN_NAME << ").ConfigureAwait(false);" << '\n' + << indent() << '\n' + << indent() << "var " << tmpvar << " = new InternalStructs." << argsname << "() {" << '\n'; + indent_up(); - out << indent() << '\n' - << indent() << "await " << tmpvar << ".WriteAsync(OutputProtocol, " << CANCELLATION_TOKEN_NAME << ").ConfigureAwait(false);" << '\n' - << indent() << "await OutputProtocol.WriteMessageEndAsync(" << CANCELLATION_TOKEN_NAME << ").ConfigureAwait(false);" << '\n' - << indent() << "await OutputProtocol.Transport.FlushAsync(" << CANCELLATION_TOKEN_NAME << ").ConfigureAwait(false);" << '\n'; + t_struct* arg_struct = (*functions_iterator)->get_arglist(); + collect_extensions_types(arg_struct); + prepare_member_name_mapping(arg_struct); + const vector& fields = arg_struct->get_members(); + vector::const_iterator fld_iter; - indent_down(); - out << indent() << "}" << '\n' << '\n'; + for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) + { + out << indent() << prop_name(*fld_iter) << " = " << normalize_name((*fld_iter)->get_name(),true) << "," << '\n'; + } - if (!(*functions_iterator)->is_oneway()) - { - // async recv - generate_deprecation_attribute(out, (*functions_iterator)->annotations_); - out << indent() << "public async " << function_signature_async(*functions_iterator, "recv_", MODE_NO_ARGS) << '\n' - << indent() << "{" << '\n'; - indent_up(); + indent_down(); + out << indent() << "};" << '\n'; - string resultname = (*functions_iterator)->get_name() + "_result"; - t_struct noargs(program_); - t_struct* xs = (*functions_iterator)->get_xceptions(); - collect_extensions_types(xs); - prepare_member_name_mapping(xs, xs->get_members(), resultname); - tmpvar = tmp("tmp"); out << indent() << '\n' - << indent() << "var " << tmpvar << " = await InputProtocol.ReadMessageBeginAsync(" << CANCELLATION_TOKEN_NAME << ").ConfigureAwait(false);" << '\n' - << indent() << "if (" << tmpvar << ".Type == TMessageType.Exception)" << '\n' - << indent() << "{" << '\n'; - indent_up(); + << indent() << "await " << tmpvar << ".WriteAsync(" << out_protocol << ", " << CANCELLATION_TOKEN_NAME << ").ConfigureAwait(false);" << '\n' + << indent() << "await " << out_protocol << ".WriteMessageEndAsync(" << CANCELLATION_TOKEN_NAME << ").ConfigureAwait(false);" << '\n' + << indent() << "await " << out_protocol << ".Transport.FlushAsync(" << CANCELLATION_TOKEN_NAME << ").ConfigureAwait(false);" << '\n'; - tmpvar = tmp("tmp"); - out << indent() << "var " << tmpvar << " = await TApplicationException.ReadAsync(InputProtocol, " << CANCELLATION_TOKEN_NAME << ").ConfigureAwait(false);" << '\n' - << indent() << "await InputProtocol.ReadMessageEndAsync(" << CANCELLATION_TOKEN_NAME << ").ConfigureAwait(false);" << '\n' - << indent() << "throw " << tmpvar << ";" << '\n'; + if (ctx_pass) + { + end_ctx_body(ctx, cause); + } indent_down(); + out << indent() << "}" << '\n' << '\n'; - tmpvar = tmp("tmp"); - out << indent() << "}" << '\n' - << '\n' - << indent() << "var " << tmpvar << " = new InternalStructs." << resultname << "();" << '\n' - << indent() << "await " << tmpvar << ".ReadAsync(InputProtocol, " << CANCELLATION_TOKEN_NAME << ").ConfigureAwait(false);" << '\n' - << indent() << "await InputProtocol.ReadMessageEndAsync(" << CANCELLATION_TOKEN_NAME << ").ConfigureAwait(false);" << '\n'; - - if (!(*functions_iterator)->get_returntype()->is_void()) + if (!(*functions_iterator)->is_oneway()) { - out << indent() << "if (" << tmpvar << ".__isset.success)" << '\n' + // async recv + if (ctx_pass) + { + out << indent() << "/// Receives the reply of the call that was sent with ; a reply with a different sequence id is rejected. The call's cancellation token and timeout" + << " are those of the context." << '\n'; + } + generate_deprecation_attribute(out, (*functions_iterator)->annotations_); + out << indent() << "public async " << function_signature_async(*functions_iterator, "recv_", MODE_NO_ARGS | ctx_mode) << '\n' << indent() << "{" << '\n'; indent_up(); - string nullable_value = nullable_value_access((*functions_iterator)->get_returntype()); - out << indent() << "return " << tmpvar << ".Success" << nullable_value << ";" << '\n'; - indent_down(); - out << indent() << "}" << '\n'; - } + if (ctx_pass) + { + begin_ctx_body(ctx); + } - const vector& xceptions = xs->get_members(); - vector::const_iterator x_iter; - for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) - { - out << indent() << "if (" << tmpvar << ".__isset." << get_isset_name(normalize_name((*x_iter)->get_name())) << ")" << '\n' + string resultname = (*functions_iterator)->get_name() + "_result"; + t_struct noargs(program_); + t_struct* xs = (*functions_iterator)->get_xceptions(); + collect_extensions_types(xs); + prepare_member_name_mapping(xs, xs->get_members(), resultname); + + tmpvar = tmp("tmp"); + out << indent() << '\n' + << indent() << "var " << tmpvar << " = await " << in_protocol << ".ReadMessageBeginAsync(" << CANCELLATION_TOKEN_NAME << ").ConfigureAwait(false);" << '\n'; + if (ctx_pass) + { + // a reply (including an exception reply) must belong to this call + out << indent() << ctx << ".ValidateReply(" << tmpvar << ");" << '\n'; + } + out << indent() << "if (" << tmpvar << ".Type == TMessageType.Exception)" << '\n' << indent() << "{" << '\n'; indent_up(); - out << indent() << "throw " << tmpvar << "." << prop_name(*x_iter) << nullable_value_access((*x_iter)->get_type()) << ";" << '\n'; + + tmpvar = tmp("tmp"); + out << indent() << "var " << tmpvar << " = await TApplicationException.ReadAsync(" << in_protocol << ", " << CANCELLATION_TOKEN_NAME << ").ConfigureAwait(false);" << '\n' + << indent() << "await " << in_protocol << ".ReadMessageEndAsync(" << CANCELLATION_TOKEN_NAME << ").ConfigureAwait(false);" << '\n' + << indent() << "throw " << tmpvar << ";" << '\n'; indent_down(); - out << indent() << "}" << '\n'; - } - if (!(*functions_iterator)->get_returntype()->is_void()) - { - out << indent() << "throw new TApplicationException(TApplicationException.ExceptionType.MissingResult, \"" - << function_name << " failed: unknown result\");" << '\n'; + tmpvar = tmp("tmp"); + out << indent() << "}" << '\n' + << '\n' + << indent() << "var " << tmpvar << " = new InternalStructs." << resultname << "();" << '\n' + << indent() << "await " << tmpvar << ".ReadAsync(" << in_protocol << ", " << CANCELLATION_TOKEN_NAME << ").ConfigureAwait(false);" << '\n' + << indent() << "await " << in_protocol << ".ReadMessageEndAsync(" << CANCELLATION_TOKEN_NAME << ").ConfigureAwait(false);" << '\n'; + + if (!(*functions_iterator)->get_returntype()->is_void()) + { + out << indent() << "if (" << tmpvar << ".__isset.success)" << '\n' + << indent() << "{" << '\n'; + indent_up(); + string nullable_value = nullable_value_access((*functions_iterator)->get_returntype()); + out << indent() << "return " << tmpvar << ".Success" << nullable_value << ";" << '\n'; + indent_down(); + out << indent() << "}" << '\n'; + } + + const vector& xceptions = xs->get_members(); + vector::const_iterator x_iter; + for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) + { + out << indent() << "if (" << tmpvar << ".__isset." << get_isset_name(normalize_name((*x_iter)->get_name())) << ")" << '\n' + << indent() << "{" << '\n'; + indent_up(); + out << indent() << "throw " << tmpvar << "." << prop_name(*x_iter) << nullable_value_access((*x_iter)->get_type()) << ";" << '\n'; + indent_down(); + out << indent() << "}" << '\n'; + } + + if (!(*functions_iterator)->get_returntype()->is_void()) + { + out << indent() << "throw new TApplicationException(TApplicationException.ExceptionType.MissingResult, \"" + << function_name << " failed: unknown result\");" << '\n'; + } + + cleanup_member_name_mapping(xs); + if (ctx_pass) + { + end_ctx_body(ctx, cause); + } + indent_down(); + out << indent() << "}" << '\n' << '\n'; } - cleanup_member_name_mapping(xs); - indent_down(); - out << indent() << "}" << '\n' << '\n'; + cleanup_member_name_mapping(arg_struct); } - - cleanup_member_name_mapping(arg_struct); } indent_down(); @@ -4105,6 +4253,56 @@ string t_netstd_generator::function_signature(t_function* tfunction, string pref return type_name(ttype) + " " + func_name(normalize_name(prefix + tfunction->get_name())) + "(" + argument_list(tfunction->get_arglist()) + ")"; } +// A name that no parameter of the method has: the wanted name, with underscores appended until it is free. +// The parameters keep the names of the IDL, so callers that pass arguments by name are not affected by the option. +string t_netstd_generator::free_parameter_name(t_function* tfunction, string wanted, const string& also_taken) +{ + bool taken = true; + while (taken) + { + taken = (wanted == also_taken); + const vector& fields = tfunction->get_arglist()->get_members(); + for (vector::const_iterator f_iter = fields.begin(); !taken && f_iter != fields.end(); ++f_iter) + { + taken = (normalize_name((*f_iter)->get_name(), true) == wanted); + } + if (taken) + { + wanted += "_"; + } + } + return wanted; +} + +string t_netstd_generator::context_variable_name(t_function* tfunction) +{ + return free_parameter_name(tfunction, CALL_CONTEXT_NAME, ""); +} + +string t_netstd_generator::timeout_cause_name(t_function* tfunction) +{ + return free_parameter_name(tfunction, "timeoutCause_", context_variable_name(tfunction)); +} + +string t_netstd_generator::async_method_name(t_function* tfunction, string prefix) +{ + return func_name(normalize_name(prefix + tfunction->get_name()) + (add_async_postfix ? "Async" : "")); +} + +// Whether the client method of that function hides a member that the client base class declares for isolated +// calls. Any method named IsCallIsolated hides that property. CreateCallContext and CreateCallContextAsync are +// hidden only by a method without arguments, whose only parameter is then the cancellation token, as theirs is. +// Such a method is declared "new" (CS0108), and no other one is (CS0109). +bool t_netstd_generator::hides_base_client_member(t_function* tfunction) +{ + const string name = async_method_name(tfunction); + if (name == "IsCallIsolated") { + return true; + } + const bool token_only = tfunction->get_arglist()->get_members().empty(); + return token_only && ((name == "CreateCallContext") || (name == "CreateCallContextAsync")); +} + string t_netstd_generator::function_signature_async(t_function* tfunction, string prefix, int mode) { t_type* ttype = tfunction->get_returntype(); @@ -4114,16 +4312,23 @@ string t_netstd_generator::function_signature_async(t_function* tfunction, strin task += "<" + type_name(ttype) + ">"; } - string result = task + " " + func_name(normalize_name(prefix + tfunction->get_name()) + (add_async_postfix ? "Async" : "")) + "("; + string result = task + " " + async_method_name(tfunction, prefix) + "("; + string parameters; + if((mode & MODE_CALL_CONTEXT) != 0) { + parameters = "global::Thrift.TCallContext " + context_variable_name(tfunction); + } if((mode & MODE_NO_ARGS) == 0) { string args = argument_list(tfunction->get_arglist()); - result += args; if (!args.empty()) { - result += ", "; + parameters += (parameters.empty() ? "" : ", ") + args; } } - result += "CancellationToken " + CANCELLATION_TOKEN_NAME + " = default)"; + // the operations that take a call context use the token of that context + if((mode & MODE_NO_TOKEN) == 0) { + parameters += (parameters.empty() ? "" : ", ") + string("CancellationToken ") + CANCELLATION_TOKEN_NAME + " = default"; + } + result += parameters + ")"; return result; } @@ -4337,5 +4542,6 @@ THRIFT_REGISTER_GENERATOR( " net10: Enable features that require net10 and C# 14 or higher.\n" " net11: Enable features that require net11 and C# 15 or higher.\n" " no_deepcopy: Suppress generation of " + DEEP_COPY_METHOD_NAME + "() method.\n" + " isolated: Generate clients that use a per-call TCallContext (own protocol and transport view per call).\n" " async_postfix: Append \"Async\" to all service methods (maintains compatibility with existing code).\n" ) diff --git a/compiler/cpp/src/thrift/generate/t_netstd_generator.h b/compiler/cpp/src/thrift/generate/t_netstd_generator.h index e18ce920fca..f03cb351449 100644 --- a/compiler/cpp/src/thrift/generate/t_netstd_generator.h +++ b/compiler/cpp/src/thrift/generate/t_netstd_generator.h @@ -46,6 +46,7 @@ using std::vector; static const string DEEP_COPY_METHOD_NAME = "DeepCopy"; static const string CANCELLATION_TOKEN_NAME = "cancellationToken"; +static const string CALL_CONTEXT_NAME = "callContext"; class t_netstd_generator : public t_oop_generator { @@ -64,6 +65,7 @@ class t_netstd_generator : public t_oop_generator bool is_wcf_enabled() const; bool is_hashcode_enabled() const; bool is_serialize_enabled() const; + bool is_isolated_enabled() const; bool is_union_enabled() const; // overrides @@ -135,11 +137,18 @@ class t_netstd_generator : public t_oop_generator static const int MODE_FULL_DECL = 0x00; static const int MODE_NO_RETURN = 0x01; static const int MODE_NO_ARGS = 0x02; + static const int MODE_CALL_CONTEXT = 0x04; + static const int MODE_NO_TOKEN = 0x08; string type_name(t_type* ttype, bool with_namespace = true); string base_type_name(t_base_type* tbase); string declare_field(t_field* tfield, bool init = false, bool allow_nullable = true, string prefix = ""); string function_signature_async(t_function* tfunction, string prefix = "", int mode = MODE_FULL_DECL); + string async_method_name(t_function* tfunction, string prefix = ""); + bool hides_base_client_member(t_function* tfunction); + string free_parameter_name(t_function* tfunction, string wanted, const string& also_taken); + string context_variable_name(t_function* tfunction); + string timeout_cause_name(t_function* tfunction); string function_signature(t_function* tfunction, string prefix = ""); string argument_list(t_struct* tstruct, bool with_types = true); string type_to_enum(t_type* ttype); @@ -173,6 +182,7 @@ class t_netstd_generator : public t_oop_generator bool hashcode_; bool serialize_; bool wcf_; + bool isolated_; bool use_pascal_case_properties; bool suppress_deepcopy; int target_net_version; // 0 = any, 6 = net6, 8 = net8, etc. diff --git a/compiler/cpp/tests/netstd/t_netstd_generator_isolated_tests.cc b/compiler/cpp/tests/netstd/t_netstd_generator_isolated_tests.cc new file mode 100644 index 00000000000..332b8e561cf --- /dev/null +++ b/compiler/cpp/tests/netstd/t_netstd_generator_isolated_tests.cc @@ -0,0 +1,58 @@ +// Licensed to the Apache Software Foundation(ASF) under one +// or more contributor license agreements.See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership.The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "../catch/catch.hpp" +#include +#include + +// The isolated client generation is opt-in: without the option the generator must keep producing +// the shared-protocol clients, so the flag has to default to off. +TEST_CASE("t_netstd_generator keeps isolated client generation off by default", "[initialization]") +{ + t_program program("IsolatedTest.thrift", "netstd"); + map parsed_options; + string option_string = ""; + + t_netstd_generator* gen = nullptr; + REQUIRE_NOTHROW(gen = new t_netstd_generator(&program, parsed_options, option_string)); + REQUIRE_FALSE(gen->is_isolated_enabled()); + delete gen; +} + +TEST_CASE("t_netstd_generator enables isolated client generation with the isolated option", "[initialization]") +{ + t_program program("IsolatedTest.thrift", "netstd"); + map parsed_options = { { "isolated", "" }, { "net10", "" } }; + string option_string = ""; + + t_netstd_generator* gen = nullptr; + REQUIRE_NOTHROW(gen = new t_netstd_generator(&program, parsed_options, option_string)); + REQUIRE(gen->is_isolated_enabled()); + REQUIRE_FALSE(gen->is_wcf_enabled()); + delete gen; +} + +TEST_CASE("t_netstd_generator still rejects unknown options next to isolated", "[initialization]") +{ + t_program program("IsolatedTest.thrift", "netstd"); + map parsed_options = { { "isolated", "" }, { "bogus", "" } }; + string option_string = ""; + + t_netstd_generator* gen = nullptr; + REQUIRE_THROWS(gen = new t_netstd_generator(&program, parsed_options, option_string)); + delete gen; +} diff --git a/lib/netstd/Makefile.am b/lib/netstd/Makefile.am index 025065b984b..210671e56b2 100644 --- a/lib/netstd/Makefile.am +++ b/lib/netstd/Makefile.am @@ -98,6 +98,7 @@ EXTRA_DIST = \ Thrift/GlobalSuppressions.cs \ Thrift/TApplicationException.cs \ Thrift/TBaseClient.cs \ + Thrift/TCallContext.cs \ Thrift/TConfiguration.cs \ Thrift/TException.cs \ Thrift/Thrift.csproj \ diff --git a/lib/netstd/README.md b/lib/netstd/README.md index ed38e3cea5d..d772d443c83 100644 --- a/lib/netstd/README.md +++ b/lib/netstd/README.md @@ -23,6 +23,10 @@ The library ships as two packages so that non-web projects no longer pull in the or - Build with scripts +The test projects under `Tests/Thrift.Compile.Tests` generate their code with the compiler they find first, so rebuild +the compiler after changing a generator. An outdated binary produces code that no longer matches the library and +fails with confusing compile errors. + ## How to build on Unix/Linux - Ensure you have a suitable .NET Core SDK installed, or use the [Ubuntu docker image](../../build/docker/README.md) - Follow common automake build practice: `./ bootstrap && ./ configure && make` @@ -73,6 +77,140 @@ Because of the different environment requirements, migration from C# takes sligh - In case you are using Thrift server event handlers: the `SetEventHandler` method now starts with an uppercase letter - and you will also have to revise the method names of all `TServerEventHandler` descendants you have in your code +# Isolated clients (overlapping calls) + +A generated client normally shares one protocol, and with it one transport, between all of its calls. That is fine for +sequential use, but asynchronous calls that overlap can corrupt each other's request and response data. For transports +that can keep request and response state per call, the generator offers an opt-in `isolated` option: + +```bash +thrift -gen netstd:isolated my.thrift +``` + +The default output is unchanged. With `isolated`, the generated `Client` keeps its constructors that take protocols and +gains a constructor that takes a call-isolated transport and a protocol factory. The constructor you choose decides how +the client behaves: + +```csharp +// shared protocols, exactly as without the option (socketTransport: any TTransport, for example a TSocketTransport) +var legacy = new MyService.Client(new TBinaryProtocol(socketTransport)); + +// per-call transport view and protocol for every call +var transport = new THttpTransport(new Uri("http://localhost:9090/thrift"), new TConfiguration()); +using var client = new MyService.Client(transport, new TBinaryProtocol.Factory()); + +// these calls may overlap +var a = client.add(1, 2); +var b = client.add(3, 4); +await Task.WhenAll(a, b); +``` + +When a service extends another service, the base service needs the `isolated` option as well, because the client of +the derived service builds on the client of the base service. Generate the included IDL files together with `-r`, or +regenerate the base service separately with the option. A client generated with `isolated` over a base client that was +generated without it does not compile. + +Call-isolated transports implement `ICallIsolatedTransport`: `THttpTransport` and `TMemoryBufferTransport`, plus +`TIsolatedBufferedTransport` and `TIsolatedFramedTransport`, which wrap another call-isolated transport. The isolated +layers only accept an `ICallIsolatedTransport`, so stream-based transports cannot be combined with them. Stream +transports share ordered input and are not suitable for overlapping calls. A transport whose call views need +asynchronous setup implements `IAsyncCallIsolatedTransport`; the client then creates each view asynchronously. + +`client.IsCallIsolated` tells which mode a client is in. A client built with a call-isolated transport has no shared +protocols, so its `InputProtocol` and `OutputProtocol` properties throw. + +The legacy `TBufferedTransport` and `TFramedTransport` are not call-isolated. Wrapping a `THttpTransport` in one of +them and passing the result to the call-isolated constructor does not compile, and that is intended: it is not +silently ignored. Use `TIsolatedBufferedTransport` or `TIsolatedFramedTransport` instead. + +## What is shared and what is per call + +Shared by all calls of a client: the transport object and its configuration (for `THttpTransport` that is the +`HttpClient`, its headers, `ContentType`, `ConnectTimeout` and `PerCallTimeout`), the protocol factory and the counter +that hands out sequence IDs. + +Created for every call and released with it: the transport view with its request buffer and its response, the +protocol, the sequence ID, and the cancellation token and timeout of the call. + +## Multiplexed protocol + +The constructor takes a protocol factory, and the factory decides what each call's protocol looks like. To address a +service on a multiplexed server, return a `TMultiplexedProtocol` from the factory: + +```csharp +class MultiplexedFactory : TProtocolFactory +{ + public override TProtocol GetProtocol(TTransport trans) => + new TMultiplexedProtocol(new TBinaryProtocol(trans), "MyService"); +} + +using var client = new MyService.Client(transport, new MultiplexedFactory()); +``` + +## Split send and receive + +Besides the high-level methods, the client exposes `send_` and `recv_`. Without a context they use the +shared protocols, as before. For an isolated client there is a second overload of each that takes the `TCallContext` of +the call, so replies can be received in any order: + +```csharp +using var first = client.CreateCallContext(); +using var second = client.CreateCallContext(); + +await client.send_add(first, 1, 2); +await client.send_add(second, 3, 4); + +var r2 = await client.recv_add(second); +var r1 = await client.recv_add(first); +``` + +The overloads that take a context have no `CancellationToken` parameter: the call's cancellation token and timeout are +those of its context. + +## Timeouts and cancellation + +Two timeouts can apply to a call. A transport can declare a default for all of its calls, for example +`THttpTransport.PerCallTimeout`, and one call can be given a tighter one of its own. Both are linked with the +cancellation token you pass, and the shortest bound wins. A per-call timeout cannot loosen the transport's. + +```csharp +var transport = new THttpTransport(new Uri("http://localhost:9090/thrift"), new TConfiguration()) +{ + PerCallTimeout = TimeSpan.FromSeconds(60), + ConnectTimeout = 60000, // milliseconds, see below +}; +using var client = new MyService.Client(transport, new TBinaryProtocol.Factory()); + +// this call is bounded by 5 seconds instead of 60 +using var context = client.CreateCallContext(TimeSpan.FromSeconds(5)); +await client.send_add(context, 1, 2); +var sum = await client.recv_add(context); +``` + +The clock of a call runs from the creation of its context until the context is disposed, so for split calls it covers +the time between send and receive. A call that exceeds its timeout fails with a `TTransportException` of type +`TimedOut`. A call that the caller cancelled fails as cancelled instead, so the two can be told apart. Neither affects +any other call. + +A third limit applies to every request that `THttpTransport` sends: the timeout of its `HttpClient`, which you set +with `ConnectTimeout` (30 seconds by default). It is not the timeout of the call, so a `PerCallTimeout` that is longer +does not extend it. If you need calls to run longer than 30 seconds, raise `ConnectTimeout` as well, as in the example +above. When this limit elapses, the request fails with a `TTransportException` of type `TimedOut`, like the other +timeouts, and not as a cancellation by the caller. The message text of such an exception differs between .NET and +.NET Framework. + +## Lifetime and ownership + +- The high-level methods create a context, use it for exactly one call and dispose it on success, failure and + cancellation. One-way calls release their context after the request has been sent. A call whose token is already + cancelled creates no transport view at all. +- A context created with `CreateCallContext()` is owned by the caller. Dispose it once its reply has been received, or + when the call is abandoned. Disposing a context releases only that call's transport view. +- Each call gets a sequence ID that is unique within the client. A reply whose ID differs from the ID of its call is + rejected with a `TApplicationException` of type `BadSequenceId`. +- The client owns the shared transport. Disposing the client disposes the transport and rejects new calls, so keep the + client alive until all of its calls have finished. + # Fuzzing We use [SharpFuzz](https://github.com/Metalnem/sharpfuzz) (and its libfuzzer variant) to fuzz the Thrift protocol parsers. This is **not** integrated with oss-fuzz, so all fuzzing must be run locally. **Supported platform: Linux only.** diff --git a/lib/netstd/Tests/Thrift.Compile.Tests/IsolatedCall.thrift b/lib/netstd/Tests/Thrift.Compile.Tests/IsolatedCall.thrift new file mode 100644 index 00000000000..db18deeb0f3 --- /dev/null +++ b/lib/netstd/Tests/Thrift.Compile.Tests/IsolatedCall.thrift @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +// Fixture for the netstd:isolated generator option (per-call TCallContext clients) + +namespace * IsolatedCall + +exception Oops { + 1: string why +} + +service IsolatedBase { + i32 ping(1: i32 value) +} + +service IsolatedService extends IsolatedBase { + void nothing() + string echo(1: string text) throws (1: Oops oops) + i32 add(1: i32 a, 2: i32 b) + // argument names that collide with generated parameters + i32 collide(1: i32 callContext, 2: i32 cancellationToken) + oneway void fire(1: string text) +} diff --git a/lib/netstd/Tests/Thrift.Compile.Tests/IsolatedExceptionName.thrift b/lib/netstd/Tests/Thrift.Compile.Tests/IsolatedExceptionName.thrift new file mode 100644 index 00000000000..99286fd006a --- /dev/null +++ b/lib/netstd/Tests/Thrift.Compile.Tests/IsolatedExceptionName.thrift @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +// Fixture for the netstd:isolated option: the namespace of the service also defines a type named Exception. +// Generated code that took Exception to mean System.Exception would compile and then misbehave at run time. + +namespace * IsolatedExceptionName + +exception Exception { + 1: string why +} + +service Timeouts { + i32 add(1: i32 a, 2: i32 b) throws (1: Exception e) +} diff --git a/lib/netstd/Tests/Thrift.Compile.Tests/IsolatedNames.thrift b/lib/netstd/Tests/Thrift.Compile.Tests/IsolatedNames.thrift new file mode 100644 index 00000000000..0e4ded72e64 --- /dev/null +++ b/lib/netstd/Tests/Thrift.Compile.Tests/IsolatedNames.thrift @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +// Fixture for the netstd:isolated option: names of the IDL that look like names of the generated isolated client. +// The generated client must compile, and the shared-protocol operations must keep the parameter names of the IDL. + +namespace * IsolatedNames + +// the name of a runtime type that the call-context overloads use +struct TCallContext { + 1: i32 x +} + +service Names { + // names of members of the client base class + i32 IsCallIsolated(1: i32 a) + i32 CreateCallContextAsync(1: i32 a) + + // parameter names that look like the generated locals + i32 collide(1: i32 callContext, 2: i32 callContext_, 3: i32 timeoutCause_) +} + +// names of members of the client base class, declared by an ancestor service: in the client of the child service, +// "base" is the client of the parent, not the client base class +service Parent { + i32 IsCallIsolated(1: i32 a) + i32 CreateCallContextAsync(1: i32 a) +} + +service Child extends Parent { + i32 ping(1: i32 a) +} + +// the same names for methods without arguments: the cancellation token is then their only parameter, as it is for +// the methods of the client base class, so they hide those by signature, not only the property by name +service NoArgs { + i32 IsCallIsolated() + i32 CreateCallContext() + i32 CreateCallContextAsync() +} diff --git a/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.net10/Thrift.Compile.net10.csproj b/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.net10/Thrift.Compile.net10.csproj index 2cd1c35cb6e..0f8b7536eec 100644 --- a/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.net10/Thrift.Compile.net10.csproj +++ b/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.net10/Thrift.Compile.net10.csproj @@ -93,6 +93,9 @@ + + + diff --git a/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.net11/Thrift.Compile.net11.csproj b/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.net11/Thrift.Compile.net11.csproj index 1a52d241193..36436b2692e 100644 --- a/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.net11/Thrift.Compile.net11.csproj +++ b/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.net11/Thrift.Compile.net11.csproj @@ -93,6 +93,9 @@ + + + diff --git a/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.netstd2/Thrift.Compile.netstd2.csproj b/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.netstd2/Thrift.Compile.netstd2.csproj index 1145eeee80f..1fd4cdb23a5 100644 --- a/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.netstd2/Thrift.Compile.netstd2.csproj +++ b/lib/netstd/Tests/Thrift.Compile.Tests/Thrift.Compile.netstd2/Thrift.Compile.netstd2.csproj @@ -85,6 +85,9 @@ + + + diff --git a/lib/netstd/Tests/Thrift.Tests/Protocols/TCallContextClientTests.cs b/lib/netstd/Tests/Thrift.Tests/Protocols/TCallContextClientTests.cs new file mode 100644 index 00000000000..872a1ad14b7 --- /dev/null +++ b/lib/netstd/Tests/Thrift.Tests/Protocols/TCallContextClientTests.cs @@ -0,0 +1,167 @@ +// Licensed to the Apache Software Foundation(ASF) under one +// or more contributor license agreements.See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership.The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +using System; +using System.Collections.Concurrent; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Thrift.Protocol; +using Thrift.Protocol.Entities; +using Thrift.Transport; +using Thrift.Transport.Client; + +namespace Thrift.Tests.Protocols +{ + [TestClass] + public class TCallContextClientTests + { + private sealed class TestClient : TBaseClient, IDisposable + { + public TestClient(ICallIsolatedTransport transport) + : base(transport, new TBinaryProtocol.Factory()) { } + } + + /// Hands out memory views and remembers them. + private sealed class TrackingTransport : ICallIsolatedTransport + { + public readonly ConcurrentBag Views = new(); + public bool IsDisposed; + + public TTransport CreateCallTransport() + { + var v = new TMemoryBufferTransport(null); + Views.Add(v); + return v; + } + + public void Dispose() => IsDisposed = true; + } + + [TestMethod] + public void Contexts_Get_Distinct_Protocols_And_Transports() + { + var transport = new TrackingTransport(); + using var client = new TestClient(transport); + using var a = client.CreateCallContext(); + using var b = client.CreateCallContext(); + + Assert.AreNotSame(a.Protocol, b.Protocol); + Assert.AreNotSame(a.Protocol.Transport, b.Protocol.Transport); + Assert.AreNotEqual(a.SeqId, b.SeqId); + } + + [TestMethod] + public async Task Concurrent_Contexts_Get_Unique_Sequence_Ids() + { + using var client = new TestClient(new TrackingTransport()); + var ids = await Task.WhenAll(Enumerable.Range(0, 500).Select(_ => Task.Run(() => + { + using var ctx = client.CreateCallContext(); + return ctx.SeqId; + }))); + Assert.AreEqual(ids.Length, ids.Distinct().Count()); + } + + [TestMethod] + public void Matching_Reply_Is_Accepted_And_Mismatch_Is_Rejected() + { + using var client = new TestClient(new TrackingTransport()); + using var ctx = client.CreateCallContext(); + + ctx.ValidateReply(new TMessage("m", TMessageType.Reply, ctx.SeqId)); + + var ex = Assert.ThrowsExactly( + () => ctx.ValidateReply(new TMessage("m", TMessageType.Reply, ctx.SeqId + 1))); + Assert.AreEqual(TApplicationException.ExceptionType.BadSequenceId, ex.Type); + } + + [TestMethod] + public void Disposing_A_Context_Disposes_Its_Transport_But_Not_Siblings() + { + var transport = new TrackingTransport(); + using var client = new TestClient(transport); + var a = client.CreateCallContext(); + using var b = client.CreateCallContext(); + + a.Dispose(); + a.Dispose(); // idempotent + + Assert.IsFalse(transport.IsDisposed, "the shared transport belongs to the client"); + Assert.IsTrue(b.Protocol.Transport.IsOpen); + } + + [TestMethod] + public void Context_Carries_Its_Own_CancellationToken() + { + using var client = new TestClient(new TrackingTransport()); + using var cts = new CancellationTokenSource(); + using var a = client.CreateCallContext(cts.Token); + using var b = client.CreateCallContext(); + + cts.Cancel(); + Assert.IsTrue(a.CancellationToken.IsCancellationRequested); + Assert.IsFalse(b.CancellationToken.IsCancellationRequested); + } + + [TestMethod] + public void Disposing_Client_Disposes_Shared_Transport_And_Rejects_New_Calls() + { + var transport = new TrackingTransport(); + var client = new TestClient(transport); + client.Dispose(); + + Assert.IsTrue(transport.IsDisposed); + Assert.ThrowsExactly(() => client.CreateCallContext()); + } + + [TestMethod] + public void Legacy_Client_Is_Unchanged() + { + var t = new TMemoryBufferTransport(null); + var p = new TBinaryProtocol(t); + var legacy = new LegacyClient(p); + Assert.AreSame(p, legacy.InputProtocol); + Assert.AreEqual(1, legacy.SeqId); + Assert.AreEqual(2, legacy.SeqId); + Assert.ThrowsExactly(() => legacy.CreateCallContext()); + } + + [TestMethod] + public void Disposing_The_Client_Before_A_Context_Is_Safe_For_The_Caller_Owned_Context() + { + // the shared client may be disposed while a caller still holds a context; releasing the + // context afterwards must neither throw nor touch the already released transport twice + var transport = new TrackingTransport(); + var client = new TestClient(transport); + var ctx = client.CreateCallContext(); + + client.Dispose(); + ctx.Dispose(); + ctx.Dispose(); + + Assert.IsTrue(transport.IsDisposed); + Assert.ThrowsExactly(() => _ = ctx.Protocol); + } + + private sealed class LegacyClient : TBaseClient + { + public LegacyClient(TProtocol p) : base(p, p) { } + } + } +} diff --git a/lib/netstd/Tests/Thrift.Tests/Protocols/TGeneratedClientShapeTests.cs b/lib/netstd/Tests/Thrift.Tests/Protocols/TGeneratedClientShapeTests.cs new file mode 100644 index 00000000000..5721811e2ac --- /dev/null +++ b/lib/netstd/Tests/Thrift.Tests/Protocols/TGeneratedClientShapeTests.cs @@ -0,0 +1,99 @@ +// Licensed to the Apache Software Foundation(ASF) under one +// or more contributor license agreements.See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership.The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +using System; +using System.Linq; +using System.Reflection; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Thrift.Protocol; +using Thrift.Transport; + +namespace Thrift.Tests.Protocols +{ + /// + /// Pins the public shape of generated clients. The default generator output (Cassandra is generated without + /// the isolated option) must keep its shared-protocol API, while the isolated output must + /// expose the call-context API only. + /// + [TestClass] + public class TGeneratedClientShapeTests + { + [TestMethod] + public void Default_Generated_Client_Keeps_Legacy_Constructors_And_Signatures() + { + var client = typeof(Apache.Cassandra.Test.Cassandra.Client); + + var ctors = client.GetConstructors().Select(c => c.GetParameters().Select(p => p.ParameterType).ToArray()).ToList(); + Assert.AreEqual(2, ctors.Count); + Assert.IsTrue(ctors.Any(p => p.SequenceEqual(new[] { typeof(TProtocol) }))); + Assert.IsTrue(ctors.Any(p => p.SequenceEqual(new[] { typeof(TProtocol), typeof(TProtocol) }))); + + foreach (var m in client.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) + { + Assert.IsFalse(m.GetParameters().Any(p => p.ParameterType == typeof(TCallContext)), + $"{m.Name} must not take a TCallContext in default mode"); + } + } + + private static MethodInfo[] Overloads(string name) => + typeof(IsolatedCall.IsolatedService.Client).GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly) + .Where(m => m.Name == name).ToArray(); + + [TestMethod] + public void Isolated_Generated_Client_Is_Dual_Mode() + { + var ctors = typeof(IsolatedCall.IsolatedService.Client).GetConstructors() + .Select(c => c.GetParameters().Select(p => p.ParameterType).ToArray()).ToList(); + + Assert.AreEqual(3, ctors.Count); + Assert.IsTrue(ctors.Any(p => p.SequenceEqual(new[] { typeof(TProtocol) }))); + Assert.IsTrue(ctors.Any(p => p.SequenceEqual(new[] { typeof(TProtocol), typeof(TProtocol) }))); + Assert.IsTrue(ctors.Any(p => p.SequenceEqual(new[] { typeof(ICallIsolatedTransport), typeof(TProtocolFactory) }))); + } + + [TestMethod] + public void Split_Operations_Keep_Their_Signature_And_Gain_A_Context_Overload() + { + foreach (var name in new[] { "send_add", "recv_add" }) + { + var overloads = Overloads(name); + Assert.AreEqual(2, overloads.Length, name); + + var legacy = overloads.Single(m => m.GetParameters().All(p => p.ParameterType != typeof(TCallContext))); + var withContext = overloads.Single(m => m.GetParameters()[0].ParameterType == typeof(TCallContext)); + + // the shared-protocol operation still ends with its cancellation token + Assert.AreEqual(typeof(System.Threading.CancellationToken), legacy.GetParameters().Last().ParameterType, name); + // the context carries the cancellation token, so the overload takes none of its own + Assert.IsTrue(withContext.GetParameters().All(p => p.ParameterType != typeof(System.Threading.CancellationToken)), name); + } + + // the high-level operations keep their public signature + var add = typeof(IsolatedCall.IsolatedService.Client).GetMethod("add")!; + CollectionAssert.AreEqual( + new[] { typeof(int), typeof(int), typeof(System.Threading.CancellationToken) }, + add.GetParameters().Select(p => p.ParameterType).ToArray()); + } + + [TestMethod] + public void Oneway_Operation_Has_Both_Send_Overloads_And_No_Receive_Method() + { + Assert.AreEqual(2, Overloads("send_fire").Length); + Assert.AreEqual(0, Overloads("recv_fire").Length); + } + } +} diff --git a/lib/netstd/Tests/Thrift.Tests/Protocols/TIsolatedGeneratedClientTests.cs b/lib/netstd/Tests/Thrift.Tests/Protocols/TIsolatedGeneratedClientTests.cs new file mode 100644 index 00000000000..d612b222329 --- /dev/null +++ b/lib/netstd/Tests/Thrift.Tests/Protocols/TIsolatedGeneratedClientTests.cs @@ -0,0 +1,335 @@ +// Licensed to the Apache Software Foundation(ASF) under one +// or more contributor license agreements.See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership.The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +using System; +using System.Collections.Concurrent; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using IsolatedCall; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Thrift.Protocol; +using Thrift.Processor; +using Thrift.Protocol.Entities; +using Thrift.Transport; +using Thrift.Transport.Client; + +namespace Thrift.Tests.Protocols +{ + /// Exercises the code generated with netstd:isolated end to end. + [TestClass] + public class TIsolatedGeneratedClientTests + { + private sealed class Handler : IsolatedService.IAsync + { + public readonly ConcurrentBag Fired = new(); + + public Task ping(int value, CancellationToken cancellationToken = default) => Task.FromResult(value + 1000); + public Task nothing(CancellationToken cancellationToken = default) => Task.CompletedTask; + + public Task echo(string? text, CancellationToken cancellationToken = default) + { + if (text == "bad") + { + throw new Oops { Why = "because" }; + } + return Task.FromResult(text ?? ""); + } + + public Task add(int a, int b, CancellationToken cancellationToken = default) => Task.FromResult(a + b); + public Task collide(int callContext, int cancellationToken_, CancellationToken cancellationToken = default) => Task.FromResult(callContext * 100 + cancellationToken_); + + public Task fire(string? text, CancellationToken cancellationToken = default) + { + Fired.Add(text ?? ""); + return Task.CompletedTask; + } + } + + /// Call-isolated transport whose views run each request through the generated processor. + private sealed class LoopbackTransport : ICallIsolatedTransport, IPerCallTimeoutTransport + { + private readonly ITAsyncProcessor _processor; + public readonly ConcurrentBag Views = new(); + public bool IsDisposed; + public int ReplySeqIdOffset; + public TimeSpan PerCallTimeout { get; set; } = Timeout.InfiniteTimeSpan; + + /// Reading a reply waits for this task, to simulate a slow server. + public Task ReplyGate = Task.CompletedTask; + + public LoopbackTransport(IsolatedService.IAsync handler) : this(new IsolatedService.AsyncProcessor(handler)) { } + + public LoopbackTransport(ITAsyncProcessor processor) => _processor = processor; + + public TTransport CreateCallTransport() + { + var v = new View(this); + Views.Add(v); + return v; + } + + public void Dispose() => IsDisposed = true; + + public sealed class View : TEndpointTransport + { + private readonly LoopbackTransport _owner; + private readonly MemoryStream _request = new(); + private TMemoryBufferTransport? _reply; + public bool Disposed; + + public View(LoopbackTransport owner) : base(null) => _owner = owner; + + public override bool IsOpen => !Disposed; + public override Task OpenAsync(CancellationToken cancellationToken) => Task.CompletedTask; + public override void Close() { } + + public override Task WriteAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken) + { + _request.Write(buffer, offset, length); + return Task.CompletedTask; + } + + public override async Task FlushAsync(CancellationToken cancellationToken) + { + cancellationToken.ThrowIfCancellationRequested(); + var input = new TBinaryProtocol(new TMemoryBufferTransport(_request.ToArray(), null)); + var output = new TBinaryProtocol(new TMemoryBufferTransport(null)); + _request.SetLength(0); + await _owner._processor.ProcessAsync(input, output, cancellationToken); + var bytes = ((TMemoryBufferTransport)output.Transport).GetBuffer(); + if (_owner.ReplySeqIdOffset != 0 && bytes.Length > 0) + { + bytes = Rewrite(bytes, _owner.ReplySeqIdOffset); + } + _reply = new TMemoryBufferTransport(bytes, null); + } + + // strict binary: version(4) name-len(4) name(n) seqid(4) + private static byte[] Rewrite(byte[] bytes, int offset) + { + var nameLen = (bytes[4] << 24) | (bytes[5] << 16) | (bytes[6] << 8) | bytes[7]; + var pos = 8 + nameLen; + var seq = (bytes[pos] << 24) | (bytes[pos + 1] << 16) | (bytes[pos + 2] << 8) | bytes[pos + 3]; + seq += offset; + bytes[pos] = (byte)(seq >> 24); bytes[pos + 1] = (byte)(seq >> 16); bytes[pos + 2] = (byte)(seq >> 8); bytes[pos + 3] = (byte)seq; + return bytes; + } + + public override async ValueTask ReadAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken) + { + if (_reply == null) + { + throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No request has been sent"); + } + await _owner.ReplyGate.WaitAsync(cancellationToken); + return await _reply.ReadAsync(buffer, offset, length, cancellationToken); + } + + protected override void Dispose(bool disposing) => Disposed = true; + } + } + + private static (IsolatedService.Client client, LoopbackTransport transport, Handler handler) Create() + { + var handler = new Handler(); + var transport = new LoopbackTransport(handler); + return (new IsolatedService.Client(transport, new TBinaryProtocol.Factory()), transport, handler); + } + + [TestMethod] + public async Task High_Level_Call_Returns_Result_And_Disposes_Its_View() + { + var (client, transport, _) = Create(); + Assert.AreEqual(5, await client.add(2, 3)); + await client.nothing(); + Assert.AreEqual(2, transport.Views.Count); + Assert.IsTrue(transport.Views.All(v => v.Disposed)); + Assert.IsFalse(transport.IsDisposed); + } + + [TestMethod] + public async Task Inherited_Service_Method_Uses_Isolated_Path() + { + var (client, transport, _) = Create(); + Assert.AreEqual(1007, await client.ping(7)); + Assert.IsTrue(transport.Views.All(v => v.Disposed)); + } + + [TestMethod] + public async Task Colliding_Argument_Names_Are_Renamed() + { + var (client, _, _) = Create(); + Assert.AreEqual(305, await client.collide(3, 5)); + } + + [TestMethod] + public async Task Declared_Exception_Propagates_And_View_Is_Disposed() + { + var (client, transport, _) = Create(); + var ex = await Assert.ThrowsExactlyAsync(() => client.echo("bad")); + Assert.AreEqual("because", ex.Why); + Assert.IsTrue(transport.Views.All(v => v.Disposed)); + } + + [TestMethod] + public async Task Oneway_Call_Sends_And_Releases_Its_Context() + { + var (client, transport, handler) = Create(); + await client.fire("go"); + CollectionAssert.AreEqual(new[] { "go" }, handler.Fired.ToArray()); + Assert.IsTrue(transport.Views.All(v => v.Disposed)); + } + + [TestMethod] + public async Task Replies_Can_Be_Received_Out_Of_Order_With_Explicit_Contexts() + { + var (client, transport, _) = Create(); + using var first = client.CreateCallContext(); + using var second = client.CreateCallContext(); + + await client.send_add(first, 1, 2); + await client.send_add(second, 10, 20); + + Assert.AreEqual(30, await client.recv_add(second)); + Assert.AreEqual(3, await client.recv_add(first)); + Assert.IsFalse(transport.Views.Any(v => v.Disposed), "explicit contexts are owned by the caller"); + } + + [TestMethod] + public async Task Concurrent_Calls_Do_Not_Interfere() + { + var (client, _, _) = Create(); + var results = await Task.WhenAll(Enumerable.Range(0, 100).Select(i => Task.Run(() => client.add(i, i)))); + CollectionAssert.AreEqual(Enumerable.Range(0, 100).Select(i => 2 * i).ToArray(), results); + } + + [TestMethod] + public async Task Mismatched_Reply_Sequence_Id_Is_Rejected() + { + var (client, transport, _) = Create(); + transport.ReplySeqIdOffset = 7; + var ex = await Assert.ThrowsExactlyAsync(() => client.add(1, 1)); + Assert.AreEqual(TApplicationException.ExceptionType.BadSequenceId, ex.Type); + Assert.IsTrue(transport.Views.All(v => v.Disposed)); + } + + // ---- dual mode: the isolated-generated client keeps its legacy constructors ------------- + + [TestMethod] + public async Task Legacy_Constructed_Client_Uses_The_Shared_Protocols() + { + var transport = new LoopbackTransport(new Handler()); + using var shared = transport.CreateCallTransport(); + var client = new IsolatedService.Client(new TBinaryProtocol(shared)); + + Assert.IsFalse(client.IsCallIsolated); + Assert.AreEqual(5, await client.add(2, 3)); + Assert.AreEqual(9, await client.add(4, 5)); + + // the split operations without a context use the shared protocol too + await client.send_add(1, 1); + Assert.AreEqual(2, await client.recv_add()); + + Assert.AreEqual(1, transport.Views.Count, "a legacy client must not create call views"); + } + + [TestMethod] + public void Isolated_Constructed_Client_Reports_Its_Mode_And_Has_No_Shared_Protocols() + { + var (client, _, _) = Create(); + Assert.IsTrue(client.IsCallIsolated); + Assert.ThrowsExactly(() => _ = client.InputProtocol); + Assert.ThrowsExactly(() => _ = client.OutputProtocol); + } + + // ---- multiplexed protocol ------------------------------------------------------------ + + private sealed class MultiplexedFactory : TProtocolFactory + { + public override TProtocol GetProtocol(TTransport trans) => + new TMultiplexedProtocol(new TBinaryProtocol(trans), "IsolatedService"); + } + + [TestMethod] + public async Task Protocol_Factory_Can_Return_A_Multiplexed_Protocol() + { + var multiplexer = new TMultiplexedProcessor(); + multiplexer.RegisterProcessor("IsolatedService", new IsolatedService.AsyncProcessor(new Handler())); + var transport = new LoopbackTransport(multiplexer); + var client = new IsolatedService.Client(transport, new MultiplexedFactory()); + + // overlapping calls, each with its own multiplexed protocol + var sums = await Task.WhenAll(client.add(1, 2), client.add(3, 4)); + CollectionAssert.AreEqual(new[] { 3, 7 }, sums); + Assert.IsTrue(transport.Views.All(v => v.Disposed)); + } + + // ---- per-call timeout through the generated client ---------------------------------- + + [TestMethod] + public async Task Transport_Timeout_Fails_A_Slow_High_Level_Call_As_TimedOut() + { + var (client, transport, _) = Create(); + transport.PerCallTimeout = TimeSpan.FromMilliseconds(50); + transport.ReplyGate = new TaskCompletionSource().Task; // the reply never arrives + + var ex = await Assert.ThrowsExactlyAsync(() => client.add(1, 1)); + Assert.AreEqual(TTransportException.ExceptionType.TimedOut, ex.Type); + Assert.IsTrue(transport.Views.All(v => v.Disposed)); + } + + [TestMethod] + public async Task Per_Call_Timeout_Spans_Split_Send_And_Receive() + { + var (client, transport, _) = Create(); + transport.ReplyGate = new TaskCompletionSource().Task; + using var ctx = client.CreateCallContext(TimeSpan.FromMilliseconds(50)); + + await client.send_add(ctx, 1, 1); + var ex = await Assert.ThrowsExactlyAsync(() => client.recv_add(ctx)); + Assert.AreEqual(TTransportException.ExceptionType.TimedOut, ex.Type); + } + + [TestMethod] + public async Task Caller_Cancellation_Of_A_Slow_Call_Is_Not_Reported_As_A_Timeout() + { + var (client, transport, _) = Create(); + transport.PerCallTimeout = TimeSpan.FromMinutes(10); + transport.ReplyGate = new TaskCompletionSource().Task; + using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(50)); + + await Assert.ThrowsAsync(() => client.add(1, 1, cts.Token)); + Assert.IsTrue(transport.Views.All(v => v.Disposed)); + } + + [TestMethod] + public async Task Cancelled_Call_Disposes_Its_View_And_Leaves_Others_Usable() + { + var (client, transport, _) = Create(); + using var cts = new CancellationTokenSource(); + cts.Cancel(); + + // a call that is cancelled before it starts creates no view at all, so nothing can leak + await Assert.ThrowsAsync(() => client.add(1, 1, cts.Token)); + Assert.AreEqual(0, transport.Views.Count); + + Assert.AreEqual(4, await client.add(2, 2)); + } + } +} diff --git a/lib/netstd/Tests/Thrift.Tests/Protocols/TIsolatedGeneratedNameTests.cs b/lib/netstd/Tests/Thrift.Tests/Protocols/TIsolatedGeneratedNameTests.cs new file mode 100644 index 00000000000..1332f676a91 --- /dev/null +++ b/lib/netstd/Tests/Thrift.Tests/Protocols/TIsolatedGeneratedNameTests.cs @@ -0,0 +1,84 @@ +// Licensed to the Apache Software Foundation(ASF) under one +// or more contributor license agreements.See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership.The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +using System; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Thrift.Protocol; +using Thrift.Transport; +using NameCollision = IsolatedExceptionName; + +namespace Thrift.Tests.Protocols +{ + /// + /// The namespace of the service under test defines an exception named Exception. Generated code that + /// meant System.Exception by an unqualified Exception still compiles, and then catches the wrong + /// type: a timeout would not be reported as one. Only a run time test can see that. + /// + [TestClass] + public class TIsolatedGeneratedNameTests + { + /// A call-isolated transport whose server never answers, with a declared per-call timeout. + private sealed class SilentServerTransport : ICallIsolatedTransport, IPerCallTimeoutTransport + { + public TimeSpan PerCallTimeout { get; set; } = TimeSpan.FromMilliseconds(100); + + public TTransport CreateCallTransport() => new SilentView(); + + public void Dispose() { } + + private sealed class SilentView : TEndpointTransport + { + public SilentView() : base(null) { } + + public override bool IsOpen => true; + public override Task OpenAsync(CancellationToken cancellationToken) => Task.CompletedTask; + public override void Close() { } + public override Task WriteAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken) => Task.CompletedTask; + public override Task FlushAsync(CancellationToken cancellationToken) => Task.CompletedTask; + protected override void Dispose(bool disposing) { } + + // the reply never arrives: only cancellation ends the wait + public override async ValueTask ReadAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken) + { + await Task.Delay(Timeout.Infinite, cancellationToken); + return 0; + } + } + } + + [TestMethod] + public async Task Timeout_Is_Reported_As_TimedOut_When_The_Idl_Defines_An_Exception_Type() + { + var client = new NameCollision.Timeouts.Client(new SilentServerTransport(), new TBinaryProtocol.Factory()); + + var ex = await Assert.ThrowsExactlyAsync(() => client.add(1, 2)); + Assert.AreEqual(TTransportException.ExceptionType.TimedOut, ex.Type); + } + + [TestMethod] + public async Task Caller_Cancellation_Is_Still_Not_A_Timeout_When_The_Idl_Defines_An_Exception_Type() + { + var client = new NameCollision.Timeouts.Client(new SilentServerTransport { PerCallTimeout = TimeSpan.FromMinutes(10) }, + new TBinaryProtocol.Factory()); + using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100)); + + await Assert.ThrowsAsync(() => client.add(1, 2, cts.Token)); + } + } +} diff --git a/lib/netstd/Tests/Thrift.Tests/Protocols/TIsolatedGeneratedNamesTests.cs b/lib/netstd/Tests/Thrift.Tests/Protocols/TIsolatedGeneratedNamesTests.cs new file mode 100644 index 00000000000..d1fdadaf9cf --- /dev/null +++ b/lib/netstd/Tests/Thrift.Tests/Protocols/TIsolatedGeneratedNamesTests.cs @@ -0,0 +1,115 @@ +// Licensed to the Apache Software Foundation(ASF) under one +// or more contributor license agreements.See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership.The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +using System; +using System.Linq; +using System.Reflection; +using System.Threading; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Thrift.Protocol; +using Thrift.Transport; +using Thrift.Transport.Client; +using NamesIdl = IsolatedNames; + +namespace Thrift.Tests.Protocols +{ + /// + /// The IDL of the fixture uses names that look like the names of the generated isolated client: a type + /// TCallContext, methods IsCallIsolated, CreateCallContext and CreateCallContextAsync, + /// and parameters callContext, callContext_ and timeoutCause_. Compiling the generated code + /// is the first half of the test; the second half is that the operations keep the parameter names of the IDL. + /// + [TestClass] + public class TIsolatedGeneratedNamesTests + { + private static readonly Type ClientType = typeof(NamesIdl.Names.Client); + + private static MethodInfo[] Overloads(string name) => + ClientType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly) + .Where(m => m.Name == name).ToArray(); + + [TestMethod] + public void Shared_Protocol_And_High_Level_Operations_Keep_The_Parameter_Names_Of_The_Idl() + { + var expected = new[] { "callContext", "callContext_", "timeoutCause_" }; + + var highLevel = Overloads("collide").Single(); + CollectionAssert.AreEqual(expected.Concat(new[] { "cancellationToken" }).ToArray(), + highLevel.GetParameters().Select(p => p.Name).ToArray()); + + var legacySend = Overloads("send_collide") + .Single(m => m.GetParameters().All(p => p.ParameterType != typeof(TCallContext))); + CollectionAssert.AreEqual(expected.Concat(new[] { "cancellationToken" }).ToArray(), + legacySend.GetParameters().Select(p => p.Name).ToArray()); + } + + [TestMethod] + public void The_Context_Parameter_Does_Not_Collide_With_A_Parameter_Of_The_Idl() + { + var withContext = Overloads("send_collide") + .Single(m => m.GetParameters().Any(p => p.ParameterType == typeof(TCallContext))); + var names = withContext.GetParameters().Select(p => p.Name).ToArray(); + + Assert.AreEqual(typeof(TCallContext), withContext.GetParameters()[0].ParameterType, "the runtime type, not the type of the IDL"); + Assert.AreEqual(names.Length, names.Distinct().Count(), "duplicate parameter names: " + string.Join(", ", names)); + CollectionAssert.IsSubsetOf(new[] { "callContext", "callContext_", "timeoutCause_" }, names); + } + + [TestMethod] + public void Service_Methods_Named_Like_Client_Members_Do_Not_Replace_Them() + { + using var client = new NamesIdl.Names.Client(new TMemoryBufferTransport(null), new TBinaryProtocol.Factory()); + + // the property of the client base class still works, next to the methods of the service + Assert.IsTrue(((TBaseClient)client).IsCallIsolated); + Assert.IsNotNull(ClientType.GetMethod("IsCallIsolated", new[] { typeof(int), typeof(CancellationToken) })); + Assert.IsNotNull(ClientType.GetMethod("CreateCallContextAsync", new[] { typeof(int), typeof(CancellationToken) })); + } + + [TestMethod] + public void Service_Methods_Without_Arguments_Take_Only_The_Token_Like_The_Client_Members() + { + // the fixture's methods without arguments must have the parameters of the members of the client base + // class, or compiling them would not show that the generated client declares them new + var noArgsClient = typeof(NamesIdl.NoArgs.Client); + foreach (var name in new[] { "IsCallIsolated", "CreateCallContext", "CreateCallContextAsync" }) + { + var method = noArgsClient.GetMethod(name, + BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); + Assert.IsNotNull(method, name); + CollectionAssert.AreEqual(new[] { typeof(CancellationToken) }, + method.GetParameters().Select(p => p.ParameterType).ToArray(), name); + } + + using var client = new NamesIdl.NoArgs.Client(new TMemoryBufferTransport(null), new TBinaryProtocol.Factory()); + Assert.IsTrue(((TBaseClient)client).IsCallIsolated); + } + + [TestMethod] + public async System.Threading.Tasks.Task A_Child_Client_Does_Not_Bind_To_Members_Of_The_Parent_Client() + { + // "base" of the child client is the parent client, which declares methods with these names; compiling + // the child is the first half, and the child must still start calls through the client base class + using var client = new NamesIdl.Child.Client(new TMemoryBufferTransport(null), new TBinaryProtocol.Factory()); + Assert.IsTrue(((TBaseClient)client).IsCallIsolated); + + using var cts = new CancellationTokenSource(); + cts.Cancel(); + await Assert.ThrowsAsync(() => client.ping(1, cts.Token)); + } + } +} diff --git a/lib/netstd/Tests/Thrift.Tests/Transports/TCallIsolatedLayerTests.cs b/lib/netstd/Tests/Thrift.Tests/Transports/TCallIsolatedLayerTests.cs new file mode 100644 index 00000000000..95901225753 --- /dev/null +++ b/lib/netstd/Tests/Thrift.Tests/Transports/TCallIsolatedLayerTests.cs @@ -0,0 +1,166 @@ +// Licensed to the Apache Software Foundation(ASF) under one +// or more contributor license agreements.See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership.The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +using System; +using System.Linq; +using System.Reflection; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Thrift.Transport; +using Thrift.Transport.Client; + +namespace Thrift.Tests.Transports +{ + [TestClass] + public class TCallIsolatedLayerTests + { + [TestMethod] + public async Task MemoryBuffer_Views_Do_Not_Share_Contents() + { + using var shared = new TMemoryBufferTransport(null); + using var a = shared.CreateCallTransport(); + using var b = shared.CreateCallTransport(); + + await a.WriteAsync(new byte[] { 1, 2, 3 }, CancellationToken.None); + await b.WriteAsync(new byte[] { 9 }, CancellationToken.None); + + Assert.AreEqual(3, ((TMemoryBufferTransport)a).Length); + Assert.AreEqual(1, ((TMemoryBufferTransport)b).Length); + CollectionAssert.AreEqual(new byte[] { 1, 2, 3 }, ((TMemoryBufferTransport)a).GetBuffer()); + CollectionAssert.AreEqual(new byte[] { 9 }, ((TMemoryBufferTransport)b).GetBuffer()); + Assert.AreEqual(0, shared.Length, "the shared transport itself must stay untouched"); + } + + [TestMethod] + public void Buffered_Layer_Creates_Distinct_Layered_Views_Over_Distinct_Inner_Views() + { + using var layer = new TIsolatedBufferedTransport(new TMemoryBufferTransport(null)); + using var a = (TLayeredTransport)layer.CreateCallTransport(); + using var b = (TLayeredTransport)layer.CreateCallTransport(); + + Assert.IsInstanceOfType(a); + Assert.AreNotSame(a, b); + Assert.AreNotSame(a.InnerTransport, b.InnerTransport); + } + + [TestMethod] + public async Task Framed_Layer_Views_Exchange_Independent_Data() + { + using var layer = new TIsolatedFramedTransport(new TMemoryBufferTransport(null)); + using var a = layer.CreateCallTransport(); + using var b = layer.CreateCallTransport(); + Assert.IsInstanceOfType(a); + + await a.WriteAsync(new byte[] { 1, 2 }, CancellationToken.None); + await b.WriteAsync(new byte[] { 7, 8, 9 }, CancellationToken.None); + await a.FlushAsync(CancellationToken.None); + await b.FlushAsync(CancellationToken.None); + + var innerA = (TMemoryBufferTransport)((TLayeredTransport)a).InnerTransport; + var innerB = (TMemoryBufferTransport)((TLayeredTransport)b).InnerTransport; + Assert.AreEqual(4 + 2, innerA.Length); + Assert.AreEqual(4 + 3, innerB.Length); + } + + [TestMethod] + public void Disposing_A_Layered_View_Disposes_Only_Its_Own_Inner_View() + { + using var layer = new TIsolatedBufferedTransport(new TMemoryBufferTransport(null)); + var a = layer.CreateCallTransport(); + using var b = layer.CreateCallTransport(); + a.Dispose(); + Assert.IsTrue(b.IsOpen); + } + + [TestMethod] + public void Isolation_Layers_Only_Accept_CallIsolated_Transports() + { + foreach (var type in new[] { typeof(TIsolatedBufferedTransport), typeof(TIsolatedFramedTransport) }) + { + foreach (var ctor in type.GetConstructors()) + { + Assert.IsTrue(typeof(ICallIsolatedTransport).IsAssignableFrom(ctor.GetParameters()[0].ParameterType), + $"{type.Name} constructor must require ICallIsolatedTransport"); + } + } + } + + [TestMethod] + public async Task Disposed_Memory_Call_View_Reports_NotOpen_For_Every_Operation() + { + using var shared = new TMemoryBufferTransport(null); + var view = shared.CreateCallTransport(); + view.Dispose(); + + Assert.IsFalse(view.IsOpen); + foreach (var operation in new Func[] + { + () => view.WriteAsync(new byte[] { 1 }, 0, 1, CancellationToken.None), + () => view.FlushAsync(CancellationToken.None), + async () => await view.ReadAsync(new byte[1], 0, 1, CancellationToken.None), + }) + { + var ex = await Assert.ThrowsExactlyAsync(operation); + Assert.AreEqual(TTransportException.ExceptionType.NotOpen, ex.Type); + } + + // the shared memory transport, and the legacy use of the class, are not affected + Assert.IsTrue(shared.IsOpen); + } + + [TestMethod] + public async Task Disposed_Memory_Transport_Creates_No_Call_Views() + { + var shared = new TMemoryBufferTransport(null); + shared.Dispose(); + + Assert.ThrowsExactly(() => shared.CreateCallTransport()); + await Assert.ThrowsExactlyAsync( + () => ((IAsyncCallIsolatedTransport)shared).CreateCallTransportAsync(CancellationToken.None)); + } + + private sealed class NoViewTransport : IAsyncCallIsolatedTransport + { + public TTransport CreateCallTransport() => null!; + public Task CreateCallTransportAsync(CancellationToken cancellationToken) => Task.FromResult(null!); + public void Dispose() { } + } + + [TestMethod] + public async Task Layer_Reports_The_Real_Cause_When_The_Inner_Transport_Returns_No_View() + { + using var layer = new TIsolatedBufferedTransport(new NoViewTransport()); + + var sync = Assert.ThrowsExactly(() => layer.CreateCallTransport()); + StringAssert.Contains(sync.Message, "no transport view"); + + var asyncEx = await Assert.ThrowsExactlyAsync( + () => ((IAsyncCallIsolatedTransport)layer).CreateCallTransportAsync(CancellationToken.None)); + StringAssert.Contains(asyncEx.Message, "no transport view"); + } + + [TestMethod] + public void Legacy_Layers_Still_Accept_Any_Transport() + { + using var t = new TBufferedTransport(new TMemoryBufferTransport(null)); + using var f = new TFramedTransport(new TMemoryBufferTransport(null)); + Assert.IsNotNull(t.UnderlyingTransport); + Assert.IsNotNull(f); + } + } +} diff --git a/lib/netstd/Tests/Thrift.Tests/Transports/TCallTimeoutAndAsyncCreationTests.cs b/lib/netstd/Tests/Thrift.Tests/Transports/TCallTimeoutAndAsyncCreationTests.cs new file mode 100644 index 00000000000..22268552992 --- /dev/null +++ b/lib/netstd/Tests/Thrift.Tests/Transports/TCallTimeoutAndAsyncCreationTests.cs @@ -0,0 +1,349 @@ +// Licensed to the Apache Software Foundation(ASF) under one +// or more contributor license agreements.See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership.The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +using System; +using System.Collections.Concurrent; +using System.Linq; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Thrift.Protocol; +using Thrift.Transport; +using Thrift.Transport.Client; + +namespace Thrift.Tests.Transports +{ + [TestClass] + public class TCallTimeoutAndAsyncCreationTests + { + private sealed class TestClient : TBaseClient, IDisposable + { + public TestClient(ICallIsolatedTransport transport) : base(transport, new TBinaryProtocol.Factory()) { } + } + + /// Async-capable transport that declares a timeout and records the views it created. + private sealed class AsyncTimeoutTransport : IAsyncCallIsolatedTransport, IPerCallTimeoutTransport + { + public readonly ConcurrentBag Views = new(); + public TimeSpan PerCallTimeout { get; set; } = Timeout.InfiniteTimeSpan; + public Func? Setup; + + public TTransport CreateCallTransport() + { + // a call view of a memory buffer reports that it is not open once it was disposed + var v = (TMemoryBufferTransport)new TMemoryBufferTransport(null).CreateCallTransport(); + Views.Add(v); + return v; + } + + public async Task CreateCallTransportAsync(CancellationToken cancellationToken) + { + cancellationToken.ThrowIfCancellationRequested(); + if (Setup != null) + { + await Setup(cancellationToken); + } + return CreateCallTransport(); + } + + public void Dispose() { } + } + + private static async Task WaitUntilCancelled(CancellationToken token) + { + try { await Task.Delay(Timeout.Infinite, token); } + catch (OperationCanceledException) { } + } + + // ---- asynchronous creation ------------------------------------------------- + + [TestMethod] + public async Task Async_Creation_Returns_A_View_Owned_By_The_Caller() + { + var transport = new AsyncTimeoutTransport { Setup = async token => await Task.Delay(10, token) }; + using var client = new TestClient(transport); + + using var ctx = await client.CreateCallContextAsync(); + Assert.AreEqual(1, transport.Views.Count); + Assert.IsTrue(ctx.Protocol.Transport.IsOpen); + } + + [TestMethod] + public async Task Async_Creation_Falls_Back_To_Sync_Creation_For_Transports_Without_Async_Setup() + { + var transport = new TMemoryBufferTransport(null); + using var client = new TestClient(transport); + using var ctx = await client.CreateCallContextAsync(); + Assert.IsNotNull(ctx.Protocol); + } + + [TestMethod] + public async Task Cancellation_That_Arrives_During_Creation_Disposes_The_View_Even_When_The_Creator_Ignores_It() + { + // the creator takes 200 ms and does not look at its token; the token is cancelled after 50 ms + var transport = new AsyncTimeoutTransport { Setup = _ => Task.Delay(200) }; + using var layer = new TIsolatedFramedTransport(transport); + using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(50)); + + await Assert.ThrowsAsync( + () => ((IAsyncCallIsolatedTransport)layer).CreateCallTransportAsync(cts.Token)); + + Assert.AreEqual(1, transport.Views.Count, "the creator did create a view"); + Assert.IsTrue(transport.Views.All(v => !v.IsOpen), "the view created in the meantime must be disposed"); + } + + [TestMethod] + public async Task Client_Does_Not_Return_A_Context_When_Cancellation_Arrives_During_Creation() + { + var transport = new AsyncTimeoutTransport { Setup = _ => Task.Delay(200) }; + using var client = new TestClient(transport); + using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(50)); + + await Assert.ThrowsAsync(() => client.CreateCallContextAsync(cts.Token)); + + Assert.AreEqual(1, transport.Views.Count); + Assert.IsTrue(transport.Views.All(v => !v.IsOpen), "the view created in the meantime must be disposed"); + } + + [TestMethod] + public async Task Cancelling_During_Async_Creation_Leaves_No_View() + { + var transport = new AsyncTimeoutTransport(); + using var cts = new CancellationTokenSource(); + cts.Cancel(); + using var client = new TestClient(transport); + + await Assert.ThrowsAsync(() => client.CreateCallContextAsync(cts.Token)); + Assert.AreEqual(0, transport.Views.Count); + } + + [TestMethod] + public async Task Layers_Forward_Async_Creation_And_Dispose_The_Inner_View_On_Failure() + { + var inner = new AsyncTimeoutTransport(); + using var layer = new TIsolatedFramedTransport(inner); + + using var view = await ((IAsyncCallIsolatedTransport)layer).CreateCallTransportAsync(CancellationToken.None); + Assert.IsInstanceOfType(view); + Assert.AreEqual(1, inner.Views.Count); + + using var cts = new CancellationTokenSource(); + cts.Cancel(); + await Assert.ThrowsAsync( + () => ((IAsyncCallIsolatedTransport)layer).CreateCallTransportAsync(cts.Token)); + Assert.AreEqual(1, inner.Views.Count, "no view may be created for a cancelled request"); + } + + [TestMethod] + public async Task Http_And_Memory_Transports_Support_Async_Creation() + { + using var http = new THttpTransport(new HttpClient(), null, new Uri("http://localhost/x")); + using var viaHttp = await ((IAsyncCallIsolatedTransport)http).CreateCallTransportAsync(CancellationToken.None); + Assert.IsNotNull(viaHttp); + + using var memory = new TMemoryBufferTransport(null); + using var viaMemory = await ((IAsyncCallIsolatedTransport)memory).CreateCallTransportAsync(CancellationToken.None); + Assert.IsNotNull(viaMemory); + } + + // ---- synchronous creation and cancellation --------------------------------------- + + [TestMethod] + public void Sync_Creation_Does_Not_Create_A_View_For_A_Cancelled_Token() + { + var transport = new AsyncTimeoutTransport(); + using var client = new TestClient(transport); + using var cts = new CancellationTokenSource(); + cts.Cancel(); + + Assert.Throws(() => client.CreateCallContext(cts.Token)); + Assert.AreEqual(0, transport.Views.Count, "no view may be created for a cancelled request"); + } + + [TestMethod] + public void Sync_Creation_Disposes_The_View_When_Cancellation_Arrives_During_Creation() + { + using var cts = new CancellationTokenSource(); + var transport = new AsyncTimeoutTransport { Setup = _ => Task.CompletedTask }; + var cancelling = new CancellingTransport(transport, cts); + using var client = new TestClient(cancelling); + + Assert.Throws(() => client.CreateCallContext(cts.Token)); + Assert.AreEqual(1, transport.Views.Count, "the creator did create a view"); + Assert.IsTrue(transport.Views.All(v => !v.IsOpen), "the view created in the meantime must be disposed"); + } + + /// Cancels the caller's token while the view is being created, and does not look at it. + private sealed class CancellingTransport : ICallIsolatedTransport + { + private readonly ICallIsolatedTransport _inner; + private readonly CancellationTokenSource _source; + + public CancellingTransport(ICallIsolatedTransport inner, CancellationTokenSource source) + { + _inner = inner; + _source = source; + } + + public TTransport CreateCallTransport() + { + var view = _inner.CreateCallTransport(); + _source.Cancel(); + return view; + } + + public void Dispose() { } + } + + // ---- ownership when the context cannot be built ----------------------------------- + + private sealed class RecordingProtocol : TBinaryProtocol + { + public bool IsProtocolDisposed; + + public RecordingProtocol(TTransport transport) : base(transport) { } + + protected override void Dispose(bool disposing) + { + IsProtocolDisposed = true; + base.Dispose(disposing); + } + } + + private sealed class RecordingFactory : TProtocolFactory + { + public RecordingProtocol? Created; + + public override TProtocol GetProtocol(TTransport trans) => Created = new RecordingProtocol(trans); + } + + [TestMethod] + public void A_Context_That_Cannot_Be_Built_Disposes_The_Protocol_And_The_View() + { + var transport = new AsyncTimeoutTransport(); + var factory = new RecordingFactory(); + using var client = new TBaseClientWithFactory(transport, factory); + + Assert.Throws(() => client.CreateCallContext(TimeSpan.FromSeconds(-1))); + + Assert.IsTrue(factory.Created!.IsProtocolDisposed, "the protocol may hold resources of its own"); + Assert.IsTrue(transport.Views.All(v => !v.IsOpen), "the view must be disposed as well"); + } + + private sealed class TBaseClientWithFactory : TBaseClient, IDisposable + { + public TBaseClientWithFactory(ICallIsolatedTransport transport, TProtocolFactory factory) : base(transport, factory) { } + } + + // ---- per-call timeout --------------------------------------------------------- + + [TestMethod] + public void Transports_Are_Unbounded_Unless_They_Declare_A_Timeout() + { + using var http = new THttpTransport(new HttpClient(), null, new Uri("http://localhost/x")); + Assert.AreEqual(Timeout.InfiniteTimeSpan, http.PerCallTimeout); + http.PerCallTimeout = TimeSpan.FromSeconds(5); + Assert.AreEqual(TimeSpan.FromSeconds(5), http.PerCallTimeout); + + using var layer = new TIsolatedBufferedTransport(http); + Assert.AreEqual(TimeSpan.FromSeconds(5), ((IPerCallTimeoutTransport)layer).PerCallTimeout, "layers report the inner timeout"); + } + + [TestMethod] + public async Task Per_Call_Timeout_Cancels_Only_Its_Own_Call() + { + var transport = new AsyncTimeoutTransport(); + using var client = new TestClient(transport); + using var slow = await client.CreateCallContextAsync(TimeSpan.FromMilliseconds(50)); + using var other = await client.CreateCallContextAsync(); + + await WaitUntilCancelled(slow.CancellationToken); + + Assert.IsTrue(slow.IsTimedOut); + Assert.IsFalse(other.CancellationToken.IsCancellationRequested); + Assert.IsFalse(other.IsTimedOut); + } + + [TestMethod] + public async Task Transport_Timeout_Applies_To_Every_Call() + { + var transport = new AsyncTimeoutTransport { PerCallTimeout = TimeSpan.FromMilliseconds(50) }; + using var client = new TestClient(transport); + using var ctx = await client.CreateCallContextAsync(); + + await WaitUntilCancelled(ctx.CancellationToken); + Assert.IsTrue(ctx.IsTimedOut); + } + + [TestMethod] + public async Task Per_Call_Timeout_Can_Tighten_But_Not_Loosen_The_Transport_Timeout() + { + var transport = new AsyncTimeoutTransport { PerCallTimeout = TimeSpan.FromMilliseconds(400) }; + using var client = new TestClient(transport); + + using var tighter = await client.CreateCallContextAsync(TimeSpan.FromMilliseconds(30)); + await WaitUntilCancelled(tighter.CancellationToken); + Assert.IsTrue(tighter.IsTimedOut); + + using var looser = await client.CreateCallContextAsync(TimeSpan.FromMinutes(10)); + var sw = System.Diagnostics.Stopwatch.StartNew(); + await WaitUntilCancelled(looser.CancellationToken); + Assert.IsTrue(looser.IsTimedOut); + Assert.IsTrue(sw.Elapsed < TimeSpan.FromSeconds(5), "the transport's timeout must still apply"); + } + + [TestMethod] + public async Task Caller_Cancellation_Is_Not_Reported_As_A_Timeout() + { + var transport = new AsyncTimeoutTransport { PerCallTimeout = TimeSpan.FromMinutes(10) }; + using var client = new TestClient(transport); + using var cts = new CancellationTokenSource(); + using var ctx = await client.CreateCallContextAsync(cts.Token); + + cts.Cancel(); + Assert.IsTrue(ctx.CancellationToken.IsCancellationRequested); + Assert.IsFalse(ctx.IsTimedOut); + Assert.IsFalse(ctx.IsTimeoutFailure(new OperationCanceledException())); + } + + [TestMethod] + public async Task Timeout_Failures_Are_Recognized_And_Converted() + { + var transport = new AsyncTimeoutTransport { PerCallTimeout = TimeSpan.FromMilliseconds(30) }; + using var client = new TestClient(transport); + using var ctx = await client.CreateCallContextAsync(); + await WaitUntilCancelled(ctx.CancellationToken); + + Assert.IsTrue(ctx.IsTimeoutFailure(new OperationCanceledException())); + Assert.IsTrue(ctx.IsTimeoutFailure(new TTransportException(TTransportException.ExceptionType.Interrupted, "x"))); + Assert.IsFalse(ctx.IsTimeoutFailure(new InvalidOperationException()), "unrelated failures must pass through"); + + var ex = ctx.CreateTimeoutException(new OperationCanceledException()); + Assert.AreEqual(TTransportException.ExceptionType.TimedOut, ex.Type); + } + + [TestMethod] + public async Task Without_Timeouts_The_Context_Uses_The_Callers_Token_Directly() + { + using var client = new TestClient(new AsyncTimeoutTransport()); + using var cts = new CancellationTokenSource(); + using var ctx = await client.CreateCallContextAsync(cts.Token); + Assert.AreEqual(cts.Token, ctx.CancellationToken, "no linked source is allocated when no timeout is configured"); + } + } +} diff --git a/lib/netstd/Tests/Thrift.Tests/Transports/TCallViewCancellationTests.cs b/lib/netstd/Tests/Thrift.Tests/Transports/TCallViewCancellationTests.cs new file mode 100644 index 00000000000..d0bcc70e89c --- /dev/null +++ b/lib/netstd/Tests/Thrift.Tests/Transports/TCallViewCancellationTests.cs @@ -0,0 +1,91 @@ +// Licensed to the Apache Software Foundation(ASF) under one +// or more contributor license agreements.See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership.The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +using System; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Thrift.Protocol; +using Thrift.Transport; +using Thrift.Transport.Client; + +namespace Thrift.Tests.Transports +{ + /// A cancelled or timed-out call must not keep reading or writing through its transport view. + [TestClass] + public class TCallViewCancellationTests + { + private static CancellationToken Cancelled() + { + var cts = new CancellationTokenSource(); + cts.Cancel(); + return cts.Token; + } + + private static TTransport HttpView() + { + var http = new THttpTransport(new HttpClient(), null, new Uri("http://localhost/x")); + return http.CreateCallTransport(); + } + + private static TTransport MemoryView() => new TMemoryBufferTransport(null).CreateCallTransport(); + + [TestMethod] + public async Task Http_Call_View_Honors_The_Token_Of_The_Convenience_Write_Overload() + { + using var view = HttpView(); + await Assert.ThrowsAsync(() => view.WriteAsync(new byte[] { 1, 2 }, Cancelled())); + } + + [TestMethod] + public async Task Http_Call_View_Honors_The_Token_Of_Write_Through_The_Json_Protocol() + { + // the Json protocol writes its quotes through the overload without offset and count + using var view = HttpView(); + var protocol = new TJsonProtocol(view); + await Assert.ThrowsAsync(() => protocol.WriteStringAsync("a\\b", Cancelled())); + } + + [TestMethod] + public async Task Memory_Call_View_Honors_The_Token_On_Write() + { + using var view = MemoryView(); + await Assert.ThrowsAsync(() => view.WriteAsync(new byte[] { 1, 2 }, 0, 2, Cancelled())); + await Assert.ThrowsAsync(() => view.WriteAsync(new byte[] { 1, 2 }, Cancelled())); + } + + [TestMethod] + public async Task Memory_Call_View_Honors_The_Token_On_Read() + { + using var view = MemoryView(); + await view.WriteAsync(new byte[] { 1, 2, 3 }, CancellationToken.None); + await Assert.ThrowsAsync(() => view.ReadAsync(new byte[3], 0, 3, Cancelled()).AsTask()); + } + + [TestMethod] + public async Task Memory_Call_View_Still_Works_With_A_Live_Token() + { + using var view = MemoryView(); + await view.WriteAsync(new byte[] { 1, 2, 3 }, CancellationToken.None); + ((TMemoryBufferTransport)view).Seek(0, System.IO.SeekOrigin.Begin); + var buffer = new byte[3]; + Assert.AreEqual(3, await view.ReadAsync(buffer, 0, 3, CancellationToken.None)); + CollectionAssert.AreEqual(new byte[] { 1, 2, 3 }, buffer); + } + } +} diff --git a/lib/netstd/Tests/Thrift.Tests/Transports/THttpTransportTests.cs b/lib/netstd/Tests/Thrift.Tests/Transports/THttpTransportTests.cs index ef207281667..dcb1238b6e1 100644 --- a/lib/netstd/Tests/Thrift.Tests/Transports/THttpTransportTests.cs +++ b/lib/netstd/Tests/Thrift.Tests/Transports/THttpTransportTests.cs @@ -5,9 +5,9 @@ // to you under the Apache License, Version 2.0 (the // "License"); you may not use this file except in compliance // with the License. You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, // software distributed under the License is distributed on an // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -17,6 +17,7 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Net; using System.Net.Http; @@ -24,6 +25,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Thrift.Transport; using Thrift.Transport.Client; namespace Thrift.Tests.Transports @@ -57,6 +59,56 @@ private static async Task> SendRequestAsync(THttpTransport transpor return handler.AcceptEncodings[0]; } + private static readonly Uri TestUri = new Uri("http://localhost/thrift"); + + /// Echoes each request body back, prefixed with "re:", after the optional gate completes. + private sealed class EchoHandler : HttpMessageHandler + { + public readonly List Responses = new(); + public Func? Gate; + + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + var body = await request.Content!.ReadAsStringAsync(cancellationToken); + if (Gate != null) + { + await Gate(body).WaitAsync(cancellationToken); + } + + var content = new TrackedContent("re:" + body); + lock (Responses) + { + Responses.Add(content); + } + return new HttpResponseMessage(HttpStatusCode.OK) { Content = content }; + } + } + + private sealed class TrackedContent : StringContent + { + public bool IsDisposed; + public TrackedContent(string s) : base(s) { } + protected override void Dispose(bool disposing) + { + IsDisposed = true; + base.Dispose(disposing); + } + } + + private static async Task WriteRequestAsync(TTransport t, string text, CancellationToken ct = default) + { + var bytes = System.Text.Encoding.UTF8.GetBytes(text); + await t.WriteAsync(bytes, 0, bytes.Length, ct); + await t.FlushAsync(ct); + } + + private static async Task ReadResponseAsync(TTransport t, int len, CancellationToken ct = default) + { + var buf = new byte[len]; + var n = await t.ReadAllAsync(buf, 0, len, ct); + return System.Text.Encoding.UTF8.GetString(buf, 0, n); + } + [TestMethod] public void THttpTransport_Uses_Configured_ConnectionTimeout_Test() { @@ -102,5 +154,240 @@ public async Task THttpTransport_Injected_Client_Keeps_Callers_Accept_Encoding_T var sent = await SendRequestAsync(transport, handler); CollectionAssert.AreEqual(new[] { "gzip" }, sent, "Accept-Encoding sent: " + string.Join(", ", sent)); } + + [TestMethod] + public async Task Overlapping_Calls_Keep_Their_Own_Request_And_Response() + { + var handler = new EchoHandler(); + var release = new TaskCompletionSource(); + var arrived = new TaskCompletionSource(); + handler.Gate = async body => + { + if (body == "A") + { + arrived.TrySetResult(); + await release.Task; // A's response is held until B has completed + } + }; + + using var shared = new THttpTransport(new HttpClient(handler), null, TestUri); + using var callA = shared.CreateCallTransport(); + using var callB = shared.CreateCallTransport(); + + var flushA = WriteRequestAsync(callA, "A"); + await arrived.Task; + await WriteRequestAsync(callB, "B"); + Assert.AreEqual("re:B", await ReadResponseAsync(callB, 4)); + + release.SetResult(); + await flushA; + Assert.AreEqual("re:A", await ReadResponseAsync(callA, 4)); + } + + [TestMethod] + public async Task Disposing_One_Call_Leaves_Sibling_Usable() + { + var handler = new EchoHandler(); + using var shared = new THttpTransport(new HttpClient(handler), null, TestUri); + var callA = shared.CreateCallTransport(); + using var callB = shared.CreateCallTransport(); + + await WriteRequestAsync(callA, "A"); + await WriteRequestAsync(callB, "B"); + Assert.AreEqual("re:A", await ReadResponseAsync(callA, 4)); + callA.Dispose(); + + Assert.IsTrue(handler.Responses[0].IsDisposed, "A's response must be released with its view"); + Assert.IsFalse(handler.Responses[1].IsDisposed, "B's response must stay alive"); + Assert.AreEqual("re:B", await ReadResponseAsync(callB, 4)); + } + + [TestMethod] + public async Task Cancelling_One_Call_Does_Not_Cancel_Sibling() + { + var handler = new EchoHandler(); + var releaseB = new TaskCompletionSource(); + var arrivedA = new TaskCompletionSource(); + handler.Gate = async body => + { + if (body == "A") + { + arrivedA.TrySetResult(); + await Task.Delay(Timeout.Infinite); + } + else + { + await releaseB.Task; + } + }; + + using var shared = new THttpTransport(new HttpClient(handler), null, TestUri); + using var callA = shared.CreateCallTransport(); + using var callB = shared.CreateCallTransport(); + using var cts = new CancellationTokenSource(); + + var flushA = WriteRequestAsync(callA, "A", cts.Token); + await arrivedA.Task; + var flushB = WriteRequestAsync(callB, "B"); + + cts.Cancel(); + var ex = await Assert.ThrowsExactlyAsync(() => flushA); + Assert.AreEqual(TTransportException.ExceptionType.Interrupted, ex.Type); + + releaseB.SetResult(); + await flushB; + Assert.AreEqual("re:B", await ReadResponseAsync(callB, 4)); + } + + [TestMethod] + public async Task Failed_Call_Releases_Its_Resources_And_New_Calls_Are_Rejected_After_Dispose() + { + var handler = new EchoHandler(); + var shared = new THttpTransport(new HttpClient(handler), null, TestUri); + var call = shared.CreateCallTransport(); + call.Dispose(); + var ex = await Assert.ThrowsExactlyAsync(() => WriteRequestAsync(call, "x")); + Assert.AreEqual(TTransportException.ExceptionType.NotOpen, ex.Type); + + shared.Dispose(); + Assert.ThrowsExactly(() => shared.CreateCallTransport()); + } + + [TestMethod] + public async Task Disposed_Http_Call_View_Reports_NotOpen_For_Every_Operation() + { + using var shared = new THttpTransport(new HttpClient(new EchoHandler()), null, TestUri); + var call = shared.CreateCallTransport(); + call.Dispose(); + + Assert.IsFalse(call.IsOpen); + foreach (var operation in new Func[] + { + () => call.WriteAsync(new byte[] { 1 }, 0, 1, CancellationToken.None), + () => call.FlushAsync(CancellationToken.None), + async () => await call.ReadAsync(new byte[1], 0, 1, CancellationToken.None), + }) + { + var ex = await Assert.ThrowsExactlyAsync(operation); + Assert.AreEqual(TTransportException.ExceptionType.NotOpen, ex.Type); + } + } + + // ---- the timeout of the HttpClient also bounds every request of a call view ------------- + + /// A server that never answers; only cancellation ends the request. + private sealed class StalledServerHandler : HttpMessageHandler + { + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + await Task.Delay(Timeout.Infinite, cancellationToken); + return new HttpResponseMessage(HttpStatusCode.OK); + } + } + + private sealed class TestClient : TBaseClient, IDisposable + { + public TestClient(ICallIsolatedTransport transport) : base(transport, new Thrift.Protocol.TBinaryProtocol.Factory()) { } + } + + [TestMethod] + public async Task Http_Client_Timeout_Is_Reported_As_TimedOut() + { + using var shared = new THttpTransport(new HttpClient(new StalledServerHandler()), null, TestUri) { ConnectTimeout = 100 }; + using var call = shared.CreateCallTransport(); + + // nobody cancels: only the timeout of the HttpClient can end the request + var ex = await Assert.ThrowsExactlyAsync(() => WriteRequestAsync(call, "x")); + Assert.AreEqual(TTransportException.ExceptionType.TimedOut, ex.Type); + } + + [TestMethod] + public async Task Caller_Cancellation_Is_Still_Reported_As_Interrupted() + { + using var shared = new THttpTransport(new HttpClient(new StalledServerHandler()), null, TestUri) { ConnectTimeout = 60000 }; + using var call = shared.CreateCallTransport(); + using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(100)); + + var ex = await Assert.ThrowsExactlyAsync(() => WriteRequestAsync(call, "x", cts.Token)); + Assert.AreEqual(TTransportException.ExceptionType.Interrupted, ex.Type); + } + + [TestMethod] + public async Task Longer_Per_Call_Timeout_Does_Not_Extend_The_Http_Client_Timeout() + { + using var shared = new THttpTransport(new HttpClient(new StalledServerHandler()), null, TestUri) + { + ConnectTimeout = 100, + PerCallTimeout = TimeSpan.FromMinutes(10), + }; + using var client = new TestClient(shared); + using var context = await client.CreateCallContextAsync(); + + // the call's own token (linked to the per-call timeout) is not cancelled; the HttpClient timeout is + var started = DateTime.UtcNow; + var ex = await Assert.ThrowsExactlyAsync(() => WriteRequestAsync(context.Protocol.Transport, "x", context.CancellationToken)); + Assert.AreEqual(TTransportException.ExceptionType.TimedOut, ex.Type); + Assert.IsTrue(DateTime.UtcNow - started < TimeSpan.FromSeconds(30), "the per-call timeout must not have extended the HttpClient timeout"); + } + + [TestMethod] + public async Task Late_Response_Is_Released_When_The_View_Was_Disposed_While_The_Request_Was_Pending() + { + var handler = new EchoHandler(); + var release = new TaskCompletionSource(); + var arrived = new TaskCompletionSource(); + handler.Gate = async _ => + { + arrived.TrySetResult(); + await release.Task; + }; + + using var shared = new THttpTransport(new HttpClient(handler), null, TestUri); + var call = shared.CreateCallTransport(); + + var flush = WriteRequestAsync(call, "A"); + await arrived.Task; + call.Dispose(); // the caller gave up while the request is on its way + release.SetResult(); // and the server answers afterwards + + var ex = await Assert.ThrowsExactlyAsync(() => flush); + Assert.AreEqual(TTransportException.ExceptionType.NotOpen, ex.Type); + Assert.HasCount(1, handler.Responses); + Assert.IsTrue(handler.Responses[0].IsDisposed, "the late response must be released with the view"); + } + + [TestMethod] + public async Task Small_Reply_Is_Read_Through_A_Buffered_Layer_When_The_Size_Limit_Is_Below_The_Buffer_Size() + { + // the buffered layer asks the call view for a full buffer (1024 bytes), the reply is 4 bytes + using var shared = new THttpTransport(new HttpClient(new EchoHandler()), new TConfiguration { MaxMessageSize = 100 }, TestUri); + using var layer = new TIsolatedBufferedTransport(shared); + using var view = layer.CreateCallTransport(); + + await WriteRequestAsync(view, "A"); + Assert.AreEqual("re:A", await ReadResponseAsync(view, 4)); + } + + [TestMethod] + public async Task Reply_Above_The_Size_Limit_Is_Still_Rejected_Through_A_Buffered_Layer() + { + using var shared = new THttpTransport(new HttpClient(new EchoHandler()), new TConfiguration { MaxMessageSize = 100 }, TestUri); + using var layer = new TIsolatedBufferedTransport(shared); + using var view = layer.CreateCallTransport(); + + var request = new string('x', 200); // the echo is 203 bytes + await WriteRequestAsync(view, request); + var ex = await Assert.ThrowsExactlyAsync(() => ReadResponseAsync(view, 203)); + Assert.AreEqual(TTransportException.ExceptionType.MessageSizeLimit, ex.Type); + } + + [TestMethod] + public async Task Read_Before_Flush_Reports_NotOpen() + { + using var shared = new THttpTransport(new HttpClient(new EchoHandler()), null, TestUri); + using var call = shared.CreateCallTransport(); + var ex = await Assert.ThrowsExactlyAsync(() => ReadResponseAsync(call, 1)); + Assert.AreEqual(TTransportException.ExceptionType.NotOpen, ex.Type); + } } } diff --git a/lib/netstd/Tests/codegen/run-NetStd-Codegen-Tests.ps1 b/lib/netstd/Tests/codegen/run-NetStd-Codegen-Tests.ps1 index 6e580146348..aa3b0ef9545 100644 --- a/lib/netstd/Tests/codegen/run-NetStd-Codegen-Tests.ps1 +++ b/lib/netstd/Tests/codegen/run-NetStd-Codegen-Tests.ps1 @@ -20,7 +20,8 @@ param( [string[]] $TargetVersions = @(), # override NET_VERSIONS; when empty all defaults are tested [string] $TargetFolder = "", # output dir; when empty uses system temp - [string] $ExtraThriftFiles = "" # additional .thrift search path; when empty none are added + [string] $ExtraThriftFiles = "", # additional .thrift search path; when empty none are added + [switch] $Isolated # generate with the netstd:isolated option (per-call clients) ) #---- failing tests -------------------------------------------- @@ -51,7 +52,10 @@ if ($TargetVersions.Count -gt 0) { $NET_VERSIONS = $TargetVersions } #---- consts -------------------------------------------- -$EXE_EXT = ($OsType -eq "Windows") ? ".exe" : "" +$EXE_EXT = $IsWindows ? ".exe" : "" + +# extra generator options; the isolated mode needs services, so IDL files without them just generate as usual +$GEN_OPTIONS = $Isolated ? ",isolated" : "" #---- functions -------------------------------------------- @@ -127,6 +131,30 @@ function CopyFilesFrom([string] $source, $text) { return $counter } +function TestClientConstructorDocs([string] $gendir, [string] $idl) { + $ok = $true + gci $gendir -filter *.cs -recurse | where { $_.FullName -notmatch '[\\/](obj|bin)[\\/]' } | foreach { + $file = $_ + $lines = get-content $file.FullName + for( $i = 1; $i -lt $lines.Count; $i++) { + # only the constructors of a service client; an IDL type may be called Client as well + if( $lines[$i] -match '^\s*public Client\((TProtocol|global::Thrift\.Transport\.ICallIsolatedTransport) ') { + # a constructor is documented when the line above it is part of an XML documentation comment + $documented = $lines[$i - 1].Trim().StartsWith('///') + if( $Isolated -and (-not $documented)) { + write-host -foregroundcolor red ("Undocumented client constructor in " + $file.Name + " (" + $idl + "): " + $lines[$i].Trim()) + $ok = $false + } + if( (-not $Isolated) -and $documented) { + write-host -foregroundcolor red ("Default output must not document client constructors, " + $file.Name + " (" + $idl + "): " + $lines[$i].Trim()) + $ok = $false + } + } + } + } + return $ok +} + function TestIdlFile([string] $idl) { # expected to fail at Thrift Compiler $filter = $false @@ -143,13 +171,18 @@ function TestIdlFile([string] $idl) { $gendir = [System.IO.Path]::Combine($TARGET, "gen-netstd") $thrift_csproj = [System.IO.Path]::Combine($ROOTDIR, "lib", "netstd", "Thrift", "Thrift.csproj") InitializeFolder $gendir "*.cs" - &$THRIFT_EXE $VERBOSE -r --gen "netstd:$net_version" $idl | out-file ([System.IO.Path]::Combine($TARGET, "thrift.log")) + &$THRIFT_EXE $VERBOSE -r --gen "netstd:$net_version$GEN_OPTIONS" $idl | out-file ([System.IO.Path]::Combine($TARGET, "thrift.log")) if( -not $?) { get-content ([System.IO.Path]::Combine($TARGET, "thrift.log")) | out-default write-host -foregroundcolor red "Thrift compilation failed: $idl" return $false } + # with the isolated option every generated client constructor says when to use it; without it nothing is added + if( -not (TestClientConstructorDocs $gendir $idl)) { + return $false + } + # generate solution $slnxfile = [System.IO.Path]::Combine($gendir, "$TESTAPP.slnx") if( -not (test-path $slnxfile)) { diff --git a/lib/netstd/Thrift/TBaseClient.cs b/lib/netstd/Thrift/TBaseClient.cs index d6d5ac561b0..d24a7211159 100644 --- a/lib/netstd/Thrift/TBaseClient.cs +++ b/lib/netstd/Thrift/TBaseClient.cs @@ -19,6 +19,7 @@ using System.Threading; using System.Threading.Tasks; using Thrift.Protocol; +using Thrift.Transport; namespace Thrift { @@ -32,6 +33,8 @@ public abstract class TBaseClient { private readonly TProtocol _inputProtocol; private readonly TProtocol _outputProtocol; + private readonly ICallIsolatedTransport _callTransport; + private readonly TProtocolFactory _protocolFactory; private bool _isDisposed; private int _seqId; public readonly Guid ClientId = Guid.NewGuid(); @@ -42,13 +45,24 @@ protected TBaseClient(TProtocol inputProtocol, TProtocol outputProtocol) _outputProtocol = outputProtocol ?? throw new ArgumentNullException(nameof(outputProtocol)); } - public TProtocol InputProtocol => _inputProtocol; + /// + /// Creates an isolated client: every call gets its own transport view and protocol through + /// . The client owns + /// and must outlive its calls. + /// + protected TBaseClient(ICallIsolatedTransport transport, TProtocolFactory protocolFactory) + { + _callTransport = transport ?? throw new ArgumentNullException(nameof(transport)); + _protocolFactory = protocolFactory ?? throw new ArgumentNullException(nameof(protocolFactory)); + } - public TProtocol OutputProtocol => _outputProtocol; + public TProtocol InputProtocol => _inputProtocol ?? throw NotSupportedInIsolatedMode(); + + public TProtocol OutputProtocol => _outputProtocol ?? throw NotSupportedInIsolatedMode(); public int SeqId { - get { return ++_seqId; } + get { return Interlocked.Increment(ref _seqId); } } public virtual async Task OpenTransportAsync() @@ -58,6 +72,12 @@ public virtual async Task OpenTransportAsync() public virtual async Task OpenTransportAsync(CancellationToken cancellationToken) { + if (_callTransport != null) + { + cancellationToken.ThrowIfCancellationRequested(); + return; // call views are created on demand and need no shared open + } + if (!_inputProtocol.Transport.IsOpen) { await _inputProtocol.Transport.OpenAsync(cancellationToken).ConfigureAwait(false); @@ -69,6 +89,138 @@ public virtual async Task OpenTransportAsync(CancellationToken cancellationToken } } + /// True when this client was created with a call-isolated transport and uses a context per call. + public bool IsCallIsolated => _callTransport != null; + + /// + /// Starts a new call: creates a transport view and a protocol for it and assigns a unique sequence id. + /// The caller owns the returned context and must dispose it when the call has completed. + /// + /// Cancels the call; combined with the timeouts into the context's token. + public TCallContext CreateCallContext(CancellationToken cancellationToken = default) + { + return CreateCallContextCore(null, cancellationToken); + } + + /// + /// Starts a new call with a timeout of its own. The timeout can only tighten the timeout declared by the + /// transport, never loosen it; the shortest bound wins. + /// + public TCallContext CreateCallContext(TimeSpan timeout, CancellationToken cancellationToken = default) + { + return CreateCallContextCore(timeout, cancellationToken); + } + + /// + /// Starts a new call like , creating the transport view + /// asynchronously when the transport supports it () and + /// synchronously otherwise. + /// + public Task CreateCallContextAsync(CancellationToken cancellationToken = default) + { + return CreateCallContextCoreAsync(null, cancellationToken); + } + + /// Starts a new call with a timeout of its own, see . + public Task CreateCallContextAsync(TimeSpan timeout, CancellationToken cancellationToken = default) + { + return CreateCallContextCoreAsync(timeout, cancellationToken); + } + + private TCallContext CreateCallContextCore(TimeSpan? timeout, CancellationToken cancellationToken) + { + var callTransport = CheckCanStartCall(); + cancellationToken.ThrowIfCancellationRequested(); + + var view = callTransport.CreateCallTransport(); + DisposeIfCancelled(view, cancellationToken); + return CreateContext(view, timeout, cancellationToken); + } + + private async Task CreateCallContextCoreAsync(TimeSpan? timeout, CancellationToken cancellationToken) + { + var callTransport = CheckCanStartCall(); + cancellationToken.ThrowIfCancellationRequested(); + + var view = callTransport is IAsyncCallIsolatedTransport asyncTransport + ? await asyncTransport.CreateCallTransportAsync(cancellationToken).ConfigureAwait(false) + : callTransport.CreateCallTransport(); + + DisposeIfCancelled(view, cancellationToken); + return CreateContext(view, timeout, cancellationToken); + } + + // the creator may finish after the token was cancelled, and may not even look at it + private static void DisposeIfCancelled(TTransport view, CancellationToken cancellationToken) + { + if (view != null && cancellationToken.IsCancellationRequested) + { + view.Dispose(); + cancellationToken.ThrowIfCancellationRequested(); + } + } + + private ICallIsolatedTransport CheckCanStartCall() + { + if (_callTransport == null) + { + throw NotSupportedInLegacyMode(); + } + + if (_isDisposed) + { + throw new ObjectDisposedException(GetType().Name); + } + + return _callTransport; + } + + private TCallContext CreateContext(TTransport view, TimeSpan? timeout, CancellationToken cancellationToken) + { + if (view == null) + { + throw new InvalidOperationException("The call-isolated transport returned no transport view."); + } + + TProtocol protocol = null; + try + { + protocol = _protocolFactory.GetProtocol(view); + return new TCallContext(protocol, SeqId, cancellationToken, EffectiveTimeout(timeout)); + } + catch + { + // the protocol may hold resources of its own, and disposing it disposes the view; when the factory + // failed there is no protocol, and the view is still ours to dispose + protocol?.Dispose(); + view.Dispose(); + throw; + } + } + + // the shortest of the transport's declared timeout and the call's own timeout wins + private TimeSpan? EffectiveTimeout(TimeSpan? callTimeout) + { + var transportTimeout = (_callTransport as IPerCallTimeoutTransport)?.PerCallTimeout ?? Timeout.InfiniteTimeSpan; + if (callTimeout == null || callTimeout.Value == Timeout.InfiniteTimeSpan) + { + return transportTimeout; + } + + if (transportTimeout == Timeout.InfiniteTimeSpan) + { + return callTimeout; + } + + return callTimeout.Value < transportTimeout ? callTimeout : transportTimeout; + } + + private static InvalidOperationException NotSupportedInIsolatedMode() => + new InvalidOperationException("This client uses per-call contexts; use CreateCallContext()."); + + private static InvalidOperationException NotSupportedInLegacyMode() => + new InvalidOperationException("This client was not created with a call-isolated transport."); + public void Dispose() { Dispose(true); @@ -82,6 +234,7 @@ protected virtual void Dispose(bool disposing) { _inputProtocol?.Dispose(); _outputProtocol?.Dispose(); + _callTransport?.Dispose(); } } diff --git a/lib/netstd/Thrift/TCallContext.cs b/lib/netstd/Thrift/TCallContext.cs new file mode 100644 index 00000000000..a79ede8dba2 --- /dev/null +++ b/lib/netstd/Thrift/TCallContext.cs @@ -0,0 +1,115 @@ +// Licensed to the Apache Software Foundation(ASF) under one +// or more contributor license agreements.See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership.The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +using System; +using System.Threading; +using Thrift.Protocol; +using Thrift.Protocol.Entities; +using Thrift.Transport; + +namespace Thrift +{ + /// + /// State of one logical call of an isolated client: its own protocol over its own transport view, its sequence + /// identifier and its cancellation token. The token combines the caller's token with an optional timeout, and the + /// context owns the resources of that combination. Disposing the context disposes the protocol and thereby the + /// per-call transport view; the shared transport is not affected. + /// + public sealed class TCallContext : IDisposable + { + private readonly CancellationToken _callerToken; + private readonly CancellationTokenSource _timeoutSource; + private readonly CancellationTokenSource _linkedSource; + private TProtocol _protocol; + + /// The protocol over this call's own transport view; the context takes ownership. + /// The sequence id assigned to this call. + /// Cancellation token of the caller. + /// Maximum duration of the call, measured from creation of the context until it is + /// disposed; null or for no bound. + public TCallContext(TProtocol protocol, int seqId, CancellationToken cancellationToken = default, TimeSpan? timeout = null) + { + _protocol = protocol ?? throw new ArgumentNullException(nameof(protocol)); + SeqId = seqId; + _callerToken = cancellationToken; + + if (timeout.HasValue && timeout.Value != Timeout.InfiniteTimeSpan) + { + if (timeout.Value < TimeSpan.Zero) + { + throw new ArgumentOutOfRangeException(nameof(timeout), "The timeout must be non-negative or infinite."); + } + + // the caller's token and the timeout are linked; the timeout source tells the two reasons apart + _timeoutSource = new CancellationTokenSource(timeout.Value); + _linkedSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _timeoutSource.Token); + CancellationToken = _linkedSource.Token; + } + else + { + CancellationToken = cancellationToken; // nothing to combine, nothing to allocate + } + } + + public TProtocol Protocol => _protocol ?? throw new ObjectDisposedException(nameof(TCallContext)); + + public int SeqId { get; } + + /// Cancelled when the caller cancels or the call's timeout elapses. + public CancellationToken CancellationToken { get; } + + /// True when the call's timeout elapsed and the caller had not cancelled. + public bool IsTimedOut => _timeoutSource != null && _timeoutSource.IsCancellationRequested && !_callerToken.IsCancellationRequested; + + /// True when is the effect of this call's timeout. + public bool IsTimeoutFailure(Exception exception) + { + if (!IsTimedOut) + { + return false; + } + + return exception is OperationCanceledException + || (exception is TTransportException transportException + && transportException.Type == TTransportException.ExceptionType.Interrupted); + } + + /// Creates the exception reported for a call that exceeded its timeout. + public TTransportException CreateTimeoutException(Exception inner) + { + return new TTransportException(TTransportException.ExceptionType.TimedOut, "The call timed out.", inner); + } + + /// Throws a bad-sequence-id application exception when a reply does not belong to this call. + public void ValidateReply(TMessage message) + { + if (message.SeqID != SeqId) + { + throw new TApplicationException(TApplicationException.ExceptionType.BadSequenceId, + $"{message.Name} failed: out of sequence response (expected {SeqId}, got {message.SeqID})"); + } + } + + public void Dispose() + { + var protocol = Interlocked.Exchange(ref _protocol, null); + protocol?.Dispose(); + _linkedSource?.Dispose(); + _timeoutSource?.Dispose(); + } + } +} diff --git a/lib/netstd/Thrift/Transport/Client/THttpTransport.cs b/lib/netstd/Thrift/Transport/Client/THttpTransport.cs index ad67512b45c..d408a28eb6b 100644 --- a/lib/netstd/Thrift/Transport/Client/THttpTransport.cs +++ b/lib/netstd/Thrift/Transport/Client/THttpTransport.cs @@ -32,7 +32,7 @@ namespace Thrift.Transport.Client { // ReSharper disable once InconsistentNaming - public class THttpTransport : TEndpointTransport + public class THttpTransport : TEndpointTransport, IAsyncCallIsolatedTransport, IPerCallTimeoutTransport { private readonly X509Certificate[] _certificates; private readonly Uri _uri; @@ -112,6 +112,230 @@ public int ConnectTimeout public override bool IsOpen => true; + /// + /// Maximum duration of one call made through a call view, from creation of its context until it is disposed. + /// (the default) means unbounded; the per-request + /// of the underlying client still applies. + /// + public TimeSpan PerCallTimeout { get; set; } = Timeout.InfiniteTimeSpan; + + /// Creates a call view; HTTP call views need no asynchronous setup. + public Task CreateCallTransportAsync(CancellationToken cancellationToken) + { + cancellationToken.ThrowIfCancellationRequested(); + return Task.FromResult(CreateCallTransport()); + } + + /// + /// Creates a transport view for one call. The view owns its request buffer and its response, so calls + /// made through the same THttpTransport can overlap. The caller must dispose the view; the shared + /// transport (and its HttpClient) must outlive it. + /// + public TTransport CreateCallTransport() + { + if (_isDisposed || _httpClient == null) + { + throw new ObjectDisposedException(nameof(THttpTransport)); + } + + return new CallTransport(this, _httpClient); + } + + private sealed class CallTransport : TEndpointTransport + { + private readonly THttpTransport _owner; + private readonly HttpClient _httpClient; + private MemoryStream _outputStream = new MemoryStream(); + private HttpResponseMessage _response; + private Stream _inputStream; + private volatile bool _isDisposed; + + public CallTransport(THttpTransport owner, HttpClient httpClient) + : base(owner.Configuration) + { + _owner = owner; + _httpClient = httpClient; + } + + public override bool IsOpen => !_isDisposed; + + public override Task OpenAsync(CancellationToken cancellationToken) + { + CheckNotDisposed(); + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; + } + + public override void Close() + { + ReleaseResponse(); + } + + public override async ValueTask ReadAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken) + { + CheckNotDisposed(); + cancellationToken.ThrowIfCancellationRequested(); + + if (_inputStream == null) + throw new TTransportException(TTransportException.ExceptionType.NotOpen, "No request has been sent"); + + // A buffered layer asks for a whole buffer, more than a short reply holds. Read at most what the message + // size limit still allows, so that a reply that fits is not rejected because of the size of the request; + // once nothing is left, or when the request is invalid, the limit is enforced as usual. + if (length > RemainingMessageSize && RemainingMessageSize > 0) + { + length = (int)RemainingMessageSize; + } + CheckReadBytesAvailable(length); + + try + { +#if NET5_0_OR_GREATER + var ret = await _inputStream.ReadAsync(new Memory(buffer, offset, length), cancellationToken).ConfigureAwait(false); +#else + var ret = await _inputStream.ReadAsync(buffer, offset, length, cancellationToken).ConfigureAwait(false); +#endif + CountConsumedMessageBytes(ret); + return ret; + } + catch (IOException iox) + { + throw new TTransportException(TTransportException.ExceptionType.Unknown, iox.ToString(), iox); + } + catch (OperationCanceledException ocx) + { + throw new TTransportException(TTransportException.ExceptionType.Interrupted, ocx.Message, ocx); + } + } + + // the base class would pass no token to the overload below, and a cancelled call would keep writing + public override Task WriteAsync(byte[] buffer, CancellationToken cancellationToken) + { + return WriteAsync(buffer, 0, buffer.Length, cancellationToken); + } + + public override async Task WriteAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken) + { + CheckNotDisposed(); + cancellationToken.ThrowIfCancellationRequested(); + +#if NET5_0_OR_GREATER + await _outputStream.WriteAsync(buffer.AsMemory(offset, length), cancellationToken).ConfigureAwait(false); +#else + await _outputStream.WriteAsync(buffer, offset, length, cancellationToken).ConfigureAwait(false); +#endif + } + + public override async Task FlushAsync(CancellationToken cancellationToken) + { + CheckNotDisposed(); + + var request = _outputStream; + _outputStream = new MemoryStream(); + try + { + request.Seek(0, SeekOrigin.Begin); + ReleaseResponse(); + + using (var contentStream = new StreamContent(request)) + { + contentStream.Headers.ContentType = _owner.ContentType ?? new MediaTypeHeaderValue(@"application/x-thrift"); + + // the response is owned by this view and released with it + var response = await _httpClient.PostAsync(_owner._uri, contentStream, cancellationToken).ConfigureAwait(false); + + // the view was disposed while the request was pending: nobody will read this response + if (_isDisposed) + { + response.Dispose(); + throw new TTransportException(TTransportException.ExceptionType.NotOpen, "The call transport has been disposed"); + } + + _response = response; + response.EnsureSuccessStatusCode(); +#if NET5_0_OR_GREATER + _inputStream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false); +#else + _inputStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); +#endif + if (_inputStream.CanSeek) + { + _inputStream.Seek(0, SeekOrigin.Begin); + } + } + } + catch (TTransportException) + { + ReleaseResponse(); + throw; + } + catch (IOException iox) + { + ReleaseResponse(); + throw new TTransportException(TTransportException.ExceptionType.Unknown, iox.ToString(), iox); + } + catch (HttpRequestException wx) + { + ReleaseResponse(); + throw new TTransportException(TTransportException.ExceptionType.Unknown, "Couldn't connect to server: " + wx, wx); + } + catch (OperationCanceledException ocx) + { + ReleaseResponse(); + + // A cancelled request whose own token was not cancelled was ended by the timeout of the HttpClient + // (ConnectTimeout), which bounds every request whatever the per-call timeout is. Report it as a + // timeout so that it can be told apart from a cancellation by the caller. The token of a call that + // ran into its own timeout is cancelled, so the call context still reports that one. + var type = cancellationToken.IsCancellationRequested + ? TTransportException.ExceptionType.Interrupted + : TTransportException.ExceptionType.TimedOut; + throw new TTransportException(type, ocx.Message, ocx); + } + catch (Exception ex) + { + ReleaseResponse(); + throw new TTransportException(TTransportException.ExceptionType.Unknown, ex.Message, ex); + } + finally + { + request.Dispose(); + ResetMessageSizeAndConsumedBytes(); + } + } + + private void ReleaseResponse() + { + _inputStream?.Dispose(); + _inputStream = null; + _response?.Dispose(); + _response = null; + } + + private void CheckNotDisposed() + { + if (_isDisposed) + { + throw new TTransportException(TTransportException.ExceptionType.NotOpen, "The call transport has been disposed"); + } + } + + // Only this view's own resources are released; the shared HttpClient belongs to the owner. + protected override void Dispose(bool disposing) + { + if (!_isDisposed) + { + _isDisposed = true; + if (disposing) + { + ReleaseResponse(); + _outputStream?.Dispose(); + } + } + } + } + + public HttpRequestHeaders RequestHeaders => _httpClient.DefaultRequestHeaders; public MediaTypeHeaderValue ContentType { get; set; } diff --git a/lib/netstd/Thrift/Transport/Client/TMemoryBufferTransport.cs b/lib/netstd/Thrift/Transport/Client/TMemoryBufferTransport.cs index 9acd0e5a778..1c0a5d0ff53 100644 --- a/lib/netstd/Thrift/Transport/Client/TMemoryBufferTransport.cs +++ b/lib/netstd/Thrift/Transport/Client/TMemoryBufferTransport.cs @@ -25,7 +25,7 @@ namespace Thrift.Transport.Client { // ReSharper disable once InconsistentNaming - public class TMemoryBufferTransport : TEndpointTransport + public class TMemoryBufferTransport : TEndpointTransport, IAsyncCallIsolatedTransport { private bool IsDisposed; private byte[] Bytes; @@ -84,6 +84,73 @@ public void SetLength(int value) public override bool IsOpen => true; + /// Creates an empty, independent memory buffer for one call; the caller owns it. + public TTransport CreateCallTransport() + { + if (IsDisposed) + { + throw new ObjectDisposedException(nameof(TMemoryBufferTransport)); + } + + return new CallBuffer(Configuration); + } + + // The call view is a memory buffer that reports that it is not open once the call has disposed it. + // The shared transport, and the use of this class outside of calls, keep their behavior. + private sealed class CallBuffer : TMemoryBufferTransport + { + private bool _isViewDisposed; + + public CallBuffer(TConfiguration config) + : base(config) + { + } + + public override bool IsOpen => !_isViewDisposed; + + // a cancelled or timed-out call must not keep reading or modifying its buffer; the shared class keeps its behavior + public override ValueTask ReadAsync(byte[] buffer, int offset, int length, CancellationToken cancellationToken) + { + CheckOpen(); + cancellationToken.ThrowIfCancellationRequested(); + return base.ReadAsync(buffer, offset, length, cancellationToken); + } + + public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) + { + CheckOpen(); + cancellationToken.ThrowIfCancellationRequested(); + return base.WriteAsync(buffer, offset, count, cancellationToken); + } + + public override Task FlushAsync(CancellationToken cancellationToken) + { + CheckOpen(); + return base.FlushAsync(cancellationToken); + } + + private void CheckOpen() + { + if (_isViewDisposed) + { + throw new TTransportException(TTransportException.ExceptionType.NotOpen, "The call transport has been disposed"); + } + } + + protected override void Dispose(bool disposing) + { + _isViewDisposed = true; + base.Dispose(disposing); + } + } + + /// Creates an independent memory buffer for one call; no asynchronous setup is needed. + public Task CreateCallTransportAsync(CancellationToken cancellationToken) + { + cancellationToken.ThrowIfCancellationRequested(); + return Task.FromResult(CreateCallTransport()); + } + public override Task OpenAsync(CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); diff --git a/lib/netstd/Thrift/Transport/ICallIsolatedTransport.cs b/lib/netstd/Thrift/Transport/ICallIsolatedTransport.cs new file mode 100644 index 00000000000..26db828a7e1 --- /dev/null +++ b/lib/netstd/Thrift/Transport/ICallIsolatedTransport.cs @@ -0,0 +1,64 @@ +// Licensed to the Apache Software Foundation(ASF) under one +// or more contributor license agreements.See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership.The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace Thrift.Transport +{ + /// + /// A shared transport that can hand out an independent transport view for each logical call. + /// + /// + /// Each view owns its request and response state. The caller of is + /// responsible for disposing the returned view; disposing a view never releases the shared transport or any + /// sibling view. The shared transport must outlive the calls made through it. Stream-based transports share + /// ordered input and therefore do not implement this interface. + /// + public interface ICallIsolatedTransport : IDisposable + { + /// + /// Creates a transport view for exactly one call. The caller owns and must dispose the view. + /// + /// The shared transport has been disposed. + TTransport CreateCallTransport(); + } + + /// + /// A call-isolated transport whose call views need asynchronous setup (for example acquiring a connection). + /// Transports without such setup implement only . + /// + public interface IAsyncCallIsolatedTransport : ICallIsolatedTransport + { + /// + /// Creates a transport view for exactly one call. The caller owns and must dispose the view. When the token + /// is cancelled before creation completes, no view is left undisposed. + /// + Task CreateCallTransportAsync(CancellationToken cancellationToken); + } + + /// + /// A call-isolated transport that declares the maximum duration of one call. Transports that do not implement + /// this interface are unbounded. + /// + public interface IPerCallTimeoutTransport + { + /// The maximum duration of one call, or when unbounded. + TimeSpan PerCallTimeout { get; } + } +} diff --git a/lib/netstd/Thrift/Transport/Layered/TIsolatedLayeredTransport.cs b/lib/netstd/Thrift/Transport/Layered/TIsolatedLayeredTransport.cs new file mode 100644 index 00000000000..c7143d45a6e --- /dev/null +++ b/lib/netstd/Thrift/Transport/Layered/TIsolatedLayeredTransport.cs @@ -0,0 +1,141 @@ +// Licensed to the Apache Software Foundation(ASF) under one +// or more contributor license agreements.See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership.The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace Thrift.Transport +{ + /// + /// Base class for layers that preserve call isolation: every call view wraps a distinct inner call view. + /// + /// + /// The constructor only accepts an , so composing a layer over a transport + /// without per-call isolation is rejected at compile time. Disposing the layer disposes the shared inner transport. + /// + public abstract class TIsolatedLayeredTransport : IAsyncCallIsolatedTransport, IPerCallTimeoutTransport + { + protected readonly ICallIsolatedTransport InnerTransport; + private bool _isDisposed; + + protected TIsolatedLayeredTransport(ICallIsolatedTransport transport) + { + InnerTransport = transport ?? throw new ArgumentNullException(nameof(transport)); + } + + /// Wraps a freshly created inner call view in this layer's per-call transport. + protected abstract TTransport CreateLayer(TTransport callTransport); + + public TTransport CreateCallTransport() + { + if (_isDisposed) + { + throw new ObjectDisposedException(GetType().Name); + } + + var inner = RequireView(InnerTransport.CreateCallTransport()); + try + { + return CreateLayer(inner); + } + catch + { + inner.Dispose(); + throw; + } + } + + public async Task CreateCallTransportAsync(CancellationToken cancellationToken) + { + if (_isDisposed) + { + throw new ObjectDisposedException(GetType().Name); + } + + cancellationToken.ThrowIfCancellationRequested(); + var inner = RequireView(InnerTransport is IAsyncCallIsolatedTransport asyncInner + ? await asyncInner.CreateCallTransportAsync(cancellationToken).ConfigureAwait(false) + : InnerTransport.CreateCallTransport()); + + // the creator may finish after the token was cancelled, and may not even look at it + if (cancellationToken.IsCancellationRequested) + { + inner.Dispose(); + cancellationToken.ThrowIfCancellationRequested(); + } + + try + { + return CreateLayer(inner); + } + catch + { + inner.Dispose(); + throw; + } + } + + // an inner transport that returns no view is a bug of that transport; report it as such, and do not try to + // clean up a view that does not exist, which would hide the real cause + private static TTransport RequireView(TTransport view) + { + return view ?? throw new InvalidOperationException("The inner call-isolated transport returned no transport view."); + } + + /// The timeout of the inner transport; layers add none of their own. + public TimeSpan PerCallTimeout => (InnerTransport as IPerCallTimeoutTransport)?.PerCallTimeout ?? Timeout.InfiniteTimeSpan; + + public void Dispose() + { + if (!_isDisposed) + { + _isDisposed = true; + InnerTransport.Dispose(); + } + } + } + + // ReSharper disable once InconsistentNaming + public class TIsolatedBufferedTransport : TIsolatedLayeredTransport + { + private readonly int _bufSize; + + public TIsolatedBufferedTransport(ICallIsolatedTransport transport, int bufSize = 1024) + : base(transport) + { + if (bufSize <= 0) + { + throw new ArgumentOutOfRangeException(nameof(bufSize), "Buffer size must be a positive number."); + } + _bufSize = bufSize; + } + + protected override TTransport CreateLayer(TTransport callTransport) => new TBufferedTransport(callTransport, _bufSize); + } + + // ReSharper disable once InconsistentNaming + public class TIsolatedFramedTransport : TIsolatedLayeredTransport + { + public TIsolatedFramedTransport(ICallIsolatedTransport transport) + : base(transport) + { + } + + protected override TTransport CreateLayer(TTransport callTransport) => new TFramedTransport(callTransport); + } +} diff --git a/tutorial/netstd/Client/Program.cs b/tutorial/netstd/Client/Program.cs index 29e3f3c3597..3cbb7a7c959 100644 --- a/tutorial/netstd/Client/Program.cs +++ b/tutorial/netstd/Client/Program.cs @@ -63,7 +63,7 @@ private static void DisplayHelp() Client -help will diplay help information - Client -tr: -bf: -pr: [-mc:] [-multiplex] + Client -tr: -bf: -pr: [-mc:] [-multiplex] [-isolated] will run client with specified arguments (tcp transport and binary protocol by default) and with 1 client Options: @@ -85,6 +85,10 @@ will run client with specified arguments (tcp transport and binary protocol by d -multiplex - adds multiplexed protocol + -isolated - every call gets its own transport view and protocol, so calls can overlap on one client. + Needs a call-isolated transport, so it works with -tr:http only, and not with -multiplex or -bf:framed. + Besides the usual calls, the client then runs overlapping calls and receives replies out of order. + -mc (multiple clients): - number of multiple clients to connect to server (max 100, default 1) @@ -130,9 +134,32 @@ private static async Task RunAsync(string[] args, CancellationToken cancel if (Logger.IsEnabled(LogLevel.Information)) Logger.LogInformation("Multiplex {mplex}", mplex); + var isolated = args.Any(x => x.Equals("-isolated")); + if (isolated && (transport != Transport.Http || mplex)) + { + if (Logger.IsEnabled(LogLevel.Error)) + Logger.LogError("-isolated needs a call-isolated transport: use -tr:http, without -multiplex"); + return false; + } + + // the HTTP server of the tutorial reads unframed requests + if (isolated && GetBuffering(args) == Buffering.Framed) + { + if (Logger.IsEnabled(LogLevel.Error)) + Logger.LogError("-isolated does not work with -bf:framed: the HTTP server of the tutorial reads unframed requests"); + return false; + } + var tasks = new Task[numClients]; for (int i = 0; i < numClients; i++) { + if (isolated) + { + // each client shares one call-isolated transport between all of its calls + tasks[i] = RunIsolatedClientAsync(args, cancellationToken); + continue; + } + // each client gets a transport and protocol of its own var protocol = MakeProtocol(args, MakeTransport(args)); var task = RunClientAsync(protocol, mplex, cancellationToken); @@ -309,6 +336,75 @@ private static TProtocol MakeProtocol(string[] args, TTransport transport) }; } + private static ICallIsolatedTransport MakeIsolatedTransport(string[] args) + { + // the call views of this transport own their request and response, so calls can overlap + ICallIsolatedTransport transport = new THttpTransport(new Uri("http://localhost:9090"), Configuration); + return GetBuffering(args) switch + { + Buffering.Buffered => new TIsolatedBufferedTransport(transport), + Buffering.Framed => new TIsolatedFramedTransport(transport), + _ => transport, + }; + } + + private static TProtocolFactory MakeProtocolFactory(string[] args) + { + return GetProtocol(args) switch + { + Protocol.Binary => new TBinaryProtocol.Factory(), + Protocol.Compact => new TCompactProtocol.Factory(), + Protocol.Json => new TJsonProtocol.Factory(), + _ => throw new Exception("unhandled protocol"), + }; + } + + private static async Task RunIsolatedClientAsync(string[] args, CancellationToken cancellationToken) + { + try + { + // disposing the client disposes the shared transport; keep it alive until all calls have finished + using var client = new Calculator.Client(MakeIsolatedTransport(args), MakeProtocolFactory(args)); + + await ExecuteCalculatorClientOperations(client, cancellationToken); + await ExecuteOverlappingCalls(client, cancellationToken); + return true; + } + catch (Exception ex) + { + if (Logger.IsEnabled(LogLevel.Error)) + Logger.LogError("{ex}", ex); + } + return false; + } + + private static async Task ExecuteOverlappingCalls(Calculator.Client client, CancellationToken cancellationToken) + { + if (Logger.IsEnabled(LogLevel.Information)) + Logger.LogInformation("{client.ClientId} Add() x5, all calls in flight at once", client.ClientId); + + // every call gets its own transport view, so the requests and responses cannot get mixed up + var sums = await Task.WhenAll(Enumerable.Range(1, 5).Select(i => client.add(i, i, cancellationToken))); + for (int i = 1; i <= 5; i++) + { + if (sums[i - 1] != 2 * i) + throw new InvalidOperationException($"Add({i},{i}) returned {sums[i - 1]}"); + } + + if (Logger.IsEnabled(LogLevel.Information)) + Logger.LogInformation("{client.ClientId} send_add() twice, then recv_add() in the opposite order", client.ClientId); + + // with explicit call contexts the requests and replies of the calls can be interleaved freely + using var first = client.CreateCallContext(cancellationToken); + using var second = client.CreateCallContext(cancellationToken); + await client.send_add(first, 1, 2); + await client.send_add(second, 3, 4); + var secondSum = await client.recv_add(second); + var firstSum = await client.recv_add(first); + if (firstSum != 3 || secondSum != 7) + throw new InvalidOperationException($"Interleaved calls returned {firstSum} and {secondSum}"); + } + private static async Task RunClientAsync(TProtocol protocol, bool multiplex, CancellationToken cancellationToken) { try diff --git a/tutorial/netstd/Interfaces/Interfaces.csproj b/tutorial/netstd/Interfaces/Interfaces.csproj index 83aa2e2d8c8..4e0ab67bd57 100644 --- a/tutorial/netstd/Interfaces/Interfaces.csproj +++ b/tutorial/netstd/Interfaces/Interfaces.csproj @@ -43,8 +43,8 @@ - - - + + + diff --git a/tutorial/netstd/README.md b/tutorial/netstd/README.md index 8301e2702ed..559fc1020d1 100644 --- a/tutorial/netstd/README.md +++ b/tutorial/netstd/README.md @@ -99,9 +99,17 @@ Options: -mc (multiple clients): - number of multiple clients to connect to server (max 100, default 1) + -isolated: + every call gets its own transport view and protocol, so calls can overlap on one client. This needs a + call-isolated transport, so it works with -tr:http only (not with -multiplex or -bf:framed). The client runs five + overlapping calls and then receives the replies of two calls in the opposite order of their requests. The + tutorial interfaces are generated with the `isolated` option for this. See "Isolated clients" in + lib/netstd/README.md. + Sample: Client -tr:tcp -pr:binary -mc:10 + Client -tr:http -isolated Remarks: diff --git a/tutorial/netstd/smoketest.sh b/tutorial/netstd/smoketest.sh index f786b084a20..52daf8ac573 100755 --- a/tutorial/netstd/smoketest.sh +++ b/tutorial/netstd/smoketest.sh @@ -36,6 +36,7 @@ CASES=( "-tr:tcptls" "-tr:namedpipe" "-tr:http" + "-tr:http -isolated" ) cd "$(dirname "$0")" || exit 1