| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 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 <grpc/grpc.h> |
| 23 |
#include <grpc/grpc_security.h> |
| 24 |
#include <grpc/impl/codegen/propagation_bits.h> |
| 25 |
#include <grpc/slice.h> |
| 26 |
#include <grpc/status.h> |
| 27 |
#include <grpc/support/log.h> |
| 28 |
#include <grpc/support/time.h> |
| 29 |
|
| 30 |
#include "src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h" |
| 31 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 32 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 33 |
#include "test/core/end2end/cq_verifier.h" |
| 34 |
#include "test/core/util/test_config.h" |
| 35 |
|
| 36 |
static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); } |
| 37 |
|
| 38 |
void run_test(bool wait_for_ready) { |
| 39 |
gpr_log(GPR_INFO, "TEST: wait_for_ready=%d", wait_for_ready); |
| 40 |
|
| 41 |
grpc_init(); |
| 42 |
|
| 43 |
grpc_completion_queue* cq = grpc_completion_queue_create_for_next(nullptr); |
| 44 |
grpc_core::CqVerifier cqv(cq); |
| 45 |
|
| 46 |
grpc_core::RefCountedPtr<grpc_core::FakeResolverResponseGenerator> |
| 47 |
response_generator = |
| 48 |
grpc_core::MakeRefCounted<grpc_core::FakeResolverResponseGenerator>(); |
| 49 |
grpc_arg arg = grpc_core::FakeResolverResponseGenerator::MakeChannelArg( |
| 50 |
response_generator.get()); |
| 51 |
grpc_channel_args args = {1, &arg}; |
| 52 |
|
| 53 |
/* create a call, channel to a non existant server */ |
| 54 |
grpc_channel_credentials* creds = grpc_insecure_credentials_create(); |
| 55 |
grpc_channel* chan = grpc_channel_create("fake:nonexistant", creds, &args); |
| 56 |
grpc_channel_credentials_release(creds); |
| 57 |
gpr_timespec deadline = grpc_timeout_seconds_to_deadline(2); |
| 58 |
grpc_call* call = grpc_channel_create_call( |
| 59 |
chan, nullptr, GRPC_PROPAGATE_DEFAULTS, cq, |
| 60 |
grpc_slice_from_static_string("/Foo"), nullptr, deadline, nullptr); |
| 61 |
|
| 62 |
grpc_op ops[6]; |
| 63 |
memset(ops, 0, sizeof(ops)); |
| 64 |
grpc_op* op = ops; |
| 65 |
op->op = GRPC_OP_SEND_INITIAL_METADATA; |
| 66 |
op->data.send_initial_metadata.count = 0; |
| 67 |
op->flags = wait_for_ready ? GRPC_INITIAL_METADATA_WAIT_FOR_READY : 0; |
| 68 |
op->reserved = nullptr; |
| 69 |
op++; |
| 70 |
grpc_metadata_array trailing_metadata_recv; |
| 71 |
grpc_metadata_array_init(&trailing_metadata_recv); |
| 72 |
grpc_status_code status; |
| 73 |
grpc_slice details; |
| 74 |
op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; |
| 75 |
op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv; |
| 76 |
op->data.recv_status_on_client.status = &status; |
| 77 |
op->data.recv_status_on_client.status_details = &details; |
| 78 |
op->flags = 0; |
| 79 |
op->reserved = nullptr; |
| 80 |
op++; |
| 81 |
GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(call, ops, |
| 82 |
(size_t)(op - ops), tag(1), |
| 83 |
nullptr)); |
| 84 |
|
| 85 |
{ |
| 86 |
grpc_core::ExecCtx exec_ctx; |
| 87 |
response_generator->SetFailure(); |
| 88 |
} |
| 89 |
|
| 90 |
/* verify that all tags get completed */ |
| 91 |
cqv.Expect(tag(1), true); |
| 92 |
cqv.Verify(); |
| 93 |
|
| 94 |
gpr_log(GPR_INFO, "call status: %d", status); |
| 95 |
if (wait_for_ready) { |
| 96 |
GPR_ASSERT(status == GRPC_STATUS_DEADLINE_EXCEEDED); |
| 97 |
} else { |
| 98 |
GPR_ASSERT(status == GRPC_STATUS_UNAVAILABLE); |
| 99 |
} |
| 100 |
|
| 101 |
grpc_slice_unref(details); |
| 102 |
grpc_metadata_array_destroy(&trailing_metadata_recv); |
| 103 |
|
| 104 |
grpc_completion_queue_shutdown(cq); |
| 105 |
while (grpc_completion_queue_next(cq, gpr_inf_future(GPR_CLOCK_REALTIME), |
| 106 |
nullptr) |
| 107 |
.type != GRPC_QUEUE_SHUTDOWN) { |
| 108 |
} |
| 109 |
grpc_completion_queue_destroy(cq); |
| 110 |
grpc_call_unref(call); |
| 111 |
grpc_channel_destroy(chan); |
| 112 |
|
| 113 |
grpc_shutdown(); |
| 114 |
} |
| 115 |
|
| 116 |
int main(int argc, char** argv) { |
| 117 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 118 |
run_test(true /* wait_for_ready */); |
| 119 |
run_test(false /* wait_for_ready */); |
| 120 |
return 0; |
| 121 |
} |