blob: 4896549380d8cc1751c7e259de60c816217f6253 [file] [log] [blame]
michajlof963a2c2020-08-28 12:19:01 -07001// Copyright 2020 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.devtools.build.lib.server;
16
17import static com.google.common.truth.Truth.assertThat;
18import static java.nio.charset.StandardCharsets.US_ASCII;
19import static org.junit.Assert.assertThrows;
20
janakr97c0bd12020-09-08 13:19:03 -070021import com.google.devtools.build.lib.vfs.DigestHashFunction;
michajlof963a2c2020-08-28 12:19:01 -070022import com.google.devtools.build.lib.vfs.FileSystem;
23import com.google.devtools.build.lib.vfs.FileSystemUtils;
24import com.google.devtools.build.lib.vfs.Path;
25import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
26import java.io.IOException;
27import org.junit.Before;
28import org.junit.Test;
29import org.junit.runner.RunWith;
30import org.junit.runners.JUnit4;
31
32/** Tests for {@link PidFileWatcher}. */
33@RunWith(JUnit4.class)
34public class PidFileWatcherTest {
35
36 private static final int EXPECTED_PID = 42;
37 private static final IllegalStateException THROWN_ON_HALT = new IllegalStateException("crash!");
38
39 private Path pidFile;
40 private PidFileWatcher underTest;
41
42 @Before
43 public void setUp() {
janakr97c0bd12020-09-08 13:19:03 -070044 FileSystem fileSystem = new InMemoryFileSystem(DigestHashFunction.SHA256);
michajlof963a2c2020-08-28 12:19:01 -070045 pidFile = fileSystem.getPath("/pid");
46 underTest =
47 new PidFileWatcher(
48 pidFile,
49 EXPECTED_PID,
50 () -> {
51 throw THROWN_ON_HALT;
52 });
53 }
54
55 @Test
56 public void testMissingPidFileHaltsProgram() throws IOException {
57 // Delete just in case.
58 pidFile.delete();
59
60 assertPidCheckHaltsProgram();
61 }
62
63 @Test
64 public void testEmptyPidFileCountsAsChanged() throws IOException {
65 FileSystemUtils.writeContent(pidFile, new byte[0]);
66
67 assertPidCheckHaltsProgram();
68 }
69
70 @Test
71 public void testGarbagePidFileCountsAsChanged() throws IOException {
72 FileSystemUtils.writeContent(pidFile, "junk".getBytes(US_ASCII));
73
74 assertPidCheckHaltsProgram();
75 }
76
77 @Test
78 public void testPidFileContinuesExecution() throws IOException {
79 FileSystemUtils.writeContent(pidFile, "42".getBytes(US_ASCII));
80
81 assertThat(underTest.runPidFileChecks()).isTrue();
82 }
83
84 @Test
85 public void testPidFileTrailingWhitespaceNotTolerated() throws IOException {
86 FileSystemUtils.writeContent(pidFile, "42\n".getBytes(US_ASCII));
87
88 assertPidCheckHaltsProgram();
89 }
90
91 @Test
92 public void testPidFileChangeAfterShutdownNotificationStopsWatcher() throws IOException {
93 FileSystemUtils.writeContent(pidFile, "42\n".getBytes(US_ASCII));
94
95 underTest.signalShutdown();
96 assertThat(underTest.runPidFileChecks()).isFalse();
97 }
98
99 private void assertPidCheckHaltsProgram() {
100 IllegalStateException expected =
101 assertThrows(IllegalStateException.class, underTest::runPidFileChecks);
102 assertThat(expected).isSameInstanceAs(THROWN_ON_HALT);
103 }
104}