aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/util/port_posix.c
diff options
context:
space:
mode:
authorGravatar Craig Tiller <craig.tiller@gmail.com>2015-04-09 15:51:41 -0700
committerGravatar Craig Tiller <craig.tiller@gmail.com>2015-04-09 15:51:41 -0700
commite9a6eb7332eef19805e1177e813dfd301096743d (patch)
tree74342a43590cf30c0ceaf98cb8c60e3f9b4db7bc /test/core/util/port_posix.c
parent176f921b9b01bfd592298b0da4fb9f1b2b26e1f3 (diff)
Allow RunScenarios to spawn in-process workers
This allows us to get back to single binary tests where appropriate, which will help in-depth profiling efforts. I've built this atop my smoke_test changes as they inspired me to get this done.
Diffstat (limited to 'test/core/util/port_posix.c')
-rw-r--r--test/core/util/port_posix.c33
1 files changed, 32 insertions, 1 deletions
diff --git a/test/core/util/port_posix.c b/test/core/util/port_posix.c
index 7467c2f9ea..726ee3bd6c 100644
--- a/test/core/util/port_posix.c
+++ b/test/core/util/port_posix.c
@@ -44,10 +44,37 @@
#include <string.h>
#include <unistd.h>
+#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#define NUM_RANDOM_PORTS_TO_PICK 100
+static int *chosen_ports = NULL;
+static size_t num_chosen_ports = 0;
+
+static int has_port_been_chosen(int port) {
+ size_t i;
+ for (i = 0; i < num_chosen_ports; i++) {
+ if (chosen_ports[i] == port) {
+ return 1;
+ }
+ }
+ return 0;
+}
+
+static void free_chosen_ports() {
+ gpr_free(chosen_ports);
+}
+
+static void chose_port(int port) {
+ if (chosen_ports == NULL) {
+ atexit(free_chosen_ports);
+ }
+ num_chosen_ports++;
+ chosen_ports = gpr_realloc(chosen_ports, sizeof(int) * num_chosen_ports);
+ chosen_ports[num_chosen_ports - 1] = port;
+}
+
static int is_port_available(int *port, int is_tcp) {
const int proto = is_tcp ? IPPROTO_TCP : 0;
const int fd = socket(AF_INET, is_tcp ? SOCK_STREAM : SOCK_DGRAM, proto);
@@ -127,6 +154,10 @@ int grpc_pick_unused_port(void) {
port = 0;
}
+ if (has_port_been_chosen(port)) {
+ continue;
+ }
+
if (!is_port_available(&port, is_tcp)) {
continue;
}
@@ -142,7 +173,7 @@ int grpc_pick_unused_port(void) {
/* TODO(ctiller): consider caching this port in some structure, to avoid
handing it out again */
-
+ chose_port(port);
return port;
}