| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2016 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <stdint.h> |
| 20 |
#include <string.h> |
| 21 |
|
| 22 |
#include <string> |
| 23 |
|
| 24 |
#include <grpc/grpc.h> |
| 25 |
#include <grpc/grpc_security.h> |
| 26 |
#include <grpc/impl/codegen/propagation_bits.h> |
| 27 |
#include <grpc/slice.h> |
| 28 |
#include <grpc/status.h> |
| 29 |
#include <grpc/support/log.h> |
| 30 |
#include <grpc/support/time.h> |
| 31 |
|
| 32 |
#include "src/core/lib/channel/channel_args.h" |
| 33 |
#include "src/core/lib/gprpp/host_port.h" |
| 34 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 35 |
#include "test/core/end2end/cq_verifier.h" |
| 36 |
#include "test/core/util/port.h" |
| 37 |
#include "test/core/util/test_config.h" |
| 38 |
|
| 39 |
static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); } |
| 40 |
|
| 41 |
static void run_test(bool wait_for_ready, bool use_service_config) { |
| 42 |
grpc_channel* chan; |
| 43 |
grpc_call* call; |
| 44 |
grpc_completion_queue* cq; |
| 45 |
grpc_op ops[6]; |
| 46 |
grpc_op* op; |
| 47 |
grpc_metadata_array trailing_metadata_recv; |
| 48 |
grpc_status_code status; |
| 49 |
grpc_slice details; |
| 50 |
|
| 51 |
gpr_log(GPR_INFO, "TEST: wait_for_ready=%d use_service_config=%d", |
| 52 |
wait_for_ready, use_service_config); |
| 53 |
|
| 54 |
grpc_init(); |
| 55 |
|
| 56 |
grpc_metadata_array_init(&trailing_metadata_recv); |
| 57 |
|
| 58 |
cq = grpc_completion_queue_create_for_next(nullptr); |
| 59 |
grpc_core::CqVerifier cqv(cq); |
| 60 |
|
| 61 |
/* if using service config, create channel args */ |
| 62 |
grpc_channel_args* args = nullptr; |
| 63 |
if (use_service_config) { |
| 64 |
GPR_ASSERT(wait_for_ready); |
| 65 |
grpc_arg arg; |
| 66 |
arg.type = GRPC_ARG_STRING; |
| 67 |
arg.key = const_cast<char*>(GRPC_ARG_SERVICE_CONFIG); |
| 68 |
arg.value.string = const_cast<char*>( |
| 69 |
"{\n" |
| 70 |
" \"methodConfig\": [ {\n" |
| 71 |
" \"name\": [\n" |
| 72 |
" { \"service\": \"service\", \"method\": \"method\" }\n" |
| 73 |
" ],\n" |
| 74 |
" \"waitForReady\": true\n" |
| 75 |
" } ]\n" |
| 76 |
"}"); |
| 77 |
args = grpc_channel_args_copy_and_add(args, &arg, 1); |
| 78 |
} |
| 79 |
|
| 80 |
/* create a call, channel to a port which will refuse connection */ |
| 81 |
int port = grpc_pick_unused_port_or_die(); |
| 82 |
std::string addr = grpc_core::JoinHostPort("127.0.0.1", port); |
| 83 |
gpr_log(GPR_INFO, "server: %s", addr.c_str()); |
| 84 |
grpc_channel_credentials* creds = grpc_insecure_credentials_create(); |
| 85 |
chan = grpc_channel_create(addr.c_str(), creds, args); |
| 86 |
grpc_channel_credentials_release(creds); |
| 87 |
grpc_slice host = grpc_slice_from_static_string("nonexistant"); |
| 88 |
gpr_timespec deadline = grpc_timeout_seconds_to_deadline(2); |
| 89 |
call = |
| 90 |
grpc_channel_create_call(chan, nullptr, GRPC_PROPAGATE_DEFAULTS, cq, |
| 91 |
grpc_slice_from_static_string("/service/method"), |
| 92 |
&host, deadline, nullptr); |
| 93 |
|
| 94 |
memset(ops, 0, sizeof(ops)); |
| 95 |
op = ops; |
| 96 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 97 |
op->data.send_initial_metadata.count = 0; |
| 98 |
op->flags = (wait_for_ready && !use_service_config) |
| 99 |
? GRPC_INITIAL_METADATA_WAIT_FOR_READY |
| 100 |
: 0; |
| 101 |
op->reserved = nullptr; |
| 102 |
op++; |
| 103 |
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 104 |
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 105 |
op->data.recv_status_on_client.status = &status; |
| 106 |
op->data.recv_status_on_client.status_details = &details; |
| 107 |
op->flags = 0; |
| 108 |
op->reserved = nullptr; |
| 109 |
op++; |
| 110 |
GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(call, ops, |
| 111 |
(size_t)(op - ops), tag(1), |
| 112 |
nullptr)); |
| 113 |
/* verify that all tags get completed */ |
| 114 |
cqv.Expect(tag(1), true); |
| 115 |
cqv.Verify(); |
| 116 |
|
| 117 |
if (wait_for_ready) { |
| 118 |
GPR_ASSERT(status == GRPC_STATUS_DEADLINE_EXCEEDED); |
| 119 |
} else { |
| 120 |
GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE); |
| 121 |
} |
| 122 |
|
| 123 |
grpc_completion_queue_shutdown(cq); |
| 124 |
while (grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), |
| 125 |
nullptr) |
| 126 |
.type != GRPC_QUEUE_SHUTDOWN) { |
| 127 |
} |
| 128 |
grpc_completion_queue_destroy(cq); |
| 129 |
grpc_call_unref(call); |
| 130 |
grpc_channel_destroy(chan); |
| 131 |
|
| 132 |
grpc_slice_unref(details); |
| 133 |
grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 134 |
|
| 135 |
{ |
| 136 |
grpc_core::ExecCtx exec_ctx; |
| 137 |
if (args != nullptr) grpc_channel_args_destroy(args); |
| 138 |
} |
| 139 |
|
| 140 |
grpc_shutdown(); |
| 141 |
} |
| 142 |
|
| 143 |
int main(int argc, char** argv) { |
| 144 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 145 |
run_test(false /* wait_for_ready */, false /* use_service_config */); |
| 146 |
run_test(true /* wait_for_ready */, false /* use_service_config */); |
| 147 |
run_test(true /* wait_for_ready */, true /* use_service_config */); |
| 148 |
return 0; |
| 149 |
} |