16
16
*
17
17
*/
18
18
19
+ #include < vector>
20
+
19
21
#include < grpc/grpc.h>
20
22
#include < grpc/grpc_security.h>
23
+ #include < grpc/impl/codegen/grpc_types.h>
21
24
#include < grpc/support/alloc.h>
22
25
#include < grpc/support/log.h>
23
26
36
39
typedef struct test_fixture {
37
40
const char * name;
38
41
void (*add_server_port)(grpc_server* server, const char * addr);
39
- grpc_channel* (*create_channel)(const char * addr);
42
+ // Have the creds here so all the channels will share the same one to enabled
43
+ // subchannel sharing if needed.
44
+ grpc_channel_credentials* creds;
40
45
} test_fixture;
41
46
42
- /* TODO(yashykt): When our macos testing infrastructure becomes good enough, we
43
- * wouldn't need to reduce the number of connections on MacOS */
44
- #ifdef __APPLE__
45
47
#define NUM_CONNECTIONS 100
46
- #else
47
- #define NUM_CONNECTIONS 1000
48
- #endif /* __APPLE__ */
49
48
50
49
typedef struct {
51
50
grpc_server* server;
@@ -61,8 +60,31 @@ static void server_thread_func(void* args) {
61
60
GPR_ASSERT (ev.success == true );
62
61
}
63
62
64
- static void run_test (const test_fixture* fixture) {
65
- gpr_log (GPR_INFO, " TEST: %s" , fixture->name );
63
+ static grpc_channel* create_test_channel (const char * addr,
64
+ grpc_channel_credentials* creds,
65
+ bool share_subchannel) {
66
+ grpc_channel* channel = nullptr ;
67
+ std::vector<grpc_arg> args;
68
+ args.push_back (grpc_channel_arg_integer_create (
69
+ const_cast <char *>(GRPC_ARG_USE_LOCAL_SUBCHANNEL_POOL),
70
+ !share_subchannel));
71
+ if (creds != nullptr ) {
72
+ args.push_back (grpc_channel_arg_string_create (
73
+ const_cast <char *>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
74
+ const_cast <char *>(" foo.test.google.fr" )));
75
+ }
76
+ grpc_channel_args channel_args = {args.size (), args.data ()};
77
+ if (creds != nullptr ) {
78
+ channel = grpc_secure_channel_create (creds, addr, &channel_args, nullptr );
79
+ } else {
80
+ channel = grpc_insecure_channel_create (addr, &channel_args, nullptr );
81
+ }
82
+ return channel;
83
+ }
84
+
85
+ static void run_test (const test_fixture* fixture, bool share_subchannel) {
86
+ gpr_log (GPR_INFO, " TEST: %s sharing subchannel: %d" , fixture->name ,
87
+ share_subchannel);
66
88
67
89
grpc_init ();
68
90
@@ -83,7 +105,8 @@ static void run_test(const test_fixture* fixture) {
83
105
grpc_completion_queue* cq = grpc_completion_queue_create_for_next (nullptr );
84
106
grpc_channel* channels[NUM_CONNECTIONS];
85
107
for (size_t i = 0 ; i < NUM_CONNECTIONS; i++) {
86
- channels[i] = fixture->create_channel (addr.c_str ());
108
+ channels[i] =
109
+ create_test_channel (addr.c_str (), fixture->creds , share_subchannel);
87
110
88
111
gpr_timespec connect_deadline = grpc_timeout_seconds_to_deadline (30 );
89
112
grpc_connectivity_state state;
@@ -132,16 +155,6 @@ static void insecure_test_add_port(grpc_server* server, const char* addr) {
132
155
grpc_server_add_insecure_http2_port (server, addr);
133
156
}
134
157
135
- static grpc_channel* insecure_test_create_channel (const char * addr) {
136
- return grpc_insecure_channel_create (addr, nullptr , nullptr );
137
- }
138
-
139
- static const test_fixture insecure_test = {
140
- " insecure" ,
141
- insecure_test_add_port,
142
- insecure_test_create_channel,
143
- };
144
-
145
158
static void secure_test_add_port (grpc_server* server, const char * addr) {
146
159
grpc_slice cert_slice, key_slice;
147
160
GPR_ASSERT (GRPC_LOG_IF_ERROR (
@@ -161,7 +174,18 @@ static void secure_test_add_port(grpc_server* server, const char* addr) {
161
174
grpc_server_credentials_release (ssl_creds);
162
175
}
163
176
164
- static grpc_channel* secure_test_create_channel (const char * addr) {
177
+ int main (int argc, char ** argv) {
178
+ grpc::testing::TestEnvironment env (argc, argv);
179
+
180
+ const test_fixture insecure_test = {
181
+ " insecure" ,
182
+ insecure_test_add_port,
183
+ nullptr ,
184
+ };
185
+
186
+ run_test (&insecure_test, /* share_subchannel=*/ true );
187
+ run_test (&insecure_test, /* share_subchannel=*/ false );
188
+
165
189
grpc_slice ca_slice;
166
190
GPR_ASSERT (GRPC_LOG_IF_ERROR (" load_file" ,
167
191
grpc_load_file (CA_CERT_PATH, 1 , &ca_slice)));
@@ -170,31 +194,12 @@ static grpc_channel* secure_test_create_channel(const char* addr) {
170
194
grpc_channel_credentials* ssl_creds =
171
195
grpc_ssl_credentials_create (test_root_cert, nullptr , nullptr , nullptr );
172
196
grpc_slice_unref (ca_slice);
173
- grpc_arg ssl_name_override = {
174
- GRPC_ARG_STRING,
175
- const_cast <char *>(GRPC_SSL_TARGET_NAME_OVERRIDE_ARG),
176
- {const_cast <char *>(" foo.test.google.fr" )}};
177
- grpc_channel_args* new_client_args =
178
- grpc_channel_args_copy_and_add (nullptr , &ssl_name_override, 1 );
179
- grpc_channel* channel =
180
- grpc_secure_channel_create (ssl_creds, addr, new_client_args, nullptr );
181
- {
182
- grpc_core::ExecCtx exec_ctx;
183
- grpc_channel_args_destroy (new_client_args);
184
- }
197
+ const test_fixture secure_test = {
198
+ " secure" ,
199
+ secure_test_add_port,
200
+ ssl_creds,
201
+ };
202
+ run_test (&secure_test, /* share_subchannel=*/ true );
203
+ run_test (&secure_test, /* share_subchannel=*/ false );
185
204
grpc_channel_credentials_release (ssl_creds);
186
- return channel;
187
- }
188
-
189
- static const test_fixture secure_test = {
190
- " secure" ,
191
- secure_test_add_port,
192
- secure_test_create_channel,
193
- };
194
-
195
- int main (int argc, char ** argv) {
196
- grpc::testing::TestEnvironment env (argc, argv);
197
-
198
- run_test (&insecure_test);
199
- run_test (&secure_test);
200
205
}
0 commit comments