mirror of https://github.com/grpc/grpc.git
commit
b357f2f548
114 changed files with 23677 additions and 8238 deletions
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,178 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015 gRPC authors. |
||||
* |
||||
* Licensed 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 <grpc/support/port_platform.h> |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
|
||||
#include "src/core/ext/filters/client_channel/method_params.h" |
||||
#include "src/core/ext/filters/client_channel/status_util.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "src/core/lib/gprpp/memory.h" |
||||
|
||||
// As per the retry design, we do not allow more than 5 retry attempts.
|
||||
#define MAX_MAX_RETRY_ATTEMPTS 5 |
||||
|
||||
namespace grpc_core { |
||||
namespace internal { |
||||
|
||||
namespace { |
||||
|
||||
bool ParseWaitForReady( |
||||
grpc_json* field, ClientChannelMethodParams::WaitForReady* wait_for_ready) { |
||||
if (field->type != GRPC_JSON_TRUE && field->type != GRPC_JSON_FALSE) { |
||||
return false; |
||||
} |
||||
*wait_for_ready = field->type == GRPC_JSON_TRUE |
||||
? ClientChannelMethodParams::WAIT_FOR_READY_TRUE |
||||
: ClientChannelMethodParams::WAIT_FOR_READY_FALSE; |
||||
return true; |
||||
} |
||||
|
||||
// Parses a JSON field of the form generated for a google.proto.Duration
|
||||
// proto message, as per:
|
||||
// https://developers.google.com/protocol-buffers/docs/proto3#json
|
||||
bool ParseDuration(grpc_json* field, grpc_millis* duration) { |
||||
if (field->type != GRPC_JSON_STRING) return false; |
||||
size_t len = strlen(field->value); |
||||
if (field->value[len - 1] != 's') return false; |
||||
UniquePtr<char> buf(gpr_strdup(field->value)); |
||||
*(buf.get() + len - 1) = '\0'; // Remove trailing 's'.
|
||||
char* decimal_point = strchr(buf.get(), '.'); |
||||
int nanos = 0; |
||||
if (decimal_point != nullptr) { |
||||
*decimal_point = '\0'; |
||||
nanos = gpr_parse_nonnegative_int(decimal_point + 1); |
||||
if (nanos == -1) { |
||||
return false; |
||||
} |
||||
int num_digits = static_cast<int>(strlen(decimal_point + 1)); |
||||
if (num_digits > 9) { // We don't accept greater precision than nanos.
|
||||
return false; |
||||
} |
||||
for (int i = 0; i < (9 - num_digits); ++i) { |
||||
nanos *= 10; |
||||
} |
||||
} |
||||
int seconds = |
||||
decimal_point == buf.get() ? 0 : gpr_parse_nonnegative_int(buf.get()); |
||||
if (seconds == -1) return false; |
||||
*duration = seconds * GPR_MS_PER_SEC + nanos / GPR_NS_PER_MS; |
||||
return true; |
||||
} |
||||
|
||||
UniquePtr<ClientChannelMethodParams::RetryPolicy> ParseRetryPolicy( |
||||
grpc_json* field) { |
||||
auto retry_policy = MakeUnique<ClientChannelMethodParams::RetryPolicy>(); |
||||
if (field->type != GRPC_JSON_OBJECT) return nullptr; |
||||
for (grpc_json* sub_field = field->child; sub_field != nullptr; |
||||
sub_field = sub_field->next) { |
||||
if (sub_field->key == nullptr) return nullptr; |
||||
if (strcmp(sub_field->key, "maxAttempts") == 0) { |
||||
if (retry_policy->max_attempts != 0) return nullptr; // Duplicate.
|
||||
if (sub_field->type != GRPC_JSON_NUMBER) return nullptr; |
||||
retry_policy->max_attempts = gpr_parse_nonnegative_int(sub_field->value); |
||||
if (retry_policy->max_attempts <= 1) return nullptr; |
||||
if (retry_policy->max_attempts > MAX_MAX_RETRY_ATTEMPTS) { |
||||
gpr_log(GPR_ERROR, |
||||
"service config: clamped retryPolicy.maxAttempts at %d", |
||||
MAX_MAX_RETRY_ATTEMPTS); |
||||
retry_policy->max_attempts = MAX_MAX_RETRY_ATTEMPTS; |
||||
} |
||||
} else if (strcmp(sub_field->key, "initialBackoff") == 0) { |
||||
if (retry_policy->initial_backoff > 0) return nullptr; // Duplicate.
|
||||
if (!ParseDuration(sub_field, &retry_policy->initial_backoff)) { |
||||
return nullptr; |
||||
} |
||||
if (retry_policy->initial_backoff == 0) return nullptr; |
||||
} else if (strcmp(sub_field->key, "maxBackoff") == 0) { |
||||
if (retry_policy->max_backoff > 0) return nullptr; // Duplicate.
|
||||
if (!ParseDuration(sub_field, &retry_policy->max_backoff)) { |
||||
return nullptr; |
||||
} |
||||
if (retry_policy->max_backoff == 0) return nullptr; |
||||
} else if (strcmp(sub_field->key, "backoffMultiplier") == 0) { |
||||
if (retry_policy->backoff_multiplier != 0) return nullptr; // Duplicate.
|
||||
if (sub_field->type != GRPC_JSON_NUMBER) return nullptr; |
||||
if (sscanf(sub_field->value, "%f", &retry_policy->backoff_multiplier) != |
||||
1) { |
||||
return nullptr; |
||||
} |
||||
if (retry_policy->backoff_multiplier <= 0) return nullptr; |
||||
} else if (strcmp(sub_field->key, "retryableStatusCodes") == 0) { |
||||
if (!retry_policy->retryable_status_codes.Empty()) { |
||||
return nullptr; // Duplicate.
|
||||
} |
||||
if (sub_field->type != GRPC_JSON_ARRAY) return nullptr; |
||||
for (grpc_json* element = sub_field->child; element != nullptr; |
||||
element = element->next) { |
||||
if (element->type != GRPC_JSON_STRING) return nullptr; |
||||
grpc_status_code status; |
||||
if (!grpc_status_code_from_string(element->value, &status)) { |
||||
return nullptr; |
||||
} |
||||
retry_policy->retryable_status_codes.Add(status); |
||||
} |
||||
if (retry_policy->retryable_status_codes.Empty()) return nullptr; |
||||
} |
||||
} |
||||
// Make sure required fields are set.
|
||||
if (retry_policy->max_attempts == 0 || retry_policy->initial_backoff == 0 || |
||||
retry_policy->max_backoff == 0 || retry_policy->backoff_multiplier == 0 || |
||||
retry_policy->retryable_status_codes.Empty()) { |
||||
return nullptr; |
||||
} |
||||
return retry_policy; |
||||
} |
||||
|
||||
} // namespace
|
||||
|
||||
RefCountedPtr<ClientChannelMethodParams> |
||||
ClientChannelMethodParams::CreateFromJson(const grpc_json* json) { |
||||
RefCountedPtr<ClientChannelMethodParams> method_params = |
||||
MakeRefCounted<ClientChannelMethodParams>(); |
||||
for (grpc_json* field = json->child; field != nullptr; field = field->next) { |
||||
if (field->key == nullptr) continue; |
||||
if (strcmp(field->key, "waitForReady") == 0) { |
||||
if (method_params->wait_for_ready_ != WAIT_FOR_READY_UNSET) { |
||||
return nullptr; // Duplicate.
|
||||
} |
||||
if (!ParseWaitForReady(field, &method_params->wait_for_ready_)) { |
||||
return nullptr; |
||||
} |
||||
} else if (strcmp(field->key, "timeout") == 0) { |
||||
if (method_params->timeout_ > 0) return nullptr; // Duplicate.
|
||||
if (!ParseDuration(field, &method_params->timeout_)) return nullptr; |
||||
} else if (strcmp(field->key, "retryPolicy") == 0) { |
||||
if (method_params->retry_policy_ != nullptr) { |
||||
return nullptr; // Duplicate.
|
||||
} |
||||
method_params->retry_policy_ = ParseRetryPolicy(field); |
||||
if (method_params->retry_policy_ == nullptr) return nullptr; |
||||
} |
||||
} |
||||
return method_params; |
||||
} |
||||
|
||||
} // namespace internal
|
||||
} // namespace grpc_core
|
@ -0,0 +1,74 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015 gRPC authors. |
||||
* |
||||
* Licensed 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. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_METHOD_PARAMS_H |
||||
#define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_METHOD_PARAMS_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/ext/filters/client_channel/status_util.h" |
||||
#include "src/core/lib/gprpp/ref_counted.h" |
||||
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
||||
#include "src/core/lib/iomgr/exec_ctx.h" // for grpc_millis |
||||
#include "src/core/lib/json/json.h" |
||||
|
||||
namespace grpc_core { |
||||
namespace internal { |
||||
|
||||
class ClientChannelMethodParams : public RefCounted<ClientChannelMethodParams> { |
||||
public: |
||||
enum WaitForReady { |
||||
WAIT_FOR_READY_UNSET = 0, |
||||
WAIT_FOR_READY_FALSE, |
||||
WAIT_FOR_READY_TRUE |
||||
}; |
||||
|
||||
struct RetryPolicy { |
||||
int max_attempts = 0; |
||||
grpc_millis initial_backoff = 0; |
||||
grpc_millis max_backoff = 0; |
||||
float backoff_multiplier = 0; |
||||
StatusCodeSet retryable_status_codes; |
||||
}; |
||||
|
||||
/// Creates a method_parameters object from \a json.
|
||||
/// Intended for use with ServiceConfig::CreateMethodConfigTable().
|
||||
static RefCountedPtr<ClientChannelMethodParams> CreateFromJson( |
||||
const grpc_json* json); |
||||
|
||||
grpc_millis timeout() const { return timeout_; } |
||||
WaitForReady wait_for_ready() const { return wait_for_ready_; } |
||||
const RetryPolicy* retry_policy() const { return retry_policy_.get(); } |
||||
|
||||
private: |
||||
// So New() can call our private ctor.
|
||||
template <typename T, typename... Args> |
||||
friend T* grpc_core::New(Args&&... args); |
||||
|
||||
ClientChannelMethodParams() {} |
||||
virtual ~ClientChannelMethodParams() {} |
||||
|
||||
grpc_millis timeout_ = 0; |
||||
WaitForReady wait_for_ready_ = WAIT_FOR_READY_UNSET; |
||||
UniquePtr<RetryPolicy> retry_policy_; |
||||
}; |
||||
|
||||
} // namespace internal
|
||||
} // namespace grpc_core
|
||||
|
||||
#endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_METHOD_PARAMS_H */ |
@ -0,0 +1,100 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed 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 <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/ext/filters/client_channel/status_util.h" |
||||
|
||||
#include "src/core/lib/gpr/useful.h" |
||||
|
||||
typedef struct { |
||||
const char* str; |
||||
grpc_status_code status; |
||||
} status_string_entry; |
||||
|
||||
static const status_string_entry g_status_string_entries[] = { |
||||
{"OK", GRPC_STATUS_OK}, |
||||
{"CANCELLED", GRPC_STATUS_CANCELLED}, |
||||
{"UNKNOWN", GRPC_STATUS_UNKNOWN}, |
||||
{"INVALID_ARGUMENT", GRPC_STATUS_INVALID_ARGUMENT}, |
||||
{"DEADLINE_EXCEEDED", GRPC_STATUS_DEADLINE_EXCEEDED}, |
||||
{"NOT_FOUND", GRPC_STATUS_NOT_FOUND}, |
||||
{"ALREADY_EXISTS", GRPC_STATUS_ALREADY_EXISTS}, |
||||
{"PERMISSION_DENIED", GRPC_STATUS_PERMISSION_DENIED}, |
||||
{"UNAUTHENTICATED", GRPC_STATUS_UNAUTHENTICATED}, |
||||
{"RESOURCE_EXHAUSTED", GRPC_STATUS_RESOURCE_EXHAUSTED}, |
||||
{"FAILED_PRECONDITION", GRPC_STATUS_FAILED_PRECONDITION}, |
||||
{"ABORTED", GRPC_STATUS_ABORTED}, |
||||
{"OUT_OF_RANGE", GRPC_STATUS_OUT_OF_RANGE}, |
||||
{"UNIMPLEMENTED", GRPC_STATUS_UNIMPLEMENTED}, |
||||
{"INTERNAL", GRPC_STATUS_INTERNAL}, |
||||
{"UNAVAILABLE", GRPC_STATUS_UNAVAILABLE}, |
||||
{"DATA_LOSS", GRPC_STATUS_DATA_LOSS}, |
||||
}; |
||||
|
||||
bool grpc_status_code_from_string(const char* status_str, |
||||
grpc_status_code* status) { |
||||
for (size_t i = 0; i < GPR_ARRAY_SIZE(g_status_string_entries); ++i) { |
||||
if (strcmp(status_str, g_status_string_entries[i].str) == 0) { |
||||
*status = g_status_string_entries[i].status; |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
const char* grpc_status_code_to_string(grpc_status_code status) { |
||||
switch (status) { |
||||
case GRPC_STATUS_OK: |
||||
return "OK"; |
||||
case GRPC_STATUS_CANCELLED: |
||||
return "CANCELLED"; |
||||
case GRPC_STATUS_UNKNOWN: |
||||
return "UNKNOWN"; |
||||
case GRPC_STATUS_INVALID_ARGUMENT: |
||||
return "INVALID_ARGUMENT"; |
||||
case GRPC_STATUS_DEADLINE_EXCEEDED: |
||||
return "DEADLINE_EXCEEDED"; |
||||
case GRPC_STATUS_NOT_FOUND: |
||||
return "NOT_FOUND"; |
||||
case GRPC_STATUS_ALREADY_EXISTS: |
||||
return "ALREADY_EXISTS"; |
||||
case GRPC_STATUS_PERMISSION_DENIED: |
||||
return "PERMISSION_DENIED"; |
||||
case GRPC_STATUS_UNAUTHENTICATED: |
||||
return "UNAUTHENTICATED"; |
||||
case GRPC_STATUS_RESOURCE_EXHAUSTED: |
||||
return "RESOURCE_EXHAUSTED"; |
||||
case GRPC_STATUS_FAILED_PRECONDITION: |
||||
return "FAILED_PRECONDITION"; |
||||
case GRPC_STATUS_ABORTED: |
||||
return "ABORTED"; |
||||
case GRPC_STATUS_OUT_OF_RANGE: |
||||
return "OUT_OF_RANGE"; |
||||
case GRPC_STATUS_UNIMPLEMENTED: |
||||
return "UNIMPLEMENTED"; |
||||
case GRPC_STATUS_INTERNAL: |
||||
return "INTERNAL"; |
||||
case GRPC_STATUS_UNAVAILABLE: |
||||
return "UNAVAILABLE"; |
||||
case GRPC_STATUS_DATA_LOSS: |
||||
return "DATA_LOSS"; |
||||
default: |
||||
return "UNKNOWN"; |
||||
} |
||||
} |
@ -0,0 +1,58 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed 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. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_STATUS_UTIL_H |
||||
#define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_STATUS_UTIL_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include <grpc/status.h> |
||||
|
||||
#include <stdbool.h> |
||||
#include <string.h> |
||||
|
||||
/// If \a status_str is a valid status string, sets \a status to the
|
||||
/// corresponding status value and returns true.
|
||||
bool grpc_status_code_from_string(const char* status_str, |
||||
grpc_status_code* status); |
||||
|
||||
/// Returns the string form of \a status, or "UNKNOWN" if invalid.
|
||||
const char* grpc_status_code_to_string(grpc_status_code status); |
||||
|
||||
namespace grpc_core { |
||||
namespace internal { |
||||
|
||||
/// A set of grpc_status_code values.
|
||||
class StatusCodeSet { |
||||
public: |
||||
bool Empty() const { return status_code_mask_ == 0; } |
||||
|
||||
void Add(grpc_status_code status) { status_code_mask_ |= (1 << status); } |
||||
|
||||
bool Contains(grpc_status_code status) const { |
||||
return status_code_mask_ & (1 << status); |
||||
} |
||||
|
||||
private: |
||||
int status_code_mask_ = 0; // A bitfield of status codes in the set.
|
||||
}; |
||||
|
||||
} // namespace internal
|
||||
} // namespace grpc_core
|
||||
|
||||
#endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_STATUS_UTIL_H */ |
@ -1,61 +0,0 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed 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 <grpc/support/port_platform.h> |
||||
|
||||
#include <grpc/support/log.h> |
||||
|
||||
#include "src/core/lib/channel/channel_args.h" |
||||
#include "src/core/lib/security/transport/lb_targets_info.h" |
||||
|
||||
/* Channel arg key for the mapping of LB server addresses to their names for
|
||||
* secure naming purposes. */ |
||||
#define GRPC_ARG_LB_SECURE_NAMING_MAP "grpc.lb_secure_naming_map" |
||||
|
||||
static void* targets_info_copy(void* p) { |
||||
return grpc_slice_hash_table_ref(static_cast<grpc_slice_hash_table*>(p)); |
||||
} |
||||
static void targets_info_destroy(void* p) { |
||||
grpc_slice_hash_table_unref(static_cast<grpc_slice_hash_table*>(p)); |
||||
} |
||||
static int targets_info_cmp(void* a, void* b) { |
||||
return grpc_slice_hash_table_cmp( |
||||
static_cast<const grpc_slice_hash_table*>(a), |
||||
static_cast<const grpc_slice_hash_table*>(b)); |
||||
} |
||||
static const grpc_arg_pointer_vtable server_to_balancer_names_vtable = { |
||||
targets_info_copy, targets_info_destroy, targets_info_cmp}; |
||||
|
||||
grpc_arg grpc_lb_targets_info_create_channel_arg( |
||||
grpc_slice_hash_table* targets_info) { |
||||
return grpc_channel_arg_pointer_create((char*)GRPC_ARG_LB_SECURE_NAMING_MAP, |
||||
targets_info, |
||||
&server_to_balancer_names_vtable); |
||||
} |
||||
|
||||
grpc_slice_hash_table* grpc_lb_targets_info_find_in_args( |
||||
const grpc_channel_args* args) { |
||||
const grpc_arg* targets_info_arg = |
||||
grpc_channel_args_find(args, GRPC_ARG_LB_SECURE_NAMING_MAP); |
||||
if (targets_info_arg != nullptr) { |
||||
GPR_ASSERT(targets_info_arg->type == GRPC_ARG_POINTER); |
||||
return static_cast<grpc_slice_hash_table*>( |
||||
targets_info_arg->value.pointer.p); |
||||
} |
||||
return nullptr; |
||||
} |
@ -0,0 +1,75 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed 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 <grpc/support/port_platform.h> |
||||
|
||||
#include <grpc/support/log.h> |
||||
|
||||
#include "src/core/lib/channel/channel_args.h" |
||||
#include "src/core/lib/security/transport/target_authority_table.h" |
||||
|
||||
// Channel arg key for the mapping of target addresses to their authorities.
|
||||
#define GRPC_ARG_TARGET_AUTHORITY_TABLE "grpc.target_authority_table" |
||||
|
||||
namespace grpc_core { |
||||
namespace { |
||||
|
||||
void* target_authority_table_copy(void* p) { |
||||
TargetAuthorityTable* table = static_cast<TargetAuthorityTable*>(p); |
||||
// TODO(roth): When channel_args are converted to C++, pass the
|
||||
// RefCountedPtr<> directly instead of managing the ref manually.
|
||||
table->Ref().release(); |
||||
return p; |
||||
} |
||||
void target_authority_table_destroy(void* p) { |
||||
TargetAuthorityTable* table = static_cast<TargetAuthorityTable*>(p); |
||||
table->Unref(); |
||||
} |
||||
int target_authority_table_cmp(void* a, void* b) { |
||||
return TargetAuthorityTable::Cmp( |
||||
*static_cast<const TargetAuthorityTable*>(a), |
||||
*static_cast<const TargetAuthorityTable*>(b)); |
||||
} |
||||
const grpc_arg_pointer_vtable target_authority_table_arg_vtable = { |
||||
target_authority_table_copy, target_authority_table_destroy, |
||||
target_authority_table_cmp}; |
||||
|
||||
} // namespace
|
||||
|
||||
grpc_arg CreateTargetAuthorityTableChannelArg(TargetAuthorityTable* table) { |
||||
return grpc_channel_arg_pointer_create((char*)GRPC_ARG_TARGET_AUTHORITY_TABLE, |
||||
table, |
||||
&target_authority_table_arg_vtable); |
||||
} |
||||
|
||||
TargetAuthorityTable* FindTargetAuthorityTableInArgs( |
||||
const grpc_channel_args* args) { |
||||
const grpc_arg* arg = |
||||
grpc_channel_args_find(args, GRPC_ARG_TARGET_AUTHORITY_TABLE); |
||||
if (arg != nullptr) { |
||||
if (arg->type == GRPC_ARG_POINTER) { |
||||
return static_cast<TargetAuthorityTable*>(arg->value.pointer.p); |
||||
} else { |
||||
gpr_log(GPR_ERROR, "value of " GRPC_ARG_TARGET_AUTHORITY_TABLE |
||||
" channel arg was not pointer type; ignoring"); |
||||
} |
||||
} |
||||
return nullptr; |
||||
} |
||||
|
||||
} // namespace grpc_core
|
@ -0,0 +1,40 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed 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. |
||||
* |
||||
*/ |
||||
|
||||
#ifndef GRPC_CORE_LIB_SECURITY_TRANSPORT_TARGET_AUTHORITY_TABLE_H |
||||
#define GRPC_CORE_LIB_SECURITY_TRANSPORT_TARGET_AUTHORITY_TABLE_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/slice/slice_hash_table.h" |
||||
|
||||
namespace grpc_core { |
||||
|
||||
/// A hash table mapping target addresses to authorities.
|
||||
typedef SliceHashTable<UniquePtr<char>> TargetAuthorityTable; |
||||
|
||||
/// Returns a channel argument containing \a table.
|
||||
grpc_arg CreateTargetAuthorityTableChannelArg(TargetAuthorityTable* table); |
||||
|
||||
/// Returns the target authority table from \a args or nullptr.
|
||||
TargetAuthorityTable* FindTargetAuthorityTableInArgs( |
||||
const grpc_channel_args* args); |
||||
|
||||
} // namespace grpc_core
|
||||
|
||||
#endif /* GRPC_CORE_LIB_SECURITY_TRANSPORT_TARGET_AUTHORITY_TABLE_H */ |
@ -1,147 +0,0 @@ |
||||
//
|
||||
// Copyright 2016 gRPC authors.
|
||||
//
|
||||
// Licensed 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 <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/slice/slice_hash_table.h" |
||||
|
||||
#include <stdbool.h> |
||||
#include <string.h> |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
|
||||
#include "src/core/lib/slice/slice_internal.h" |
||||
#include "src/core/lib/transport/metadata.h" |
||||
|
||||
struct grpc_slice_hash_table { |
||||
gpr_refcount refs; |
||||
void (*destroy_value)(void* value); |
||||
int (*value_cmp)(void* a, void* b); |
||||
size_t size; |
||||
size_t max_num_probes; |
||||
grpc_slice_hash_table_entry* entries; |
||||
}; |
||||
|
||||
static bool is_empty(grpc_slice_hash_table_entry* entry) { |
||||
return entry->value == nullptr; |
||||
} |
||||
|
||||
static void grpc_slice_hash_table_add(grpc_slice_hash_table* table, |
||||
grpc_slice key, void* value) { |
||||
GPR_ASSERT(value != nullptr); |
||||
const size_t hash = grpc_slice_hash(key); |
||||
for (size_t offset = 0; offset < table->size; ++offset) { |
||||
const size_t idx = (hash + offset) % table->size; |
||||
if (is_empty(&table->entries[idx])) { |
||||
table->entries[idx].key = key; |
||||
table->entries[idx].value = value; |
||||
// Keep track of the maximum number of probes needed, since this
|
||||
// provides an upper bound for lookups.
|
||||
if (offset > table->max_num_probes) table->max_num_probes = offset; |
||||
return; |
||||
} |
||||
} |
||||
GPR_ASSERT(false); // Table should never be full.
|
||||
} |
||||
|
||||
grpc_slice_hash_table* grpc_slice_hash_table_create( |
||||
size_t num_entries, grpc_slice_hash_table_entry* entries, |
||||
void (*destroy_value)(void* value), int (*value_cmp)(void* a, void* b)) { |
||||
grpc_slice_hash_table* table = |
||||
static_cast<grpc_slice_hash_table*>(gpr_zalloc(sizeof(*table))); |
||||
gpr_ref_init(&table->refs, 1); |
||||
table->destroy_value = destroy_value; |
||||
table->value_cmp = value_cmp; |
||||
// Keep load factor low to improve performance of lookups.
|
||||
table->size = num_entries * 2; |
||||
const size_t entry_size = sizeof(grpc_slice_hash_table_entry) * table->size; |
||||
table->entries = |
||||
static_cast<grpc_slice_hash_table_entry*>(gpr_zalloc(entry_size)); |
||||
for (size_t i = 0; i < num_entries; ++i) { |
||||
grpc_slice_hash_table_entry* entry = &entries[i]; |
||||
grpc_slice_hash_table_add(table, entry->key, entry->value); |
||||
} |
||||
return table; |
||||
} |
||||
|
||||
grpc_slice_hash_table* grpc_slice_hash_table_ref(grpc_slice_hash_table* table) { |
||||
if (table != nullptr) gpr_ref(&table->refs); |
||||
return table; |
||||
} |
||||
|
||||
void grpc_slice_hash_table_unref(grpc_slice_hash_table* table) { |
||||
if (table != nullptr && gpr_unref(&table->refs)) { |
||||
for (size_t i = 0; i < table->size; ++i) { |
||||
grpc_slice_hash_table_entry* entry = &table->entries[i]; |
||||
if (!is_empty(entry)) { |
||||
grpc_slice_unref_internal(entry->key); |
||||
table->destroy_value(entry->value); |
||||
} |
||||
} |
||||
gpr_free(table->entries); |
||||
gpr_free(table); |
||||
} |
||||
} |
||||
|
||||
void* grpc_slice_hash_table_get(const grpc_slice_hash_table* table, |
||||
const grpc_slice key) { |
||||
const size_t hash = grpc_slice_hash(key); |
||||
// We cap the number of probes at the max number recorded when
|
||||
// populating the table.
|
||||
for (size_t offset = 0; offset <= table->max_num_probes; ++offset) { |
||||
const size_t idx = (hash + offset) % table->size; |
||||
if (is_empty(&table->entries[idx])) break; |
||||
if (grpc_slice_eq(table->entries[idx].key, key)) { |
||||
return table->entries[idx].value; |
||||
} |
||||
} |
||||
return nullptr; // Not found.
|
||||
} |
||||
|
||||
static int pointer_cmp(void* a, void* b) { return GPR_ICMP(a, b); } |
||||
int grpc_slice_hash_table_cmp(const grpc_slice_hash_table* a, |
||||
const grpc_slice_hash_table* b) { |
||||
int (*const value_cmp_fn_a)(void* a, void* b) = |
||||
a->value_cmp != nullptr ? a->value_cmp : pointer_cmp; |
||||
int (*const value_cmp_fn_b)(void* a, void* b) = |
||||
b->value_cmp != nullptr ? b->value_cmp : pointer_cmp; |
||||
// Compare value_fns
|
||||
const int value_fns_cmp = |
||||
GPR_ICMP((void*)value_cmp_fn_a, (void*)value_cmp_fn_b); |
||||
if (value_fns_cmp != 0) return value_fns_cmp; |
||||
// Compare sizes
|
||||
if (a->size < b->size) return -1; |
||||
if (a->size > b->size) return 1; |
||||
// Compare rows.
|
||||
for (size_t i = 0; i < a->size; ++i) { |
||||
if (is_empty(&a->entries[i])) { |
||||
if (!is_empty(&b->entries[i])) { |
||||
return -1; // a empty but b non-empty
|
||||
} |
||||
continue; // both empty, no need to check key or value
|
||||
} else if (is_empty(&b->entries[i])) { |
||||
return 1; // a non-empty but b empty
|
||||
} |
||||
// neither entry is empty
|
||||
const int key_cmp = grpc_slice_cmp(a->entries[i].key, b->entries[i].key); |
||||
if (key_cmp != 0) return key_cmp; |
||||
const int value_cmp = |
||||
value_cmp_fn_a(a->entries[i].value, b->entries[i].value); |
||||
if (value_cmp != 0) return value_cmp; |
||||
} |
||||
return 0; |
||||
} |
@ -0,0 +1,105 @@ |
||||
/*
|
||||
* Copyright 2016 gRPC authors. |
||||
* |
||||
* Licensed 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. |
||||
*/ |
||||
|
||||
#ifndef GRPC_CORE_LIB_SLICE_SLICE_WEAK_HASH_TABLE_H |
||||
#define GRPC_CORE_LIB_SLICE_SLICE_WEAK_HASH_TABLE_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/gprpp/memory.h" |
||||
#include "src/core/lib/gprpp/ref_counted.h" |
||||
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
||||
#include "src/core/lib/slice/slice_internal.h" |
||||
|
||||
/// Weak hash table implementation.
|
||||
///
|
||||
/// This entries in this table are weak: an entry may be removed at any time due
|
||||
/// to a number of reasons: memory pressure, hash collisions, etc.
|
||||
///
|
||||
/// The keys are \a grpc_slice objects. The values are of arbitrary type.
|
||||
///
|
||||
/// This class is thread unsafe. It's the caller's responsibility to ensure
|
||||
/// proper locking when accessing its methods.
|
||||
|
||||
namespace grpc_core { |
||||
|
||||
template <typename T, size_t Size> |
||||
class SliceWeakHashTable : public RefCounted<SliceWeakHashTable<T, Size>> { |
||||
public: |
||||
/// Creates a new table of at most \a size entries.
|
||||
static RefCountedPtr<SliceWeakHashTable> Create() { |
||||
return MakeRefCounted<SliceWeakHashTable<T, Size>>(); |
||||
} |
||||
|
||||
/// Add a mapping from \a key to \a value, taking ownership of \a key. This
|
||||
/// operation will always succeed. It may discard older entries.
|
||||
void Add(grpc_slice key, T value) { |
||||
const size_t idx = grpc_slice_hash(key) % Size; |
||||
entries_[idx].Set(key, std::move(value)); |
||||
return; |
||||
} |
||||
|
||||
/// Returns the value from the table associated with / \a key or null if not
|
||||
/// found.
|
||||
const T* Get(const grpc_slice key) const { |
||||
const size_t idx = grpc_slice_hash(key) % Size; |
||||
const auto& entry = entries_[idx]; |
||||
return grpc_slice_eq(entry.key(), key) ? entry.value() : nullptr; |
||||
} |
||||
|
||||
private: |
||||
// So New() can call our private ctor.
|
||||
template <typename T2, typename... Args> |
||||
friend T2* New(Args&&... args); |
||||
|
||||
SliceWeakHashTable() = default; |
||||
~SliceWeakHashTable() = default; |
||||
|
||||
/// The type of the table "rows".
|
||||
class Entry { |
||||
public: |
||||
Entry() = default; |
||||
~Entry() { |
||||
if (is_set_) grpc_slice_unref_internal(key_); |
||||
} |
||||
grpc_slice key() const { return key_; } |
||||
|
||||
/// Return the entry's value, or null if unset.
|
||||
const T* value() const { |
||||
if (!is_set_) return nullptr; |
||||
return &value_; |
||||
} |
||||
|
||||
/// Set the \a key and \a value (which is moved) for the entry.
|
||||
void Set(grpc_slice key, T&& value) { |
||||
if (is_set_) grpc_slice_unref_internal(key_); |
||||
key_ = key; |
||||
value_ = std::move(value); |
||||
is_set_ = true; |
||||
} |
||||
|
||||
private: |
||||
grpc_slice key_; |
||||
T value_; |
||||
bool is_set_ = false; |
||||
}; |
||||
|
||||
Entry entries_[Size]; |
||||
}; |
||||
|
||||
} // namespace grpc_core
|
||||
|
||||
#endif /* GRPC_CORE_LIB_SLICE_SLICE_WEAK_HASH_TABLE_H */ |
@ -0,0 +1,54 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed 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 <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/transport/status_metadata.h" |
||||
|
||||
#include "src/core/lib/slice/slice_string_helpers.h" |
||||
#include "src/core/lib/transport/static_metadata.h" |
||||
|
||||
/* we offset status by a small amount when storing it into transport metadata
|
||||
as metadata cannot store a 0 value (which is used as OK for grpc_status_codes |
||||
*/ |
||||
#define STATUS_OFFSET 1 |
||||
|
||||
static void destroy_status(void* ignored) {} |
||||
|
||||
grpc_status_code grpc_get_status_code_from_metadata(grpc_mdelem md) { |
||||
if (grpc_mdelem_eq(md, GRPC_MDELEM_GRPC_STATUS_0)) { |
||||
return GRPC_STATUS_OK; |
||||
} |
||||
if (grpc_mdelem_eq(md, GRPC_MDELEM_GRPC_STATUS_1)) { |
||||
return GRPC_STATUS_CANCELLED; |
||||
} |
||||
if (grpc_mdelem_eq(md, GRPC_MDELEM_GRPC_STATUS_2)) { |
||||
return GRPC_STATUS_UNKNOWN; |
||||
} |
||||
void* user_data = grpc_mdelem_get_user_data(md, destroy_status); |
||||
if (user_data != nullptr) { |
||||
return static_cast<grpc_status_code>((intptr_t)user_data - STATUS_OFFSET); |
||||
} |
||||
uint32_t status; |
||||
if (!grpc_parse_slice_to_uint32(GRPC_MDVALUE(md), &status)) { |
||||
status = GRPC_STATUS_UNKNOWN; /* could not parse status code */ |
||||
} |
||||
grpc_mdelem_set_user_data( |
||||
md, destroy_status, (void*)static_cast<intptr_t>(status + STATUS_OFFSET)); |
||||
return static_cast<grpc_status_code>(status); |
||||
} |
@ -1,5 +1,5 @@ |
||||
{ |
||||
"sdk": { |
||||
"version": "1.0.0" |
||||
"version": "2.1.4" |
||||
} |
||||
} |
||||
|
@ -0,0 +1,49 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed 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 "src/core/ext/filters/client_channel/status_util.h" |
||||
|
||||
#include <gtest/gtest.h> |
||||
|
||||
namespace grpc_core { |
||||
namespace internal { |
||||
namespace { |
||||
|
||||
TEST(StatusCodeSet, Basic) { |
||||
StatusCodeSet set; |
||||
EXPECT_TRUE(set.Empty()); |
||||
EXPECT_FALSE(set.Contains(GRPC_STATUS_OK)); |
||||
EXPECT_FALSE(set.Contains(GRPC_STATUS_UNAVAILABLE)); |
||||
set.Add(GRPC_STATUS_OK); |
||||
EXPECT_FALSE(set.Empty()); |
||||
EXPECT_TRUE(set.Contains(GRPC_STATUS_OK)); |
||||
EXPECT_FALSE(set.Contains(GRPC_STATUS_UNAVAILABLE)); |
||||
set.Add(GRPC_STATUS_UNAVAILABLE); |
||||
EXPECT_FALSE(set.Empty()); |
||||
EXPECT_TRUE(set.Contains(GRPC_STATUS_OK)); |
||||
EXPECT_TRUE(set.Contains(GRPC_STATUS_UNAVAILABLE)); |
||||
} |
||||
|
||||
} // namespace
|
||||
} // namespace internal
|
||||
} // namespace grpc_core
|
||||
|
||||
int main(int argc, char** argv) { |
||||
::testing::InitGoogleTest(&argc, argv); |
||||
return RUN_ALL_TESTS(); |
||||
} |
@ -0,0 +1,325 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed 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 "test/core/end2end/end2end_tests.h" |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include <grpc/byte_buffer.h> |
||||
#include <grpc/grpc.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
#include <grpc/support/time.h> |
||||
|
||||
#include "src/core/lib/channel/channel_args.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "src/core/lib/gpr/useful.h" |
||||
#include "src/core/lib/iomgr/exec_ctx.h" |
||||
#include "src/core/lib/transport/static_metadata.h" |
||||
|
||||
#include "test/core/end2end/cq_verifier.h" |
||||
#include "test/core/end2end/tests/cancel_test_helpers.h" |
||||
|
||||
static void* tag(intptr_t t) { return (void*)t; } |
||||
|
||||
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
||||
const char* test_name, |
||||
grpc_channel_args* client_args, |
||||
grpc_channel_args* server_args) { |
||||
grpc_end2end_test_fixture f; |
||||
gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name); |
||||
f = config.create_fixture(client_args, server_args); |
||||
config.init_server(&f, server_args); |
||||
config.init_client(&f, client_args); |
||||
return f; |
||||
} |
||||
|
||||
static gpr_timespec n_seconds_from_now(int n) { |
||||
return grpc_timeout_seconds_to_deadline(n); |
||||
} |
||||
|
||||
static gpr_timespec five_seconds_from_now(void) { |
||||
return n_seconds_from_now(5); |
||||
} |
||||
|
||||
static void drain_cq(grpc_completion_queue* cq) { |
||||
grpc_event ev; |
||||
do { |
||||
ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
||||
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
||||
} |
||||
|
||||
static void shutdown_server(grpc_end2end_test_fixture* f) { |
||||
if (!f->server) return; |
||||
grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000)); |
||||
GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000), |
||||
grpc_timeout_seconds_to_deadline(5), |
||||
nullptr) |
||||
.type == GRPC_OP_COMPLETE); |
||||
grpc_server_destroy(f->server); |
||||
f->server = nullptr; |
||||
} |
||||
|
||||
static void shutdown_client(grpc_end2end_test_fixture* f) { |
||||
if (!f->client) return; |
||||
grpc_channel_destroy(f->client); |
||||
f->client = nullptr; |
||||
} |
||||
|
||||
static void end_test(grpc_end2end_test_fixture* f) { |
||||
shutdown_server(f); |
||||
shutdown_client(f); |
||||
|
||||
grpc_completion_queue_shutdown(f->cq); |
||||
drain_cq(f->cq); |
||||
grpc_completion_queue_destroy(f->cq); |
||||
grpc_completion_queue_destroy(f->shutdown_cq); |
||||
} |
||||
|
||||
// Tests a basic retry scenario:
|
||||
// - 2 retries allowed for ABORTED status
|
||||
// - first attempt returns ABORTED
|
||||
// - second attempt returns OK
|
||||
static void test_retry(grpc_end2end_test_config config) { |
||||
grpc_call* c; |
||||
grpc_call* s; |
||||
grpc_op ops[6]; |
||||
grpc_op* op; |
||||
grpc_metadata_array initial_metadata_recv; |
||||
grpc_metadata_array trailing_metadata_recv; |
||||
grpc_metadata_array request_metadata_recv; |
||||
grpc_call_details call_details; |
||||
grpc_slice request_payload_slice = grpc_slice_from_static_string("foo"); |
||||
grpc_slice response_payload_slice = grpc_slice_from_static_string("bar"); |
||||
grpc_byte_buffer* request_payload = |
||||
grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
||||
grpc_byte_buffer* response_payload = |
||||
grpc_raw_byte_buffer_create(&response_payload_slice, 1); |
||||
grpc_byte_buffer* request_payload_recv = nullptr; |
||||
grpc_byte_buffer* response_payload_recv = nullptr; |
||||
grpc_status_code status; |
||||
grpc_call_error error; |
||||
grpc_slice details; |
||||
int was_cancelled = 2; |
||||
char* peer; |
||||
|
||||
grpc_arg arg; |
||||
arg.type = GRPC_ARG_STRING; |
||||
arg.key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG); |
||||
arg.value.string = const_cast<char*>( |
||||
"{\n" |
||||
" \"methodConfig\": [ {\n" |
||||
" \"name\": [\n" |
||||
" { \"service\": \"service\", \"method\": \"method\" }\n" |
||||
" ],\n" |
||||
" \"retryPolicy\": {\n" |
||||
" \"maxAttempts\": 3,\n" |
||||
" \"initialBackoff\": \"1s\",\n" |
||||
" \"maxBackoff\": \"120s\",\n" |
||||
" \"backoffMultiplier\": 1.6,\n" |
||||
" \"retryableStatusCodes\": [ \"ABORTED\" ]\n" |
||||
" }\n" |
||||
" } ]\n" |
||||
"}"); |
||||
grpc_channel_args client_args = {1, &arg}; |
||||
grpc_end2end_test_fixture f = |
||||
begin_test(config, "retry", &client_args, nullptr); |
||||
|
||||
cq_verifier* cqv = cq_verifier_create(f.cq); |
||||
|
||||
gpr_timespec deadline = five_seconds_from_now(); |
||||
c = grpc_channel_create_call( |
||||
f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
||||
grpc_slice_from_static_string("/service/method"), |
||||
get_host_override_slice("foo.test.google.fr:1234", config), deadline, |
||||
nullptr); |
||||
GPR_ASSERT(c); |
||||
|
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
grpc_metadata_array_init(&initial_metadata_recv); |
||||
grpc_metadata_array_init(&trailing_metadata_recv); |
||||
grpc_metadata_array_init(&request_metadata_recv); |
||||
grpc_call_details_init(&call_details); |
||||
grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = request_payload; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &response_payload_recv; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
||||
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
||||
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
||||
op->data.recv_status_on_client.status = &status; |
||||
op->data.recv_status_on_client.status_details = &details; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
error = |
||||
grpc_server_request_call(f.server, &s, &call_details, |
||||
&request_metadata_recv, f.cq, f.cq, tag(101)); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(101), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Make sure the "grpc-previous-rpc-attempts" header was not sent in the
|
||||
// initial attempt.
|
||||
for (size_t i = 0; i < request_metadata_recv.count; ++i) { |
||||
GPR_ASSERT(!grpc_slice_eq(request_metadata_recv.metadata[i].key, |
||||
GRPC_MDSTR_GRPC_PREVIOUS_RPC_ATTEMPTS)); |
||||
} |
||||
|
||||
peer = grpc_call_get_peer(s); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "server_peer=%s", peer); |
||||
gpr_free(peer); |
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
op->data.send_status_from_server.trailing_metadata_count = 0; |
||||
op->data.send_status_from_server.status = GRPC_STATUS_ABORTED; |
||||
op->data.send_status_from_server.status_details = &status_details; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
||||
op->data.recv_close_on_server.cancelled = &was_cancelled; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
CQ_EXPECT_COMPLETION(cqv, tag(102), true); |
||||
cq_verify(cqv); |
||||
|
||||
grpc_call_unref(s); |
||||
grpc_metadata_array_destroy(&request_metadata_recv); |
||||
grpc_metadata_array_init(&request_metadata_recv); |
||||
grpc_call_details_destroy(&call_details); |
||||
grpc_call_details_init(&call_details); |
||||
|
||||
error = |
||||
grpc_server_request_call(f.server, &s, &call_details, |
||||
&request_metadata_recv, f.cq, f.cq, tag(201)); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(201), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Make sure the "grpc-previous-rpc-attempts" header was sent in the retry.
|
||||
bool found_retry_header = false; |
||||
for (size_t i = 0; i < request_metadata_recv.count; ++i) { |
||||
if (grpc_slice_eq(request_metadata_recv.metadata[i].key, |
||||
GRPC_MDSTR_GRPC_PREVIOUS_RPC_ATTEMPTS)) { |
||||
GPR_ASSERT( |
||||
grpc_slice_eq(request_metadata_recv.metadata[i].value, GRPC_MDSTR_1)); |
||||
found_retry_header = true; |
||||
break; |
||||
} |
||||
} |
||||
GPR_ASSERT(found_retry_header); |
||||
|
||||
peer = grpc_call_get_peer(s); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "server_peer=%s", peer); |
||||
gpr_free(peer); |
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &request_payload_recv; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = response_payload; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
op->data.send_status_from_server.trailing_metadata_count = 0; |
||||
op->data.send_status_from_server.status = GRPC_STATUS_OK; |
||||
op->data.send_status_from_server.status_details = &status_details; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
||||
op->data.recv_close_on_server.cancelled = &was_cancelled; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(202), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
CQ_EXPECT_COMPLETION(cqv, tag(202), true); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(1), true); |
||||
cq_verify(cqv); |
||||
|
||||
GPR_ASSERT(status == GRPC_STATUS_OK); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz")); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method")); |
||||
validate_host_override_string("foo.test.google.fr:1234", call_details.host, |
||||
config); |
||||
GPR_ASSERT(0 == call_details.flags); |
||||
GPR_ASSERT(was_cancelled == 0); |
||||
|
||||
grpc_slice_unref(details); |
||||
grpc_metadata_array_destroy(&initial_metadata_recv); |
||||
grpc_metadata_array_destroy(&trailing_metadata_recv); |
||||
grpc_metadata_array_destroy(&request_metadata_recv); |
||||
grpc_call_details_destroy(&call_details); |
||||
grpc_byte_buffer_destroy(request_payload); |
||||
grpc_byte_buffer_destroy(response_payload); |
||||
grpc_byte_buffer_destroy(request_payload_recv); |
||||
grpc_byte_buffer_destroy(response_payload_recv); |
||||
|
||||
grpc_call_unref(c); |
||||
grpc_call_unref(s); |
||||
|
||||
cq_verifier_destroy(cqv); |
||||
|
||||
end_test(&f); |
||||
config.tear_down_data(&f); |
||||
} |
||||
|
||||
void retry(grpc_end2end_test_config config) { |
||||
GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL); |
||||
test_retry(config); |
||||
} |
||||
|
||||
void retry_pre_init(void) {} |
@ -0,0 +1,277 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed 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 "test/core/end2end/end2end_tests.h" |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include <grpc/byte_buffer.h> |
||||
#include <grpc/grpc.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
#include <grpc/support/time.h> |
||||
|
||||
#include "src/core/lib/channel/channel_args.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "src/core/lib/gpr/useful.h" |
||||
#include "src/core/lib/iomgr/exec_ctx.h" |
||||
#include "src/core/lib/transport/static_metadata.h" |
||||
|
||||
#include "test/core/end2end/cq_verifier.h" |
||||
#include "test/core/end2end/tests/cancel_test_helpers.h" |
||||
|
||||
static void* tag(intptr_t t) { return (void*)t; } |
||||
|
||||
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
||||
const char* test_name, |
||||
grpc_channel_args* client_args, |
||||
grpc_channel_args* server_args) { |
||||
grpc_end2end_test_fixture f; |
||||
gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name); |
||||
f = config.create_fixture(client_args, server_args); |
||||
config.init_server(&f, server_args); |
||||
config.init_client(&f, client_args); |
||||
return f; |
||||
} |
||||
|
||||
static gpr_timespec n_seconds_from_now(int n) { |
||||
return grpc_timeout_seconds_to_deadline(n); |
||||
} |
||||
|
||||
static gpr_timespec five_seconds_from_now(void) { |
||||
return n_seconds_from_now(5); |
||||
} |
||||
|
||||
static void drain_cq(grpc_completion_queue* cq) { |
||||
grpc_event ev; |
||||
do { |
||||
ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
||||
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
||||
} |
||||
|
||||
static void shutdown_server(grpc_end2end_test_fixture* f) { |
||||
if (!f->server) return; |
||||
grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000)); |
||||
GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000), |
||||
grpc_timeout_seconds_to_deadline(5), |
||||
nullptr) |
||||
.type == GRPC_OP_COMPLETE); |
||||
grpc_server_destroy(f->server); |
||||
f->server = nullptr; |
||||
} |
||||
|
||||
static void shutdown_client(grpc_end2end_test_fixture* f) { |
||||
if (!f->client) return; |
||||
grpc_channel_destroy(f->client); |
||||
f->client = nullptr; |
||||
} |
||||
|
||||
static void end_test(grpc_end2end_test_fixture* f) { |
||||
shutdown_server(f); |
||||
shutdown_client(f); |
||||
|
||||
grpc_completion_queue_shutdown(f->cq); |
||||
drain_cq(f->cq); |
||||
grpc_completion_queue_destroy(f->cq); |
||||
grpc_completion_queue_destroy(f->shutdown_cq); |
||||
} |
||||
|
||||
// Tests retry cancellation.
|
||||
static void test_retry_cancellation(grpc_end2end_test_config config, |
||||
cancellation_mode mode) { |
||||
grpc_call* c; |
||||
grpc_call* s; |
||||
grpc_op ops[6]; |
||||
grpc_op* op; |
||||
grpc_metadata_array initial_metadata_recv; |
||||
grpc_metadata_array trailing_metadata_recv; |
||||
grpc_metadata_array request_metadata_recv; |
||||
grpc_call_details call_details; |
||||
grpc_slice request_payload_slice = grpc_slice_from_static_string("foo"); |
||||
grpc_slice response_payload_slice = grpc_slice_from_static_string("bar"); |
||||
grpc_byte_buffer* request_payload = |
||||
grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
||||
grpc_byte_buffer* response_payload = |
||||
grpc_raw_byte_buffer_create(&response_payload_slice, 1); |
||||
grpc_byte_buffer* request_payload_recv = nullptr; |
||||
grpc_byte_buffer* response_payload_recv = nullptr; |
||||
grpc_status_code status; |
||||
grpc_call_error error; |
||||
grpc_slice details; |
||||
int was_cancelled = 2; |
||||
char* peer; |
||||
|
||||
grpc_arg arg; |
||||
arg.type = GRPC_ARG_STRING; |
||||
arg.key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG); |
||||
arg.value.string = const_cast<char*>( |
||||
"{\n" |
||||
" \"methodConfig\": [ {\n" |
||||
" \"name\": [\n" |
||||
" { \"service\": \"service\", \"method\": \"method\" }\n" |
||||
" ],\n" |
||||
" \"retryPolicy\": {\n" |
||||
" \"maxAttempts\": 3,\n" |
||||
" \"initialBackoff\": \"1s\",\n" |
||||
" \"maxBackoff\": \"120s\",\n" |
||||
" \"backoffMultiplier\": 1.6,\n" |
||||
" \"retryableStatusCodes\": [ \"ABORTED\" ]\n" |
||||
" },\n" |
||||
" \"timeout\": \"5s\"\n" |
||||
" } ]\n" |
||||
"}"); |
||||
grpc_channel_args client_args = {1, &arg}; |
||||
char* name; |
||||
gpr_asprintf(&name, "retry_cancellation/%s", mode.name); |
||||
grpc_end2end_test_fixture f = begin_test(config, name, &client_args, nullptr); |
||||
gpr_free(name); |
||||
|
||||
cq_verifier* cqv = cq_verifier_create(f.cq); |
||||
|
||||
gpr_timespec deadline = five_seconds_from_now(); |
||||
c = grpc_channel_create_call( |
||||
f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
||||
grpc_slice_from_static_string("/service/method"), |
||||
get_host_override_slice("foo.test.google.fr:1234", config), deadline, |
||||
nullptr); |
||||
GPR_ASSERT(c); |
||||
|
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
grpc_metadata_array_init(&initial_metadata_recv); |
||||
grpc_metadata_array_init(&trailing_metadata_recv); |
||||
grpc_metadata_array_init(&request_metadata_recv); |
||||
grpc_call_details_init(&call_details); |
||||
grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
||||
|
||||
// Client starts a batch with all 6 ops.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = request_payload; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &response_payload_recv; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
||||
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
||||
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
||||
op->data.recv_status_on_client.status = &status; |
||||
op->data.recv_status_on_client.status_details = &details; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
// Server gets a call and fails with retryable status.
|
||||
error = |
||||
grpc_server_request_call(f.server, &s, &call_details, |
||||
&request_metadata_recv, f.cq, f.cq, tag(101)); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(101), true); |
||||
cq_verify(cqv); |
||||
|
||||
peer = grpc_call_get_peer(s); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "server_peer=%s", peer); |
||||
gpr_free(peer); |
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
op->data.send_status_from_server.trailing_metadata_count = 0; |
||||
op->data.send_status_from_server.status = GRPC_STATUS_ABORTED; |
||||
op->data.send_status_from_server.status_details = &status_details; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
||||
op->data.recv_close_on_server.cancelled = &was_cancelled; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
CQ_EXPECT_COMPLETION(cqv, tag(102), true); |
||||
cq_verify(cqv); |
||||
|
||||
grpc_call_unref(s); |
||||
grpc_metadata_array_destroy(&request_metadata_recv); |
||||
grpc_metadata_array_init(&request_metadata_recv); |
||||
grpc_call_details_destroy(&call_details); |
||||
grpc_call_details_init(&call_details); |
||||
|
||||
// Server gets a second call (the retry).
|
||||
error = |
||||
grpc_server_request_call(f.server, &s, &call_details, |
||||
&request_metadata_recv, f.cq, f.cq, tag(201)); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(201), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Initiate cancellation.
|
||||
GPR_ASSERT(GRPC_CALL_OK == mode.initiate_cancel(c, nullptr)); |
||||
|
||||
CQ_EXPECT_COMPLETION(cqv, tag(1), true); |
||||
cq_verify(cqv); |
||||
|
||||
GPR_ASSERT(status == mode.expect_status); |
||||
GPR_ASSERT(was_cancelled == 1); |
||||
|
||||
grpc_slice_unref(details); |
||||
grpc_metadata_array_destroy(&initial_metadata_recv); |
||||
grpc_metadata_array_destroy(&trailing_metadata_recv); |
||||
grpc_metadata_array_destroy(&request_metadata_recv); |
||||
grpc_call_details_destroy(&call_details); |
||||
grpc_byte_buffer_destroy(request_payload); |
||||
grpc_byte_buffer_destroy(response_payload); |
||||
grpc_byte_buffer_destroy(request_payload_recv); |
||||
grpc_byte_buffer_destroy(response_payload_recv); |
||||
|
||||
grpc_call_unref(c); |
||||
grpc_call_unref(s); |
||||
|
||||
cq_verifier_destroy(cqv); |
||||
|
||||
end_test(&f); |
||||
config.tear_down_data(&f); |
||||
} |
||||
|
||||
void retry_cancellation(grpc_end2end_test_config config) { |
||||
GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL); |
||||
for (size_t i = 0; i < GPR_ARRAY_SIZE(cancellation_modes); ++i) { |
||||
test_retry_cancellation(config, cancellation_modes[i]); |
||||
} |
||||
} |
||||
|
||||
void retry_cancellation_pre_init(void) {} |
@ -0,0 +1,262 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed 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 "test/core/end2end/end2end_tests.h" |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include <grpc/byte_buffer.h> |
||||
#include <grpc/grpc.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
#include <grpc/support/time.h> |
||||
|
||||
#include "src/core/lib/channel/channel_args.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "src/core/lib/gpr/useful.h" |
||||
#include "src/core/lib/iomgr/exec_ctx.h" |
||||
#include "src/core/lib/transport/static_metadata.h" |
||||
|
||||
#include "test/core/end2end/cq_verifier.h" |
||||
#include "test/core/end2end/tests/cancel_test_helpers.h" |
||||
|
||||
static void* tag(intptr_t t) { return (void*)t; } |
||||
|
||||
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
||||
const char* test_name, |
||||
grpc_channel_args* client_args, |
||||
grpc_channel_args* server_args) { |
||||
grpc_end2end_test_fixture f; |
||||
gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name); |
||||
f = config.create_fixture(client_args, server_args); |
||||
config.init_server(&f, server_args); |
||||
config.init_client(&f, client_args); |
||||
return f; |
||||
} |
||||
|
||||
static gpr_timespec n_seconds_from_now(int n) { |
||||
return grpc_timeout_seconds_to_deadline(n); |
||||
} |
||||
|
||||
static gpr_timespec five_seconds_from_now(void) { |
||||
return n_seconds_from_now(5); |
||||
} |
||||
|
||||
static void drain_cq(grpc_completion_queue* cq) { |
||||
grpc_event ev; |
||||
do { |
||||
ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
||||
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
||||
} |
||||
|
||||
static void shutdown_server(grpc_end2end_test_fixture* f) { |
||||
if (!f->server) return; |
||||
grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000)); |
||||
GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000), |
||||
grpc_timeout_seconds_to_deadline(5), |
||||
nullptr) |
||||
.type == GRPC_OP_COMPLETE); |
||||
grpc_server_destroy(f->server); |
||||
f->server = nullptr; |
||||
} |
||||
|
||||
static void shutdown_client(grpc_end2end_test_fixture* f) { |
||||
if (!f->client) return; |
||||
grpc_channel_destroy(f->client); |
||||
f->client = nullptr; |
||||
} |
||||
|
||||
static void end_test(grpc_end2end_test_fixture* f) { |
||||
shutdown_server(f); |
||||
shutdown_client(f); |
||||
|
||||
grpc_completion_queue_shutdown(f->cq); |
||||
drain_cq(f->cq); |
||||
grpc_completion_queue_destroy(f->cq); |
||||
grpc_completion_queue_destroy(f->shutdown_cq); |
||||
} |
||||
|
||||
// Tests that we don't retry when retries are disabled via the
|
||||
// GRPC_ARG_ENABLE_RETRIES channel arg, even when there is retry
|
||||
// configuration in the service config.
|
||||
// - 1 retry allowed for ABORTED status
|
||||
// - first attempt returns ABORTED but does not retry
|
||||
static void test_retry_disabled(grpc_end2end_test_config config) { |
||||
grpc_call* c; |
||||
grpc_call* s; |
||||
grpc_op ops[6]; |
||||
grpc_op* op; |
||||
grpc_metadata_array initial_metadata_recv; |
||||
grpc_metadata_array trailing_metadata_recv; |
||||
grpc_metadata_array request_metadata_recv; |
||||
grpc_call_details call_details; |
||||
grpc_slice request_payload_slice = grpc_slice_from_static_string("foo"); |
||||
grpc_slice response_payload_slice = grpc_slice_from_static_string("bar"); |
||||
grpc_byte_buffer* request_payload = |
||||
grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
||||
grpc_byte_buffer* response_payload = |
||||
grpc_raw_byte_buffer_create(&response_payload_slice, 1); |
||||
grpc_byte_buffer* request_payload_recv = nullptr; |
||||
grpc_byte_buffer* response_payload_recv = nullptr; |
||||
grpc_status_code status; |
||||
grpc_call_error error; |
||||
grpc_slice details; |
||||
int was_cancelled = 2; |
||||
char* peer; |
||||
|
||||
grpc_arg args[2]; |
||||
args[0].type = GRPC_ARG_STRING; |
||||
args[0].key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG); |
||||
args[0].value.string = const_cast<char*>( |
||||
"{\n" |
||||
" \"methodConfig\": [ {\n" |
||||
" \"name\": [\n" |
||||
" { \"service\": \"service\", \"method\": \"method\" }\n" |
||||
" ],\n" |
||||
" \"retryPolicy\": {\n" |
||||
" \"maxAttempts\": 2,\n" |
||||
" \"initialBackoff\": \"1s\",\n" |
||||
" \"maxBackoff\": \"120s\",\n" |
||||
" \"backoffMultiplier\": 1.6,\n" |
||||
" \"retryableStatusCodes\": [ \"ABORTED\" ]\n" |
||||
" }\n" |
||||
" } ]\n" |
||||
"}"); |
||||
args[1].type = GRPC_ARG_INTEGER; |
||||
args[1].key = const_cast<char*>(GRPC_ARG_ENABLE_RETRIES); |
||||
args[1].value.integer = 0; |
||||
grpc_channel_args client_args = {GPR_ARRAY_SIZE(args), args}; |
||||
grpc_end2end_test_fixture f = |
||||
begin_test(config, "retry_disabled", &client_args, nullptr); |
||||
|
||||
cq_verifier* cqv = cq_verifier_create(f.cq); |
||||
|
||||
gpr_timespec deadline = five_seconds_from_now(); |
||||
c = grpc_channel_create_call( |
||||
f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
||||
grpc_slice_from_static_string("/service/method"), |
||||
get_host_override_slice("foo.test.google.fr:1234", config), deadline, |
||||
nullptr); |
||||
GPR_ASSERT(c); |
||||
|
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
grpc_metadata_array_init(&initial_metadata_recv); |
||||
grpc_metadata_array_init(&trailing_metadata_recv); |
||||
grpc_metadata_array_init(&request_metadata_recv); |
||||
grpc_call_details_init(&call_details); |
||||
grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = request_payload; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &response_payload_recv; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
||||
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
||||
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
||||
op->data.recv_status_on_client.status = &status; |
||||
op->data.recv_status_on_client.status_details = &details; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
error = |
||||
grpc_server_request_call(f.server, &s, &call_details, |
||||
&request_metadata_recv, f.cq, f.cq, tag(101)); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(101), true); |
||||
cq_verify(cqv); |
||||
|
||||
peer = grpc_call_get_peer(s); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "server_peer=%s", peer); |
||||
gpr_free(peer); |
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
op->data.send_status_from_server.trailing_metadata_count = 0; |
||||
op->data.send_status_from_server.status = GRPC_STATUS_ABORTED; |
||||
op->data.send_status_from_server.status_details = &status_details; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
||||
op->data.recv_close_on_server.cancelled = &was_cancelled; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
CQ_EXPECT_COMPLETION(cqv, tag(102), true); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(1), true); |
||||
cq_verify(cqv); |
||||
|
||||
GPR_ASSERT(status == GRPC_STATUS_ABORTED); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz")); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method")); |
||||
validate_host_override_string("foo.test.google.fr:1234", call_details.host, |
||||
config); |
||||
GPR_ASSERT(0 == call_details.flags); |
||||
GPR_ASSERT(was_cancelled == 1); |
||||
|
||||
grpc_slice_unref(details); |
||||
grpc_metadata_array_destroy(&initial_metadata_recv); |
||||
grpc_metadata_array_destroy(&trailing_metadata_recv); |
||||
grpc_metadata_array_destroy(&request_metadata_recv); |
||||
grpc_call_details_destroy(&call_details); |
||||
grpc_byte_buffer_destroy(request_payload); |
||||
grpc_byte_buffer_destroy(response_payload); |
||||
grpc_byte_buffer_destroy(request_payload_recv); |
||||
grpc_byte_buffer_destroy(response_payload_recv); |
||||
|
||||
grpc_call_unref(c); |
||||
grpc_call_unref(s); |
||||
|
||||
cq_verifier_destroy(cqv); |
||||
|
||||
end_test(&f); |
||||
config.tear_down_data(&f); |
||||
} |
||||
|
||||
void retry_disabled(grpc_end2end_test_config config) { |
||||
GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL); |
||||
test_retry_disabled(config); |
||||
} |
||||
|
||||
void retry_disabled_pre_init(void) {} |
@ -0,0 +1,266 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed 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 "test/core/end2end/end2end_tests.h" |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include <grpc/byte_buffer.h> |
||||
#include <grpc/grpc.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
#include <grpc/support/time.h> |
||||
|
||||
#include "src/core/lib/channel/channel_args.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "src/core/lib/gpr/useful.h" |
||||
#include "src/core/lib/iomgr/exec_ctx.h" |
||||
#include "src/core/lib/transport/static_metadata.h" |
||||
|
||||
#include "test/core/end2end/cq_verifier.h" |
||||
#include "test/core/end2end/tests/cancel_test_helpers.h" |
||||
|
||||
static void* tag(intptr_t t) { return (void*)t; } |
||||
|
||||
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
||||
const char* test_name, |
||||
grpc_channel_args* client_args, |
||||
grpc_channel_args* server_args) { |
||||
grpc_end2end_test_fixture f; |
||||
gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name); |
||||
f = config.create_fixture(client_args, server_args); |
||||
config.init_server(&f, server_args); |
||||
config.init_client(&f, client_args); |
||||
return f; |
||||
} |
||||
|
||||
static gpr_timespec n_seconds_from_now(int n) { |
||||
return grpc_timeout_seconds_to_deadline(n); |
||||
} |
||||
|
||||
static gpr_timespec five_seconds_from_now(void) { |
||||
return n_seconds_from_now(5); |
||||
} |
||||
|
||||
static void drain_cq(grpc_completion_queue* cq) { |
||||
grpc_event ev; |
||||
do { |
||||
ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
||||
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
||||
} |
||||
|
||||
static void shutdown_server(grpc_end2end_test_fixture* f) { |
||||
if (!f->server) return; |
||||
grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000)); |
||||
GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000), |
||||
grpc_timeout_seconds_to_deadline(5), |
||||
nullptr) |
||||
.type == GRPC_OP_COMPLETE); |
||||
grpc_server_destroy(f->server); |
||||
f->server = nullptr; |
||||
} |
||||
|
||||
static void shutdown_client(grpc_end2end_test_fixture* f) { |
||||
if (!f->client) return; |
||||
grpc_channel_destroy(f->client); |
||||
f->client = nullptr; |
||||
} |
||||
|
||||
static void end_test(grpc_end2end_test_fixture* f) { |
||||
shutdown_server(f); |
||||
shutdown_client(f); |
||||
|
||||
grpc_completion_queue_shutdown(f->cq); |
||||
drain_cq(f->cq); |
||||
grpc_completion_queue_destroy(f->cq); |
||||
grpc_completion_queue_destroy(f->shutdown_cq); |
||||
} |
||||
|
||||
// Tests that we don't make any further attempts after we exceed the
|
||||
// max buffer size.
|
||||
// - 1 retry allowed for ABORTED status
|
||||
// - buffer size set to 2 bytes
|
||||
// - client sends a 3-byte message
|
||||
// - first attempt gets ABORTED but is not retried
|
||||
static void test_retry_exceeds_buffer_size_in_initial_batch( |
||||
grpc_end2end_test_config config) { |
||||
grpc_call* c; |
||||
grpc_call* s; |
||||
grpc_op ops[6]; |
||||
grpc_op* op; |
||||
grpc_metadata_array initial_metadata_recv; |
||||
grpc_metadata_array trailing_metadata_recv; |
||||
grpc_metadata_array request_metadata_recv; |
||||
grpc_call_details call_details; |
||||
grpc_slice request_payload_slice = grpc_slice_from_static_string("foo"); |
||||
grpc_slice response_payload_slice = grpc_slice_from_static_string("bar"); |
||||
grpc_byte_buffer* request_payload = |
||||
grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
||||
grpc_byte_buffer* response_payload = |
||||
grpc_raw_byte_buffer_create(&response_payload_slice, 1); |
||||
grpc_byte_buffer* request_payload_recv = nullptr; |
||||
grpc_byte_buffer* response_payload_recv = nullptr; |
||||
grpc_status_code status; |
||||
grpc_call_error error; |
||||
grpc_slice details; |
||||
int was_cancelled = 2; |
||||
char* peer; |
||||
|
||||
grpc_arg args[2]; |
||||
args[0].type = GRPC_ARG_STRING; |
||||
args[0].key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG); |
||||
args[0].value.string = const_cast<char*>( |
||||
"{\n" |
||||
" \"methodConfig\": [ {\n" |
||||
" \"name\": [\n" |
||||
" { \"service\": \"service\", \"method\": \"method\" }\n" |
||||
" ],\n" |
||||
" \"retryPolicy\": {\n" |
||||
" \"maxAttempts\": 2,\n" |
||||
" \"initialBackoff\": \"1s\",\n" |
||||
" \"maxBackoff\": \"120s\",\n" |
||||
" \"backoffMultiplier\": 1.6,\n" |
||||
" \"retryableStatusCodes\": [ \"ABORTED\" ]\n" |
||||
" }\n" |
||||
" } ]\n" |
||||
"}"); |
||||
args[1].type = GRPC_ARG_INTEGER; |
||||
args[1].key = const_cast<char*>(GRPC_ARG_PER_RPC_RETRY_BUFFER_SIZE); |
||||
args[1].value.integer = 2; |
||||
grpc_channel_args client_args = {GPR_ARRAY_SIZE(args), args}; |
||||
grpc_end2end_test_fixture f = |
||||
begin_test(config, "retry_exceeds_buffer_size_in_initial_batch", |
||||
&client_args, nullptr); |
||||
|
||||
cq_verifier* cqv = cq_verifier_create(f.cq); |
||||
|
||||
gpr_timespec deadline = five_seconds_from_now(); |
||||
c = grpc_channel_create_call( |
||||
f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
||||
grpc_slice_from_static_string("/service/method"), |
||||
get_host_override_slice("foo.test.google.fr:1234", config), deadline, |
||||
nullptr); |
||||
GPR_ASSERT(c); |
||||
|
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
grpc_metadata_array_init(&initial_metadata_recv); |
||||
grpc_metadata_array_init(&trailing_metadata_recv); |
||||
grpc_metadata_array_init(&request_metadata_recv); |
||||
grpc_call_details_init(&call_details); |
||||
grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = request_payload; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &response_payload_recv; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
||||
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
||||
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
||||
op->data.recv_status_on_client.status = &status; |
||||
op->data.recv_status_on_client.status_details = &details; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
error = |
||||
grpc_server_request_call(f.server, &s, &call_details, |
||||
&request_metadata_recv, f.cq, f.cq, tag(101)); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(101), true); |
||||
cq_verify(cqv); |
||||
|
||||
peer = grpc_call_get_peer(s); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "server_peer=%s", peer); |
||||
gpr_free(peer); |
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
op->data.send_status_from_server.trailing_metadata_count = 0; |
||||
op->data.send_status_from_server.status = GRPC_STATUS_ABORTED; |
||||
op->data.send_status_from_server.status_details = &status_details; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
||||
op->data.recv_close_on_server.cancelled = &was_cancelled; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
CQ_EXPECT_COMPLETION(cqv, tag(102), true); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(1), true); |
||||
cq_verify(cqv); |
||||
|
||||
GPR_ASSERT(status == GRPC_STATUS_ABORTED); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz")); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method")); |
||||
validate_host_override_string("foo.test.google.fr:1234", call_details.host, |
||||
config); |
||||
GPR_ASSERT(0 == call_details.flags); |
||||
GPR_ASSERT(was_cancelled == 1); |
||||
|
||||
grpc_slice_unref(details); |
||||
grpc_metadata_array_destroy(&initial_metadata_recv); |
||||
grpc_metadata_array_destroy(&trailing_metadata_recv); |
||||
grpc_metadata_array_destroy(&request_metadata_recv); |
||||
grpc_call_details_destroy(&call_details); |
||||
grpc_byte_buffer_destroy(request_payload); |
||||
grpc_byte_buffer_destroy(response_payload); |
||||
grpc_byte_buffer_destroy(request_payload_recv); |
||||
grpc_byte_buffer_destroy(response_payload_recv); |
||||
|
||||
grpc_call_unref(c); |
||||
grpc_call_unref(s); |
||||
|
||||
cq_verifier_destroy(cqv); |
||||
|
||||
end_test(&f); |
||||
config.tear_down_data(&f); |
||||
} |
||||
|
||||
void retry_exceeds_buffer_size_in_initial_batch( |
||||
grpc_end2end_test_config config) { |
||||
GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL); |
||||
test_retry_exceeds_buffer_size_in_initial_batch(config); |
||||
} |
||||
|
||||
void retry_exceeds_buffer_size_in_initial_batch_pre_init(void) {} |
@ -0,0 +1,276 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed 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 "test/core/end2end/end2end_tests.h" |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include <grpc/byte_buffer.h> |
||||
#include <grpc/grpc.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
#include <grpc/support/time.h> |
||||
|
||||
#include "src/core/lib/channel/channel_args.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "src/core/lib/gpr/useful.h" |
||||
#include "src/core/lib/iomgr/exec_ctx.h" |
||||
#include "src/core/lib/transport/static_metadata.h" |
||||
|
||||
#include "test/core/end2end/cq_verifier.h" |
||||
#include "test/core/end2end/tests/cancel_test_helpers.h" |
||||
|
||||
static void* tag(intptr_t t) { return (void*)t; } |
||||
|
||||
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
||||
const char* test_name, |
||||
grpc_channel_args* client_args, |
||||
grpc_channel_args* server_args) { |
||||
grpc_end2end_test_fixture f; |
||||
gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name); |
||||
f = config.create_fixture(client_args, server_args); |
||||
config.init_server(&f, server_args); |
||||
config.init_client(&f, client_args); |
||||
return f; |
||||
} |
||||
|
||||
static gpr_timespec n_seconds_from_now(int n) { |
||||
return grpc_timeout_seconds_to_deadline(n); |
||||
} |
||||
|
||||
static gpr_timespec five_seconds_from_now(void) { |
||||
return n_seconds_from_now(5); |
||||
} |
||||
|
||||
static void drain_cq(grpc_completion_queue* cq) { |
||||
grpc_event ev; |
||||
do { |
||||
ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
||||
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
||||
} |
||||
|
||||
static void shutdown_server(grpc_end2end_test_fixture* f) { |
||||
if (!f->server) return; |
||||
grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000)); |
||||
GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000), |
||||
grpc_timeout_seconds_to_deadline(5), |
||||
nullptr) |
||||
.type == GRPC_OP_COMPLETE); |
||||
grpc_server_destroy(f->server); |
||||
f->server = nullptr; |
||||
} |
||||
|
||||
static void shutdown_client(grpc_end2end_test_fixture* f) { |
||||
if (!f->client) return; |
||||
grpc_channel_destroy(f->client); |
||||
f->client = nullptr; |
||||
} |
||||
|
||||
static void end_test(grpc_end2end_test_fixture* f) { |
||||
shutdown_server(f); |
||||
shutdown_client(f); |
||||
|
||||
grpc_completion_queue_shutdown(f->cq); |
||||
drain_cq(f->cq); |
||||
grpc_completion_queue_destroy(f->cq); |
||||
grpc_completion_queue_destroy(f->shutdown_cq); |
||||
} |
||||
|
||||
// Similar to the retry_exceeds_buffer_size_in_initial_batch test, but we
|
||||
// don't exceed the buffer size until the second batch.
|
||||
// - 1 retry allowed for ABORTED status
|
||||
// - buffer size set to 100 KiB (larger than initial metadata)
|
||||
// - client sends a 100 KiB message
|
||||
// - first attempt gets ABORTED but is not retried
|
||||
static void test_retry_exceeds_buffer_size_in_subsequent_batch( |
||||
grpc_end2end_test_config config) { |
||||
grpc_call* c; |
||||
grpc_call* s; |
||||
grpc_op ops[6]; |
||||
grpc_op* op; |
||||
grpc_metadata_array initial_metadata_recv; |
||||
grpc_metadata_array trailing_metadata_recv; |
||||
grpc_metadata_array request_metadata_recv; |
||||
grpc_call_details call_details; |
||||
char buf[102401]; |
||||
memset(buf, 'a', sizeof(buf) - 1); |
||||
buf[sizeof(buf) - 1] = '\0'; |
||||
grpc_slice request_payload_slice = grpc_slice_from_static_string(buf); |
||||
grpc_slice response_payload_slice = grpc_slice_from_static_string("bar"); |
||||
grpc_byte_buffer* request_payload = |
||||
grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
||||
grpc_byte_buffer* response_payload = |
||||
grpc_raw_byte_buffer_create(&response_payload_slice, 1); |
||||
grpc_byte_buffer* request_payload_recv = nullptr; |
||||
grpc_byte_buffer* response_payload_recv = nullptr; |
||||
grpc_status_code status; |
||||
grpc_call_error error; |
||||
grpc_slice details; |
||||
int was_cancelled = 2; |
||||
char* peer; |
||||
|
||||
grpc_arg args[2]; |
||||
args[0].type = GRPC_ARG_STRING; |
||||
args[0].key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG); |
||||
args[0].value.string = const_cast<char*>( |
||||
"{\n" |
||||
" \"methodConfig\": [ {\n" |
||||
" \"name\": [\n" |
||||
" { \"service\": \"service\", \"method\": \"method\" }\n" |
||||
" ],\n" |
||||
" \"retryPolicy\": {\n" |
||||
" \"maxAttempts\": 2,\n" |
||||
" \"initialBackoff\": \"1s\",\n" |
||||
" \"maxBackoff\": \"120s\",\n" |
||||
" \"backoffMultiplier\": 1.6,\n" |
||||
" \"retryableStatusCodes\": [ \"ABORTED\" ]\n" |
||||
" }\n" |
||||
" } ]\n" |
||||
"}"); |
||||
args[1].type = GRPC_ARG_INTEGER; |
||||
args[1].key = const_cast<char*>(GRPC_ARG_PER_RPC_RETRY_BUFFER_SIZE); |
||||
args[1].value.integer = 102400; |
||||
grpc_channel_args client_args = {GPR_ARRAY_SIZE(args), args}; |
||||
grpc_end2end_test_fixture f = |
||||
begin_test(config, "retry_exceeds_buffer_size_in_subsequent_batch", |
||||
&client_args, nullptr); |
||||
|
||||
cq_verifier* cqv = cq_verifier_create(f.cq); |
||||
|
||||
gpr_timespec deadline = five_seconds_from_now(); |
||||
c = grpc_channel_create_call( |
||||
f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
||||
grpc_slice_from_static_string("/service/method"), |
||||
get_host_override_slice("foo.test.google.fr:1234", config), deadline, |
||||
nullptr); |
||||
GPR_ASSERT(c); |
||||
|
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
grpc_metadata_array_init(&initial_metadata_recv); |
||||
grpc_metadata_array_init(&trailing_metadata_recv); |
||||
grpc_metadata_array_init(&request_metadata_recv); |
||||
grpc_call_details_init(&call_details); |
||||
grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(1), true); |
||||
cq_verify(cqv); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = request_payload; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &response_payload_recv; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
||||
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
||||
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
||||
op->data.recv_status_on_client.status = &status; |
||||
op->data.recv_status_on_client.status_details = &details; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(2), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
error = |
||||
grpc_server_request_call(f.server, &s, &call_details, |
||||
&request_metadata_recv, f.cq, f.cq, tag(101)); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(101), true); |
||||
cq_verify(cqv); |
||||
|
||||
peer = grpc_call_get_peer(s); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "server_peer=%s", peer); |
||||
gpr_free(peer); |
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
op->data.send_status_from_server.trailing_metadata_count = 0; |
||||
op->data.send_status_from_server.status = GRPC_STATUS_ABORTED; |
||||
op->data.send_status_from_server.status_details = &status_details; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
||||
op->data.recv_close_on_server.cancelled = &was_cancelled; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
CQ_EXPECT_COMPLETION(cqv, tag(102), true); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(2), true); |
||||
cq_verify(cqv); |
||||
|
||||
GPR_ASSERT(status == GRPC_STATUS_ABORTED); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz")); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method")); |
||||
validate_host_override_string("foo.test.google.fr:1234", call_details.host, |
||||
config); |
||||
GPR_ASSERT(0 == call_details.flags); |
||||
GPR_ASSERT(was_cancelled == 1); |
||||
|
||||
grpc_slice_unref(details); |
||||
grpc_metadata_array_destroy(&initial_metadata_recv); |
||||
grpc_metadata_array_destroy(&trailing_metadata_recv); |
||||
grpc_metadata_array_destroy(&request_metadata_recv); |
||||
grpc_call_details_destroy(&call_details); |
||||
grpc_byte_buffer_destroy(request_payload); |
||||
grpc_byte_buffer_destroy(response_payload); |
||||
grpc_byte_buffer_destroy(request_payload_recv); |
||||
grpc_byte_buffer_destroy(response_payload_recv); |
||||
|
||||
grpc_call_unref(c); |
||||
grpc_call_unref(s); |
||||
|
||||
cq_verifier_destroy(cqv); |
||||
|
||||
end_test(&f); |
||||
config.tear_down_data(&f); |
||||
} |
||||
|
||||
void retry_exceeds_buffer_size_in_subsequent_batch( |
||||
grpc_end2end_test_config config) { |
||||
GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL); |
||||
test_retry_exceeds_buffer_size_in_subsequent_batch(config); |
||||
} |
||||
|
||||
void retry_exceeds_buffer_size_in_subsequent_batch_pre_init(void) {} |
@ -0,0 +1,257 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed 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 "test/core/end2end/end2end_tests.h" |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include <grpc/byte_buffer.h> |
||||
#include <grpc/grpc.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
#include <grpc/support/time.h> |
||||
|
||||
#include "src/core/lib/channel/channel_args.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "src/core/lib/gpr/useful.h" |
||||
#include "src/core/lib/iomgr/exec_ctx.h" |
||||
#include "src/core/lib/transport/static_metadata.h" |
||||
|
||||
#include "test/core/end2end/cq_verifier.h" |
||||
#include "test/core/end2end/tests/cancel_test_helpers.h" |
||||
|
||||
static void* tag(intptr_t t) { return (void*)t; } |
||||
|
||||
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
||||
const char* test_name, |
||||
grpc_channel_args* client_args, |
||||
grpc_channel_args* server_args) { |
||||
grpc_end2end_test_fixture f; |
||||
gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name); |
||||
f = config.create_fixture(client_args, server_args); |
||||
config.init_server(&f, server_args); |
||||
config.init_client(&f, client_args); |
||||
return f; |
||||
} |
||||
|
||||
static gpr_timespec n_seconds_from_now(int n) { |
||||
return grpc_timeout_seconds_to_deadline(n); |
||||
} |
||||
|
||||
static gpr_timespec five_seconds_from_now(void) { |
||||
return n_seconds_from_now(5); |
||||
} |
||||
|
||||
static void drain_cq(grpc_completion_queue* cq) { |
||||
grpc_event ev; |
||||
do { |
||||
ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
||||
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
||||
} |
||||
|
||||
static void shutdown_server(grpc_end2end_test_fixture* f) { |
||||
if (!f->server) return; |
||||
grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000)); |
||||
GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000), |
||||
grpc_timeout_seconds_to_deadline(5), |
||||
nullptr) |
||||
.type == GRPC_OP_COMPLETE); |
||||
grpc_server_destroy(f->server); |
||||
f->server = nullptr; |
||||
} |
||||
|
||||
static void shutdown_client(grpc_end2end_test_fixture* f) { |
||||
if (!f->client) return; |
||||
grpc_channel_destroy(f->client); |
||||
f->client = nullptr; |
||||
} |
||||
|
||||
static void end_test(grpc_end2end_test_fixture* f) { |
||||
shutdown_server(f); |
||||
shutdown_client(f); |
||||
|
||||
grpc_completion_queue_shutdown(f->cq); |
||||
drain_cq(f->cq); |
||||
grpc_completion_queue_destroy(f->cq); |
||||
grpc_completion_queue_destroy(f->shutdown_cq); |
||||
} |
||||
|
||||
// Tests that we don't retry for non-retryable status codes.
|
||||
// - 1 retry allowed for ABORTED status
|
||||
// - first attempt gets INVALID_ARGUMENT, so no retry is done
|
||||
static void test_retry_non_retriable_status(grpc_end2end_test_config config) { |
||||
grpc_call* c; |
||||
grpc_call* s; |
||||
grpc_op ops[6]; |
||||
grpc_op* op; |
||||
grpc_metadata_array initial_metadata_recv; |
||||
grpc_metadata_array trailing_metadata_recv; |
||||
grpc_metadata_array request_metadata_recv; |
||||
grpc_call_details call_details; |
||||
grpc_slice request_payload_slice = grpc_slice_from_static_string("foo"); |
||||
grpc_slice response_payload_slice = grpc_slice_from_static_string("bar"); |
||||
grpc_byte_buffer* request_payload = |
||||
grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
||||
grpc_byte_buffer* response_payload = |
||||
grpc_raw_byte_buffer_create(&response_payload_slice, 1); |
||||
grpc_byte_buffer* request_payload_recv = nullptr; |
||||
grpc_byte_buffer* response_payload_recv = nullptr; |
||||
grpc_status_code status; |
||||
grpc_call_error error; |
||||
grpc_slice details; |
||||
int was_cancelled = 2; |
||||
char* peer; |
||||
|
||||
grpc_arg arg; |
||||
arg.type = GRPC_ARG_STRING; |
||||
arg.key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG); |
||||
arg.value.string = const_cast<char*>( |
||||
"{\n" |
||||
" \"methodConfig\": [ {\n" |
||||
" \"name\": [\n" |
||||
" { \"service\": \"service\", \"method\": \"method\" }\n" |
||||
" ],\n" |
||||
" \"retryPolicy\": {\n" |
||||
" \"maxAttempts\": 2,\n" |
||||
" \"initialBackoff\": \"1s\",\n" |
||||
" \"maxBackoff\": \"120s\",\n" |
||||
" \"backoffMultiplier\": 1.6,\n" |
||||
" \"retryableStatusCodes\": [ \"ABORTED\" ]\n" |
||||
" }\n" |
||||
" } ]\n" |
||||
"}"); |
||||
grpc_channel_args client_args = {1, &arg}; |
||||
grpc_end2end_test_fixture f = |
||||
begin_test(config, "retry_non_retriable_status", &client_args, nullptr); |
||||
|
||||
cq_verifier* cqv = cq_verifier_create(f.cq); |
||||
|
||||
gpr_timespec deadline = five_seconds_from_now(); |
||||
c = grpc_channel_create_call( |
||||
f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
||||
grpc_slice_from_static_string("/service/method"), |
||||
get_host_override_slice("foo.test.google.fr:1234", config), deadline, |
||||
nullptr); |
||||
GPR_ASSERT(c); |
||||
|
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
grpc_metadata_array_init(&initial_metadata_recv); |
||||
grpc_metadata_array_init(&trailing_metadata_recv); |
||||
grpc_metadata_array_init(&request_metadata_recv); |
||||
grpc_call_details_init(&call_details); |
||||
grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = request_payload; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &response_payload_recv; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
||||
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
||||
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
||||
op->data.recv_status_on_client.status = &status; |
||||
op->data.recv_status_on_client.status_details = &details; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
error = |
||||
grpc_server_request_call(f.server, &s, &call_details, |
||||
&request_metadata_recv, f.cq, f.cq, tag(101)); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(101), true); |
||||
cq_verify(cqv); |
||||
|
||||
peer = grpc_call_get_peer(s); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "server_peer=%s", peer); |
||||
gpr_free(peer); |
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
op->data.send_status_from_server.trailing_metadata_count = 0; |
||||
op->data.send_status_from_server.status = GRPC_STATUS_INVALID_ARGUMENT; |
||||
op->data.send_status_from_server.status_details = &status_details; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
||||
op->data.recv_close_on_server.cancelled = &was_cancelled; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
CQ_EXPECT_COMPLETION(cqv, tag(102), true); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(1), true); |
||||
cq_verify(cqv); |
||||
|
||||
GPR_ASSERT(status == GRPC_STATUS_INVALID_ARGUMENT); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz")); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method")); |
||||
validate_host_override_string("foo.test.google.fr:1234", call_details.host, |
||||
config); |
||||
GPR_ASSERT(0 == call_details.flags); |
||||
GPR_ASSERT(was_cancelled == 1); |
||||
|
||||
grpc_slice_unref(details); |
||||
grpc_metadata_array_destroy(&initial_metadata_recv); |
||||
grpc_metadata_array_destroy(&trailing_metadata_recv); |
||||
grpc_metadata_array_destroy(&request_metadata_recv); |
||||
grpc_call_details_destroy(&call_details); |
||||
grpc_byte_buffer_destroy(request_payload); |
||||
grpc_byte_buffer_destroy(response_payload); |
||||
grpc_byte_buffer_destroy(request_payload_recv); |
||||
grpc_byte_buffer_destroy(response_payload_recv); |
||||
|
||||
grpc_call_unref(c); |
||||
grpc_call_unref(s); |
||||
|
||||
cq_verifier_destroy(cqv); |
||||
|
||||
end_test(&f); |
||||
config.tear_down_data(&f); |
||||
} |
||||
|
||||
void retry_non_retriable_status(grpc_end2end_test_config config) { |
||||
GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL); |
||||
test_retry_non_retriable_status(config); |
||||
} |
||||
|
||||
void retry_non_retriable_status_pre_init(void) {} |
@ -0,0 +1,268 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed 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 "test/core/end2end/end2end_tests.h" |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include <grpc/byte_buffer.h> |
||||
#include <grpc/grpc.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
#include <grpc/support/time.h> |
||||
|
||||
#include "src/core/lib/channel/channel_args.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "src/core/lib/gpr/useful.h" |
||||
#include "src/core/lib/iomgr/exec_ctx.h" |
||||
#include "src/core/lib/transport/static_metadata.h" |
||||
|
||||
#include "test/core/end2end/cq_verifier.h" |
||||
#include "test/core/end2end/tests/cancel_test_helpers.h" |
||||
|
||||
static void* tag(intptr_t t) { return (void*)t; } |
||||
|
||||
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
||||
const char* test_name, |
||||
grpc_channel_args* client_args, |
||||
grpc_channel_args* server_args) { |
||||
grpc_end2end_test_fixture f; |
||||
gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name); |
||||
f = config.create_fixture(client_args, server_args); |
||||
config.init_server(&f, server_args); |
||||
config.init_client(&f, client_args); |
||||
return f; |
||||
} |
||||
|
||||
static gpr_timespec n_seconds_from_now(int n) { |
||||
return grpc_timeout_seconds_to_deadline(n); |
||||
} |
||||
|
||||
static gpr_timespec five_seconds_from_now(void) { |
||||
return n_seconds_from_now(5); |
||||
} |
||||
|
||||
static void drain_cq(grpc_completion_queue* cq) { |
||||
grpc_event ev; |
||||
do { |
||||
ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
||||
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
||||
} |
||||
|
||||
static void shutdown_server(grpc_end2end_test_fixture* f) { |
||||
if (!f->server) return; |
||||
grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000)); |
||||
GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000), |
||||
grpc_timeout_seconds_to_deadline(5), |
||||
nullptr) |
||||
.type == GRPC_OP_COMPLETE); |
||||
grpc_server_destroy(f->server); |
||||
f->server = nullptr; |
||||
} |
||||
|
||||
static void shutdown_client(grpc_end2end_test_fixture* f) { |
||||
if (!f->client) return; |
||||
grpc_channel_destroy(f->client); |
||||
f->client = nullptr; |
||||
} |
||||
|
||||
static void end_test(grpc_end2end_test_fixture* f) { |
||||
shutdown_server(f); |
||||
shutdown_client(f); |
||||
|
||||
grpc_completion_queue_shutdown(f->cq); |
||||
drain_cq(f->cq); |
||||
grpc_completion_queue_destroy(f->cq); |
||||
grpc_completion_queue_destroy(f->shutdown_cq); |
||||
} |
||||
|
||||
// Tests that receiving initial metadata commits the call.
|
||||
// - 1 retry allowed for ABORTED status
|
||||
// - first attempt receives initial metadata before trailing metadata,
|
||||
// so no retry is done even though status was ABORTED
|
||||
static void test_retry_recv_initial_metadata(grpc_end2end_test_config config) { |
||||
grpc_call* c; |
||||
grpc_call* s; |
||||
grpc_op ops[6]; |
||||
grpc_op* op; |
||||
grpc_metadata_array initial_metadata_recv; |
||||
grpc_metadata_array trailing_metadata_recv; |
||||
grpc_metadata_array request_metadata_recv; |
||||
grpc_call_details call_details; |
||||
grpc_slice request_payload_slice = grpc_slice_from_static_string("foo"); |
||||
grpc_slice response_payload_slice = grpc_slice_from_static_string("bar"); |
||||
grpc_byte_buffer* request_payload = |
||||
grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
||||
grpc_byte_buffer* response_payload = |
||||
grpc_raw_byte_buffer_create(&response_payload_slice, 1); |
||||
grpc_byte_buffer* request_payload_recv = nullptr; |
||||
grpc_byte_buffer* response_payload_recv = nullptr; |
||||
grpc_status_code status; |
||||
grpc_call_error error; |
||||
grpc_slice details; |
||||
int was_cancelled = 2; |
||||
char* peer; |
||||
|
||||
grpc_arg arg; |
||||
arg.type = GRPC_ARG_STRING; |
||||
arg.key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG); |
||||
arg.value.string = const_cast<char*>( |
||||
"{\n" |
||||
" \"methodConfig\": [ {\n" |
||||
" \"name\": [\n" |
||||
" { \"service\": \"service\", \"method\": \"method\" }\n" |
||||
" ],\n" |
||||
" \"retryPolicy\": {\n" |
||||
" \"maxAttempts\": 2,\n" |
||||
" \"initialBackoff\": \"1s\",\n" |
||||
" \"maxBackoff\": \"120s\",\n" |
||||
" \"backoffMultiplier\": 1.6,\n" |
||||
" \"retryableStatusCodes\": [ \"ABORTED\" ]\n" |
||||
" }\n" |
||||
" } ]\n" |
||||
"}"); |
||||
grpc_channel_args client_args = {1, &arg}; |
||||
grpc_end2end_test_fixture f = |
||||
begin_test(config, "retry_recv_initial_metadata", &client_args, nullptr); |
||||
|
||||
cq_verifier* cqv = cq_verifier_create(f.cq); |
||||
|
||||
gpr_timespec deadline = five_seconds_from_now(); |
||||
c = grpc_channel_create_call( |
||||
f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
||||
grpc_slice_from_static_string("/service/method"), |
||||
get_host_override_slice("foo.test.google.fr:1234", config), deadline, |
||||
nullptr); |
||||
GPR_ASSERT(c); |
||||
|
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
grpc_metadata_array_init(&initial_metadata_recv); |
||||
grpc_metadata_array_init(&trailing_metadata_recv); |
||||
grpc_metadata_array_init(&request_metadata_recv); |
||||
grpc_call_details_init(&call_details); |
||||
grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = request_payload; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &response_payload_recv; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
||||
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
||||
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
||||
op->data.recv_status_on_client.status = &status; |
||||
op->data.recv_status_on_client.status_details = &details; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
error = |
||||
grpc_server_request_call(f.server, &s, &call_details, |
||||
&request_metadata_recv, f.cq, f.cq, tag(101)); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(101), true); |
||||
cq_verify(cqv); |
||||
|
||||
peer = grpc_call_get_peer(s); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "server_peer=%s", peer); |
||||
gpr_free(peer); |
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
// Server sends initial metadata in its own batch, before sending
|
||||
// trailing metadata.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
CQ_EXPECT_COMPLETION(cqv, tag(102), true); |
||||
cq_verify(cqv); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
op->data.send_status_from_server.trailing_metadata_count = 0; |
||||
op->data.send_status_from_server.status = GRPC_STATUS_ABORTED; |
||||
op->data.send_status_from_server.status_details = &status_details; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
||||
op->data.recv_close_on_server.cancelled = &was_cancelled; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(103), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
CQ_EXPECT_COMPLETION(cqv, tag(103), true); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(1), true); |
||||
cq_verify(cqv); |
||||
|
||||
GPR_ASSERT(status == GRPC_STATUS_ABORTED); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz")); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method")); |
||||
validate_host_override_string("foo.test.google.fr:1234", call_details.host, |
||||
config); |
||||
GPR_ASSERT(0 == call_details.flags); |
||||
GPR_ASSERT(was_cancelled == 1); |
||||
|
||||
grpc_slice_unref(details); |
||||
grpc_metadata_array_destroy(&initial_metadata_recv); |
||||
grpc_metadata_array_destroy(&trailing_metadata_recv); |
||||
grpc_metadata_array_destroy(&request_metadata_recv); |
||||
grpc_call_details_destroy(&call_details); |
||||
grpc_byte_buffer_destroy(request_payload); |
||||
grpc_byte_buffer_destroy(response_payload); |
||||
grpc_byte_buffer_destroy(request_payload_recv); |
||||
grpc_byte_buffer_destroy(response_payload_recv); |
||||
|
||||
grpc_call_unref(c); |
||||
grpc_call_unref(s); |
||||
|
||||
cq_verifier_destroy(cqv); |
||||
|
||||
end_test(&f); |
||||
config.tear_down_data(&f); |
||||
} |
||||
|
||||
void retry_recv_initial_metadata(grpc_end2end_test_config config) { |
||||
GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL); |
||||
test_retry_recv_initial_metadata(config); |
||||
} |
||||
|
||||
void retry_recv_initial_metadata_pre_init(void) {} |
@ -0,0 +1,261 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed 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 "test/core/end2end/end2end_tests.h" |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include <grpc/byte_buffer.h> |
||||
#include <grpc/grpc.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
#include <grpc/support/time.h> |
||||
|
||||
#include "src/core/lib/channel/channel_args.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "src/core/lib/gpr/useful.h" |
||||
#include "src/core/lib/iomgr/exec_ctx.h" |
||||
#include "src/core/lib/transport/static_metadata.h" |
||||
|
||||
#include "test/core/end2end/cq_verifier.h" |
||||
#include "test/core/end2end/tests/cancel_test_helpers.h" |
||||
|
||||
static void* tag(intptr_t t) { return (void*)t; } |
||||
|
||||
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
||||
const char* test_name, |
||||
grpc_channel_args* client_args, |
||||
grpc_channel_args* server_args) { |
||||
grpc_end2end_test_fixture f; |
||||
gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name); |
||||
f = config.create_fixture(client_args, server_args); |
||||
config.init_server(&f, server_args); |
||||
config.init_client(&f, client_args); |
||||
return f; |
||||
} |
||||
|
||||
static gpr_timespec n_seconds_from_now(int n) { |
||||
return grpc_timeout_seconds_to_deadline(n); |
||||
} |
||||
|
||||
static gpr_timespec five_seconds_from_now(void) { |
||||
return n_seconds_from_now(5); |
||||
} |
||||
|
||||
static void drain_cq(grpc_completion_queue* cq) { |
||||
grpc_event ev; |
||||
do { |
||||
ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
||||
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
||||
} |
||||
|
||||
static void shutdown_server(grpc_end2end_test_fixture* f) { |
||||
if (!f->server) return; |
||||
grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000)); |
||||
GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000), |
||||
grpc_timeout_seconds_to_deadline(5), |
||||
nullptr) |
||||
.type == GRPC_OP_COMPLETE); |
||||
grpc_server_destroy(f->server); |
||||
f->server = nullptr; |
||||
} |
||||
|
||||
static void shutdown_client(grpc_end2end_test_fixture* f) { |
||||
if (!f->client) return; |
||||
grpc_channel_destroy(f->client); |
||||
f->client = nullptr; |
||||
} |
||||
|
||||
static void end_test(grpc_end2end_test_fixture* f) { |
||||
shutdown_server(f); |
||||
shutdown_client(f); |
||||
|
||||
grpc_completion_queue_shutdown(f->cq); |
||||
drain_cq(f->cq); |
||||
grpc_completion_queue_destroy(f->cq); |
||||
grpc_completion_queue_destroy(f->shutdown_cq); |
||||
} |
||||
|
||||
// Tests that receiving a message commits the call.
|
||||
// - 1 retry allowed for ABORTED status
|
||||
// - first attempt receives a message and therefore does not retry even
|
||||
// though the final status is ABORTED
|
||||
static void test_retry_recv_message(grpc_end2end_test_config config) { |
||||
grpc_call* c; |
||||
grpc_call* s; |
||||
grpc_op ops[6]; |
||||
grpc_op* op; |
||||
grpc_metadata_array initial_metadata_recv; |
||||
grpc_metadata_array trailing_metadata_recv; |
||||
grpc_metadata_array request_metadata_recv; |
||||
grpc_call_details call_details; |
||||
grpc_slice request_payload_slice = grpc_slice_from_static_string("foo"); |
||||
grpc_slice response_payload_slice = grpc_slice_from_static_string("bar"); |
||||
grpc_byte_buffer* request_payload = |
||||
grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
||||
grpc_byte_buffer* response_payload = |
||||
grpc_raw_byte_buffer_create(&response_payload_slice, 1); |
||||
grpc_byte_buffer* request_payload_recv = nullptr; |
||||
grpc_byte_buffer* response_payload_recv = nullptr; |
||||
grpc_status_code status; |
||||
grpc_call_error error; |
||||
grpc_slice details; |
||||
int was_cancelled = 2; |
||||
char* peer; |
||||
|
||||
grpc_arg arg; |
||||
arg.type = GRPC_ARG_STRING; |
||||
arg.key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG); |
||||
arg.value.string = const_cast<char*>( |
||||
"{\n" |
||||
" \"methodConfig\": [ {\n" |
||||
" \"name\": [\n" |
||||
" { \"service\": \"service\", \"method\": \"method\" }\n" |
||||
" ],\n" |
||||
" \"retryPolicy\": {\n" |
||||
" \"maxAttempts\": 2,\n" |
||||
" \"initialBackoff\": \"1s\",\n" |
||||
" \"maxBackoff\": \"120s\",\n" |
||||
" \"backoffMultiplier\": 1.6,\n" |
||||
" \"retryableStatusCodes\": [ \"ABORTED\" ]\n" |
||||
" }\n" |
||||
" } ]\n" |
||||
"}"); |
||||
grpc_channel_args client_args = {1, &arg}; |
||||
grpc_end2end_test_fixture f = |
||||
begin_test(config, "retry_recv_message", &client_args, nullptr); |
||||
|
||||
cq_verifier* cqv = cq_verifier_create(f.cq); |
||||
|
||||
gpr_timespec deadline = five_seconds_from_now(); |
||||
c = grpc_channel_create_call( |
||||
f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
||||
grpc_slice_from_static_string("/service/method"), |
||||
get_host_override_slice("foo.test.google.fr:1234", config), deadline, |
||||
nullptr); |
||||
GPR_ASSERT(c); |
||||
|
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
grpc_metadata_array_init(&initial_metadata_recv); |
||||
grpc_metadata_array_init(&trailing_metadata_recv); |
||||
grpc_metadata_array_init(&request_metadata_recv); |
||||
grpc_call_details_init(&call_details); |
||||
grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = request_payload; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &response_payload_recv; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
||||
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
||||
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
||||
op->data.recv_status_on_client.status = &status; |
||||
op->data.recv_status_on_client.status_details = &details; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
error = |
||||
grpc_server_request_call(f.server, &s, &call_details, |
||||
&request_metadata_recv, f.cq, f.cq, tag(101)); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(101), true); |
||||
cq_verify(cqv); |
||||
|
||||
peer = grpc_call_get_peer(s); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "server_peer=%s", peer); |
||||
gpr_free(peer); |
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = response_payload; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
op->data.send_status_from_server.trailing_metadata_count = 0; |
||||
op->data.send_status_from_server.status = GRPC_STATUS_ABORTED; |
||||
op->data.send_status_from_server.status_details = &status_details; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
||||
op->data.recv_close_on_server.cancelled = &was_cancelled; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(103), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
CQ_EXPECT_COMPLETION(cqv, tag(103), true); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(1), true); |
||||
cq_verify(cqv); |
||||
|
||||
GPR_ASSERT(status == GRPC_STATUS_ABORTED); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz")); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method")); |
||||
validate_host_override_string("foo.test.google.fr:1234", call_details.host, |
||||
config); |
||||
GPR_ASSERT(0 == call_details.flags); |
||||
GPR_ASSERT(was_cancelled == 1); |
||||
|
||||
grpc_slice_unref(details); |
||||
grpc_metadata_array_destroy(&initial_metadata_recv); |
||||
grpc_metadata_array_destroy(&trailing_metadata_recv); |
||||
grpc_metadata_array_destroy(&request_metadata_recv); |
||||
grpc_call_details_destroy(&call_details); |
||||
grpc_byte_buffer_destroy(request_payload); |
||||
grpc_byte_buffer_destroy(response_payload); |
||||
grpc_byte_buffer_destroy(request_payload_recv); |
||||
grpc_byte_buffer_destroy(response_payload_recv); |
||||
|
||||
grpc_call_unref(c); |
||||
grpc_call_unref(s); |
||||
|
||||
cq_verifier_destroy(cqv); |
||||
|
||||
end_test(&f); |
||||
config.tear_down_data(&f); |
||||
} |
||||
|
||||
void retry_recv_message(grpc_end2end_test_config config) { |
||||
GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL); |
||||
test_retry_recv_message(config); |
||||
} |
||||
|
||||
void retry_recv_message_pre_init(void) {} |
@ -0,0 +1,318 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed 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 "test/core/end2end/end2end_tests.h" |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include <grpc/byte_buffer.h> |
||||
#include <grpc/grpc.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
#include <grpc/support/time.h> |
||||
|
||||
#include "src/core/lib/channel/channel_args.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "src/core/lib/gpr/useful.h" |
||||
#include "src/core/lib/iomgr/exec_ctx.h" |
||||
#include "src/core/lib/transport/static_metadata.h" |
||||
|
||||
#include "test/core/end2end/cq_verifier.h" |
||||
#include "test/core/end2end/tests/cancel_test_helpers.h" |
||||
|
||||
static void* tag(intptr_t t) { return (void*)t; } |
||||
|
||||
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
||||
const char* test_name, |
||||
grpc_channel_args* client_args, |
||||
grpc_channel_args* server_args) { |
||||
grpc_end2end_test_fixture f; |
||||
gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name); |
||||
f = config.create_fixture(client_args, server_args); |
||||
config.init_server(&f, server_args); |
||||
config.init_client(&f, client_args); |
||||
return f; |
||||
} |
||||
|
||||
static gpr_timespec n_seconds_from_now(int n) { |
||||
return grpc_timeout_seconds_to_deadline(n); |
||||
} |
||||
|
||||
static gpr_timespec five_seconds_from_now(void) { |
||||
return n_seconds_from_now(5); |
||||
} |
||||
|
||||
static void drain_cq(grpc_completion_queue* cq) { |
||||
grpc_event ev; |
||||
do { |
||||
ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
||||
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
||||
} |
||||
|
||||
static void shutdown_server(grpc_end2end_test_fixture* f) { |
||||
if (!f->server) return; |
||||
grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000)); |
||||
GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000), |
||||
grpc_timeout_seconds_to_deadline(5), |
||||
nullptr) |
||||
.type == GRPC_OP_COMPLETE); |
||||
grpc_server_destroy(f->server); |
||||
f->server = nullptr; |
||||
} |
||||
|
||||
static void shutdown_client(grpc_end2end_test_fixture* f) { |
||||
if (!f->client) return; |
||||
grpc_channel_destroy(f->client); |
||||
f->client = nullptr; |
||||
} |
||||
|
||||
static void end_test(grpc_end2end_test_fixture* f) { |
||||
shutdown_server(f); |
||||
shutdown_client(f); |
||||
|
||||
grpc_completion_queue_shutdown(f->cq); |
||||
drain_cq(f->cq); |
||||
grpc_completion_queue_destroy(f->cq); |
||||
grpc_completion_queue_destroy(f->shutdown_cq); |
||||
} |
||||
|
||||
// Tests that we honor server push-back delay.
|
||||
// - 2 retries allowed for ABORTED status
|
||||
// - first attempt gets ABORTED with a long delay
|
||||
// - second attempt succeeds
|
||||
static void test_retry_server_pushback_delay(grpc_end2end_test_config config) { |
||||
grpc_call* c; |
||||
grpc_call* s; |
||||
grpc_op ops[6]; |
||||
grpc_op* op; |
||||
grpc_metadata_array initial_metadata_recv; |
||||
grpc_metadata_array trailing_metadata_recv; |
||||
grpc_metadata_array request_metadata_recv; |
||||
grpc_call_details call_details; |
||||
grpc_slice request_payload_slice = grpc_slice_from_static_string("foo"); |
||||
grpc_slice response_payload_slice = grpc_slice_from_static_string("bar"); |
||||
grpc_byte_buffer* request_payload = |
||||
grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
||||
grpc_byte_buffer* response_payload = |
||||
grpc_raw_byte_buffer_create(&response_payload_slice, 1); |
||||
grpc_byte_buffer* request_payload_recv = nullptr; |
||||
grpc_byte_buffer* response_payload_recv = nullptr; |
||||
grpc_status_code status; |
||||
grpc_call_error error; |
||||
grpc_slice details; |
||||
int was_cancelled = 2; |
||||
char* peer; |
||||
|
||||
grpc_metadata pushback_md; |
||||
memset(&pushback_md, 0, sizeof(pushback_md)); |
||||
pushback_md.key = GRPC_MDSTR_GRPC_RETRY_PUSHBACK_MS; |
||||
pushback_md.value = grpc_slice_from_static_string("2000"); |
||||
|
||||
grpc_arg arg; |
||||
arg.type = GRPC_ARG_STRING; |
||||
arg.key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG); |
||||
arg.value.string = const_cast<char*>( |
||||
"{\n" |
||||
" \"methodConfig\": [ {\n" |
||||
" \"name\": [\n" |
||||
" { \"service\": \"service\", \"method\": \"method\" }\n" |
||||
" ],\n" |
||||
" \"retryPolicy\": {\n" |
||||
" \"maxAttempts\": 3,\n" |
||||
" \"initialBackoff\": \"1s\",\n" |
||||
" \"maxBackoff\": \"120s\",\n" |
||||
" \"backoffMultiplier\": 1.6,\n" |
||||
" \"retryableStatusCodes\": [ \"ABORTED\" ]\n" |
||||
" }\n" |
||||
" } ]\n" |
||||
"}"); |
||||
grpc_channel_args client_args = {1, &arg}; |
||||
grpc_end2end_test_fixture f = |
||||
begin_test(config, "retry_server_pushback_delay", &client_args, nullptr); |
||||
|
||||
cq_verifier* cqv = cq_verifier_create(f.cq); |
||||
|
||||
gpr_timespec deadline = five_seconds_from_now(); |
||||
c = grpc_channel_create_call( |
||||
f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
||||
grpc_slice_from_static_string("/service/method"), |
||||
get_host_override_slice("foo.test.google.fr:1234", config), deadline, |
||||
nullptr); |
||||
GPR_ASSERT(c); |
||||
|
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
grpc_metadata_array_init(&initial_metadata_recv); |
||||
grpc_metadata_array_init(&trailing_metadata_recv); |
||||
grpc_metadata_array_init(&request_metadata_recv); |
||||
grpc_call_details_init(&call_details); |
||||
grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = request_payload; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &response_payload_recv; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
||||
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
||||
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
||||
op->data.recv_status_on_client.status = &status; |
||||
op->data.recv_status_on_client.status_details = &details; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
error = |
||||
grpc_server_request_call(f.server, &s, &call_details, |
||||
&request_metadata_recv, f.cq, f.cq, tag(101)); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(101), true); |
||||
cq_verify(cqv); |
||||
|
||||
peer = grpc_call_get_peer(s); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "server_peer=%s", peer); |
||||
gpr_free(peer); |
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
op->data.send_status_from_server.trailing_metadata_count = 1; |
||||
op->data.send_status_from_server.trailing_metadata = &pushback_md; |
||||
op->data.send_status_from_server.status = GRPC_STATUS_ABORTED; |
||||
op->data.send_status_from_server.status_details = &status_details; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
||||
op->data.recv_close_on_server.cancelled = &was_cancelled; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
CQ_EXPECT_COMPLETION(cqv, tag(102), true); |
||||
cq_verify(cqv); |
||||
|
||||
gpr_timespec before_retry = gpr_now(GPR_CLOCK_MONOTONIC); |
||||
|
||||
grpc_call_unref(s); |
||||
grpc_metadata_array_destroy(&request_metadata_recv); |
||||
grpc_metadata_array_init(&request_metadata_recv); |
||||
grpc_call_details_destroy(&call_details); |
||||
grpc_call_details_init(&call_details); |
||||
|
||||
error = |
||||
grpc_server_request_call(f.server, &s, &call_details, |
||||
&request_metadata_recv, f.cq, f.cq, tag(201)); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(201), true); |
||||
cq_verify(cqv); |
||||
|
||||
gpr_timespec after_retry = gpr_now(GPR_CLOCK_MONOTONIC); |
||||
gpr_timespec retry_delay = gpr_time_sub(after_retry, before_retry); |
||||
// Configured back-off was 1 second, server push-back said 2 seconds.
|
||||
// To avoid flakiness, we allow some fudge factor here.
|
||||
gpr_log(GPR_INFO, "retry delay was {.tv_sec=%" PRId64 ", .tv_nsec=%d}", |
||||
retry_delay.tv_sec, retry_delay.tv_nsec); |
||||
GPR_ASSERT(retry_delay.tv_sec >= 1); |
||||
if (retry_delay.tv_sec == 1) { |
||||
GPR_ASSERT(retry_delay.tv_nsec >= 900000000); |
||||
} |
||||
|
||||
peer = grpc_call_get_peer(s); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "server_peer=%s", peer); |
||||
gpr_free(peer); |
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
op->data.send_status_from_server.trailing_metadata_count = 0; |
||||
op->data.send_status_from_server.status = GRPC_STATUS_OK; |
||||
op->data.send_status_from_server.status_details = &status_details; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
||||
op->data.recv_close_on_server.cancelled = &was_cancelled; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(202), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
CQ_EXPECT_COMPLETION(cqv, tag(202), true); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(1), true); |
||||
cq_verify(cqv); |
||||
|
||||
GPR_ASSERT(status == GRPC_STATUS_OK); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz")); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method")); |
||||
validate_host_override_string("foo.test.google.fr:1234", call_details.host, |
||||
config); |
||||
GPR_ASSERT(0 == call_details.flags); |
||||
GPR_ASSERT(was_cancelled == 0); |
||||
|
||||
grpc_slice_unref(details); |
||||
grpc_metadata_array_destroy(&initial_metadata_recv); |
||||
grpc_metadata_array_destroy(&trailing_metadata_recv); |
||||
grpc_metadata_array_destroy(&request_metadata_recv); |
||||
grpc_call_details_destroy(&call_details); |
||||
grpc_byte_buffer_destroy(request_payload); |
||||
grpc_byte_buffer_destroy(response_payload); |
||||
grpc_byte_buffer_destroy(request_payload_recv); |
||||
grpc_byte_buffer_destroy(response_payload_recv); |
||||
|
||||
grpc_call_unref(c); |
||||
grpc_call_unref(s); |
||||
|
||||
cq_verifier_destroy(cqv); |
||||
|
||||
end_test(&f); |
||||
config.tear_down_data(&f); |
||||
} |
||||
|
||||
void retry_server_pushback_delay(grpc_end2end_test_config config) { |
||||
GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL); |
||||
test_retry_server_pushback_delay(config); |
||||
} |
||||
|
||||
void retry_server_pushback_delay_pre_init(void) {} |
@ -0,0 +1,306 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed 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 "test/core/end2end/end2end_tests.h" |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include <grpc/byte_buffer.h> |
||||
#include <grpc/grpc.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
#include <grpc/support/time.h> |
||||
|
||||
#include "src/core/lib/channel/channel_args.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "src/core/lib/gpr/useful.h" |
||||
#include "src/core/lib/iomgr/exec_ctx.h" |
||||
#include "src/core/lib/transport/static_metadata.h" |
||||
|
||||
#include "test/core/end2end/cq_verifier.h" |
||||
#include "test/core/end2end/tests/cancel_test_helpers.h" |
||||
|
||||
static void* tag(intptr_t t) { return (void*)t; } |
||||
|
||||
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
||||
const char* test_name, |
||||
grpc_channel_args* client_args, |
||||
grpc_channel_args* server_args) { |
||||
grpc_end2end_test_fixture f; |
||||
gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name); |
||||
f = config.create_fixture(client_args, server_args); |
||||
config.init_server(&f, server_args); |
||||
config.init_client(&f, client_args); |
||||
return f; |
||||
} |
||||
|
||||
static gpr_timespec n_seconds_from_now(int n) { |
||||
return grpc_timeout_seconds_to_deadline(n); |
||||
} |
||||
|
||||
static gpr_timespec five_seconds_from_now(void) { |
||||
return n_seconds_from_now(5); |
||||
} |
||||
|
||||
static void drain_cq(grpc_completion_queue* cq) { |
||||
grpc_event ev; |
||||
do { |
||||
ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
||||
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
||||
} |
||||
|
||||
static void shutdown_server(grpc_end2end_test_fixture* f) { |
||||
if (!f->server) return; |
||||
grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000)); |
||||
GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000), |
||||
grpc_timeout_seconds_to_deadline(5), |
||||
nullptr) |
||||
.type == GRPC_OP_COMPLETE); |
||||
grpc_server_destroy(f->server); |
||||
f->server = nullptr; |
||||
} |
||||
|
||||
static void shutdown_client(grpc_end2end_test_fixture* f) { |
||||
if (!f->client) return; |
||||
grpc_channel_destroy(f->client); |
||||
f->client = nullptr; |
||||
} |
||||
|
||||
static void end_test(grpc_end2end_test_fixture* f) { |
||||
shutdown_server(f); |
||||
shutdown_client(f); |
||||
|
||||
grpc_completion_queue_shutdown(f->cq); |
||||
drain_cq(f->cq); |
||||
grpc_completion_queue_destroy(f->cq); |
||||
grpc_completion_queue_destroy(f->shutdown_cq); |
||||
} |
||||
|
||||
// Tests that we don't retry when disabled by server push-back.
|
||||
// - 2 retries allowed for ABORTED status
|
||||
// - first attempt gets ABORTED
|
||||
// - second attempt gets ABORTED but server push back disables retrying
|
||||
static void test_retry_server_pushback_disabled( |
||||
grpc_end2end_test_config config) { |
||||
grpc_call* c; |
||||
grpc_call* s; |
||||
grpc_op ops[6]; |
||||
grpc_op* op; |
||||
grpc_metadata_array initial_metadata_recv; |
||||
grpc_metadata_array trailing_metadata_recv; |
||||
grpc_metadata_array request_metadata_recv; |
||||
grpc_call_details call_details; |
||||
grpc_slice request_payload_slice = grpc_slice_from_static_string("foo"); |
||||
grpc_slice response_payload_slice = grpc_slice_from_static_string("bar"); |
||||
grpc_byte_buffer* request_payload = |
||||
grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
||||
grpc_byte_buffer* response_payload = |
||||
grpc_raw_byte_buffer_create(&response_payload_slice, 1); |
||||
grpc_byte_buffer* request_payload_recv = nullptr; |
||||
grpc_byte_buffer* response_payload_recv = nullptr; |
||||
grpc_status_code status; |
||||
grpc_call_error error; |
||||
grpc_slice details; |
||||
int was_cancelled = 2; |
||||
char* peer; |
||||
|
||||
grpc_metadata pushback_md; |
||||
memset(&pushback_md, 0, sizeof(pushback_md)); |
||||
pushback_md.key = GRPC_MDSTR_GRPC_RETRY_PUSHBACK_MS; |
||||
pushback_md.value = grpc_slice_from_static_string("-1"); |
||||
|
||||
grpc_arg arg; |
||||
arg.type = GRPC_ARG_STRING; |
||||
arg.key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG); |
||||
arg.value.string = const_cast<char*>( |
||||
"{\n" |
||||
" \"methodConfig\": [ {\n" |
||||
" \"name\": [\n" |
||||
" { \"service\": \"service\", \"method\": \"method\" }\n" |
||||
" ],\n" |
||||
" \"retryPolicy\": {\n" |
||||
" \"maxAttempts\": 3,\n" |
||||
" \"initialBackoff\": \"1s\",\n" |
||||
" \"maxBackoff\": \"120s\",\n" |
||||
" \"backoffMultiplier\": 1.6,\n" |
||||
" \"retryableStatusCodes\": [ \"ABORTED\" ]\n" |
||||
" }\n" |
||||
" } ]\n" |
||||
"}"); |
||||
grpc_channel_args client_args = {1, &arg}; |
||||
grpc_end2end_test_fixture f = begin_test( |
||||
config, "retry_server_pushback_disabled", &client_args, nullptr); |
||||
|
||||
cq_verifier* cqv = cq_verifier_create(f.cq); |
||||
|
||||
gpr_timespec deadline = five_seconds_from_now(); |
||||
c = grpc_channel_create_call( |
||||
f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
||||
grpc_slice_from_static_string("/service/method"), |
||||
get_host_override_slice("foo.test.google.fr:1234", config), deadline, |
||||
nullptr); |
||||
GPR_ASSERT(c); |
||||
|
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
grpc_metadata_array_init(&initial_metadata_recv); |
||||
grpc_metadata_array_init(&trailing_metadata_recv); |
||||
grpc_metadata_array_init(&request_metadata_recv); |
||||
grpc_call_details_init(&call_details); |
||||
grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = request_payload; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &response_payload_recv; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
||||
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
||||
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
||||
op->data.recv_status_on_client.status = &status; |
||||
op->data.recv_status_on_client.status_details = &details; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
error = |
||||
grpc_server_request_call(f.server, &s, &call_details, |
||||
&request_metadata_recv, f.cq, f.cq, tag(101)); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(101), true); |
||||
cq_verify(cqv); |
||||
|
||||
peer = grpc_call_get_peer(s); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "server_peer=%s", peer); |
||||
gpr_free(peer); |
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
op->data.send_status_from_server.trailing_metadata_count = 0; |
||||
op->data.send_status_from_server.status = GRPC_STATUS_ABORTED; |
||||
op->data.send_status_from_server.status_details = &status_details; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
||||
op->data.recv_close_on_server.cancelled = &was_cancelled; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
CQ_EXPECT_COMPLETION(cqv, tag(102), true); |
||||
cq_verify(cqv); |
||||
|
||||
grpc_call_unref(s); |
||||
grpc_metadata_array_destroy(&request_metadata_recv); |
||||
grpc_metadata_array_init(&request_metadata_recv); |
||||
grpc_call_details_destroy(&call_details); |
||||
grpc_call_details_init(&call_details); |
||||
|
||||
error = |
||||
grpc_server_request_call(f.server, &s, &call_details, |
||||
&request_metadata_recv, f.cq, f.cq, tag(201)); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(201), true); |
||||
cq_verify(cqv); |
||||
|
||||
peer = grpc_call_get_peer(s); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "server_peer=%s", peer); |
||||
gpr_free(peer); |
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
op->data.send_status_from_server.trailing_metadata_count = 1; |
||||
op->data.send_status_from_server.trailing_metadata = &pushback_md; |
||||
op->data.send_status_from_server.status = GRPC_STATUS_ABORTED; |
||||
op->data.send_status_from_server.status_details = &status_details; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
||||
op->data.recv_close_on_server.cancelled = &was_cancelled; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(202), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
CQ_EXPECT_COMPLETION(cqv, tag(202), true); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(1), true); |
||||
cq_verify(cqv); |
||||
|
||||
GPR_ASSERT(status == GRPC_STATUS_ABORTED); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz")); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method")); |
||||
validate_host_override_string("foo.test.google.fr:1234", call_details.host, |
||||
config); |
||||
GPR_ASSERT(0 == call_details.flags); |
||||
GPR_ASSERT(was_cancelled == 1); |
||||
|
||||
grpc_slice_unref(details); |
||||
grpc_metadata_array_destroy(&initial_metadata_recv); |
||||
grpc_metadata_array_destroy(&trailing_metadata_recv); |
||||
grpc_metadata_array_destroy(&request_metadata_recv); |
||||
grpc_call_details_destroy(&call_details); |
||||
grpc_byte_buffer_destroy(request_payload); |
||||
grpc_byte_buffer_destroy(response_payload); |
||||
grpc_byte_buffer_destroy(request_payload_recv); |
||||
grpc_byte_buffer_destroy(response_payload_recv); |
||||
|
||||
grpc_call_unref(c); |
||||
grpc_call_unref(s); |
||||
|
||||
cq_verifier_destroy(cqv); |
||||
|
||||
end_test(&f); |
||||
config.tear_down_data(&f); |
||||
} |
||||
|
||||
void retry_server_pushback_disabled(grpc_end2end_test_config config) { |
||||
GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL); |
||||
test_retry_server_pushback_disabled(config); |
||||
} |
||||
|
||||
void retry_server_pushback_disabled_pre_init(void) {} |
@ -0,0 +1,424 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed 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 "test/core/end2end/end2end_tests.h" |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include <grpc/byte_buffer.h> |
||||
#include <grpc/grpc.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
#include <grpc/support/time.h> |
||||
|
||||
#include "src/core/lib/channel/channel_args.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "src/core/lib/gpr/useful.h" |
||||
#include "src/core/lib/iomgr/exec_ctx.h" |
||||
#include "src/core/lib/transport/static_metadata.h" |
||||
|
||||
#include "test/core/end2end/cq_verifier.h" |
||||
#include "test/core/end2end/tests/cancel_test_helpers.h" |
||||
|
||||
static void* tag(intptr_t t) { return (void*)t; } |
||||
|
||||
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
||||
const char* test_name, |
||||
grpc_channel_args* client_args, |
||||
grpc_channel_args* server_args) { |
||||
grpc_end2end_test_fixture f; |
||||
gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name); |
||||
f = config.create_fixture(client_args, server_args); |
||||
config.init_server(&f, server_args); |
||||
config.init_client(&f, client_args); |
||||
return f; |
||||
} |
||||
|
||||
static gpr_timespec n_seconds_from_now(int n) { |
||||
return grpc_timeout_seconds_to_deadline(n); |
||||
} |
||||
|
||||
static gpr_timespec five_seconds_from_now(void) { |
||||
return n_seconds_from_now(5); |
||||
} |
||||
|
||||
static void drain_cq(grpc_completion_queue* cq) { |
||||
grpc_event ev; |
||||
do { |
||||
ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
||||
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
||||
} |
||||
|
||||
static void shutdown_server(grpc_end2end_test_fixture* f) { |
||||
if (!f->server) return; |
||||
grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000)); |
||||
GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000), |
||||
grpc_timeout_seconds_to_deadline(5), |
||||
nullptr) |
||||
.type == GRPC_OP_COMPLETE); |
||||
grpc_server_destroy(f->server); |
||||
f->server = nullptr; |
||||
} |
||||
|
||||
static void shutdown_client(grpc_end2end_test_fixture* f) { |
||||
if (!f->client) return; |
||||
grpc_channel_destroy(f->client); |
||||
f->client = nullptr; |
||||
} |
||||
|
||||
static void end_test(grpc_end2end_test_fixture* f) { |
||||
shutdown_server(f); |
||||
shutdown_client(f); |
||||
|
||||
grpc_completion_queue_shutdown(f->cq); |
||||
drain_cq(f->cq); |
||||
grpc_completion_queue_destroy(f->cq); |
||||
grpc_completion_queue_destroy(f->shutdown_cq); |
||||
} |
||||
|
||||
// Tests retrying a streaming RPC. This is the same as
|
||||
// the basic retry test, except that the client sends two messages on the
|
||||
// call before the initial attempt fails.
|
||||
// FIXME: We should also test the case where the retry is committed after
|
||||
// replaying 1 of 2 previously-completed send_message ops. However,
|
||||
// there's no way to trigger that from an end2end test, because the
|
||||
// replayed ops happen under the hood -- they are not surfaced to the
|
||||
// C-core API, and therefore we have no way to inject the commit at the
|
||||
// right point.
|
||||
static void test_retry_streaming(grpc_end2end_test_config config) { |
||||
grpc_call* c; |
||||
grpc_call* s; |
||||
grpc_op ops[6]; |
||||
grpc_op* op; |
||||
grpc_metadata_array initial_metadata_recv; |
||||
grpc_metadata_array trailing_metadata_recv; |
||||
grpc_metadata_array request_metadata_recv; |
||||
grpc_call_details call_details; |
||||
grpc_slice request_payload_slice = grpc_slice_from_static_string("foo"); |
||||
grpc_slice request2_payload_slice = grpc_slice_from_static_string("bar"); |
||||
grpc_slice request3_payload_slice = grpc_slice_from_static_string("baz"); |
||||
grpc_slice response_payload_slice = grpc_slice_from_static_string("quux"); |
||||
grpc_byte_buffer* request_payload = |
||||
grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
||||
grpc_byte_buffer* request2_payload = |
||||
grpc_raw_byte_buffer_create(&request2_payload_slice, 1); |
||||
grpc_byte_buffer* request3_payload = |
||||
grpc_raw_byte_buffer_create(&request3_payload_slice, 1); |
||||
grpc_byte_buffer* response_payload = |
||||
grpc_raw_byte_buffer_create(&response_payload_slice, 1); |
||||
grpc_byte_buffer* request_payload_recv = nullptr; |
||||
grpc_byte_buffer* request2_payload_recv = nullptr; |
||||
grpc_byte_buffer* request3_payload_recv = nullptr; |
||||
grpc_byte_buffer* response_payload_recv = nullptr; |
||||
grpc_status_code status; |
||||
grpc_call_error error; |
||||
grpc_slice details; |
||||
int was_cancelled = 2; |
||||
char* peer; |
||||
|
||||
grpc_arg arg; |
||||
arg.type = GRPC_ARG_STRING; |
||||
arg.key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG); |
||||
arg.value.string = const_cast<char*>( |
||||
"{\n" |
||||
" \"methodConfig\": [ {\n" |
||||
" \"name\": [\n" |
||||
" { \"service\": \"service\", \"method\": \"method\" }\n" |
||||
" ],\n" |
||||
" \"retryPolicy\": {\n" |
||||
" \"maxAttempts\": 3,\n" |
||||
" \"initialBackoff\": \"1s\",\n" |
||||
" \"maxBackoff\": \"120s\",\n" |
||||
" \"backoffMultiplier\": 1.6,\n" |
||||
" \"retryableStatusCodes\": [ \"ABORTED\" ]\n" |
||||
" }\n" |
||||
" } ]\n" |
||||
"}"); |
||||
grpc_channel_args client_args = {1, &arg}; |
||||
grpc_end2end_test_fixture f = |
||||
begin_test(config, "retry_streaming", &client_args, nullptr); |
||||
|
||||
cq_verifier* cqv = cq_verifier_create(f.cq); |
||||
|
||||
gpr_timespec deadline = five_seconds_from_now(); |
||||
c = grpc_channel_create_call( |
||||
f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
||||
grpc_slice_from_static_string("/service/method"), |
||||
get_host_override_slice("foo.test.google.fr:1234", config), deadline, |
||||
nullptr); |
||||
GPR_ASSERT(c); |
||||
|
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
grpc_metadata_array_init(&initial_metadata_recv); |
||||
grpc_metadata_array_init(&trailing_metadata_recv); |
||||
grpc_metadata_array_init(&request_metadata_recv); |
||||
grpc_call_details_init(&call_details); |
||||
grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
||||
|
||||
// Client starts a batch for receiving initial metadata, a message,
|
||||
// and trailing metadata.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
||||
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &response_payload_recv; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
||||
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
||||
op->data.recv_status_on_client.status = &status; |
||||
op->data.recv_status_on_client.status_details = &details; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
// Client sends initial metadata and a message.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = request_payload; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(2), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(2), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Server gets a call with received initial metadata.
|
||||
error = |
||||
grpc_server_request_call(f.server, &s, &call_details, |
||||
&request_metadata_recv, f.cq, f.cq, tag(101)); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(101), true); |
||||
cq_verify(cqv); |
||||
|
||||
peer = grpc_call_get_peer(s); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "server_peer=%s", peer); |
||||
gpr_free(peer); |
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
// Server receives a message.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &request_payload_recv; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(102), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Client sends a second message.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = request2_payload; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(3), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(3), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Server receives the second message.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &request2_payload_recv; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(103), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(103), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Server sends both initial and trailing metadata.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
||||
op->data.recv_close_on_server.cancelled = &was_cancelled; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
op->data.send_status_from_server.trailing_metadata_count = 0; |
||||
op->data.send_status_from_server.status = GRPC_STATUS_ABORTED; |
||||
op->data.send_status_from_server.status_details = &status_details; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(104), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(104), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Clean up from first attempt.
|
||||
grpc_call_unref(s); |
||||
grpc_metadata_array_destroy(&request_metadata_recv); |
||||
grpc_metadata_array_init(&request_metadata_recv); |
||||
grpc_call_details_destroy(&call_details); |
||||
grpc_call_details_init(&call_details); |
||||
GPR_ASSERT(byte_buffer_eq_slice(request_payload_recv, request_payload_slice)); |
||||
grpc_byte_buffer_destroy(request_payload_recv); |
||||
request_payload_recv = nullptr; |
||||
GPR_ASSERT( |
||||
byte_buffer_eq_slice(request2_payload_recv, request2_payload_slice)); |
||||
grpc_byte_buffer_destroy(request2_payload_recv); |
||||
request2_payload_recv = nullptr; |
||||
|
||||
// Server gets a second call (the retry).
|
||||
error = |
||||
grpc_server_request_call(f.server, &s, &call_details, |
||||
&request_metadata_recv, f.cq, f.cq, tag(201)); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(201), true); |
||||
cq_verify(cqv); |
||||
|
||||
peer = grpc_call_get_peer(s); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "server_peer=%s", peer); |
||||
gpr_free(peer); |
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
// Server receives a message.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &request_payload_recv; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(202), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(202), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Server receives a second message.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &request2_payload_recv; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(203), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(203), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Client sends a third message and a close.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = request3_payload; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(4), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(4), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Server receives a third message.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &request3_payload_recv; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(204), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(204), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Server receives a close and sends initial metadata, a message, and
|
||||
// trailing metadata.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
||||
op->data.recv_close_on_server.cancelled = &was_cancelled; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = response_payload; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
op->data.send_status_from_server.trailing_metadata_count = 0; |
||||
// Returning a retriable code, but because we are also sending a
|
||||
// message, the client will commit instead of retrying again.
|
||||
op->data.send_status_from_server.status = GRPC_STATUS_ABORTED; |
||||
op->data.send_status_from_server.status_details = &status_details; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(205), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(205), true); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(1), true); |
||||
cq_verify(cqv); |
||||
|
||||
GPR_ASSERT(status == GRPC_STATUS_ABORTED); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz")); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method")); |
||||
validate_host_override_string("foo.test.google.fr:1234", call_details.host, |
||||
config); |
||||
GPR_ASSERT(0 == call_details.flags); |
||||
GPR_ASSERT(was_cancelled == 1); |
||||
|
||||
grpc_slice_unref(details); |
||||
grpc_metadata_array_destroy(&initial_metadata_recv); |
||||
grpc_metadata_array_destroy(&trailing_metadata_recv); |
||||
grpc_metadata_array_destroy(&request_metadata_recv); |
||||
grpc_call_details_destroy(&call_details); |
||||
grpc_byte_buffer_destroy(request_payload); |
||||
grpc_byte_buffer_destroy(request2_payload); |
||||
grpc_byte_buffer_destroy(request3_payload); |
||||
grpc_byte_buffer_destroy(response_payload); |
||||
GPR_ASSERT(byte_buffer_eq_slice(request_payload_recv, request_payload_slice)); |
||||
grpc_byte_buffer_destroy(request_payload_recv); |
||||
GPR_ASSERT( |
||||
byte_buffer_eq_slice(request2_payload_recv, request2_payload_slice)); |
||||
grpc_byte_buffer_destroy(request2_payload_recv); |
||||
GPR_ASSERT( |
||||
byte_buffer_eq_slice(request3_payload_recv, request3_payload_slice)); |
||||
grpc_byte_buffer_destroy(request3_payload_recv); |
||||
grpc_byte_buffer_destroy(response_payload_recv); |
||||
|
||||
grpc_call_unref(c); |
||||
grpc_call_unref(s); |
||||
|
||||
cq_verifier_destroy(cqv); |
||||
|
||||
end_test(&f); |
||||
config.tear_down_data(&f); |
||||
} |
||||
|
||||
void retry_streaming(grpc_end2end_test_config config) { |
||||
GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL); |
||||
test_retry_streaming(config); |
||||
} |
||||
|
||||
void retry_streaming_pre_init(void) {} |
@ -0,0 +1,354 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed 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 "test/core/end2end/end2end_tests.h" |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include <grpc/byte_buffer.h> |
||||
#include <grpc/grpc.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
#include <grpc/support/time.h> |
||||
|
||||
#include "src/core/lib/channel/channel_args.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "src/core/lib/gpr/useful.h" |
||||
#include "src/core/lib/iomgr/exec_ctx.h" |
||||
#include "src/core/lib/transport/static_metadata.h" |
||||
|
||||
#include "test/core/end2end/cq_verifier.h" |
||||
#include "test/core/end2end/tests/cancel_test_helpers.h" |
||||
|
||||
static void* tag(intptr_t t) { return (void*)t; } |
||||
|
||||
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
||||
const char* test_name, |
||||
grpc_channel_args* client_args, |
||||
grpc_channel_args* server_args) { |
||||
grpc_end2end_test_fixture f; |
||||
gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name); |
||||
f = config.create_fixture(client_args, server_args); |
||||
config.init_server(&f, server_args); |
||||
config.init_client(&f, client_args); |
||||
return f; |
||||
} |
||||
|
||||
static gpr_timespec n_seconds_from_now(int n) { |
||||
return grpc_timeout_seconds_to_deadline(n); |
||||
} |
||||
|
||||
static gpr_timespec five_seconds_from_now(void) { |
||||
return n_seconds_from_now(5); |
||||
} |
||||
|
||||
static void drain_cq(grpc_completion_queue* cq) { |
||||
grpc_event ev; |
||||
do { |
||||
ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
||||
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
||||
} |
||||
|
||||
static void shutdown_server(grpc_end2end_test_fixture* f) { |
||||
if (!f->server) return; |
||||
grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000)); |
||||
GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000), |
||||
grpc_timeout_seconds_to_deadline(5), |
||||
nullptr) |
||||
.type == GRPC_OP_COMPLETE); |
||||
grpc_server_destroy(f->server); |
||||
f->server = nullptr; |
||||
} |
||||
|
||||
static void shutdown_client(grpc_end2end_test_fixture* f) { |
||||
if (!f->client) return; |
||||
grpc_channel_destroy(f->client); |
||||
f->client = nullptr; |
||||
} |
||||
|
||||
static void end_test(grpc_end2end_test_fixture* f) { |
||||
shutdown_server(f); |
||||
shutdown_client(f); |
||||
|
||||
grpc_completion_queue_shutdown(f->cq); |
||||
drain_cq(f->cq); |
||||
grpc_completion_queue_destroy(f->cq); |
||||
grpc_completion_queue_destroy(f->shutdown_cq); |
||||
} |
||||
|
||||
// Tests that we can continue to send/recv messages on a streaming call
|
||||
// after retries are committed.
|
||||
static void test_retry_streaming_after_commit(grpc_end2end_test_config config) { |
||||
grpc_call* c; |
||||
grpc_call* s; |
||||
grpc_op ops[6]; |
||||
grpc_op* op; |
||||
grpc_metadata_array initial_metadata_recv; |
||||
grpc_metadata_array trailing_metadata_recv; |
||||
grpc_metadata_array request_metadata_recv; |
||||
grpc_call_details call_details; |
||||
grpc_slice request_payload_slice = grpc_slice_from_static_string("foo"); |
||||
grpc_slice request2_payload_slice = grpc_slice_from_static_string("bar"); |
||||
grpc_slice response_payload_slice = grpc_slice_from_static_string("baz"); |
||||
grpc_slice response2_payload_slice = grpc_slice_from_static_string("quux"); |
||||
grpc_byte_buffer* request_payload = |
||||
grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
||||
grpc_byte_buffer* request2_payload = |
||||
grpc_raw_byte_buffer_create(&request2_payload_slice, 1); |
||||
grpc_byte_buffer* response_payload = |
||||
grpc_raw_byte_buffer_create(&response_payload_slice, 1); |
||||
grpc_byte_buffer* response2_payload = |
||||
grpc_raw_byte_buffer_create(&response2_payload_slice, 1); |
||||
grpc_byte_buffer* request_payload_recv = nullptr; |
||||
grpc_byte_buffer* request2_payload_recv = nullptr; |
||||
grpc_byte_buffer* response_payload_recv = nullptr; |
||||
grpc_byte_buffer* response2_payload_recv = nullptr; |
||||
grpc_status_code status; |
||||
grpc_call_error error; |
||||
grpc_slice details; |
||||
int was_cancelled = 2; |
||||
char* peer; |
||||
|
||||
grpc_arg arg; |
||||
arg.type = GRPC_ARG_STRING; |
||||
arg.key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG); |
||||
arg.value.string = const_cast<char*>( |
||||
"{\n" |
||||
" \"methodConfig\": [ {\n" |
||||
" \"name\": [\n" |
||||
" { \"service\": \"service\", \"method\": \"method\" }\n" |
||||
" ],\n" |
||||
" \"retryPolicy\": {\n" |
||||
" \"maxAttempts\": 3,\n" |
||||
" \"initialBackoff\": \"1s\",\n" |
||||
" \"maxBackoff\": \"120s\",\n" |
||||
" \"backoffMultiplier\": 1.6,\n" |
||||
" \"retryableStatusCodes\": [ \"ABORTED\" ]\n" |
||||
" }\n" |
||||
" } ]\n" |
||||
"}"); |
||||
grpc_channel_args client_args = {1, &arg}; |
||||
grpc_end2end_test_fixture f = |
||||
begin_test(config, "retry_streaming_after_commit", &client_args, nullptr); |
||||
|
||||
cq_verifier* cqv = cq_verifier_create(f.cq); |
||||
|
||||
gpr_timespec deadline = five_seconds_from_now(); |
||||
c = grpc_channel_create_call( |
||||
f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
||||
grpc_slice_from_static_string("/service/method"), |
||||
get_host_override_slice("foo.test.google.fr:1234", config), deadline, |
||||
nullptr); |
||||
GPR_ASSERT(c); |
||||
|
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
grpc_metadata_array_init(&initial_metadata_recv); |
||||
grpc_metadata_array_init(&trailing_metadata_recv); |
||||
grpc_metadata_array_init(&request_metadata_recv); |
||||
grpc_call_details_init(&call_details); |
||||
grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
||||
|
||||
// Client starts a batch for receiving initial metadata and a message.
|
||||
// This will commit retries.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
||||
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &response_payload_recv; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(2), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
// Client sends initial metadata and a message.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = request_payload; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(3), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(3), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Server gets a call with received initial metadata.
|
||||
error = |
||||
grpc_server_request_call(f.server, &s, &call_details, |
||||
&request_metadata_recv, f.cq, f.cq, tag(101)); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(101), true); |
||||
cq_verify(cqv); |
||||
|
||||
peer = grpc_call_get_peer(s); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "server_peer=%s", peer); |
||||
gpr_free(peer); |
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
// Server receives a message.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &request_payload_recv; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(102), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Server sends initial metadata and a message.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = response_payload; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(103), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(103), true); |
||||
// Client receives initial metadata and a message.
|
||||
CQ_EXPECT_COMPLETION(cqv, tag(2), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Client sends a second message and a close.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = request2_payload; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(4), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(4), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Server receives a second message.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &request2_payload_recv; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(104), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(104), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Server receives a close, sends a second message, and sends status.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
||||
op->data.recv_close_on_server.cancelled = &was_cancelled; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = response2_payload; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
op->data.send_status_from_server.trailing_metadata_count = 0; |
||||
// Returning a retriable code, but because retries are already
|
||||
// committed, the client will not retry.
|
||||
op->data.send_status_from_server.status = GRPC_STATUS_ABORTED; |
||||
op->data.send_status_from_server.status_details = &status_details; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(105), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(105), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Client receives a second message.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &response2_payload_recv; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(5), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(5), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Client receives status.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
||||
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
||||
op->data.recv_status_on_client.status = &status; |
||||
op->data.recv_status_on_client.status_details = &details; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(1), true); |
||||
cq_verify(cqv); |
||||
|
||||
GPR_ASSERT(status == GRPC_STATUS_ABORTED); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz")); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method")); |
||||
validate_host_override_string("foo.test.google.fr:1234", call_details.host, |
||||
config); |
||||
GPR_ASSERT(0 == call_details.flags); |
||||
GPR_ASSERT(was_cancelled == 1); |
||||
|
||||
grpc_slice_unref(details); |
||||
grpc_metadata_array_destroy(&initial_metadata_recv); |
||||
grpc_metadata_array_destroy(&trailing_metadata_recv); |
||||
grpc_metadata_array_destroy(&request_metadata_recv); |
||||
grpc_call_details_destroy(&call_details); |
||||
grpc_byte_buffer_destroy(request_payload); |
||||
grpc_byte_buffer_destroy(request2_payload); |
||||
grpc_byte_buffer_destroy(response_payload); |
||||
grpc_byte_buffer_destroy(response2_payload); |
||||
GPR_ASSERT(byte_buffer_eq_slice(request_payload_recv, request_payload_slice)); |
||||
grpc_byte_buffer_destroy(request_payload_recv); |
||||
GPR_ASSERT( |
||||
byte_buffer_eq_slice(request2_payload_recv, request2_payload_slice)); |
||||
grpc_byte_buffer_destroy(request2_payload_recv); |
||||
GPR_ASSERT( |
||||
byte_buffer_eq_slice(response_payload_recv, response_payload_slice)); |
||||
grpc_byte_buffer_destroy(response_payload_recv); |
||||
GPR_ASSERT( |
||||
byte_buffer_eq_slice(response2_payload_recv, response2_payload_slice)); |
||||
grpc_byte_buffer_destroy(response2_payload_recv); |
||||
grpc_call_unref(c); |
||||
grpc_call_unref(s); |
||||
|
||||
cq_verifier_destroy(cqv); |
||||
|
||||
end_test(&f); |
||||
config.tear_down_data(&f); |
||||
} |
||||
|
||||
void retry_streaming_after_commit(grpc_end2end_test_config config) { |
||||
GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL); |
||||
test_retry_streaming_after_commit(config); |
||||
} |
||||
|
||||
void retry_streaming_after_commit_pre_init(void) {} |
@ -0,0 +1,400 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed 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 "test/core/end2end/end2end_tests.h" |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include <grpc/byte_buffer.h> |
||||
#include <grpc/grpc.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
#include <grpc/support/time.h> |
||||
|
||||
#include "src/core/lib/channel/channel_args.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "src/core/lib/gpr/useful.h" |
||||
#include "src/core/lib/iomgr/exec_ctx.h" |
||||
#include "src/core/lib/transport/static_metadata.h" |
||||
|
||||
#include "test/core/end2end/cq_verifier.h" |
||||
#include "test/core/end2end/tests/cancel_test_helpers.h" |
||||
|
||||
static void* tag(intptr_t t) { return (void*)t; } |
||||
|
||||
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
||||
const char* test_name, |
||||
grpc_channel_args* client_args, |
||||
grpc_channel_args* server_args) { |
||||
grpc_end2end_test_fixture f; |
||||
gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name); |
||||
f = config.create_fixture(client_args, server_args); |
||||
config.init_server(&f, server_args); |
||||
config.init_client(&f, client_args); |
||||
return f; |
||||
} |
||||
|
||||
static gpr_timespec n_seconds_from_now(int n) { |
||||
return grpc_timeout_seconds_to_deadline(n); |
||||
} |
||||
|
||||
static gpr_timespec five_seconds_from_now(void) { |
||||
return n_seconds_from_now(5); |
||||
} |
||||
|
||||
static void drain_cq(grpc_completion_queue* cq) { |
||||
grpc_event ev; |
||||
do { |
||||
ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
||||
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
||||
} |
||||
|
||||
static void shutdown_server(grpc_end2end_test_fixture* f) { |
||||
if (!f->server) return; |
||||
grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000)); |
||||
GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000), |
||||
grpc_timeout_seconds_to_deadline(5), |
||||
nullptr) |
||||
.type == GRPC_OP_COMPLETE); |
||||
grpc_server_destroy(f->server); |
||||
f->server = nullptr; |
||||
} |
||||
|
||||
static void shutdown_client(grpc_end2end_test_fixture* f) { |
||||
if (!f->client) return; |
||||
grpc_channel_destroy(f->client); |
||||
f->client = nullptr; |
||||
} |
||||
|
||||
static void end_test(grpc_end2end_test_fixture* f) { |
||||
shutdown_server(f); |
||||
shutdown_client(f); |
||||
|
||||
grpc_completion_queue_shutdown(f->cq); |
||||
drain_cq(f->cq); |
||||
grpc_completion_queue_destroy(f->cq); |
||||
grpc_completion_queue_destroy(f->shutdown_cq); |
||||
} |
||||
|
||||
// Tests that we correctly clean up if the second attempt finishes
|
||||
// before we have finished replaying all of the send ops.
|
||||
static void test_retry_streaming_succeeds_before_replay_finished( |
||||
grpc_end2end_test_config config) { |
||||
grpc_call* c; |
||||
grpc_call* s; |
||||
grpc_op ops[6]; |
||||
grpc_op* op; |
||||
grpc_metadata_array initial_metadata_recv; |
||||
grpc_metadata_array trailing_metadata_recv; |
||||
grpc_metadata_array request_metadata_recv; |
||||
grpc_call_details call_details; |
||||
grpc_slice request_payload_slice = grpc_slice_from_static_string("foo"); |
||||
grpc_slice request2_payload_slice = grpc_slice_from_static_string("bar"); |
||||
grpc_slice request3_payload_slice = grpc_slice_from_static_string("baz"); |
||||
grpc_slice response_payload_slice = grpc_slice_from_static_string("quux"); |
||||
grpc_byte_buffer* request_payload = |
||||
grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
||||
grpc_byte_buffer* request2_payload = |
||||
grpc_raw_byte_buffer_create(&request2_payload_slice, 1); |
||||
grpc_byte_buffer* request3_payload = |
||||
grpc_raw_byte_buffer_create(&request3_payload_slice, 1); |
||||
grpc_byte_buffer* response_payload = |
||||
grpc_raw_byte_buffer_create(&response_payload_slice, 1); |
||||
grpc_byte_buffer* request_payload_recv = nullptr; |
||||
grpc_byte_buffer* request2_payload_recv = nullptr; |
||||
grpc_byte_buffer* request3_payload_recv = nullptr; |
||||
grpc_byte_buffer* response_payload_recv = nullptr; |
||||
grpc_status_code status; |
||||
grpc_call_error error; |
||||
grpc_slice details; |
||||
int was_cancelled = 2; |
||||
char* peer; |
||||
|
||||
grpc_arg arg; |
||||
arg.type = GRPC_ARG_STRING; |
||||
arg.key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG); |
||||
arg.value.string = const_cast<char*>( |
||||
"{\n" |
||||
" \"methodConfig\": [ {\n" |
||||
" \"name\": [\n" |
||||
" { \"service\": \"service\", \"method\": \"method\" }\n" |
||||
" ],\n" |
||||
" \"retryPolicy\": {\n" |
||||
" \"maxAttempts\": 3,\n" |
||||
" \"initialBackoff\": \"1s\",\n" |
||||
" \"maxBackoff\": \"120s\",\n" |
||||
" \"backoffMultiplier\": 1.6,\n" |
||||
" \"retryableStatusCodes\": [ \"ABORTED\" ]\n" |
||||
" }\n" |
||||
" } ]\n" |
||||
"}"); |
||||
grpc_channel_args client_args = {1, &arg}; |
||||
grpc_end2end_test_fixture f = |
||||
begin_test(config, "retry_streaming", &client_args, nullptr); |
||||
|
||||
cq_verifier* cqv = cq_verifier_create(f.cq); |
||||
|
||||
gpr_timespec deadline = five_seconds_from_now(); |
||||
c = grpc_channel_create_call( |
||||
f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
||||
grpc_slice_from_static_string("/service/method"), |
||||
get_host_override_slice("foo.test.google.fr:1234", config), deadline, |
||||
nullptr); |
||||
GPR_ASSERT(c); |
||||
|
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
grpc_metadata_array_init(&initial_metadata_recv); |
||||
grpc_metadata_array_init(&trailing_metadata_recv); |
||||
grpc_metadata_array_init(&request_metadata_recv); |
||||
grpc_call_details_init(&call_details); |
||||
grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
||||
|
||||
// Client starts a batch for receiving initial metadata, a message,
|
||||
// and trailing metadata.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
||||
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &response_payload_recv; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
||||
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
||||
op->data.recv_status_on_client.status = &status; |
||||
op->data.recv_status_on_client.status_details = &details; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
// Client sends initial metadata and a message.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = request_payload; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(2), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(2), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Server gets a call with received initial metadata.
|
||||
error = |
||||
grpc_server_request_call(f.server, &s, &call_details, |
||||
&request_metadata_recv, f.cq, f.cq, tag(101)); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(101), true); |
||||
cq_verify(cqv); |
||||
|
||||
peer = grpc_call_get_peer(s); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "server_peer=%s", peer); |
||||
gpr_free(peer); |
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
// Server receives a message.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &request_payload_recv; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(102), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Client sends a second message.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = request2_payload; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(3), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(3), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Server receives the second message.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &request2_payload_recv; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(103), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(103), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Client sends a third message.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = request3_payload; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(4), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(4), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Server receives the third message.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &request3_payload_recv; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(104), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(104), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Server sends both initial and trailing metadata.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
||||
op->data.recv_close_on_server.cancelled = &was_cancelled; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
op->data.send_status_from_server.trailing_metadata_count = 0; |
||||
op->data.send_status_from_server.status = GRPC_STATUS_ABORTED; |
||||
op->data.send_status_from_server.status_details = &status_details; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(105), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(105), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Clean up from first attempt.
|
||||
grpc_call_unref(s); |
||||
grpc_metadata_array_destroy(&request_metadata_recv); |
||||
grpc_metadata_array_init(&request_metadata_recv); |
||||
grpc_call_details_destroy(&call_details); |
||||
grpc_call_details_init(&call_details); |
||||
GPR_ASSERT(byte_buffer_eq_slice(request_payload_recv, request_payload_slice)); |
||||
grpc_byte_buffer_destroy(request_payload_recv); |
||||
request_payload_recv = nullptr; |
||||
GPR_ASSERT( |
||||
byte_buffer_eq_slice(request2_payload_recv, request2_payload_slice)); |
||||
grpc_byte_buffer_destroy(request2_payload_recv); |
||||
request2_payload_recv = nullptr; |
||||
GPR_ASSERT( |
||||
byte_buffer_eq_slice(request3_payload_recv, request3_payload_slice)); |
||||
grpc_byte_buffer_destroy(request3_payload_recv); |
||||
request3_payload_recv = nullptr; |
||||
|
||||
// Server gets a second call (the retry).
|
||||
error = |
||||
grpc_server_request_call(f.server, &s, &call_details, |
||||
&request_metadata_recv, f.cq, f.cq, tag(201)); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(201), true); |
||||
cq_verify(cqv); |
||||
|
||||
peer = grpc_call_get_peer(s); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "server_peer=%s", peer); |
||||
gpr_free(peer); |
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
// Server receives the first message (and does not receive any others).
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &request_payload_recv; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(202), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(202), true); |
||||
cq_verify(cqv); |
||||
|
||||
// Server sends initial metadata, a message, and trailing metadata.
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = response_payload; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
op->data.send_status_from_server.trailing_metadata_count = 0; |
||||
// Returning a retriable code, but because we are also sending a
|
||||
// message, the client will commit instead of retrying again.
|
||||
op->data.send_status_from_server.status = GRPC_STATUS_ABORTED; |
||||
op->data.send_status_from_server.status_details = &status_details; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(205), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(205), true); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(1), true); |
||||
cq_verify(cqv); |
||||
|
||||
GPR_ASSERT(status == GRPC_STATUS_ABORTED); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz")); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method")); |
||||
validate_host_override_string("foo.test.google.fr:1234", call_details.host, |
||||
config); |
||||
GPR_ASSERT(0 == call_details.flags); |
||||
GPR_ASSERT(was_cancelled == 1); |
||||
|
||||
grpc_slice_unref(details); |
||||
grpc_metadata_array_destroy(&initial_metadata_recv); |
||||
grpc_metadata_array_destroy(&trailing_metadata_recv); |
||||
grpc_metadata_array_destroy(&request_metadata_recv); |
||||
grpc_call_details_destroy(&call_details); |
||||
grpc_byte_buffer_destroy(request_payload); |
||||
grpc_byte_buffer_destroy(request2_payload); |
||||
grpc_byte_buffer_destroy(request3_payload); |
||||
grpc_byte_buffer_destroy(response_payload); |
||||
GPR_ASSERT(byte_buffer_eq_slice(request_payload_recv, request_payload_slice)); |
||||
grpc_byte_buffer_destroy(request_payload_recv); |
||||
grpc_byte_buffer_destroy(response_payload_recv); |
||||
|
||||
grpc_call_unref(c); |
||||
grpc_call_unref(s); |
||||
|
||||
cq_verifier_destroy(cqv); |
||||
|
||||
end_test(&f); |
||||
config.tear_down_data(&f); |
||||
} |
||||
|
||||
void retry_streaming_succeeds_before_replay_finished( |
||||
grpc_end2end_test_config config) { |
||||
GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL); |
||||
test_retry_streaming_succeeds_before_replay_finished(config); |
||||
} |
||||
|
||||
void retry_streaming_succeeds_before_replay_finished_pre_init(void) {} |
@ -0,0 +1,264 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed 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 "test/core/end2end/end2end_tests.h" |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include <grpc/byte_buffer.h> |
||||
#include <grpc/grpc.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
#include <grpc/support/time.h> |
||||
|
||||
#include "src/core/lib/channel/channel_args.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "src/core/lib/gpr/useful.h" |
||||
#include "src/core/lib/iomgr/exec_ctx.h" |
||||
#include "src/core/lib/transport/static_metadata.h" |
||||
|
||||
#include "test/core/end2end/cq_verifier.h" |
||||
#include "test/core/end2end/tests/cancel_test_helpers.h" |
||||
|
||||
static void* tag(intptr_t t) { return (void*)t; } |
||||
|
||||
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
||||
const char* test_name, |
||||
grpc_channel_args* client_args, |
||||
grpc_channel_args* server_args) { |
||||
grpc_end2end_test_fixture f; |
||||
gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name); |
||||
f = config.create_fixture(client_args, server_args); |
||||
config.init_server(&f, server_args); |
||||
config.init_client(&f, client_args); |
||||
return f; |
||||
} |
||||
|
||||
static gpr_timespec n_seconds_from_now(int n) { |
||||
return grpc_timeout_seconds_to_deadline(n); |
||||
} |
||||
|
||||
static gpr_timespec five_seconds_from_now(void) { |
||||
return n_seconds_from_now(5); |
||||
} |
||||
|
||||
static void drain_cq(grpc_completion_queue* cq) { |
||||
grpc_event ev; |
||||
do { |
||||
ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
||||
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
||||
} |
||||
|
||||
static void shutdown_server(grpc_end2end_test_fixture* f) { |
||||
if (!f->server) return; |
||||
grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000)); |
||||
GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000), |
||||
grpc_timeout_seconds_to_deadline(5), |
||||
nullptr) |
||||
.type == GRPC_OP_COMPLETE); |
||||
grpc_server_destroy(f->server); |
||||
f->server = nullptr; |
||||
} |
||||
|
||||
static void shutdown_client(grpc_end2end_test_fixture* f) { |
||||
if (!f->client) return; |
||||
grpc_channel_destroy(f->client); |
||||
f->client = nullptr; |
||||
} |
||||
|
||||
static void end_test(grpc_end2end_test_fixture* f) { |
||||
shutdown_server(f); |
||||
shutdown_client(f); |
||||
|
||||
grpc_completion_queue_shutdown(f->cq); |
||||
drain_cq(f->cq); |
||||
grpc_completion_queue_destroy(f->cq); |
||||
grpc_completion_queue_destroy(f->shutdown_cq); |
||||
} |
||||
|
||||
// Tests that we don't retry when throttled.
|
||||
// - 1 retry allowed for ABORTED status
|
||||
// - first attempt gets ABORTED but is over limit, so no retry is done
|
||||
static void test_retry_throttled(grpc_end2end_test_config config) { |
||||
grpc_call* c; |
||||
grpc_call* s; |
||||
grpc_op ops[6]; |
||||
grpc_op* op; |
||||
grpc_metadata_array initial_metadata_recv; |
||||
grpc_metadata_array trailing_metadata_recv; |
||||
grpc_metadata_array request_metadata_recv; |
||||
grpc_call_details call_details; |
||||
grpc_slice request_payload_slice = grpc_slice_from_static_string("foo"); |
||||
grpc_slice response_payload_slice = grpc_slice_from_static_string("bar"); |
||||
grpc_byte_buffer* request_payload = |
||||
grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
||||
grpc_byte_buffer* response_payload = |
||||
grpc_raw_byte_buffer_create(&response_payload_slice, 1); |
||||
grpc_byte_buffer* request_payload_recv = nullptr; |
||||
grpc_byte_buffer* response_payload_recv = nullptr; |
||||
grpc_status_code status; |
||||
grpc_call_error error; |
||||
grpc_slice details; |
||||
int was_cancelled = 2; |
||||
char* peer; |
||||
|
||||
grpc_arg arg; |
||||
arg.type = GRPC_ARG_STRING; |
||||
arg.key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG); |
||||
arg.value.string = const_cast<char*>( |
||||
"{\n" |
||||
" \"methodConfig\": [ {\n" |
||||
" \"name\": [\n" |
||||
" { \"service\": \"service\", \"method\": \"method\" }\n" |
||||
" ],\n" |
||||
" \"retryPolicy\": {\n" |
||||
" \"maxAttempts\": 2,\n" |
||||
" \"initialBackoff\": \"1s\",\n" |
||||
" \"maxBackoff\": \"120s\",\n" |
||||
" \"backoffMultiplier\": 1.6,\n" |
||||
" \"retryableStatusCodes\": [ \"ABORTED\" ]\n" |
||||
" }\n" |
||||
" } ],\n" |
||||
// A single failure will cause us to be throttled.
|
||||
// (This is not a very realistic config, but it works for the
|
||||
// purposes of this test.)
|
||||
" \"retryThrottling\": {\n" |
||||
" \"maxTokens\": 2,\n" |
||||
" \"tokenRatio\": 1.0,\n" |
||||
" }\n" |
||||
"}"); |
||||
grpc_channel_args client_args = {1, &arg}; |
||||
grpc_end2end_test_fixture f = |
||||
begin_test(config, "retry_throttled", &client_args, nullptr); |
||||
|
||||
cq_verifier* cqv = cq_verifier_create(f.cq); |
||||
|
||||
gpr_timespec deadline = five_seconds_from_now(); |
||||
c = grpc_channel_create_call( |
||||
f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
||||
grpc_slice_from_static_string("/service/method"), |
||||
get_host_override_slice("foo.test.google.fr:1234", config), deadline, |
||||
nullptr); |
||||
GPR_ASSERT(c); |
||||
|
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
grpc_metadata_array_init(&initial_metadata_recv); |
||||
grpc_metadata_array_init(&trailing_metadata_recv); |
||||
grpc_metadata_array_init(&request_metadata_recv); |
||||
grpc_call_details_init(&call_details); |
||||
grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = request_payload; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &response_payload_recv; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
||||
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
||||
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
||||
op->data.recv_status_on_client.status = &status; |
||||
op->data.recv_status_on_client.status_details = &details; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
error = |
||||
grpc_server_request_call(f.server, &s, &call_details, |
||||
&request_metadata_recv, f.cq, f.cq, tag(101)); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(101), true); |
||||
cq_verify(cqv); |
||||
|
||||
peer = grpc_call_get_peer(s); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "server_peer=%s", peer); |
||||
gpr_free(peer); |
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
op->data.send_status_from_server.trailing_metadata_count = 0; |
||||
op->data.send_status_from_server.status = GRPC_STATUS_ABORTED; |
||||
op->data.send_status_from_server.status_details = &status_details; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
||||
op->data.recv_close_on_server.cancelled = &was_cancelled; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
CQ_EXPECT_COMPLETION(cqv, tag(102), true); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(1), true); |
||||
cq_verify(cqv); |
||||
|
||||
GPR_ASSERT(status == GRPC_STATUS_ABORTED); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz")); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method")); |
||||
validate_host_override_string("foo.test.google.fr:1234", call_details.host, |
||||
config); |
||||
GPR_ASSERT(0 == call_details.flags); |
||||
GPR_ASSERT(was_cancelled == 1); |
||||
|
||||
grpc_slice_unref(details); |
||||
grpc_metadata_array_destroy(&initial_metadata_recv); |
||||
grpc_metadata_array_destroy(&trailing_metadata_recv); |
||||
grpc_metadata_array_destroy(&request_metadata_recv); |
||||
grpc_call_details_destroy(&call_details); |
||||
grpc_byte_buffer_destroy(request_payload); |
||||
grpc_byte_buffer_destroy(response_payload); |
||||
grpc_byte_buffer_destroy(request_payload_recv); |
||||
grpc_byte_buffer_destroy(response_payload_recv); |
||||
|
||||
grpc_call_unref(c); |
||||
grpc_call_unref(s); |
||||
|
||||
cq_verifier_destroy(cqv); |
||||
|
||||
end_test(&f); |
||||
config.tear_down_data(&f); |
||||
} |
||||
|
||||
void retry_throttled(grpc_end2end_test_config config) { |
||||
GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL); |
||||
test_retry_throttled(config); |
||||
} |
||||
|
||||
void retry_throttled_pre_init(void) {} |
@ -0,0 +1,299 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed 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 "test/core/end2end/end2end_tests.h" |
||||
|
||||
#include <stdio.h> |
||||
#include <string.h> |
||||
|
||||
#include <grpc/byte_buffer.h> |
||||
#include <grpc/grpc.h> |
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
#include <grpc/support/time.h> |
||||
|
||||
#include "src/core/lib/channel/channel_args.h" |
||||
#include "src/core/lib/gpr/string.h" |
||||
#include "src/core/lib/gpr/useful.h" |
||||
#include "src/core/lib/iomgr/exec_ctx.h" |
||||
#include "src/core/lib/transport/static_metadata.h" |
||||
|
||||
#include "test/core/end2end/cq_verifier.h" |
||||
#include "test/core/end2end/tests/cancel_test_helpers.h" |
||||
|
||||
static void* tag(intptr_t t) { return (void*)t; } |
||||
|
||||
static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config, |
||||
const char* test_name, |
||||
grpc_channel_args* client_args, |
||||
grpc_channel_args* server_args) { |
||||
grpc_end2end_test_fixture f; |
||||
gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name); |
||||
f = config.create_fixture(client_args, server_args); |
||||
config.init_server(&f, server_args); |
||||
config.init_client(&f, client_args); |
||||
return f; |
||||
} |
||||
|
||||
static gpr_timespec n_seconds_from_now(int n) { |
||||
return grpc_timeout_seconds_to_deadline(n); |
||||
} |
||||
|
||||
static gpr_timespec five_seconds_from_now(void) { |
||||
return n_seconds_from_now(5); |
||||
} |
||||
|
||||
static void drain_cq(grpc_completion_queue* cq) { |
||||
grpc_event ev; |
||||
do { |
||||
ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr); |
||||
} while (ev.type != GRPC_QUEUE_SHUTDOWN); |
||||
} |
||||
|
||||
static void shutdown_server(grpc_end2end_test_fixture* f) { |
||||
if (!f->server) return; |
||||
grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000)); |
||||
GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000), |
||||
grpc_timeout_seconds_to_deadline(5), |
||||
nullptr) |
||||
.type == GRPC_OP_COMPLETE); |
||||
grpc_server_destroy(f->server); |
||||
f->server = nullptr; |
||||
} |
||||
|
||||
static void shutdown_client(grpc_end2end_test_fixture* f) { |
||||
if (!f->client) return; |
||||
grpc_channel_destroy(f->client); |
||||
f->client = nullptr; |
||||
} |
||||
|
||||
static void end_test(grpc_end2end_test_fixture* f) { |
||||
shutdown_server(f); |
||||
shutdown_client(f); |
||||
|
||||
grpc_completion_queue_shutdown(f->cq); |
||||
drain_cq(f->cq); |
||||
grpc_completion_queue_destroy(f->cq); |
||||
grpc_completion_queue_destroy(f->shutdown_cq); |
||||
} |
||||
|
||||
// Tests that we stop retrying after the configured number of attempts.
|
||||
// - 1 retry allowed for ABORTED status
|
||||
// - first attempt gets ABORTED
|
||||
// - second attempt gets ABORTED but does not retry
|
||||
static void test_retry_too_many_attempts(grpc_end2end_test_config config) { |
||||
grpc_call* c; |
||||
grpc_call* s; |
||||
grpc_op ops[6]; |
||||
grpc_op* op; |
||||
grpc_metadata_array initial_metadata_recv; |
||||
grpc_metadata_array trailing_metadata_recv; |
||||
grpc_metadata_array request_metadata_recv; |
||||
grpc_call_details call_details; |
||||
grpc_slice request_payload_slice = grpc_slice_from_static_string("foo"); |
||||
grpc_slice response_payload_slice = grpc_slice_from_static_string("bar"); |
||||
grpc_byte_buffer* request_payload = |
||||
grpc_raw_byte_buffer_create(&request_payload_slice, 1); |
||||
grpc_byte_buffer* response_payload = |
||||
grpc_raw_byte_buffer_create(&response_payload_slice, 1); |
||||
grpc_byte_buffer* request_payload_recv = nullptr; |
||||
grpc_byte_buffer* response_payload_recv = nullptr; |
||||
grpc_status_code status; |
||||
grpc_call_error error; |
||||
grpc_slice details; |
||||
int was_cancelled = 2; |
||||
char* peer; |
||||
|
||||
grpc_arg arg; |
||||
arg.type = GRPC_ARG_STRING; |
||||
arg.key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG); |
||||
arg.value.string = const_cast<char*>( |
||||
"{\n" |
||||
" \"methodConfig\": [ {\n" |
||||
" \"name\": [\n" |
||||
" { \"service\": \"service\", \"method\": \"method\" }\n" |
||||
" ],\n" |
||||
" \"retryPolicy\": {\n" |
||||
" \"maxAttempts\": 2,\n" |
||||
" \"initialBackoff\": \"1s\",\n" |
||||
" \"maxBackoff\": \"120s\",\n" |
||||
" \"backoffMultiplier\": 1.6,\n" |
||||
" \"retryableStatusCodes\": [ \"ABORTED\" ]\n" |
||||
" }\n" |
||||
" } ]\n" |
||||
"}"); |
||||
grpc_channel_args client_args = {1, &arg}; |
||||
grpc_end2end_test_fixture f = |
||||
begin_test(config, "retry_too_many_attempts", &client_args, nullptr); |
||||
|
||||
cq_verifier* cqv = cq_verifier_create(f.cq); |
||||
|
||||
gpr_timespec deadline = five_seconds_from_now(); |
||||
c = grpc_channel_create_call( |
||||
f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq, |
||||
grpc_slice_from_static_string("/service/method"), |
||||
get_host_override_slice("foo.test.google.fr:1234", config), deadline, |
||||
nullptr); |
||||
GPR_ASSERT(c); |
||||
|
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
grpc_metadata_array_init(&initial_metadata_recv); |
||||
grpc_metadata_array_init(&trailing_metadata_recv); |
||||
grpc_metadata_array_init(&request_metadata_recv); |
||||
grpc_call_details_init(&call_details); |
||||
grpc_slice status_details = grpc_slice_from_static_string("xyz"); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_MESSAGE; |
||||
op->data.send_message.send_message = request_payload; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_MESSAGE; |
||||
op->data.recv_message.recv_message = &response_payload_recv; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_INITIAL_METADATA; |
||||
op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
||||
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
||||
op->data.recv_status_on_client.status = &status; |
||||
op->data.recv_status_on_client.status_details = &details; |
||||
op++; |
||||
error = grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
error = |
||||
grpc_server_request_call(f.server, &s, &call_details, |
||||
&request_metadata_recv, f.cq, f.cq, tag(101)); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(101), true); |
||||
cq_verify(cqv); |
||||
|
||||
peer = grpc_call_get_peer(s); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "server_peer=%s", peer); |
||||
gpr_free(peer); |
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
op->data.send_status_from_server.trailing_metadata_count = 0; |
||||
op->data.send_status_from_server.status = GRPC_STATUS_ABORTED; |
||||
op->data.send_status_from_server.status_details = &status_details; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
||||
op->data.recv_close_on_server.cancelled = &was_cancelled; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(102), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
CQ_EXPECT_COMPLETION(cqv, tag(102), true); |
||||
cq_verify(cqv); |
||||
|
||||
grpc_call_unref(s); |
||||
grpc_metadata_array_destroy(&request_metadata_recv); |
||||
grpc_metadata_array_init(&request_metadata_recv); |
||||
grpc_call_details_destroy(&call_details); |
||||
grpc_call_details_init(&call_details); |
||||
|
||||
error = |
||||
grpc_server_request_call(f.server, &s, &call_details, |
||||
&request_metadata_recv, f.cq, f.cq, tag(201)); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(201), true); |
||||
cq_verify(cqv); |
||||
|
||||
peer = grpc_call_get_peer(s); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "server_peer=%s", peer); |
||||
gpr_free(peer); |
||||
peer = grpc_call_get_peer(c); |
||||
GPR_ASSERT(peer != nullptr); |
||||
gpr_log(GPR_DEBUG, "client_peer=%s", peer); |
||||
gpr_free(peer); |
||||
|
||||
memset(ops, 0, sizeof(ops)); |
||||
op = ops; |
||||
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
||||
op->data.send_initial_metadata.count = 0; |
||||
op++; |
||||
op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; |
||||
op->data.send_status_from_server.trailing_metadata_count = 0; |
||||
op->data.send_status_from_server.status = GRPC_STATUS_ABORTED; |
||||
op->data.send_status_from_server.status_details = &status_details; |
||||
op++; |
||||
op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; |
||||
op->data.recv_close_on_server.cancelled = &was_cancelled; |
||||
op++; |
||||
error = grpc_call_start_batch(s, ops, (size_t)(op - ops), tag(202), nullptr); |
||||
GPR_ASSERT(GRPC_CALL_OK == error); |
||||
|
||||
CQ_EXPECT_COMPLETION(cqv, tag(202), true); |
||||
CQ_EXPECT_COMPLETION(cqv, tag(1), true); |
||||
cq_verify(cqv); |
||||
|
||||
GPR_ASSERT(status == GRPC_STATUS_ABORTED); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz")); |
||||
GPR_ASSERT(0 == grpc_slice_str_cmp(call_details.method, "/service/method")); |
||||
validate_host_override_string("foo.test.google.fr:1234", call_details.host, |
||||
config); |
||||
GPR_ASSERT(0 == call_details.flags); |
||||
GPR_ASSERT(was_cancelled == 1); |
||||
|
||||
grpc_slice_unref(details); |
||||
grpc_metadata_array_destroy(&initial_metadata_recv); |
||||
grpc_metadata_array_destroy(&trailing_metadata_recv); |
||||
grpc_metadata_array_destroy(&request_metadata_recv); |
||||
grpc_call_details_destroy(&call_details); |
||||
grpc_byte_buffer_destroy(request_payload); |
||||
grpc_byte_buffer_destroy(response_payload); |
||||
grpc_byte_buffer_destroy(request_payload_recv); |
||||
grpc_byte_buffer_destroy(response_payload_recv); |
||||
|
||||
grpc_call_unref(c); |
||||
grpc_call_unref(s); |
||||
|
||||
cq_verifier_destroy(cqv); |
||||
|
||||
end_test(&f); |
||||
config.tear_down_data(&f); |
||||
} |
||||
|
||||
void retry_too_many_attempts(grpc_end2end_test_config config) { |
||||
GPR_ASSERT(config.feature_mask & FEATURE_MASK_SUPPORTS_CLIENT_CHANNEL); |
||||
test_retry_too_many_attempts(config); |
||||
} |
||||
|
||||
void retry_too_many_attempts_pre_init(void) {} |
@ -0,0 +1,105 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2017 gRPC authors. |
||||
* |
||||
* Licensed 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 "src/core/lib/slice/slice_weak_hash_table.h" |
||||
|
||||
#include <cstring> |
||||
#include <sstream> |
||||
|
||||
#include <gtest/gtest.h> |
||||
|
||||
#include <grpc/support/alloc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc/support/string_util.h> |
||||
|
||||
#include "src/core/lib/gpr/useful.h" |
||||
#include "src/core/lib/slice/slice_internal.h" |
||||
#include "test/core/util/test_config.h" |
||||
|
||||
namespace grpc_core { |
||||
namespace { |
||||
|
||||
grpc_slice BuildRefCountedKey(const char* key_str) { |
||||
const size_t key_length = strlen(key_str); |
||||
grpc_slice key = grpc_slice_malloc_large(key_length); |
||||
memcpy(GRPC_SLICE_START_PTR(key), key_str, key_length); |
||||
return key; |
||||
} |
||||
|
||||
TEST(SliceWeakHashTable, Basic) { |
||||
auto table = SliceWeakHashTable<UniquePtr<char>, 10>::Create(); |
||||
// Single key-value insertion.
|
||||
grpc_slice key = BuildRefCountedKey("key"); |
||||
grpc_slice_ref(key); // Get doesn't own.
|
||||
table->Add(key, UniquePtr<char>(gpr_strdup("value"))); |
||||
ASSERT_NE(table->Get(key), nullptr); |
||||
ASSERT_STREQ(table->Get(key)->get(), "value"); |
||||
grpc_slice_unref(key); |
||||
// Unknown key.
|
||||
ASSERT_EQ(table->Get(grpc_slice_from_static_string("unknown_key")), nullptr); |
||||
} |
||||
|
||||
TEST(SliceWeakHashTable, ValueTypeConstructor) { |
||||
struct Value { |
||||
Value() : a(123) {} |
||||
int a; |
||||
}; |
||||
auto table = SliceWeakHashTable<Value, 1>::Create(); |
||||
grpc_slice key = BuildRefCountedKey("key"); |
||||
grpc_slice_ref(key); // Get doesn't own.
|
||||
table->Add(key, Value()); |
||||
ASSERT_EQ(table->Get(key)->a, 123); |
||||
grpc_slice_unref(key); |
||||
} |
||||
|
||||
TEST(SliceWeakHashTable, ForceOverload) { |
||||
constexpr int kTableSize = 10; |
||||
auto table = SliceWeakHashTable<UniquePtr<char>, kTableSize>::Create(); |
||||
// Insert a multiple of the maximum size table.
|
||||
for (int i = 0; i < kTableSize * 2; ++i) { |
||||
std::ostringstream oss; |
||||
oss << "key-" << i; |
||||
grpc_slice key = BuildRefCountedKey(oss.str().c_str()); |
||||
oss.clear(); |
||||
oss << "value-" << i; |
||||
table->Add(key, UniquePtr<char>(gpr_strdup(oss.str().c_str()))); |
||||
} |
||||
// Verify that some will have been replaced.
|
||||
int num_missing = 0; |
||||
for (int i = 0; i < kTableSize * 2; ++i) { |
||||
std::ostringstream oss; |
||||
oss << "key-" << i; |
||||
grpc_slice key = BuildRefCountedKey(oss.str().c_str()); |
||||
if (table->Get(key) == nullptr) num_missing++; |
||||
grpc_slice_unref(key); |
||||
} |
||||
// At least kTableSize elements will be missing.
|
||||
ASSERT_GE(num_missing, kTableSize); |
||||
} |
||||
|
||||
} // namespace
|
||||
} // namespace grpc_core
|
||||
|
||||
int main(int argc, char** argv) { |
||||
::testing::InitGoogleTest(&argc, argv); |
||||
grpc_test_init(argc, argv); |
||||
grpc_core::ExecCtx::GlobalInit(); |
||||
int result = RUN_ALL_TESTS(); |
||||
grpc_core::ExecCtx::GlobalShutdown(); |
||||
return result; |
||||
} |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue