Add subprocess GPR API

Port fling_test to it.

This will be used to:
- port remaining tests to Windows
- enable testing what happens when servers or clients mysteriously disappear
pull/1617/head
Craig Tiller 10 years ago
parent e195aa0ef7
commit c21a80957a
  1. 1
      BUILD
  2. 1
      Makefile
  3. 1
      build.json
  4. 4
      include/grpc/support/port_platform.h
  5. 49
      include/grpc/support/subprocess.h
  6. 108
      src/core/support/subprocess_posix.c
  7. 81
      test/core/fling/fling_test.c
  8. 2
      vsprojects/gpr/gpr.vcxproj
  9. 3
      vsprojects/gpr/gpr.vcxproj.filters

@ -74,6 +74,7 @@ cc_library(
"src/core/support/string.c",
"src/core/support/string_posix.c",
"src/core/support/string_win32.c",
"src/core/support/subprocess_posix.c",
"src/core/support/sync.c",
"src/core/support/sync_posix.c",
"src/core/support/sync_win32.c",

@ -2313,6 +2313,7 @@ LIBGPR_SRC = \
src/core/support/string.c \
src/core/support/string_posix.c \
src/core/support/string_win32.c \
src/core/support/subprocess_posix.c \
src/core/support/sync.c \
src/core/support/sync_posix.c \
src/core/support/sync_win32.c \

@ -362,6 +362,7 @@
"src/core/support/string.c",
"src/core/support/string_posix.c",
"src/core/support/string_win32.c",
"src/core/support/subprocess_posix.c",
"src/core/support/sync.c",
"src/core/support/sync_posix.c",
"src/core/support/sync_win32.c",

@ -77,6 +77,7 @@
#define GPR_POSIX_ENV 1
#define GPR_POSIX_FILE 1
#define GPR_POSIX_STRING 1
#define GPR_POSIX_SUBPROCESS 1
#define GPR_POSIX_SYNC 1
#define GPR_POSIX_TIME 1
#define GPR_GETPID_IN_UNISTD_H 1
@ -122,6 +123,7 @@
#endif
#define GPR_POSIX_FILE 1
#define GPR_POSIX_STRING 1
#define GPR_POSIX_SUBPROCESS 1
#define GPR_POSIX_SYNC 1
#define GPR_POSIX_TIME 1
#define GPR_GETPID_IN_UNISTD_H 1
@ -154,6 +156,7 @@
#define GPR_POSIX_ENV 1
#define GPR_POSIX_FILE 1
#define GPR_POSIX_STRING 1
#define GPR_POSIX_SUBPROCESS 1
#define GPR_POSIX_SYNC 1
#define GPR_POSIX_TIME 1
#define GPR_GETPID_IN_UNISTD_H 1
@ -180,6 +183,7 @@
#define GPR_POSIX_ENV 1
#define GPR_POSIX_FILE 1
#define GPR_POSIX_STRING 1
#define GPR_POSIX_SUBPROCESS 1
#define GPR_POSIX_SYNC 1
#define GPR_POSIX_TIME 1
#define GPR_GETPID_IN_UNISTD_H 1

@ -0,0 +1,49 @@
/*
*
* 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_SUPPORT_SUBPROCESS_H
#define GRPC_SUPPORT_SUBPROCESS_H
typedef struct gpr_subprocess gpr_subprocess;
/* .exe on windows, empty on unices */
char *gpr_subprocess_binary_extension();
gpr_subprocess *gpr_subprocess_create(int argc, char **argv);
/* if subprocess has not been joined, kill it */
void gpr_subprocess_destroy(gpr_subprocess *p);
/* returns exit status; can be called at most once */
int gpr_subprocess_join(gpr_subprocess *p);
void gpr_subprocess_interrupt(gpr_subprocess *p);
#endif

@ -0,0 +1,108 @@
/*
*
* 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>
#ifdef GPR_POSIX_SUBPROCESS
#include <grpc/support/subprocess.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
struct gpr_subprocess {
int pid;
int joined;
};
char *gpr_subprocess_binary_extension() { return ""; }
gpr_subprocess *gpr_subprocess_create(int argc, char **argv) {
gpr_subprocess *r;
int pid;
char **exec_args;
pid = fork();
if (pid == -1) {
return NULL;
} else if (pid == 0) {
exec_args = gpr_malloc((argc + 1) * sizeof(char *));
memcpy(exec_args, argv, argc * sizeof(char *));
exec_args[argc] = NULL;
execv(exec_args[0], exec_args);
/* if we reach here, an error has occurred */
gpr_log(GPR_ERROR, "execv '%s' failed: %s", exec_args[0], strerror(errno));
_exit(1);
return NULL;
} else {
r = gpr_malloc(sizeof(gpr_subprocess));
memset(r, 0, sizeof(*r));
r->pid = pid;
return r;
}
}
void gpr_subprocess_destroy(gpr_subprocess *p) {
if (!p->joined) {
kill(p->pid, SIGKILL);
gpr_subprocess_join(p);
}
gpr_free(p);
}
int gpr_subprocess_join(gpr_subprocess *p) {
int status;
if (waitpid(p->pid, &status, 0) == -1) {
gpr_log(GPR_ERROR, "waitpid failed: %s", strerror(errno));
return -1;
}
return status;
}
void gpr_subprocess_interrupt(gpr_subprocess *p) {
if (!p->joined) {
kill(p->pid, SIGINT);
}
}
#endif /* GPR_POSIX_SUBPROCESS */

@ -31,20 +31,11 @@
*
*/
#ifndef _POSIX_SOURCE
#define _POSIX_SOURCE
#endif
#include <unistd.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <grpc/support/alloc.h>
#include <grpc/support/subprocess.h>
#include <grpc/support/host_port.h>
#include "src/core/support/string.h"
#include "test/core/util/port.h"
@ -56,10 +47,7 @@ int main(int argc, char **argv) {
int port = grpc_pick_unused_port_or_die();
char *args[10];
int status;
pid_t svr, cli;
/* seed rng with pid, so we don't end up with the same random numbers as a
concurrently running test binary */
srand(getpid());
gpr_subprocess *svr, *cli;
/* figure out where we are */
if (lslash) {
memcpy(root, me, lslash - me);
@ -68,45 +56,36 @@ int main(int argc, char **argv) {
strcpy(root, ".");
}
/* start the server */
svr = fork();
if (svr == 0) {
gpr_asprintf(&args[0], "%s/fling_server", root);
args[1] = "--bind";
gpr_join_host_port(&args[2], "::", port);
args[3] = "--no-secure";
args[4] = 0;
execv(args[0], args);
gpr_asprintf(&args[0], "%s/fling_server", root);
args[1] = "--bind";
gpr_join_host_port(&args[2], "::", port);
args[3] = "--no-secure";
svr = gpr_subprocess_create(4, args);
gpr_free(args[0]);
gpr_free(args[2]);
gpr_free(args[0]);
gpr_free(args[2]);
return 1;
}
/* wait a little */
sleep(2);
/* start the client */
cli = fork();
if (cli == 0) {
gpr_asprintf(&args[0], "%s/fling_client", root);
args[1] = "--target";
gpr_join_host_port(&args[2], "127.0.0.1", port);
args[3] = "--scenario=ping-pong-request";
args[4] = "--no-secure";
args[5] = 0;
execv(args[0], args);
gpr_asprintf(&args[0], "%s/fling_client", root);
args[1] = "--target";
gpr_join_host_port(&args[2], "127.0.0.1", port);
args[3] = "--scenario=ping-pong-request";
args[4] = "--no-secure";
args[5] = 0;
cli = gpr_subprocess_create(6, args);
gpr_free(args[0]);
gpr_free(args[2]);
gpr_free(args[0]);
gpr_free(args[2]);
return 1;
}
/* wait for completion */
printf("waiting for client\n");
if (waitpid(cli, &status, 0) == -1) return 2;
if (!WIFEXITED(status)) return 4;
if (WEXITSTATUS(status)) return WEXITSTATUS(status);
printf("waiting for server\n");
kill(svr, SIGINT);
if (waitpid(svr, &status, 0) == -1) return 2;
if (!WIFEXITED(status)) return 4;
if (WEXITSTATUS(status)) return WEXITSTATUS(status);
return 0;
if ((status = gpr_subprocess_join(cli))) {
gpr_subprocess_destroy(cli);
gpr_subprocess_destroy(svr);
return status;
}
gpr_subprocess_destroy(cli);
gpr_subprocess_interrupt(svr);
status = gpr_subprocess_join(svr);
gpr_subprocess_destroy(svr);
return status;
}

@ -234,6 +234,8 @@
</ClCompile>
<ClCompile Include="..\..\src\core\support\string_win32.c">
</ClCompile>
<ClCompile Include="..\..\src\core\support\subprocess_posix.c">
</ClCompile>
<ClCompile Include="..\..\src\core\support\sync.c">
</ClCompile>
<ClCompile Include="..\..\src\core\support\sync_posix.c">

@ -79,6 +79,9 @@
<ClCompile Include="..\..\src\core\support\string_win32.c">
<Filter>src\core\support</Filter>
</ClCompile>
<ClCompile Include="..\..\src\core\support\subprocess_posix.c">
<Filter>src\core\support</Filter>
</ClCompile>
<ClCompile Include="..\..\src\core\support\sync.c">
<Filter>src\core\support</Filter>
</ClCompile>

Loading…
Cancel
Save