Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1 | // Copyright 2015 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 | package com.google.devtools.build.lib.skyframe; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertWithMessage; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 18 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 19 | import com.google.common.base.Preconditions; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
| 21 | import com.google.common.collect.Maps; |
| 22 | import com.google.devtools.build.lib.events.util.EventCollectionApparatus; |
| 23 | import com.google.devtools.build.lib.skyframe.DiffAwarenessManager.ProcessableModifiedFileSet; |
| 24 | import com.google.devtools.build.lib.vfs.FileSystem; |
| 25 | import com.google.devtools.build.lib.vfs.ModifiedFileSet; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.vfs.PathFragment; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 27 | import com.google.devtools.build.lib.vfs.Root; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
juliexxia | 618a076 | 2018-08-17 08:33:52 -0700 | [diff] [blame] | 29 | import com.google.devtools.common.options.OptionsProvider; |
Ulf Adams | 2891ec5 | 2016-10-13 11:56:18 +0000 | [diff] [blame] | 30 | import java.util.List; |
| 31 | import java.util.Map; |
| 32 | import javax.annotation.Nullable; |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 33 | import org.junit.Before; |
| 34 | import org.junit.Test; |
| 35 | import org.junit.runner.RunWith; |
| 36 | import org.junit.runners.JUnit4; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 37 | |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 38 | /** |
| 39 | * Unit tests for {@link DiffAwarenessManager}, especially of the fact that it works in a sequential |
| 40 | * manner and of its correctness in the presence of unprocesed diffs. |
| 41 | */ |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 42 | @RunWith(JUnit4.class) |
| 43 | public class DiffAwarenessManagerTest { |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 44 | private FileSystem fs; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 45 | protected EventCollectionApparatus events; |
| 46 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 47 | @Before |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 48 | public final void createFileSystem() throws Exception { |
ccalvarin | c9efd06 | 2018-07-27 12:46:46 -0700 | [diff] [blame] | 49 | fs = new InMemoryFileSystem(); |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Before |
| 53 | public final void initializeEventCollectionApparatus() { |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 54 | events = new EventCollectionApparatus(); |
| 55 | events.setFailFast(false); |
| 56 | } |
| 57 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 58 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 59 | public void testEverythingModifiedIfNoDiffAwareness() throws Exception { |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 60 | Root pathEntry = Root.fromPath(fs.getPath("/pathEntry")); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 61 | DiffAwarenessFactoryStub factory = new DiffAwarenessFactoryStub(); |
| 62 | DiffAwarenessManager manager = new DiffAwarenessManager(ImmutableList.of(factory)); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 63 | assertWithMessage("Expected EVERYTHING_MODIFIED since there are no factories") |
| 64 | .that( |
| 65 | manager |
juliexxia | 618a076 | 2018-08-17 08:33:52 -0700 | [diff] [blame] | 66 | .getDiff(events.reporter(), pathEntry, OptionsProvider.EMPTY) |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 67 | .getModifiedFileSet()) |
| 68 | .isEqualTo(ModifiedFileSet.EVERYTHING_MODIFIED); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 69 | events.assertNoWarningsOrErrors(); |
| 70 | } |
| 71 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 72 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 73 | public void testResetAndSetPathEntriesCallClose() throws Exception { |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 74 | Root pathEntry = Root.fromPath(fs.getPath("/pathEntry")); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 75 | ModifiedFileSet diff = ModifiedFileSet.NOTHING_MODIFIED; |
| 76 | DiffAwarenessStub diffAwareness1 = new DiffAwarenessStub(ImmutableList.of(diff)); |
| 77 | DiffAwarenessStub diffAwareness2 = new DiffAwarenessStub(ImmutableList.of(diff)); |
| 78 | DiffAwarenessFactoryStub factory = new DiffAwarenessFactoryStub(); |
| 79 | factory.inject(pathEntry, diffAwareness1); |
| 80 | DiffAwarenessManager manager = new DiffAwarenessManager(ImmutableList.of(factory)); |
juliexxia | 618a076 | 2018-08-17 08:33:52 -0700 | [diff] [blame] | 81 | manager.getDiff(events.reporter(), pathEntry, OptionsProvider.EMPTY); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 82 | assertWithMessage("diffAwareness1 shouldn't have been closed yet") |
| 83 | .that(diffAwareness1.closed()) |
| 84 | .isFalse(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 85 | manager.reset(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 86 | assertWithMessage("diffAwareness1 should have been closed by reset") |
| 87 | .that(diffAwareness1.closed()) |
| 88 | .isTrue(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 89 | factory.inject(pathEntry, diffAwareness2); |
juliexxia | 618a076 | 2018-08-17 08:33:52 -0700 | [diff] [blame] | 90 | manager.getDiff(events.reporter(), pathEntry, OptionsProvider.EMPTY); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 91 | assertWithMessage("diffAwareness2 shouldn't have been closed yet") |
| 92 | .that(diffAwareness2.closed()) |
| 93 | .isFalse(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 94 | events.assertNoWarningsOrErrors(); |
| 95 | } |
| 96 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 97 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 98 | public void testHandlesUnprocessedDiffs() throws Exception { |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 99 | Root pathEntry = Root.fromPath(fs.getPath("/pathEntry")); |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 100 | ModifiedFileSet diff1 = ModifiedFileSet.builder().modify(PathFragment.create("file1")).build(); |
| 101 | ModifiedFileSet diff2 = ModifiedFileSet.builder().modify(PathFragment.create("file2")).build(); |
| 102 | ModifiedFileSet diff3 = ModifiedFileSet.builder().modify(PathFragment.create("file3")).build(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 103 | DiffAwarenessStub diffAwareness = |
| 104 | new DiffAwarenessStub(ImmutableList.of(diff1, diff2, diff3, DiffAwarenessStub.BROKEN_DIFF)); |
| 105 | DiffAwarenessFactoryStub factory = new DiffAwarenessFactoryStub(); |
| 106 | factory.inject(pathEntry, diffAwareness); |
| 107 | DiffAwarenessManager manager = new DiffAwarenessManager(ImmutableList.of(factory)); |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 108 | ProcessableModifiedFileSet firstProcessableDiff = |
juliexxia | 618a076 | 2018-08-17 08:33:52 -0700 | [diff] [blame] | 109 | manager.getDiff(events.reporter(), pathEntry, OptionsProvider.EMPTY); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 110 | assertWithMessage("Expected EVERYTHING_MODIFIED on first call to getDiff") |
| 111 | .that(firstProcessableDiff.getModifiedFileSet()) |
| 112 | .isEqualTo(ModifiedFileSet.EVERYTHING_MODIFIED); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 113 | firstProcessableDiff.markProcessed(); |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 114 | ProcessableModifiedFileSet processableDiff1 = |
juliexxia | 618a076 | 2018-08-17 08:33:52 -0700 | [diff] [blame] | 115 | manager.getDiff(events.reporter(), pathEntry, OptionsProvider.EMPTY); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 116 | assertThat(processableDiff1.getModifiedFileSet()).isEqualTo(diff1); |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 117 | ProcessableModifiedFileSet processableDiff2 = |
juliexxia | 618a076 | 2018-08-17 08:33:52 -0700 | [diff] [blame] | 118 | manager.getDiff(events.reporter(), pathEntry, OptionsProvider.EMPTY); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 119 | assertThat(processableDiff2.getModifiedFileSet()) |
| 120 | .isEqualTo(ModifiedFileSet.union(diff1, diff2)); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 121 | processableDiff2.markProcessed(); |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 122 | ProcessableModifiedFileSet processableDiff3 = |
juliexxia | 618a076 | 2018-08-17 08:33:52 -0700 | [diff] [blame] | 123 | manager.getDiff(events.reporter(), pathEntry, OptionsProvider.EMPTY); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 124 | assertThat(processableDiff3.getModifiedFileSet()).isEqualTo(diff3); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 125 | events.assertNoWarningsOrErrors(); |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 126 | ProcessableModifiedFileSet processableDiff4 = |
juliexxia | 618a076 | 2018-08-17 08:33:52 -0700 | [diff] [blame] | 127 | manager.getDiff(events.reporter(), pathEntry, OptionsProvider.EMPTY); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 128 | assertThat(processableDiff4.getModifiedFileSet()) |
| 129 | .isEqualTo(ModifiedFileSet.EVERYTHING_MODIFIED); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 130 | events.assertContainsWarning("error"); |
| 131 | } |
| 132 | |
Han-Wen Nienhuys | 3b2eae3 | 2015-10-28 16:35:08 +0000 | [diff] [blame] | 133 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 134 | public void testHandlesBrokenDiffs() throws Exception { |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 135 | Root pathEntry = Root.fromPath(fs.getPath("/pathEntry")); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 136 | DiffAwarenessFactoryStub factory1 = new DiffAwarenessFactoryStub(); |
| 137 | DiffAwarenessStub diffAwareness1 = |
| 138 | new DiffAwarenessStub(ImmutableList.<ModifiedFileSet>of(), 1); |
| 139 | factory1.inject(pathEntry, diffAwareness1); |
| 140 | DiffAwarenessFactoryStub factory2 = new DiffAwarenessFactoryStub(); |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 141 | ModifiedFileSet diff2 = ModifiedFileSet.builder().modify(PathFragment.create("file2")).build(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 142 | DiffAwarenessStub diffAwareness2 = |
| 143 | new DiffAwarenessStub(ImmutableList.of(diff2, DiffAwarenessStub.BROKEN_DIFF)); |
| 144 | factory2.inject(pathEntry, diffAwareness2); |
| 145 | DiffAwarenessFactoryStub factory3 = new DiffAwarenessFactoryStub(); |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 146 | ModifiedFileSet diff3 = ModifiedFileSet.builder().modify(PathFragment.create("file3")).build(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 147 | DiffAwarenessStub diffAwareness3 = new DiffAwarenessStub(ImmutableList.of(diff3)); |
| 148 | factory3.inject(pathEntry, diffAwareness3); |
| 149 | DiffAwarenessManager manager = |
| 150 | new DiffAwarenessManager(ImmutableList.of(factory1, factory2, factory3)); |
| 151 | |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 152 | ProcessableModifiedFileSet processableDiff = |
juliexxia | 618a076 | 2018-08-17 08:33:52 -0700 | [diff] [blame] | 153 | manager.getDiff(events.reporter(), pathEntry, OptionsProvider.EMPTY); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 154 | events.assertNoWarningsOrErrors(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 155 | assertWithMessage("Expected EVERYTHING_MODIFIED on first call to getDiff for diffAwareness1") |
| 156 | .that(processableDiff.getModifiedFileSet()) |
| 157 | .isEqualTo(ModifiedFileSet.EVERYTHING_MODIFIED); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 158 | processableDiff.markProcessed(); |
| 159 | |
juliexxia | 618a076 | 2018-08-17 08:33:52 -0700 | [diff] [blame] | 160 | processableDiff = manager.getDiff(events.reporter(), pathEntry, OptionsProvider.EMPTY); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 161 | events.assertContainsEventWithFrequency("error in getCurrentView", 1); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 162 | assertWithMessage("Expected EVERYTHING_MODIFIED because of broken getCurrentView") |
| 163 | .that(processableDiff.getModifiedFileSet()) |
| 164 | .isEqualTo(ModifiedFileSet.EVERYTHING_MODIFIED); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 165 | processableDiff.markProcessed(); |
| 166 | factory1.remove(pathEntry); |
| 167 | |
juliexxia | 618a076 | 2018-08-17 08:33:52 -0700 | [diff] [blame] | 168 | processableDiff = manager.getDiff(events.reporter(), pathEntry, OptionsProvider.EMPTY); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 169 | assertWithMessage("Expected EVERYTHING_MODIFIED on first call to getDiff for diffAwareness2") |
| 170 | .that(processableDiff.getModifiedFileSet()) |
| 171 | .isEqualTo(ModifiedFileSet.EVERYTHING_MODIFIED); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 172 | processableDiff.markProcessed(); |
| 173 | |
juliexxia | 618a076 | 2018-08-17 08:33:52 -0700 | [diff] [blame] | 174 | processableDiff = manager.getDiff(events.reporter(), pathEntry, OptionsProvider.EMPTY); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 175 | assertThat(processableDiff.getModifiedFileSet()).isEqualTo(diff2); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 176 | processableDiff.markProcessed(); |
| 177 | |
juliexxia | 618a076 | 2018-08-17 08:33:52 -0700 | [diff] [blame] | 178 | processableDiff = manager.getDiff(events.reporter(), pathEntry, OptionsProvider.EMPTY); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 179 | events.assertContainsEventWithFrequency("error in getDiff", 1); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 180 | assertWithMessage("Expected EVERYTHING_MODIFIED because of broken getDiff") |
| 181 | .that(processableDiff.getModifiedFileSet()) |
| 182 | .isEqualTo(ModifiedFileSet.EVERYTHING_MODIFIED); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 183 | processableDiff.markProcessed(); |
| 184 | factory2.remove(pathEntry); |
| 185 | |
juliexxia | 618a076 | 2018-08-17 08:33:52 -0700 | [diff] [blame] | 186 | processableDiff = manager.getDiff(events.reporter(), pathEntry, OptionsProvider.EMPTY); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 187 | assertWithMessage("Expected EVERYTHING_MODIFIED on first call to getDiff for diffAwareness3") |
| 188 | .that(processableDiff.getModifiedFileSet()) |
| 189 | .isEqualTo(ModifiedFileSet.EVERYTHING_MODIFIED); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 190 | processableDiff.markProcessed(); |
| 191 | |
juliexxia | 618a076 | 2018-08-17 08:33:52 -0700 | [diff] [blame] | 192 | processableDiff = manager.getDiff(events.reporter(), pathEntry, OptionsProvider.EMPTY); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 193 | assertThat(processableDiff.getModifiedFileSet()).isEqualTo(diff3); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 194 | processableDiff.markProcessed(); |
| 195 | } |
| 196 | |
| 197 | private static class DiffAwarenessFactoryStub implements DiffAwareness.Factory { |
| 198 | |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 199 | private final Map<Root, DiffAwareness> diffAwarenesses = Maps.newHashMap(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 200 | |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 201 | public void inject(Root pathEntry, DiffAwareness diffAwareness) { |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 202 | diffAwarenesses.put(pathEntry, diffAwareness); |
| 203 | } |
| 204 | |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 205 | public void remove(Root pathEntry) { |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 206 | diffAwarenesses.remove(pathEntry); |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | @Nullable |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 211 | public DiffAwareness maybeCreate(Root pathEntry) { |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 212 | return diffAwarenesses.get(pathEntry); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | private static class DiffAwarenessStub implements DiffAwareness { |
| 217 | |
| 218 | public static final ModifiedFileSet BROKEN_DIFF = |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 219 | ModifiedFileSet.builder().modify(PathFragment.create("special broken marker")).build(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 220 | |
| 221 | private boolean closed = false; |
| 222 | private int curSequenceNum = 0; |
| 223 | private final List<ModifiedFileSet> sequentialDiffs; |
| 224 | private final int brokenViewNum; |
| 225 | |
| 226 | public DiffAwarenessStub(List<ModifiedFileSet> sequentialDiffs) { |
| 227 | this(sequentialDiffs, -1); |
| 228 | } |
| 229 | |
| 230 | public DiffAwarenessStub(List<ModifiedFileSet> sequentialDiffs, int brokenViewNum) { |
| 231 | this.sequentialDiffs = sequentialDiffs; |
| 232 | this.brokenViewNum = brokenViewNum; |
| 233 | } |
| 234 | |
| 235 | private static class ViewStub implements DiffAwareness.View { |
| 236 | private final int sequenceNum; |
| 237 | |
| 238 | public ViewStub(int sequenceNum) { |
| 239 | this.sequenceNum = sequenceNum; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | @Override |
juliexxia | 618a076 | 2018-08-17 08:33:52 -0700 | [diff] [blame] | 244 | public View getCurrentView(OptionsProvider options) throws BrokenDiffAwarenessException { |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 245 | if (curSequenceNum == brokenViewNum) { |
| 246 | throw new BrokenDiffAwarenessException("error in getCurrentView"); |
| 247 | } |
| 248 | return new ViewStub(curSequenceNum++); |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public ModifiedFileSet getDiff(View oldView, View newView) throws BrokenDiffAwarenessException { |
| 253 | assertThat(oldView).isInstanceOf(ViewStub.class); |
| 254 | assertThat(newView).isInstanceOf(ViewStub.class); |
| 255 | ViewStub oldViewStub = (ViewStub) oldView; |
| 256 | ViewStub newViewStub = (ViewStub) newView; |
| 257 | Preconditions.checkState(newViewStub.sequenceNum >= oldViewStub.sequenceNum); |
| 258 | ModifiedFileSet diff = ModifiedFileSet.NOTHING_MODIFIED; |
| 259 | for (int num = oldViewStub.sequenceNum; num < newViewStub.sequenceNum; num++) { |
| 260 | ModifiedFileSet incrementalDiff = sequentialDiffs.get(num); |
| 261 | if (incrementalDiff == BROKEN_DIFF) { |
| 262 | throw new BrokenDiffAwarenessException("error in getDiff"); |
| 263 | } |
| 264 | diff = ModifiedFileSet.union(diff, incrementalDiff); |
| 265 | } |
| 266 | return diff; |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public String name() { |
| 271 | return "testingstub"; |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public void close() { |
| 276 | closed = true; |
| 277 | } |
| 278 | |
| 279 | public boolean closed() { |
| 280 | return closed; |
| 281 | } |
| 282 | } |
| 283 | } |