aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com/google/devtools/build/lib/server
diff options
context:
space:
mode:
authorGravatar kush <kush@google.com>2017-10-16 21:28:13 +0200
committerGravatar Jakob Buchgraber <buchgr@google.com>2017-10-18 10:27:53 +0200
commit4869c4e17d5b1410070a1570f3244148d8f97b5d (patch)
tree0b7f802c8e5bc082c023a3fe01e472de4336cb0c /src/test/java/com/google/devtools/build/lib/server
parent0faf1710a4bb12e748e70d4bf445bdf16dfc9a7d (diff)
Delayed rollforward of commit 8fb311b4dced234b2f799c16c7d08148619f4087.
This was rolled back due to Tensorflow breakage but the patch I exported to gerrit (https://bazel-review.googlesource.com/c/bazel/+/18590) passed Tensorflow (https://ci.bazel.io/job/bazel/job/presubmit/52/Downstream_projects/). Confirmed with jcater@ that the "newly failing" projects in the Global Tests are known issues. I think we can check this in now. Additionally I had attempted to reproduce any tensorflow issues with this by building and testing TensorFlow locally with this patch, and all tests which passed with the released bazel had also passed with this patch. ================= Original change description ========================== Reinstate idleness checks where the server self-terminates when it's idle and there is either too much memory pressure or the workspace directory is gone. Arguably, it should kill itself when the workspace directory is gone regardless of whether it's idle or not, but let's first get us back to a known good state, then we can think about improvements. RELNOTES: None. PiperOrigin-RevId: 172361085
Diffstat (limited to 'src/test/java/com/google/devtools/build/lib/server')
-rw-r--r--src/test/java/com/google/devtools/build/lib/server/GrpcServerTest.java119
1 files changed, 119 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/server/GrpcServerTest.java b/src/test/java/com/google/devtools/build/lib/server/GrpcServerTest.java
index 5a0ae62eb5..761434dff1 100644
--- a/src/test/java/com/google/devtools/build/lib/server/GrpcServerTest.java
+++ b/src/test/java/com/google/devtools/build/lib/server/GrpcServerTest.java
@@ -23,18 +23,34 @@ import static org.mockito.Mockito.when;
import com.google.common.base.Strings;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.google.common.util.concurrent.Uninterruptibles;
+import com.google.devtools.build.lib.clock.BlazeClock;
+import com.google.devtools.build.lib.runtime.BlazeCommandDispatcher.LockingMode;
+import com.google.devtools.build.lib.runtime.CommandExecutor;
+import com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.InvocationPolicy;
+import com.google.devtools.build.lib.server.CommandProtos.PingRequest;
+import com.google.devtools.build.lib.server.CommandProtos.RunRequest;
import com.google.devtools.build.lib.server.CommandProtos.RunResponse;
+import com.google.devtools.build.lib.server.CommandServerGrpc.CommandServerBlockingStub;
import com.google.devtools.build.lib.server.GrpcServerImpl.StreamType;
import com.google.devtools.build.lib.testutil.Suite;
import com.google.devtools.build.lib.testutil.TestSpec;
import com.google.devtools.build.lib.testutil.TestThread;
import com.google.devtools.build.lib.testutil.TestUtils;
import com.google.devtools.build.lib.util.Preconditions;
+import com.google.devtools.build.lib.util.io.OutErr;
+import com.google.devtools.build.lib.vfs.FileSystem;
+import com.google.devtools.build.lib.vfs.FileSystemUtils;
+import com.google.devtools.build.lib.vfs.Path;
+import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
import com.google.protobuf.ByteString;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
+import io.grpc.netty.NettyChannelBuilder;
import io.grpc.stub.ServerCallStreamObserver;
import java.nio.charset.StandardCharsets;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@@ -334,4 +350,107 @@ public class GrpcServerTest {
.setStandardOutput(ByteString.copyFrom(chunk3.getBytes(StandardCharsets.ISO_8859_1)))
.build());
}
+
+ private static class Server {
+ private final Path workspaceDirectory;
+ private final Path serverDirectory;
+ private final CommandExecutor commandExecutor;
+ private final GrpcServerImpl impl;
+ private final TestThread serverThread;
+ private final CommandServerBlockingStub clientStub;
+
+ private Server() throws Exception {
+ FileSystem fs = new InMemoryFileSystem(BlazeClock.instance());
+ workspaceDirectory = fs.getPath("/workspace");
+ workspaceDirectory.createDirectory();
+ serverDirectory = fs.getPath("/server");
+ serverDirectory.createDirectory();
+ Path pidfile = fs.getPath("/server/server.pid.txt");
+ FileSystemUtils.writeContentAsLatin1(pidfile, "1234");
+ commandExecutor = mock(CommandExecutor.class);
+ impl =
+ new GrpcServerImpl(
+ commandExecutor, BlazeClock.instance(), 0, workspaceDirectory, serverDirectory, 1000);
+ serverThread =
+ new TestThread() {
+ @Override
+ public void runTest() throws Exception {
+ impl.serve();
+ }
+ };
+
+ serverThread.start();
+ CommandServerBlockingStub stubCandidate = null;
+ boolean ok = false;
+
+ // Wait until the server starts up. Should be reasonably quick.
+ for (int i = 0; i < 20; i++) {
+ Thread.sleep(100);
+ PingRequest request = PingRequest.newBuilder().setCookie(impl.getRequestCookie()).build();
+ if (impl.getAddress() == null) {
+ continue;
+ }
+
+ if (stubCandidate == null) {
+ stubCandidate =
+ CommandServerGrpc.newBlockingStub(
+ NettyChannelBuilder.forAddress(impl.getAddress()).usePlaintext(true).build());
+ }
+
+ try {
+ stubCandidate.ping(request);
+ ok = true;
+ break;
+ } catch (StatusRuntimeException e) {
+ continue;
+ }
+ }
+
+ if (!ok) {
+ throw new IllegalStateException("Server did not start up in time");
+ }
+
+ clientStub = stubCandidate;
+ }
+
+ private void finish() throws Exception {
+ impl.signalShutdown();
+ serverThread.joinAndAssertState(1000);
+ }
+ }
+
+ @Test
+ public void testRunCommand() throws Exception {
+ Server server = new Server();
+ when(server.commandExecutor.exec(
+ any(InvocationPolicy.class),
+ any(List.class),
+ any(OutErr.class),
+ any(LockingMode.class),
+ any(String.class),
+ any(Long.class),
+ any(Optional.class)))
+ .thenReturn(42);
+
+ RunRequest request =
+ RunRequest.newBuilder()
+ .setClientDescription("client")
+ .setCookie(server.impl.getRequestCookie())
+ .build();
+
+ Iterator<RunResponse> result = server.clientStub.run(request);
+ int exitCode = -1;
+ while (result.hasNext()) {
+ exitCode = result.next().getExitCode();
+ }
+ server.finish();
+ assertThat(exitCode).isEqualTo(42);
+ }
+
+ @Test
+ public void testIdleShutdownWhenWorkspaceDeleted() throws Exception {
+ Server server = new Server();
+ server.workspaceDirectory.delete();
+ server.serverThread.joinAndAssertState(10000); // We check the workspace dir every five seconds
+ }
}