Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(243)

Side by Side Diff: third_party/grpc/test/core/httpcli/httpcli_test.c

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 *
3 * Copyright 2015-2016, 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 "src/core/httpcli/httpcli.h"
35
36 #include <string.h>
37
38 #include <grpc/grpc.h>
39 #include <grpc/support/alloc.h>
40 #include <grpc/support/log.h>
41 #include <grpc/support/string_util.h>
42 #include <grpc/support/subprocess.h>
43 #include <grpc/support/sync.h>
44 #include "src/core/iomgr/iomgr.h"
45 #include "test/core/util/port.h"
46 #include "test/core/util/test_config.h"
47
48 static int g_done = 0;
49 static grpc_httpcli_context g_context;
50 static gpr_mu *g_mu;
51 static grpc_pollset *g_pollset;
52
53 static gpr_timespec n_seconds_time(int seconds) {
54 return GRPC_TIMEOUT_SECONDS_TO_DEADLINE(seconds);
55 }
56
57 static void on_finish(grpc_exec_ctx *exec_ctx, void *arg,
58 const grpc_httpcli_response *response) {
59 const char *expect =
60 "<html><head><title>Hello world!</title></head>"
61 "<body><p>This is a test</p></body></html>";
62 GPR_ASSERT(arg == (void *)42);
63 GPR_ASSERT(response);
64 GPR_ASSERT(response->status == 200);
65 GPR_ASSERT(response->body_length == strlen(expect));
66 GPR_ASSERT(0 == memcmp(expect, response->body, response->body_length));
67 gpr_mu_lock(g_mu);
68 g_done = 1;
69 grpc_pollset_kick(g_pollset, NULL);
70 gpr_mu_unlock(g_mu);
71 }
72
73 static void test_get(int port) {
74 grpc_httpcli_request req;
75 char *host;
76 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
77
78 g_done = 0;
79 gpr_log(GPR_INFO, "test_get");
80
81 gpr_asprintf(&host, "localhost:%d", port);
82 gpr_log(GPR_INFO, "requesting from %s", host);
83
84 memset(&req, 0, sizeof(req));
85 req.host = host;
86 req.path = "/get";
87 req.handshaker = &grpc_httpcli_plaintext;
88
89 grpc_httpcli_get(&exec_ctx, &g_context, g_pollset, &req, n_seconds_time(15),
90 on_finish, (void *)42);
91 gpr_mu_lock(g_mu);
92 while (!g_done) {
93 grpc_pollset_worker *worker = NULL;
94 grpc_pollset_work(&exec_ctx, g_pollset, &worker,
95 gpr_now(GPR_CLOCK_MONOTONIC), n_seconds_time(20));
96 gpr_mu_unlock(g_mu);
97 grpc_exec_ctx_finish(&exec_ctx);
98 gpr_mu_lock(g_mu);
99 }
100 gpr_mu_unlock(g_mu);
101 gpr_free(host);
102 }
103
104 static void test_post(int port) {
105 grpc_httpcli_request req;
106 char *host;
107 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
108
109 g_done = 0;
110 gpr_log(GPR_INFO, "test_post");
111
112 gpr_asprintf(&host, "localhost:%d", port);
113 gpr_log(GPR_INFO, "posting to %s", host);
114
115 memset(&req, 0, sizeof(req));
116 req.host = host;
117 req.path = "/post";
118 req.handshaker = &grpc_httpcli_plaintext;
119
120 grpc_httpcli_post(&exec_ctx, &g_context, g_pollset, &req, "hello", 5,
121 n_seconds_time(15), on_finish, (void *)42);
122 gpr_mu_lock(g_mu);
123 while (!g_done) {
124 grpc_pollset_worker *worker = NULL;
125 grpc_pollset_work(&exec_ctx, g_pollset, &worker,
126 gpr_now(GPR_CLOCK_MONOTONIC), n_seconds_time(20));
127 gpr_mu_unlock(g_mu);
128 grpc_exec_ctx_finish(&exec_ctx);
129 gpr_mu_lock(g_mu);
130 }
131 gpr_mu_unlock(g_mu);
132 gpr_free(host);
133 }
134
135 static void destroy_pollset(grpc_exec_ctx *exec_ctx, void *p, bool success) {
136 grpc_pollset_destroy(p);
137 }
138
139 int main(int argc, char **argv) {
140 grpc_closure destroyed;
141 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
142 gpr_subprocess *server;
143 char *me = argv[0];
144 char *lslash = strrchr(me, '/');
145 char *args[4];
146 int port = grpc_pick_unused_port_or_die();
147
148 GPR_ASSERT(argc <= 2);
149 if (argc == 2) {
150 args[0] = gpr_strdup(argv[1]);
151 } else {
152 /* figure out where we are */
153 char *root;
154 if (lslash) {
155 root = gpr_malloc((size_t)(lslash - me + 1));
156 memcpy(root, me, (size_t)(lslash - me));
157 root[lslash - me] = 0;
158 } else {
159 root = gpr_strdup(".");
160 }
161 gpr_asprintf(&args[0], "%s/../../test/core/httpcli/test_server.py", root);
162 gpr_free(root);
163 }
164
165 /* start the server */
166 args[1] = "--port";
167 gpr_asprintf(&args[2], "%d", port);
168 server = gpr_subprocess_create(3, (const char **)args);
169 GPR_ASSERT(server);
170 gpr_free(args[0]);
171 gpr_free(args[2]);
172
173 gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
174 gpr_time_from_seconds(5, GPR_TIMESPAN)));
175
176 grpc_test_init(argc, argv);
177 grpc_init();
178 grpc_httpcli_context_init(&g_context);
179 g_pollset = gpr_malloc(grpc_pollset_size());
180 grpc_pollset_init(g_pollset, &g_mu);
181
182 test_get(port);
183 test_post(port);
184
185 grpc_httpcli_context_destroy(&g_context);
186 grpc_closure_init(&destroyed, destroy_pollset, g_pollset);
187 grpc_pollset_shutdown(&exec_ctx, g_pollset, &destroyed);
188 grpc_exec_ctx_finish(&exec_ctx);
189 grpc_shutdown();
190
191 gpr_free(g_pollset);
192
193 gpr_subprocess_destroy(server);
194
195 return 0;
196 }
OLDNEW
« no previous file with comments | « third_party/grpc/test/core/httpcli/format_request_test.c ('k') | third_party/grpc/test/core/httpcli/httpscli_test.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698