aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/util
diff options
context:
space:
mode:
Diffstat (limited to 'test/core/util')
-rw-r--r--test/core/util/memory_counters.c117
-rw-r--r--test/core/util/memory_counters.h48
-rw-r--r--test/core/util/mock_endpoint.c124
-rw-r--r--test/core/util/mock_endpoint.h43
-rw-r--r--test/core/util/one_corpus_entry_fuzzer.c46
-rw-r--r--test/core/util/passthru_endpoint.c158
-rw-r--r--test/core/util/passthru_endpoint.h42
-rw-r--r--test/core/util/port_posix.c173
-rw-r--r--test/core/util/port_server_client.c215
-rw-r--r--test/core/util/port_server_client.h42
-rw-r--r--test/core/util/port_windows.c100
-rw-r--r--test/core/util/reconnect_server.c17
-rw-r--r--test/core/util/reconnect_server.h3
-rw-r--r--test/core/util/test_config.c6
-rw-r--r--test/core/util/test_config.h2
-rw-r--r--test/core/util/test_tcp_server.c8
-rw-r--r--test/core/util/test_tcp_server.h4
17 files changed, 885 insertions, 263 deletions
diff --git a/test/core/util/memory_counters.c b/test/core/util/memory_counters.c
new file mode 100644
index 0000000000..bebe94e582
--- /dev/null
+++ b/test/core/util/memory_counters.c
@@ -0,0 +1,117 @@
+/*
+ *
+ * Copyright 2016, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <stdint.h>
+#include <string.h>
+
+#include <grpc/support/alloc.h>
+#include <grpc/support/sync.h>
+
+#include "test/core/util/memory_counters.h"
+
+static gpr_mu g_memory_mutex;
+static struct grpc_memory_counters g_memory_counters;
+static gpr_allocation_functions g_old_allocs;
+
+static void *guard_malloc(size_t size);
+static void *guard_realloc(void *vptr, size_t size);
+static void guard_free(void *vptr);
+
+static void *guard_malloc(size_t size) {
+ size_t *ptr;
+ if (!size) return NULL;
+ gpr_mu_lock(&g_memory_mutex);
+ g_memory_counters.total_size_absolute += size;
+ g_memory_counters.total_size_relative += size;
+ g_memory_counters.total_allocs_absolute++;
+ g_memory_counters.total_allocs_relative++;
+ gpr_mu_unlock(&g_memory_mutex);
+ ptr = g_old_allocs.malloc_fn(size + sizeof(size));
+ *ptr++ = size;
+ return ptr;
+}
+
+static void *guard_realloc(void *vptr, size_t size) {
+ size_t *ptr = vptr;
+ if (vptr == NULL) {
+ return guard_malloc(size);
+ }
+ if (size == 0) {
+ guard_free(vptr);
+ return NULL;
+ }
+ --ptr;
+ gpr_mu_lock(&g_memory_mutex);
+ g_memory_counters.total_size_absolute += size;
+ g_memory_counters.total_size_relative -= *ptr;
+ g_memory_counters.total_size_relative += size;
+ g_memory_counters.total_allocs_absolute++;
+ gpr_mu_unlock(&g_memory_mutex);
+ ptr = g_old_allocs.realloc_fn(ptr, size + sizeof(size));
+ *ptr++ = size;
+ return ptr;
+}
+
+static void guard_free(void *vptr) {
+ size_t *ptr = vptr;
+ if (!vptr) return;
+ --ptr;
+ gpr_mu_lock(&g_memory_mutex);
+ g_memory_counters.total_size_relative -= *ptr;
+ g_memory_counters.total_allocs_relative--;
+ gpr_mu_unlock(&g_memory_mutex);
+ g_old_allocs.free_fn(ptr);
+}
+
+struct gpr_allocation_functions g_guard_allocs = {guard_malloc, guard_realloc,
+ guard_free};
+
+void grpc_memory_counters_init() {
+ memset(&g_memory_counters, 0, sizeof(g_memory_counters));
+ gpr_mu_init(&g_memory_mutex);
+ g_old_allocs = gpr_get_allocation_functions();
+ gpr_set_allocation_functions(g_guard_allocs);
+}
+
+void grpc_memory_counters_destroy() {
+ gpr_set_allocation_functions(g_old_allocs);
+ gpr_mu_destroy(&g_memory_mutex);
+}
+
+struct grpc_memory_counters grpc_memory_counters_snapshot() {
+ struct grpc_memory_counters counters;
+ gpr_mu_lock(&g_memory_mutex);
+ counters = g_memory_counters;
+ gpr_mu_unlock(&g_memory_mutex);
+ return counters;
+}
diff --git a/test/core/util/memory_counters.h b/test/core/util/memory_counters.h
new file mode 100644
index 0000000000..f332816501
--- /dev/null
+++ b/test/core/util/memory_counters.h
@@ -0,0 +1,48 @@
+/*
+ *
+ * Copyright 2016, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef GRPC_TEST_CORE_UTIL_MEMORY_COUNTERS_H
+#define GRPC_TEST_CORE_UTIL_MEMORY_COUNTERS_H
+
+struct grpc_memory_counters {
+ size_t total_size_relative;
+ size_t total_size_absolute;
+ size_t total_allocs_relative;
+ size_t total_allocs_absolute;
+};
+
+void grpc_memory_counters_init();
+void grpc_memory_counters_destroy();
+struct grpc_memory_counters grpc_memory_counters_snapshot();
+
+#endif
diff --git a/test/core/util/mock_endpoint.c b/test/core/util/mock_endpoint.c
new file mode 100644
index 0000000000..7768413095
--- /dev/null
+++ b/test/core/util/mock_endpoint.c
@@ -0,0 +1,124 @@
+/*
+ *
+ * Copyright 2016, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "test/core/util/mock_endpoint.h"
+
+#include <grpc/support/alloc.h>
+#include <grpc/support/string_util.h>
+
+typedef struct grpc_mock_endpoint {
+ grpc_endpoint base;
+ gpr_mu mu;
+ void (*on_write)(gpr_slice slice);
+ gpr_slice_buffer read_buffer;
+ gpr_slice_buffer *on_read_out;
+ grpc_closure *on_read;
+} grpc_mock_endpoint;
+
+static void me_read(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
+ gpr_slice_buffer *slices, grpc_closure *cb) {
+ grpc_mock_endpoint *m = (grpc_mock_endpoint *)ep;
+ gpr_mu_lock(&m->mu);
+ if (m->read_buffer.count > 0) {
+ gpr_slice_buffer_swap(&m->read_buffer, slices);
+ grpc_exec_ctx_enqueue(exec_ctx, cb, true, NULL);
+ } else {
+ m->on_read = cb;
+ m->on_read_out = slices;
+ }
+ gpr_mu_unlock(&m->mu);
+}
+
+static void me_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
+ gpr_slice_buffer *slices, grpc_closure *cb) {
+ grpc_mock_endpoint *m = (grpc_mock_endpoint *)ep;
+ for (size_t i = 0; i < slices->count; i++) {
+ m->on_write(slices->slices[i]);
+ }
+ grpc_exec_ctx_enqueue(exec_ctx, cb, true, NULL);
+}
+
+static void me_add_to_pollset(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
+ grpc_pollset *pollset) {}
+
+static void me_add_to_pollset_set(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
+ grpc_pollset_set *pollset) {}
+
+static void me_shutdown(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) {
+ grpc_mock_endpoint *m = (grpc_mock_endpoint *)ep;
+ gpr_mu_lock(&m->mu);
+ if (m->on_read) {
+ grpc_exec_ctx_enqueue(exec_ctx, m->on_read, false, NULL);
+ m->on_read = NULL;
+ }
+ gpr_mu_unlock(&m->mu);
+}
+
+static void me_destroy(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) {
+ grpc_mock_endpoint *m = (grpc_mock_endpoint *)ep;
+ gpr_slice_buffer_destroy(&m->read_buffer);
+ gpr_free(m);
+}
+
+static char *me_get_peer(grpc_endpoint *ep) {
+ return gpr_strdup("fake:mock_endpoint");
+}
+
+static const grpc_endpoint_vtable vtable = {
+ me_read, me_write, me_add_to_pollset, me_add_to_pollset_set,
+ me_shutdown, me_destroy, me_get_peer,
+};
+
+grpc_endpoint *grpc_mock_endpoint_create(void (*on_write)(gpr_slice slice)) {
+ grpc_mock_endpoint *m = gpr_malloc(sizeof(*m));
+ m->base.vtable = &vtable;
+ gpr_slice_buffer_init(&m->read_buffer);
+ gpr_mu_init(&m->mu);
+ m->on_write = on_write;
+ m->on_read = NULL;
+ return &m->base;
+}
+
+void grpc_mock_endpoint_put_read(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
+ gpr_slice slice) {
+ grpc_mock_endpoint *m = (grpc_mock_endpoint *)ep;
+ gpr_mu_lock(&m->mu);
+ if (m->on_read != NULL) {
+ gpr_slice_buffer_add(m->on_read_out, slice);
+ grpc_exec_ctx_enqueue(exec_ctx, m->on_read, true, NULL);
+ m->on_read = NULL;
+ } else {
+ gpr_slice_buffer_add(&m->read_buffer, slice);
+ }
+ gpr_mu_unlock(&m->mu);
+}
diff --git a/test/core/util/mock_endpoint.h b/test/core/util/mock_endpoint.h
new file mode 100644
index 0000000000..051af9866b
--- /dev/null
+++ b/test/core/util/mock_endpoint.h
@@ -0,0 +1,43 @@
+/*
+ *
+ * Copyright 2016, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef MOCK_ENDPOINT_H
+#define MOCK_ENDPOINT_H
+
+#include "src/core/lib/iomgr/endpoint.h"
+
+grpc_endpoint *grpc_mock_endpoint_create(void (*on_write)(gpr_slice slice));
+void grpc_mock_endpoint_put_read(grpc_exec_ctx *exec_ctx,
+ grpc_endpoint *mock_endpoint, gpr_slice slice);
+
+#endif
diff --git a/test/core/util/one_corpus_entry_fuzzer.c b/test/core/util/one_corpus_entry_fuzzer.c
new file mode 100644
index 0000000000..41f9558211
--- /dev/null
+++ b/test/core/util/one_corpus_entry_fuzzer.c
@@ -0,0 +1,46 @@
+/*
+ *
+ * Copyright 2016, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <grpc/support/log.h>
+#include "src/core/lib/support/load_file.h"
+
+extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
+
+int main(int argc, char **argv) {
+ int ok = 0;
+ gpr_slice buffer = gpr_load_file(argv[1], 0, &ok);
+ GPR_ASSERT(ok);
+ LLVMFuzzerTestOneInput(GPR_SLICE_START_PTR(buffer), GPR_SLICE_LENGTH(buffer));
+ gpr_slice_unref(buffer);
+ return 0;
+}
diff --git a/test/core/util/passthru_endpoint.c b/test/core/util/passthru_endpoint.c
new file mode 100644
index 0000000000..168ae59e91
--- /dev/null
+++ b/test/core/util/passthru_endpoint.c
@@ -0,0 +1,158 @@
+/*
+ *
+ * Copyright 2016, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "test/core/util/passthru_endpoint.h"
+
+#include <grpc/support/alloc.h>
+#include <grpc/support/string_util.h>
+
+typedef struct passthru_endpoint passthru_endpoint;
+
+typedef struct {
+ grpc_endpoint base;
+ passthru_endpoint *parent;
+ gpr_slice_buffer read_buffer;
+ gpr_slice_buffer *on_read_out;
+ grpc_closure *on_read;
+} half;
+
+struct passthru_endpoint {
+ gpr_mu mu;
+ int halves;
+ bool shutdown;
+ half client;
+ half server;
+};
+
+static void me_read(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
+ gpr_slice_buffer *slices, grpc_closure *cb) {
+ half *m = (half *)ep;
+ gpr_mu_lock(&m->parent->mu);
+ if (m->parent->shutdown) {
+ grpc_exec_ctx_enqueue(exec_ctx, cb, false, NULL);
+ } else if (m->read_buffer.count > 0) {
+ gpr_slice_buffer_swap(&m->read_buffer, slices);
+ grpc_exec_ctx_enqueue(exec_ctx, cb, true, NULL);
+ } else {
+ m->on_read = cb;
+ m->on_read_out = slices;
+ }
+ gpr_mu_unlock(&m->parent->mu);
+}
+
+static half *other_half(half *h) {
+ if (h == &h->parent->client) return &h->parent->server;
+ return &h->parent->client;
+}
+
+static void me_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
+ gpr_slice_buffer *slices, grpc_closure *cb) {
+ half *m = other_half((half *)ep);
+ gpr_mu_lock(&m->parent->mu);
+ bool ok = true;
+ if (m->parent->shutdown) {
+ ok = false;
+ } else if (m->on_read != NULL) {
+ gpr_slice_buffer_addn(m->on_read_out, slices->slices, slices->count);
+ grpc_exec_ctx_enqueue(exec_ctx, m->on_read, true, NULL);
+ m->on_read = NULL;
+ } else {
+ gpr_slice_buffer_addn(&m->read_buffer, slices->slices, slices->count);
+ }
+ gpr_mu_unlock(&m->parent->mu);
+ grpc_exec_ctx_enqueue(exec_ctx, cb, ok, NULL);
+}
+
+static void me_add_to_pollset(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
+ grpc_pollset *pollset) {}
+
+static void me_add_to_pollset_set(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
+ grpc_pollset_set *pollset) {}
+
+static void me_shutdown(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) {
+ half *m = (half *)ep;
+ gpr_mu_lock(&m->parent->mu);
+ m->parent->shutdown = true;
+ if (m->on_read) {
+ grpc_exec_ctx_enqueue(exec_ctx, m->on_read, false, NULL);
+ m->on_read = NULL;
+ }
+ m = other_half(m);
+ if (m->on_read) {
+ grpc_exec_ctx_enqueue(exec_ctx, m->on_read, false, NULL);
+ m->on_read = NULL;
+ }
+ gpr_mu_unlock(&m->parent->mu);
+}
+
+static void me_destroy(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) {
+ passthru_endpoint *p = ((half *)ep)->parent;
+ gpr_mu_lock(&p->mu);
+ if (0 == --p->halves) {
+ gpr_mu_unlock(&p->mu);
+ gpr_mu_destroy(&p->mu);
+ gpr_slice_buffer_destroy(&p->client.read_buffer);
+ gpr_slice_buffer_destroy(&p->server.read_buffer);
+ gpr_free(p);
+ } else {
+ gpr_mu_unlock(&p->mu);
+ }
+}
+
+static char *me_get_peer(grpc_endpoint *ep) {
+ return gpr_strdup("fake:mock_endpoint");
+}
+
+static const grpc_endpoint_vtable vtable = {
+ me_read, me_write, me_add_to_pollset, me_add_to_pollset_set,
+ me_shutdown, me_destroy, me_get_peer,
+};
+
+static void half_init(half *m, passthru_endpoint *parent) {
+ m->base.vtable = &vtable;
+ m->parent = parent;
+ gpr_slice_buffer_init(&m->read_buffer);
+ m->on_read = NULL;
+}
+
+void grpc_passthru_endpoint_create(grpc_endpoint **client,
+ grpc_endpoint **server) {
+ passthru_endpoint *m = gpr_malloc(sizeof(*m));
+ m->halves = 2;
+ m->shutdown = 0;
+ half_init(&m->client, m);
+ half_init(&m->server, m);
+ gpr_mu_init(&m->mu);
+ *client = &m->client.base;
+ *server = &m->server.base;
+}
diff --git a/test/core/util/passthru_endpoint.h b/test/core/util/passthru_endpoint.h
new file mode 100644
index 0000000000..aa1d3a1763
--- /dev/null
+++ b/test/core/util/passthru_endpoint.h
@@ -0,0 +1,42 @@
+/*
+ *
+ * Copyright 2016, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef MOCK_ENDPOINT_H
+#define MOCK_ENDPOINT_H
+
+#include "src/core/lib/iomgr/endpoint.h"
+
+void grpc_passthru_endpoint_create(grpc_endpoint **client,
+ grpc_endpoint **server);
+
+#endif
diff --git a/test/core/util/port_posix.c b/test/core/util/port_posix.c
index c4bd00c1ba..eabd62fafc 100644
--- a/test/core/util/port_posix.c
+++ b/test/core/util/port_posix.c
@@ -1,6 +1,6 @@
/*
*
- * Copyright 2015-2016, Google Inc.
+ * Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -38,7 +38,6 @@
#include "test/core/util/port.h"
#include <errno.h>
-#include <math.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
@@ -50,8 +49,9 @@
#include <grpc/support/log.h>
#include <grpc/support/string_util.h>
-#include "src/core/httpcli/httpcli.h"
-#include "src/core/support/env.h"
+#include "src/core/lib/http/httpcli.h"
+#include "src/core/lib/support/env.h"
+#include "test/core/util/port_server_client.h"
#define NUM_RANDOM_PORTS_TO_PICK 100
@@ -68,76 +68,12 @@ static int has_port_been_chosen(int port) {
return 0;
}
-typedef struct freereq {
- gpr_mu *mu;
- grpc_pollset *pollset;
- int done;
-} freereq;
-
-static void destroy_pollset_and_shutdown(grpc_exec_ctx *exec_ctx, void *p,
- bool success) {
- grpc_pollset_destroy(p);
- gpr_free(p);
- grpc_shutdown();
-}
-
-static void freed_port_from_server(grpc_exec_ctx *exec_ctx, void *arg,
- const grpc_httpcli_response *response) {
- freereq *pr = arg;
- gpr_mu_lock(pr->mu);
- pr->done = 1;
- grpc_pollset_kick(pr->pollset, NULL);
- gpr_mu_unlock(pr->mu);
-}
-
-static void free_port_using_server(char *server, int port) {
- grpc_httpcli_context context;
- grpc_httpcli_request req;
- freereq pr;
- char *path;
- grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- grpc_closure *shutdown_closure;
-
- grpc_init();
-
- memset(&pr, 0, sizeof(pr));
- memset(&req, 0, sizeof(req));
-
- pr.pollset = gpr_malloc(grpc_pollset_size());
- grpc_pollset_init(pr.pollset, &pr.mu);
- shutdown_closure =
- grpc_closure_create(destroy_pollset_and_shutdown, pr.pollset);
-
- req.host = server;
- gpr_asprintf(&path, "/drop/%d", port);
- req.path = path;
-
- grpc_httpcli_context_init(&context);
- grpc_httpcli_get(&exec_ctx, &context, pr.pollset, &req,
- GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10), freed_port_from_server,
- &pr);
- gpr_mu_lock(pr.mu);
- while (!pr.done) {
- grpc_pollset_worker *worker = NULL;
- grpc_pollset_work(&exec_ctx, pr.pollset, &worker,
- gpr_now(GPR_CLOCK_MONOTONIC),
- GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1));
- }
- gpr_mu_unlock(pr.mu);
-
- grpc_httpcli_context_destroy(&context);
- grpc_exec_ctx_finish(&exec_ctx);
- grpc_pollset_shutdown(&exec_ctx, pr.pollset, shutdown_closure);
- grpc_exec_ctx_finish(&exec_ctx);
- gpr_free(path);
-}
-
-static void free_chosen_ports() {
+static void free_chosen_ports(void) {
char *env = gpr_getenv("GRPC_TEST_PORT_SERVER");
if (env != NULL) {
size_t i;
for (i = 0; i < num_chosen_ports; i++) {
- free_port_using_server(env, chosen_ports[i]);
+ grpc_free_port_using_server(env, chosen_ports[i]);
}
gpr_free(env);
}
@@ -205,101 +141,6 @@ static int is_port_available(int *port, int is_tcp) {
return 1;
}
-typedef struct portreq {
- gpr_mu *mu;
- grpc_pollset *pollset;
- int port;
- int retries;
- char *server;
- grpc_httpcli_context *ctx;
-} portreq;
-
-static void got_port_from_server(grpc_exec_ctx *exec_ctx, void *arg,
- const grpc_httpcli_response *response) {
- size_t i;
- int port = 0;
- portreq *pr = arg;
- int failed = 0;
-
- if (!response) {
- failed = 1;
- gpr_log(GPR_DEBUG,
- "failed port pick from server: retrying [response=NULL]");
- } else if (response->status != 200) {
- failed = 1;
- gpr_log(GPR_DEBUG, "failed port pick from server: status=%d",
- response->status);
- }
-
- if (failed) {
- grpc_httpcli_request req;
- memset(&req, 0, sizeof(req));
- GPR_ASSERT(pr->retries < 10);
- sleep(1 + (unsigned)(pow(1.3, pr->retries) * rand() / RAND_MAX));
- pr->retries++;
- req.host = pr->server;
- req.path = "/get";
- grpc_httpcli_get(exec_ctx, pr->ctx, pr->pollset, &req,
- GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10), got_port_from_server,
- pr);
- return;
- }
- GPR_ASSERT(response);
- GPR_ASSERT(response->status == 200);
- for (i = 0; i < response->body_length; i++) {
- GPR_ASSERT(response->body[i] >= '0' && response->body[i] <= '9');
- port = port * 10 + response->body[i] - '0';
- }
- GPR_ASSERT(port > 1024);
- gpr_mu_lock(pr->mu);
- pr->port = port;
- grpc_pollset_kick(pr->pollset, NULL);
- gpr_mu_unlock(pr->mu);
-}
-
-static int pick_port_using_server(char *server) {
- grpc_httpcli_context context;
- grpc_httpcli_request req;
- portreq pr;
- grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- grpc_closure *shutdown_closure;
-
- grpc_init();
-
- memset(&pr, 0, sizeof(pr));
- memset(&req, 0, sizeof(req));
- pr.pollset = gpr_malloc(grpc_pollset_size());
- grpc_pollset_init(pr.pollset, &pr.mu);
- shutdown_closure =
- grpc_closure_create(destroy_pollset_and_shutdown, pr.pollset);
- pr.port = -1;
- pr.server = server;
- pr.ctx = &context;
-
- req.host = server;
- req.path = "/get";
-
- grpc_httpcli_context_init(&context);
- grpc_httpcli_get(&exec_ctx, &context, pr.pollset, &req,
- GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10), got_port_from_server,
- &pr);
- grpc_exec_ctx_finish(&exec_ctx);
- gpr_mu_lock(pr.mu);
- while (pr.port == -1) {
- grpc_pollset_worker *worker = NULL;
- grpc_pollset_work(&exec_ctx, pr.pollset, &worker,
- gpr_now(GPR_CLOCK_MONOTONIC),
- GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1));
- }
- gpr_mu_unlock(pr.mu);
-
- grpc_httpcli_context_destroy(&context);
- grpc_pollset_shutdown(&exec_ctx, pr.pollset, shutdown_closure);
- grpc_exec_ctx_finish(&exec_ctx);
-
- return pr.port;
-}
-
int grpc_pick_unused_port(void) {
/* We repeatedly pick a port and then see whether or not it is
available for use both as a TCP socket and a UDP socket. First, we
@@ -319,7 +160,7 @@ int grpc_pick_unused_port(void) {
char *env = gpr_getenv("GRPC_TEST_PORT_SERVER");
if (env) {
- int port = pick_port_using_server(env);
+ int port = grpc_pick_port_using_server(env);
gpr_free(env);
if (port != 0) {
chose_port(port);
diff --git a/test/core/util/port_server_client.c b/test/core/util/port_server_client.c
new file mode 100644
index 0000000000..84e90547aa
--- /dev/null
+++ b/test/core/util/port_server_client.c
@@ -0,0 +1,215 @@
+/*
+ *
+ * Copyright 2015, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <grpc/support/port_platform.h>
+#include "test/core/util/test_config.h"
+
+#ifdef GRPC_TEST_PICK_PORT
+#include "test/core/util/port_server_client.h"
+
+#include <math.h>
+#include <string.h>
+
+#include <grpc/grpc.h>
+#include <grpc/support/alloc.h>
+#include <grpc/support/log.h>
+#include <grpc/support/string_util.h>
+#include <grpc/support/sync.h>
+#include <grpc/support/time.h>
+
+#include "src/core/lib/http/httpcli.h"
+
+typedef struct freereq {
+ gpr_mu *mu;
+ grpc_pollset *pollset;
+ int done;
+} freereq;
+
+static void destroy_pollset_and_shutdown(grpc_exec_ctx *exec_ctx, void *p,
+ bool success) {
+ grpc_pollset_destroy(p);
+ gpr_free(p);
+ grpc_shutdown();
+}
+
+static void freed_port_from_server(grpc_exec_ctx *exec_ctx, void *arg,
+ const grpc_httpcli_response *response) {
+ freereq *pr = arg;
+ gpr_mu_lock(pr->mu);
+ pr->done = 1;
+ grpc_pollset_kick(pr->pollset, NULL);
+ gpr_mu_unlock(pr->mu);
+}
+
+void grpc_free_port_using_server(char *server, int port) {
+ grpc_httpcli_context context;
+ grpc_httpcli_request req;
+ freereq pr;
+ char *path;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_closure *shutdown_closure;
+
+ grpc_init();
+
+ memset(&pr, 0, sizeof(pr));
+ memset(&req, 0, sizeof(req));
+
+ pr.pollset = gpr_malloc(grpc_pollset_size());
+ grpc_pollset_init(pr.pollset, &pr.mu);
+ shutdown_closure =
+ grpc_closure_create(destroy_pollset_and_shutdown, pr.pollset);
+
+ req.host = server;
+ gpr_asprintf(&path, "/drop/%d", port);
+ req.http.path = path;
+
+ grpc_httpcli_context_init(&context);
+ grpc_httpcli_get(&exec_ctx, &context, pr.pollset, &req,
+ GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10), freed_port_from_server,
+ &pr);
+ gpr_mu_lock(pr.mu);
+ while (!pr.done) {
+ grpc_pollset_worker *worker = NULL;
+ grpc_pollset_work(&exec_ctx, pr.pollset, &worker,
+ gpr_now(GPR_CLOCK_MONOTONIC),
+ GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1));
+ }
+ gpr_mu_unlock(pr.mu);
+
+ grpc_httpcli_context_destroy(&context);
+ grpc_exec_ctx_finish(&exec_ctx);
+ grpc_pollset_shutdown(&exec_ctx, pr.pollset, shutdown_closure);
+ grpc_exec_ctx_finish(&exec_ctx);
+ gpr_free(path);
+}
+
+typedef struct portreq {
+ gpr_mu *mu;
+ grpc_pollset *pollset;
+ int port;
+ int retries;
+ char *server;
+ grpc_httpcli_context *ctx;
+} portreq;
+
+static void got_port_from_server(grpc_exec_ctx *exec_ctx, void *arg,
+ const grpc_httpcli_response *response) {
+ size_t i;
+ int port = 0;
+ portreq *pr = arg;
+ int failed = 0;
+
+ if (!response) {
+ failed = 1;
+ gpr_log(GPR_DEBUG,
+ "failed port pick from server: retrying [response=NULL]");
+ } else if (response->status != 200) {
+ failed = 1;
+ gpr_log(GPR_DEBUG, "failed port pick from server: status=%d",
+ response->status);
+ }
+
+ if (failed) {
+ grpc_httpcli_request req;
+ memset(&req, 0, sizeof(req));
+ GPR_ASSERT(pr->retries < 10);
+ gpr_sleep_until(gpr_time_add(
+ gpr_now(GPR_CLOCK_REALTIME),
+ gpr_time_from_millis(
+ (int64_t)(1000.0 * (1 + pow(1.3, pr->retries) * rand() / RAND_MAX)),
+ GPR_TIMESPAN)));
+ pr->retries++;
+ req.host = pr->server;
+ req.http.path = "/get";
+ grpc_httpcli_get(exec_ctx, pr->ctx, pr->pollset, &req,
+ GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10), got_port_from_server,
+ pr);
+ return;
+ }
+ GPR_ASSERT(response);
+ GPR_ASSERT(response->status == 200);
+ for (i = 0; i < response->body_length; i++) {
+ GPR_ASSERT(response->body[i] >= '0' && response->body[i] <= '9');
+ port = port * 10 + response->body[i] - '0';
+ }
+ GPR_ASSERT(port > 1024);
+ gpr_mu_lock(pr->mu);
+ pr->port = port;
+ grpc_pollset_kick(pr->pollset, NULL);
+ gpr_mu_unlock(pr->mu);
+}
+
+int grpc_pick_port_using_server(char *server) {
+ grpc_httpcli_context context;
+ grpc_httpcli_request req;
+ portreq pr;
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
+ grpc_closure *shutdown_closure;
+
+ grpc_init();
+
+ memset(&pr, 0, sizeof(pr));
+ memset(&req, 0, sizeof(req));
+ pr.pollset = gpr_malloc(grpc_pollset_size());
+ grpc_pollset_init(pr.pollset, &pr.mu);
+ shutdown_closure =
+ grpc_closure_create(destroy_pollset_and_shutdown, pr.pollset);
+ pr.port = -1;
+ pr.server = server;
+ pr.ctx = &context;
+
+ req.host = server;
+ req.http.path = "/get";
+
+ grpc_httpcli_context_init(&context);
+ grpc_httpcli_get(&exec_ctx, &context, pr.pollset, &req,
+ GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10), got_port_from_server,
+ &pr);
+ grpc_exec_ctx_finish(&exec_ctx);
+ gpr_mu_lock(pr.mu);
+ while (pr.port == -1) {
+ grpc_pollset_worker *worker = NULL;
+ grpc_pollset_work(&exec_ctx, pr.pollset, &worker,
+ gpr_now(GPR_CLOCK_MONOTONIC),
+ GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1));
+ }
+ gpr_mu_unlock(pr.mu);
+
+ grpc_httpcli_context_destroy(&context);
+ grpc_pollset_shutdown(&exec_ctx, pr.pollset, shutdown_closure);
+ grpc_exec_ctx_finish(&exec_ctx);
+
+ return pr.port;
+}
+
+#endif // GRPC_TEST_PICK_PORT
diff --git a/test/core/util/port_server_client.h b/test/core/util/port_server_client.h
new file mode 100644
index 0000000000..437006495c
--- /dev/null
+++ b/test/core/util/port_server_client.h
@@ -0,0 +1,42 @@
+/*
+ *
+ * Copyright 2015, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef GRPC_TEST_CORE_UTIL_PORT_SERVER_CLIENT_H
+#define GRPC_TEST_CORE_UTIL_PORT_SERVER_CLIENT_H
+
+// C interface to port_server.py
+
+int grpc_pick_port_using_server(char *server);
+void grpc_free_port_using_server(char *server, int port);
+
+#endif // GRPC_TEST_CORE_UTIL_PORT_SERVER_CLIENT_H
diff --git a/test/core/util/port_windows.c b/test/core/util/port_windows.c
index 3b20aeb718..2b6d3dd223 100644
--- a/test/core/util/port_windows.c
+++ b/test/core/util/port_windows.c
@@ -1,6 +1,6 @@
/*
*
- * Copyright 2015-2016, Google Inc.
+ * Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -37,18 +37,19 @@
#include "test/core/util/port.h"
+#include <errno.h>
#include <process.h>
#include <stdio.h>
-#include <errno.h>
#include <string.h>
#include <grpc/grpc.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
-#include "src/core/support/env.h"
-#include "src/core/httpcli/httpcli.h"
-#include "src/core/iomgr/sockaddr_utils.h"
+#include "src/core/lib/http/httpcli.h"
+#include "src/core/lib/iomgr/sockaddr_utils.h"
+#include "src/core/lib/support/env.h"
+#include "test/core/util/port_server_client.h"
#define NUM_RANDOM_PORTS_TO_PICK 100
@@ -65,7 +66,18 @@ static int has_port_been_chosen(int port) {
return 0;
}
-static void free_chosen_ports(void) { gpr_free(chosen_ports); }
+static void free_chosen_ports(void) {
+ char *env = gpr_getenv("GRPC_TEST_PORT_SERVER");
+ if (env != NULL) {
+ size_t i;
+ for (i = 0; i < num_chosen_ports; i++) {
+ grpc_free_port_using_server(env, chosen_ports[i]);
+ }
+ gpr_free(env);
+ }
+
+ gpr_free(chosen_ports);
+}
static void chose_port(int port) {
if (chosen_ports == NULL) {
@@ -128,80 +140,6 @@ static int is_port_available(int *port, int is_tcp) {
return 1;
}
-typedef struct portreq {
- grpc_pollset *pollset;
- gpr_mu *mu;
- int port;
-} portreq;
-
-static void got_port_from_server(grpc_exec_ctx *exec_ctx, void *arg,
- const grpc_httpcli_response *response) {
- size_t i;
- int port = 0;
- portreq *pr = arg;
- GPR_ASSERT(response);
- GPR_ASSERT(response->status == 200);
- for (i = 0; i < response->body_length; i++) {
- GPR_ASSERT(response->body[i] >= '0' && response->body[i] <= '9');
- port = port * 10 + response->body[i] - '0';
- }
- GPR_ASSERT(port > 1024);
- gpr_mu_lock(pr->mu);
- pr->port = port;
- grpc_pollset_kick(pr->pollset, NULL);
- gpr_mu_unlock(pr->mu);
-}
-
-static void destroy_pollset_and_shutdown(grpc_exec_ctx *exec_ctx, void *p,
- bool success) {
- grpc_pollset_destroy(p);
- grpc_shutdown();
-}
-
-static int pick_port_using_server(char *server) {
- grpc_httpcli_context context;
- grpc_httpcli_request req;
- portreq pr;
- grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
- grpc_closure destroy_pollset_closure;
-
- grpc_init();
-
- memset(&pr, 0, sizeof(pr));
- memset(&req, 0, sizeof(req));
- pr.pollset = gpr_malloc(grpc_pollset_size());
- grpc_pollset_init(pr.pollset, &pr.mu);
- pr.port = -1;
-
- req.host = server;
- req.path = "/get";
-
- grpc_httpcli_context_init(&context);
- grpc_httpcli_get(&exec_ctx, &context, pr.pollset, &req,
- GRPC_TIMEOUT_SECONDS_TO_DEADLINE(10), got_port_from_server,
- &pr);
- gpr_mu_lock(pr.mu);
- while (pr.port == -1) {
- grpc_pollset_worker *worker = NULL;
- grpc_pollset_work(&exec_ctx, pr.pollset, &worker,
- gpr_now(GPR_CLOCK_MONOTONIC),
- GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1));
- gpr_mu_unlock(pr.mu);
- grpc_exec_ctx_flush(&exec_ctx);
- gpr_mu_lock(pr.mu);
- }
- gpr_mu_unlock(pr.mu);
-
- grpc_httpcli_context_destroy(&context);
- grpc_closure_init(&destroy_pollset_closure, destroy_pollset_and_shutdown,
- &pr.pollset);
- grpc_pollset_shutdown(&exec_ctx, pr.pollset, &destroy_pollset_closure);
- gpr_free(pr.pollset);
-
- grpc_exec_ctx_finish(&exec_ctx);
- return pr.port;
-}
-
int grpc_pick_unused_port(void) {
/* We repeatedly pick a port and then see whether or not it is
available for use both as a TCP socket and a UDP socket. First, we
@@ -221,7 +159,7 @@ int grpc_pick_unused_port(void) {
char *env = gpr_getenv("GRPC_TEST_PORT_SERVER");
if (env) {
- int port = pick_port_using_server(env);
+ int port = grpc_pick_port_using_server(env);
gpr_free(env);
if (port != 0) {
return port;
diff --git a/test/core/util/reconnect_server.c b/test/core/util/reconnect_server.c
index 57225aa8a3..d408374a09 100644
--- a/test/core/util/reconnect_server.c
+++ b/test/core/util/reconnect_server.c
@@ -1,6 +1,6 @@
/*
*
- * Copyright 2015-2016, Google Inc.
+ * Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -40,9 +40,9 @@
#include <grpc/support/sync.h>
#include <grpc/support/time.h>
#include <string.h>
-#include "src/core/iomgr/endpoint.h"
-#include "src/core/iomgr/sockaddr.h"
-#include "src/core/iomgr/tcp_server.h"
+#include "src/core/lib/iomgr/endpoint.h"
+#include "src/core/lib/iomgr/sockaddr.h"
+#include "src/core/lib/iomgr/tcp_server.h"
#include "test/core/util/port.h"
#include "test/core/util/test_tcp_server.h"
@@ -60,8 +60,12 @@ static void pretty_print_backoffs(reconnect_server *server) {
i, backoff / 1000.0, expected_backoff / 1000.0,
(backoff - expected_backoff) * 100.0 / expected_backoff);
expected_backoff *= 1.6;
- if (expected_backoff > 120 * 1000) {
- expected_backoff = 120 * 1000;
+ int max_reconnect_backoff_ms = 120 * 1000;
+ if (server->max_reconnect_backoff_ms > 0) {
+ max_reconnect_backoff_ms = server->max_reconnect_backoff_ms;
+ }
+ if (expected_backoff > max_reconnect_backoff_ms) {
+ expected_backoff = max_reconnect_backoff_ms;
}
}
}
@@ -108,6 +112,7 @@ void reconnect_server_init(reconnect_server *server) {
server->head = NULL;
server->tail = NULL;
server->peer = NULL;
+ server->max_reconnect_backoff_ms = 0;
}
void reconnect_server_start(reconnect_server *server, int port) {
diff --git a/test/core/util/reconnect_server.h b/test/core/util/reconnect_server.h
index e2e6a02461..ed02d49512 100644
--- a/test/core/util/reconnect_server.h
+++ b/test/core/util/reconnect_server.h
@@ -1,6 +1,6 @@
/*
*
- * Copyright 2015, Google Inc.
+ * Copyright 2015-2016, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -52,6 +52,7 @@ typedef struct reconnect_server {
timestamp_list *head;
timestamp_list *tail;
char *peer;
+ int max_reconnect_backoff_ms;
} reconnect_server;
void reconnect_server_init(reconnect_server *server);
diff --git a/test/core/util/test_config.c b/test/core/util/test_config.c
index bf672e8f67..3155a4ece6 100644
--- a/test/core/util/test_config.c
+++ b/test/core/util/test_config.c
@@ -1,6 +1,6 @@
/*
*
- * Copyright 2015-2016, Google Inc.
+ * Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -39,7 +39,7 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
-#include "src/core/support/string.h"
+#include "src/core/lib/support/string.h"
double g_fixture_slowdown_factor = 1.0;
@@ -99,6 +99,7 @@ static void print_current_stack() {
SymFromAddrW(process, (DWORD64)(callers_stack[i]), 0, symbol);
fwprintf(stderr, L"*** %d: %016I64X %ls - %016I64X\n", i,
(DWORD64)callers_stack[i], symbol->Name, (DWORD64)symbol->Address);
+ fflush(stderr);
}
free(symbol);
@@ -154,6 +155,7 @@ static void print_stack_from_context(CONTEXT c) {
fwprintf(
stderr, L"*** %016I64X %ls - %016I64X\n", (DWORD64)(s.AddrPC.Offset),
has_symbol ? symbol->Name : L"<<no symbol>>", (DWORD64)symbol->Address);
+ fflush(stderr);
}
free(symbol);
diff --git a/test/core/util/test_config.h b/test/core/util/test_config.h
index f6bb2e1f72..76686f1c51 100644
--- a/test/core/util/test_config.h
+++ b/test/core/util/test_config.h
@@ -1,6 +1,6 @@
/*
*
- * Copyright 2015-2016, Google Inc.
+ * Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
diff --git a/test/core/util/test_tcp_server.c b/test/core/util/test_tcp_server.c
index ab379441d8..e39a95712c 100644
--- a/test/core/util/test_tcp_server.c
+++ b/test/core/util/test_tcp_server.c
@@ -1,6 +1,6 @@
/*
*
- * Copyright 2015-2016, Google Inc.
+ * Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -40,9 +40,9 @@
#include <grpc/support/sync.h>
#include <grpc/support/time.h>
#include <string.h>
-#include "src/core/iomgr/endpoint.h"
-#include "src/core/iomgr/sockaddr.h"
-#include "src/core/iomgr/tcp_server.h"
+#include "src/core/lib/iomgr/endpoint.h"
+#include "src/core/lib/iomgr/sockaddr.h"
+#include "src/core/lib/iomgr/tcp_server.h"
#include "test/core/util/port.h"
static void on_server_destroyed(grpc_exec_ctx *exec_ctx, void *data,
diff --git a/test/core/util/test_tcp_server.h b/test/core/util/test_tcp_server.h
index 15fcb4fb87..d10b166586 100644
--- a/test/core/util/test_tcp_server.h
+++ b/test/core/util/test_tcp_server.h
@@ -1,6 +1,6 @@
/*
*
- * Copyright 2015-2016, Google Inc.
+ * Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -35,7 +35,7 @@
#define GRPC_TEST_CORE_UTIL_TEST_TCP_SERVER_H
#include <grpc/support/sync.h>
-#include "src/core/iomgr/tcp_server.h"
+#include "src/core/lib/iomgr/tcp_server.h"
typedef struct test_tcp_server {
grpc_tcp_server *tcp_server;