blob: d5edcd4ac177d60ef08bb5344f4349a67bc0d91d [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
3 * Copyright 2014, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include "test/core/end2end/end2end_tests.h"
35
36#include <stdio.h>
37#include <string.h>
38#include <unistd.h>
39
40#include <grpc/byte_buffer.h>
41#include <grpc/support/alloc.h>
42#include <grpc/support/log.h>
43#include <grpc/support/time.h>
44#include <grpc/support/useful.h>
45#include "test/core/end2end/cq_verifier.h"
46
47/* allow cancellation by either grpc_call_cancel, or by wait_for_deadline (which
48 * does nothing) */
49typedef grpc_call_error (*canceller)(grpc_call *call);
50
51enum { TIMEOUT = 200000 };
52
53static void *tag(gpr_intptr t) { return (void *)t; }
54
55static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
56 const char *test_name,
57 grpc_channel_args *client_args,
58 grpc_channel_args *server_args) {
59 grpc_end2end_test_fixture f;
60 gpr_log(GPR_INFO, "%s/%s", test_name, config.name);
61 f = config.create_fixture(client_args, server_args);
62 config.init_client(&f, client_args);
63 config.init_server(&f, server_args);
64 return f;
65}
66
67static gpr_timespec n_seconds_time(int n) {
68 return gpr_time_add(gpr_now(), gpr_time_from_micros(GPR_US_PER_SEC * n));
69}
70
Craig Tiller32946d32015-01-15 11:37:30 -080071static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080072
73static void drain_cq(grpc_completion_queue *cq) {
74 grpc_event *ev;
75 grpc_completion_type type;
76 do {
77 ev = grpc_completion_queue_next(cq, five_seconds_time());
78 GPR_ASSERT(ev);
79 type = ev->type;
80 grpc_event_finish(ev);
81 } while (type != GRPC_QUEUE_SHUTDOWN);
82}
83
84static void shutdown_server(grpc_end2end_test_fixture *f) {
85 if (!f->server) return;
86 grpc_server_shutdown(f->server);
87 grpc_server_destroy(f->server);
88 f->server = NULL;
89}
90
91static void shutdown_client(grpc_end2end_test_fixture *f) {
92 if (!f->client) return;
93 grpc_channel_destroy(f->client);
94 f->client = NULL;
95}
96
97static void end_test(grpc_end2end_test_fixture *f) {
98 shutdown_server(f);
99 shutdown_client(f);
100
101 grpc_completion_queue_shutdown(f->server_cq);
102 drain_cq(f->server_cq);
103 grpc_completion_queue_destroy(f->server_cq);
104 grpc_completion_queue_shutdown(f->client_cq);
105 drain_cq(f->client_cq);
106 grpc_completion_queue_destroy(f->client_cq);
107}
108
109/* Cancel before invoke */
110static void test_cancel_before_invoke(grpc_end2end_test_config config) {
111 grpc_call *c;
112 grpc_end2end_test_fixture f = begin_test(config, __FUNCTION__, NULL, NULL);
113 gpr_timespec deadline = five_seconds_time();
114 cq_verifier *v_client = cq_verifier_create(f.client_cq);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800115
116 c = grpc_channel_create_call(f.client, "/foo", "test.google.com", deadline);
117 GPR_ASSERT(c);
118
119 GPR_ASSERT(GRPC_CALL_OK == grpc_call_cancel(c));
120
121 GPR_ASSERT(GRPC_CALL_OK ==
122 grpc_call_start_invoke(c, f.client_cq, tag(1), tag(2), tag(3), 0));
123 cq_expect_invoke_accepted(v_client, tag(1), GRPC_OP_ERROR);
124 cq_expect_client_metadata_read(v_client, tag(2), NULL);
ctiller2845cad2014-12-15 15:14:12 -0800125 cq_expect_finished_with_status(v_client, tag(3), GRPC_STATUS_CANCELLED, NULL,
126 NULL);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800127 cq_verify(v_client);
128
129 grpc_call_destroy(c);
130
131 cq_verifier_destroy(v_client);
132 end_test(&f);
133 config.tear_down_data(&f);
134}
135
136void grpc_end2end_tests(grpc_end2end_test_config config) {
137 test_cancel_before_invoke(config);
138}