Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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 | package com.google.devtools.build.lib.vfs; |
| 15 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 16 | import static com.google.common.truth.Truth.assertThat; |
Chris Parsons | cb5aa00 | 2016-07-26 17:52:37 +0000 | [diff] [blame] | 17 | |
tomlu | 18291e5 | 2017-12-19 11:54:08 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Iterables; |
| 19 | import com.google.common.util.concurrent.Futures; |
| 20 | import com.google.common.util.concurrent.ListenableFuture; |
| 21 | import com.google.common.util.concurrent.ListeningExecutorService; |
| 22 | import com.google.common.util.concurrent.MoreExecutors; |
Chris Parsons | cb5aa00 | 2016-07-26 17:52:37 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.testutil.ManualClock; |
tomlu | 18291e5 | 2017-12-19 11:54:08 -0800 | [diff] [blame] | 24 | import com.google.devtools.build.lib.testutil.TestUtils; |
Googler | e1cd950 | 2016-09-07 14:33:29 +0000 | [diff] [blame] | 25 | import java.io.IOException; |
| 26 | import java.nio.file.Files; |
| 27 | import java.nio.file.LinkOption; |
tomlu | 18291e5 | 2017-12-19 11:54:08 -0800 | [diff] [blame] | 28 | import java.nio.file.Paths; |
Googler | e1cd950 | 2016-09-07 14:33:29 +0000 | [diff] [blame] | 29 | import java.nio.file.attribute.BasicFileAttributes; |
tomlu | 18291e5 | 2017-12-19 11:54:08 -0800 | [diff] [blame] | 30 | import java.util.ArrayList; |
| 31 | import java.util.List; |
| 32 | import java.util.Objects; |
| 33 | import java.util.Optional; |
| 34 | import java.util.concurrent.Executors; |
| 35 | import java.util.concurrent.TimeUnit; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 36 | import org.junit.Test; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 37 | |
| 38 | /** |
ccalvarin | 94c171c | 2018-08-16 15:42:19 -0700 | [diff] [blame] | 39 | * Tests for the {@link JavaIoFileSystem}. That file system by itself is not capable of creating |
| 40 | * symlinks; use the unix one to create them, so that the test can check that the file system |
| 41 | * handles their existence correctly. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 42 | */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 43 | public class JavaIoFileSystemTest extends SymlinkAwareFileSystemTest { |
| 44 | |
Chris Parsons | cb5aa00 | 2016-07-26 17:52:37 +0000 | [diff] [blame] | 45 | private ManualClock clock; |
| 46 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 47 | @Override |
ccalvarin | 94c171c | 2018-08-16 15:42:19 -0700 | [diff] [blame] | 48 | public FileSystem getFreshFileSystem(DigestHashFunction digestHashFunction) { |
Chris Parsons | cb5aa00 | 2016-07-26 17:52:37 +0000 | [diff] [blame] | 49 | clock = new ManualClock(); |
ccalvarin | 94c171c | 2018-08-16 15:42:19 -0700 | [diff] [blame] | 50 | return new JavaIoFileSystem(clock, digestHashFunction); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 51 | } |
| 52 | |
Chris Parsons | cb5aa00 | 2016-07-26 17:52:37 +0000 | [diff] [blame] | 53 | // Tests are inherited from the FileSystemTest |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 54 | |
| 55 | // JavaIoFileSystem incorrectly throws a FileNotFoundException for all IO errors. This means that |
| 56 | // statIfFound incorrectly suppresses those errors. |
| 57 | @Override |
| 58 | @Test |
| 59 | public void testBadPermissionsThrowsExceptionOnStatIfFound() {} |
Chris Parsons | cb5aa00 | 2016-07-26 17:52:37 +0000 | [diff] [blame] | 60 | |
| 61 | @Test |
| 62 | public void testSetLastModifiedTime() throws Exception { |
| 63 | Path file = xEmptyDirectory.getChild("new-file"); |
| 64 | FileSystemUtils.createEmptyFile(file); |
| 65 | |
| 66 | file.setLastModifiedTime(1000L); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 67 | assertThat(file.getLastModifiedTime()).isEqualTo(1000L); |
Chris Parsons | cb5aa00 | 2016-07-26 17:52:37 +0000 | [diff] [blame] | 68 | file.setLastModifiedTime(0L); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 69 | assertThat(file.getLastModifiedTime()).isEqualTo(0L); |
Chris Parsons | cb5aa00 | 2016-07-26 17:52:37 +0000 | [diff] [blame] | 70 | |
| 71 | clock.advanceMillis(42000L); |
| 72 | file.setLastModifiedTime(-1L); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 73 | assertThat(file.getLastModifiedTime()).isEqualTo(42000L); |
Chris Parsons | cb5aa00 | 2016-07-26 17:52:37 +0000 | [diff] [blame] | 74 | } |
Googler | e1cd950 | 2016-09-07 14:33:29 +0000 | [diff] [blame] | 75 | |
| 76 | @Override |
| 77 | protected boolean isHardLinked(Path a, Path b) throws IOException { |
| 78 | return Files.readAttributes( |
tomlu | 18291e5 | 2017-12-19 11:54:08 -0800 | [diff] [blame] | 79 | Paths.get(a.toString()), BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS) |
Googler | e1cd950 | 2016-09-07 14:33:29 +0000 | [diff] [blame] | 80 | .fileKey() |
| 81 | .equals( |
| 82 | Files.readAttributes( |
tomlu | 18291e5 | 2017-12-19 11:54:08 -0800 | [diff] [blame] | 83 | Paths.get(b.toString()), BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS) |
Googler | e1cd950 | 2016-09-07 14:33:29 +0000 | [diff] [blame] | 84 | .fileKey()); |
| 85 | } |
tomlu | 18291e5 | 2017-12-19 11:54:08 -0800 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * This test has a large number of threads racing to create the same subdirectories. |
| 89 | * |
| 90 | * <p>We create N number of distinct directory trees, eg. the tree "0-0/0-1/0-2/0-3/0-4" followed |
| 91 | * by the tree "1-0/1-1/1-2/1-3/1-4" etc. If there is race we should quickly get a deadlock. |
| 92 | * |
| 93 | * <p>A timeout of this test is likely because of a deadlock. |
| 94 | */ |
| 95 | @Test |
| 96 | public void testCreateDirectoriesThreadSafety() throws Exception { |
| 97 | int threadCount = 200; |
| 98 | int directoryCreationCount = 500; // We create this many sets of directories |
| 99 | int subDirectoryCount = 5; // Each directory tree is this deep |
| 100 | ListeningExecutorService executor = |
| 101 | MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(threadCount)); |
| 102 | List<ListenableFuture<IOException>> futures = new ArrayList<>(); |
| 103 | for (int threadIndex = 0; threadIndex < threadCount; ++threadIndex) { |
| 104 | futures.add( |
| 105 | executor.submit( |
| 106 | () -> { |
| 107 | try { |
| 108 | for (int loopi = 0; loopi < directoryCreationCount; ++loopi) { |
| 109 | List<Path> subDirs = |
| 110 | getSubDirectories(xEmptyDirectory, loopi, subDirectoryCount); |
| 111 | Path lastDir = Iterables.getLast(subDirs); |
| 112 | FileSystemUtils.createDirectoryAndParents(lastDir); |
| 113 | } |
| 114 | } catch (IOException e) { |
| 115 | return e; |
| 116 | } |
| 117 | return null; |
| 118 | })); |
| 119 | } |
| 120 | ListenableFuture<List<IOException>> all = Futures.allAsList(futures); |
| 121 | // If the test times out here then there's likely to be a deadlock |
| 122 | List<IOException> exceptions = |
| 123 | all.get(TestUtils.WAIT_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS); |
| 124 | Optional<IOException> error = exceptions.stream().filter(Objects::nonNull).findFirst(); |
| 125 | if (error.isPresent()) { |
| 126 | throw error.get(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | private static List<Path> getSubDirectories(Path base, int loopi, int subDirectoryCount) { |
| 131 | Path path = base; |
| 132 | List<Path> subDirs = new ArrayList<>(); |
| 133 | for (int subDirIndex = 0; subDirIndex < subDirectoryCount; ++subDirIndex) { |
| 134 | path = path.getChild(String.format("%d-%d", loopi, subDirIndex)); |
| 135 | subDirs.add(path); |
| 136 | } |
| 137 | return subDirs; |
| 138 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 139 | } |