michajlo | f963a2c | 2020-08-28 12:19:01 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.devtools.build.lib.server; |
| 16 | |
| 17 | import static com.google.common.truth.Truth.assertThat; |
| 18 | import static java.nio.charset.StandardCharsets.US_ASCII; |
| 19 | import static org.junit.Assert.assertThrows; |
| 20 | |
janakr | 97c0bd1 | 2020-09-08 13:19:03 -0700 | [diff] [blame] | 21 | import com.google.devtools.build.lib.vfs.DigestHashFunction; |
michajlo | f963a2c | 2020-08-28 12:19:01 -0700 | [diff] [blame] | 22 | import com.google.devtools.build.lib.vfs.FileSystem; |
| 23 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
| 24 | import com.google.devtools.build.lib.vfs.Path; |
| 25 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
| 26 | import java.io.IOException; |
| 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
| 29 | import org.junit.runner.RunWith; |
| 30 | import org.junit.runners.JUnit4; |
| 31 | |
| 32 | /** Tests for {@link PidFileWatcher}. */ |
| 33 | @RunWith(JUnit4.class) |
| 34 | public 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() { |
janakr | 97c0bd1 | 2020-09-08 13:19:03 -0700 | [diff] [blame] | 44 | FileSystem fileSystem = new InMemoryFileSystem(DigestHashFunction.SHA256); |
michajlo | f963a2c | 2020-08-28 12:19:01 -0700 | [diff] [blame] | 45 | 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 | } |