~ubuntu-branches/ubuntu/wily/grpc/wily

« back to all changes in this revision

Viewing changes to test/core/end2end/tests/simple_delayed_request.c

  • Committer: Package Import Robot
  • Author(s): Andrew Pollock
  • Date: 2015-05-07 13:28:11 UTC
  • Revision ID: package-import@ubuntu.com-20150507132811-ybm4hfq73tnvvd2e
Tags: upstream-0.10.0
ImportĀ upstreamĀ versionĀ 0.10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 * Copyright 2015, 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
 
 
39
#include <grpc/byte_buffer.h>
 
40
#include <grpc/support/alloc.h>
 
41
#include <grpc/support/log.h>
 
42
#include <grpc/support/time.h>
 
43
#include <grpc/support/useful.h>
 
44
#include "test/core/end2end/cq_verifier.h"
 
45
 
 
46
enum { TIMEOUT = 200000 };
 
47
 
 
48
static void *tag(gpr_intptr t) { return (void *)t; }
 
49
 
 
50
static gpr_timespec n_seconds_time(int n) {
 
51
  return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(n);
 
52
}
 
53
 
 
54
static gpr_timespec five_seconds_time(void) { return n_seconds_time(5); }
 
55
 
 
56
static void drain_cq(grpc_completion_queue *cq) {
 
57
  grpc_event ev;
 
58
  do {
 
59
    ev = grpc_completion_queue_next(cq, five_seconds_time());
 
60
  } while (ev.type != GRPC_QUEUE_SHUTDOWN);
 
61
}
 
62
 
 
63
static void shutdown_server(grpc_end2end_test_fixture *f) {
 
64
  if (!f->server) return;
 
65
  grpc_server_shutdown_and_notify(f->server, f->cq, tag(1000));
 
66
  GPR_ASSERT(grpc_completion_queue_pluck(f->cq, tag(1000),
 
67
                                         GRPC_TIMEOUT_SECONDS_TO_DEADLINE(5))
 
68
                 .type == GRPC_OP_COMPLETE);
 
69
  grpc_server_destroy(f->server);
 
70
  f->server = NULL;
 
71
}
 
72
 
 
73
static void shutdown_client(grpc_end2end_test_fixture *f) {
 
74
  if (!f->client) return;
 
75
  grpc_channel_destroy(f->client);
 
76
  f->client = NULL;
 
77
}
 
78
 
 
79
static void end_test(grpc_end2end_test_fixture *f) {
 
80
  shutdown_server(f);
 
81
  shutdown_client(f);
 
82
 
 
83
  grpc_completion_queue_shutdown(f->cq);
 
84
  drain_cq(f->cq);
 
85
  grpc_completion_queue_destroy(f->cq);
 
86
}
 
87
 
 
88
static void simple_delayed_request_body(grpc_end2end_test_config config,
 
89
                                        grpc_end2end_test_fixture *f,
 
90
                                        grpc_channel_args *client_args,
 
91
                                        grpc_channel_args *server_args,
 
92
                                        long delay_us) {
 
93
  grpc_call *c;
 
94
  grpc_call *s;
 
95
  gpr_timespec deadline = five_seconds_time();
 
96
  cq_verifier *cqv = cq_verifier_create(f->cq);
 
97
  grpc_op ops[6];
 
98
  grpc_op *op;
 
99
  grpc_metadata_array initial_metadata_recv;
 
100
  grpc_metadata_array trailing_metadata_recv;
 
101
  grpc_metadata_array request_metadata_recv;
 
102
  grpc_call_details call_details;
 
103
  grpc_status_code status;
 
104
  char *details = NULL;
 
105
  size_t details_capacity = 0;
 
106
  int was_cancelled = 2;
 
107
 
 
108
  config.init_client(f, client_args);
 
109
 
 
110
  c = grpc_channel_create_call(f->client, f->cq, "/foo", "foo.test.google.fr",
 
111
                               deadline);
 
112
  GPR_ASSERT(c);
 
113
 
 
114
  grpc_metadata_array_init(&initial_metadata_recv);
 
115
  grpc_metadata_array_init(&trailing_metadata_recv);
 
116
  grpc_metadata_array_init(&request_metadata_recv);
 
117
  grpc_call_details_init(&call_details);
 
118
 
 
119
  op = ops;
 
120
  op->op = GRPC_OP_SEND_INITIAL_METADATA;
 
121
  op->data.send_initial_metadata.count = 0;
 
122
  op->flags = 0;
 
123
  op++;
 
124
  op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
 
125
  op->flags = 0;
 
126
  op++;
 
127
  op->op = GRPC_OP_RECV_INITIAL_METADATA;
 
128
  op->data.recv_initial_metadata = &initial_metadata_recv;
 
129
  op->flags = 0;
 
130
  op++;
 
131
  op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
 
132
  op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
 
133
  op->data.recv_status_on_client.status = &status;
 
134
  op->data.recv_status_on_client.status_details = &details;
 
135
  op->data.recv_status_on_client.status_details_capacity = &details_capacity;
 
136
  op->flags = 0;
 
137
  op++;
 
138
  GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(c, ops, op - ops, tag(1)));
 
139
 
 
140
  config.init_server(f, server_args);
 
141
 
 
142
  GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call(f->server, &s,
 
143
                                                      &call_details,
 
144
                                                      &request_metadata_recv,
 
145
                                                      f->cq, f->cq, tag(101)));
 
146
  cq_expect_completion(cqv, tag(101), 1);
 
147
  cq_verify(cqv);
 
148
 
 
149
  op = ops;
 
150
  op->op = GRPC_OP_SEND_INITIAL_METADATA;
 
151
  op->data.send_initial_metadata.count = 0;
 
152
  op->flags = 0;
 
153
  op++;
 
154
  op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
 
155
  op->data.send_status_from_server.trailing_metadata_count = 0;
 
156
  op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
 
157
  op->data.send_status_from_server.status_details = "xyz";
 
158
  op->flags = 0;
 
159
  op++;
 
160
  op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
 
161
  op->data.recv_close_on_server.cancelled = &was_cancelled;
 
162
  op->flags = 0;
 
163
  op++;
 
164
  GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(s, ops, op - ops, tag(102)));
 
165
 
 
166
  cq_expect_completion(cqv, tag(102), 1);
 
167
  cq_expect_completion(cqv, tag(1), 1);
 
168
  cq_verify(cqv);
 
169
 
 
170
  GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
 
171
  GPR_ASSERT(0 == strcmp(details, "xyz"));
 
172
  GPR_ASSERT(0 == strcmp(call_details.method, "/foo"));
 
173
  GPR_ASSERT(0 == strcmp(call_details.host, "foo.test.google.fr"));
 
174
  GPR_ASSERT(was_cancelled == 1);
 
175
 
 
176
  gpr_free(details);
 
177
  grpc_metadata_array_destroy(&initial_metadata_recv);
 
178
  grpc_metadata_array_destroy(&trailing_metadata_recv);
 
179
  grpc_metadata_array_destroy(&request_metadata_recv);
 
180
  grpc_call_details_destroy(&call_details);
 
181
 
 
182
  grpc_call_destroy(c);
 
183
  grpc_call_destroy(s);
 
184
 
 
185
  cq_verifier_destroy(cqv);
 
186
}
 
187
 
 
188
static void test_simple_delayed_request_short(grpc_end2end_test_config config) {
 
189
  grpc_end2end_test_fixture f;
 
190
 
 
191
  gpr_log(GPR_INFO, "%s/%s", "test_simple_delayed_request_short", config.name);
 
192
  f = config.create_fixture(NULL, NULL);
 
193
  simple_delayed_request_body(config, &f, NULL, NULL, 100000);
 
194
  end_test(&f);
 
195
  config.tear_down_data(&f);
 
196
}
 
197
 
 
198
static void test_simple_delayed_request_long(grpc_end2end_test_config config) {
 
199
  grpc_end2end_test_fixture f;
 
200
 
 
201
  gpr_log(GPR_INFO, "%s/%s", "test_simple_delayed_request_long", config.name);
 
202
  f = config.create_fixture(NULL, NULL);
 
203
  /* This timeout should be longer than a single retry */
 
204
  simple_delayed_request_body(config, &f, NULL, NULL, 1500000);
 
205
  end_test(&f);
 
206
  config.tear_down_data(&f);
 
207
}
 
208
 
 
209
void grpc_end2end_tests(grpc_end2end_test_config config) {
 
210
  if (config.feature_mask & FEATURE_MASK_SUPPORTS_DELAYED_CONNECTION) {
 
211
    test_simple_delayed_request_short(config);
 
212
    test_simple_delayed_request_long(config);
 
213
  }
 
214
}