mirror of https://github.com/grpc/grpc.git
The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#)
https://grpc.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
3.5 KiB
133 lines
3.5 KiB
8 years ago
|
/*
|
||
|
*
|
||
8 years ago
|
* Copyright 2015 gRPC authors.
|
||
8 years ago
|
*
|
||
8 years ago
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
8 years ago
|
*
|
||
8 years ago
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
8 years ago
|
*
|
||
8 years ago
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
8 years ago
|
*
|
||
|
*/
|
||
|
|
||
|
#include "src/core/lib/iomgr/port.h"
|
||
|
#include "test/core/util/test_config.h"
|
||
8 years ago
|
#if defined(GRPC_TEST_PICK_PORT)
|
||
8 years ago
|
|
||
8 years ago
|
#include "test/core/util/port.h"
|
||
|
|
||
|
#include <stdbool.h>
|
||
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
|
||
|
#include <grpc/grpc.h>
|
||
8 years ago
|
#include <grpc/support/alloc.h>
|
||
8 years ago
|
#include <grpc/support/log.h>
|
||
8 years ago
|
#include <grpc/support/string_util.h>
|
||
8 years ago
|
|
||
8 years ago
|
#include "src/core/lib/http/httpcli.h"
|
||
|
#include "src/core/lib/iomgr/resolve_address.h"
|
||
|
#include "src/core/lib/iomgr/sockaddr_utils.h"
|
||
8 years ago
|
#include "test/core/util/port_server_client.h"
|
||
|
|
||
7 years ago
|
static int* chosen_ports = NULL;
|
||
8 years ago
|
static size_t num_chosen_ports = 0;
|
||
|
|
||
|
static int free_chosen_port(int port) {
|
||
|
size_t i;
|
||
|
int found = 0;
|
||
|
size_t found_at = 0;
|
||
|
/* Find the port and erase it from the list, then tell the server it can be
|
||
|
freed. */
|
||
|
for (i = 0; i < num_chosen_ports; i++) {
|
||
|
if (chosen_ports[i] == port) {
|
||
|
GPR_ASSERT(found == 0);
|
||
|
found = 1;
|
||
|
found_at = i;
|
||
|
}
|
||
|
}
|
||
|
if (found) {
|
||
|
chosen_ports[found_at] = chosen_ports[num_chosen_ports - 1];
|
||
|
num_chosen_ports--;
|
||
8 years ago
|
grpc_free_port_using_server(port);
|
||
8 years ago
|
}
|
||
|
return found;
|
||
|
}
|
||
|
|
||
|
static void free_chosen_ports(void) {
|
||
8 years ago
|
size_t i;
|
||
8 years ago
|
grpc_init();
|
||
8 years ago
|
for (i = 0; i < num_chosen_ports; i++) {
|
||
|
grpc_free_port_using_server(chosen_ports[i]);
|
||
8 years ago
|
}
|
||
8 years ago
|
grpc_shutdown();
|
||
8 years ago
|
gpr_free(chosen_ports);
|
||
|
}
|
||
|
|
||
|
static void chose_port(int port) {
|
||
|
if (chosen_ports == NULL) {
|
||
|
atexit(free_chosen_ports);
|
||
|
}
|
||
|
num_chosen_ports++;
|
||
7 years ago
|
chosen_ports =
|
||
7 years ago
|
(int*)gpr_realloc(chosen_ports, sizeof(int) * num_chosen_ports);
|
||
8 years ago
|
chosen_ports[num_chosen_ports - 1] = port;
|
||
|
}
|
||
8 years ago
|
|
||
7 years ago
|
static int grpc_pick_unused_port_impl(void) {
|
||
8 years ago
|
int port = grpc_pick_port_using_server();
|
||
8 years ago
|
if (port != 0) {
|
||
|
chose_port(port);
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
return port;
|
||
8 years ago
|
}
|
||
|
|
||
7 years ago
|
static int grpc_pick_unused_port_or_die_impl(void) {
|
||
8 years ago
|
int port = grpc_pick_unused_port();
|
||
8 years ago
|
if (port == 0) {
|
||
|
fprintf(stderr,
|
||
|
"gRPC tests require a helper port server to allocate ports used \n"
|
||
|
"during the test.\n\n"
|
||
|
"This server is not currently running.\n\n"
|
||
|
"To start it, run tools/run_tests/start_port_server.py\n\n");
|
||
|
exit(1);
|
||
|
}
|
||
8 years ago
|
return port;
|
||
|
}
|
||
|
|
||
7 years ago
|
static void grpc_recycle_unused_port_impl(int port) {
|
||
|
GPR_ASSERT(free_chosen_port(port));
|
||
|
}
|
||
|
|
||
7 years ago
|
static grpc_pick_port_functions g_pick_port_functions = {
|
||
|
grpc_pick_unused_port_impl, grpc_pick_unused_port_or_die_impl,
|
||
|
grpc_recycle_unused_port_impl};
|
||
|
|
||
|
int grpc_pick_unused_port(void) {
|
||
|
return g_pick_port_functions.pick_unused_port_fn();
|
||
|
}
|
||
|
|
||
|
int grpc_pick_unused_port_or_die(void) {
|
||
|
return g_pick_port_functions.pick_unused_port_or_die_fn();
|
||
|
}
|
||
|
|
||
|
void grpc_recycle_unused_port(int port) {
|
||
|
g_pick_port_functions.recycle_unused_port_fn(port);
|
||
|
}
|
||
|
|
||
|
void grpc_set_pick_port_functions(grpc_pick_port_functions functions) {
|
||
|
GPR_ASSERT(functions.pick_unused_port_fn != NULL);
|
||
|
GPR_ASSERT(functions.pick_unused_port_or_die_fn != NULL);
|
||
|
GPR_ASSERT(functions.recycle_unused_port_fn != NULL);
|
||
|
g_pick_port_functions = functions;
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
#endif /* GRPC_TEST_PICK_PORT */
|