Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 1 | // Copyright 2016 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.runtime; |
| 15 | |
| 16 | import static org.junit.Assert.assertFalse; |
| 17 | import static org.junit.Assert.assertTrue; |
| 18 | import static org.mockito.Mockito.when; |
| 19 | |
Klaus Aehlig | d232d23 | 2016-04-13 09:51:25 +0000 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.actions.Action; |
| 22 | import com.google.devtools.build.lib.actions.ActionCompletionEvent; |
Klaus Aehlig | a4c7d25 | 2016-05-20 10:44:21 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.actions.ActionOwner; |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.actions.ActionStartedEvent; |
| 25 | import com.google.devtools.build.lib.actions.Artifact; |
| 26 | import com.google.devtools.build.lib.actions.Root; |
Klaus Aehlig | d232d23 | 2016-04-13 09:51:25 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.analysis.ConfiguredTarget; |
| 28 | import com.google.devtools.build.lib.buildtool.buildevent.TestFilteringCompleteEvent; |
Klaus Aehlig | b8d0c6b | 2016-04-13 11:06:41 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.cmdline.Label; |
Klaus Aehlig | c6fd6bb | 2016-05-27 11:42:32 +0000 | [diff] [blame^] | 30 | import com.google.devtools.build.lib.skyframe.LoadingPhaseStartedEvent; |
| 31 | import com.google.devtools.build.lib.skyframe.PackageProgressReceiver; |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 32 | import com.google.devtools.build.lib.testutil.FoundationTestCase; |
Klaus Aehlig | ed453e0 | 2016-04-15 11:29:05 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.testutil.LoggingTerminalWriter; |
Klaus Aehlig | a63d961 | 2016-04-04 12:15:23 +0000 | [diff] [blame] | 34 | import com.google.devtools.build.lib.testutil.ManualClock; |
Klaus Aehlig | c6fd6bb | 2016-05-27 11:42:32 +0000 | [diff] [blame^] | 35 | import com.google.devtools.build.lib.util.Pair; |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 36 | import com.google.devtools.build.lib.vfs.Path; |
| 37 | import com.google.devtools.build.lib.vfs.PathFragment; |
Klaus Aehlig | b8d0c6b | 2016-04-13 11:06:41 +0000 | [diff] [blame] | 38 | import com.google.devtools.build.lib.view.test.TestStatus.BlazeTestStatus; |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 39 | |
| 40 | import org.junit.Test; |
| 41 | import org.junit.runner.RunWith; |
| 42 | import org.junit.runners.JUnit4; |
| 43 | import org.mockito.Mockito; |
| 44 | |
| 45 | import java.io.IOException; |
Klaus Aehlig | e25642a | 2016-04-04 13:31:28 +0000 | [diff] [blame] | 46 | import java.util.concurrent.TimeUnit; |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 47 | |
| 48 | /** |
Klaus Aehlig | c23ba45 | 2016-04-06 12:16:39 +0000 | [diff] [blame] | 49 | * Tests {@link ExperimentalStateTracker}. |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 50 | */ |
| 51 | @RunWith(JUnit4.class) |
| 52 | public class ExperimentalStateTrackerTest extends FoundationTestCase { |
| 53 | |
| 54 | private Action mockAction(String progressMessage, String primaryOutput) { |
| 55 | Path path = outputBase.getRelative(new PathFragment(primaryOutput)); |
| 56 | Artifact artifact = new Artifact(path, Root.asSourceRoot(path)); |
| 57 | |
| 58 | Action action = Mockito.mock(Action.class); |
| 59 | when(action.getProgressMessage()).thenReturn(progressMessage); |
| 60 | when(action.getPrimaryOutput()).thenReturn(artifact); |
| 61 | return action; |
| 62 | } |
| 63 | |
Klaus Aehlig | 6d0876a | 2016-04-15 14:41:07 +0000 | [diff] [blame] | 64 | private int longestLine(String output) { |
| 65 | int maxLength = 0; |
| 66 | for (String line : output.split("\n")) { |
| 67 | maxLength = Math.max(maxLength, line.length()); |
| 68 | } |
| 69 | return maxLength; |
| 70 | } |
| 71 | |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 72 | @Test |
Klaus Aehlig | c6fd6bb | 2016-05-27 11:42:32 +0000 | [diff] [blame^] | 73 | public void testLoadingActivity() throws IOException { |
| 74 | // During loading phase, state and activity, as reported by the PackageProgressReceiver, |
| 75 | // should be visible in the progress bar. |
| 76 | String state = "42 packages loaded"; |
| 77 | String activity = "currently loading //src/foo/bar and 17 more"; |
| 78 | PackageProgressReceiver progress = Mockito.mock(PackageProgressReceiver.class); |
| 79 | when(progress.progressState()).thenReturn(new Pair<String, String>(state, activity)); |
| 80 | |
| 81 | ManualClock clock = new ManualClock(); |
| 82 | ExperimentalStateTracker stateTracker = new ExperimentalStateTracker(clock); |
| 83 | |
| 84 | stateTracker.loadingStarted(new LoadingPhaseStartedEvent(progress)); |
| 85 | |
| 86 | LoggingTerminalWriter terminalWriter = new LoggingTerminalWriter(/*discardHighlight=*/ true); |
| 87 | stateTracker.writeProgressBar(terminalWriter); |
| 88 | String output = terminalWriter.getTranscript(); |
| 89 | |
| 90 | assertTrue( |
| 91 | "Output should indicate that we are in the loading phase, but was:\n" + output, |
| 92 | output.contains("Loading")); |
| 93 | assertTrue( |
| 94 | "Output should contain loading state '" + state + "', but was:\n" + output, |
| 95 | output.contains(state)); |
| 96 | assertTrue( |
| 97 | "Output should contain loading state '" + activity + "', but was:\n" + output, |
| 98 | output.contains(activity)); |
| 99 | } |
| 100 | |
| 101 | @Test |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 102 | public void testActionVisible() throws IOException { |
| 103 | // If there is only one action running, it should be visible |
Klaus Aehlig | c0c8884 | 2016-04-11 12:07:38 +0000 | [diff] [blame] | 104 | // somewhere in the progress bar, and also the short version thereof. |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 105 | |
| 106 | String message = "Building foo"; |
Klaus Aehlig | a63d961 | 2016-04-04 12:15:23 +0000 | [diff] [blame] | 107 | ManualClock clock = new ManualClock(); |
| 108 | clock.advanceMillis(120000); |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 109 | |
Klaus Aehlig | a63d961 | 2016-04-04 12:15:23 +0000 | [diff] [blame] | 110 | ExperimentalStateTracker stateTracker = new ExperimentalStateTracker(clock); |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 111 | stateTracker.actionStarted(new ActionStartedEvent(mockAction(message, "bar/foo"), 123456789)); |
Klaus Aehlig | c0c8884 | 2016-04-11 12:07:38 +0000 | [diff] [blame] | 112 | |
Klaus Aehlig | ed453e0 | 2016-04-15 11:29:05 +0000 | [diff] [blame] | 113 | LoggingTerminalWriter terminalWriter = new LoggingTerminalWriter(/*discardHighlight=*/ true); |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 114 | stateTracker.writeProgressBar(terminalWriter); |
Klaus Aehlig | ed453e0 | 2016-04-15 11:29:05 +0000 | [diff] [blame] | 115 | String output = terminalWriter.getTranscript(); |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 116 | assertTrue( |
| 117 | "Action message '" + message + "' should be present in output: " + output, |
| 118 | output.contains(message)); |
Klaus Aehlig | c0c8884 | 2016-04-11 12:07:38 +0000 | [diff] [blame] | 119 | |
| 120 | terminalWriter = new LoggingTerminalWriter(); |
| 121 | stateTracker.writeProgressBar(terminalWriter, /* shortVersion=*/ true); |
Klaus Aehlig | ed453e0 | 2016-04-15 11:29:05 +0000 | [diff] [blame] | 122 | output = terminalWriter.getTranscript(); |
Klaus Aehlig | c0c8884 | 2016-04-11 12:07:38 +0000 | [diff] [blame] | 123 | assertTrue( |
| 124 | "Action message '" + message + "' should be present in short output: " + output, |
| 125 | output.contains(message)); |
| 126 | |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void testCompletedActionNotShown() throws IOException { |
Klaus Aehlig | c0c8884 | 2016-04-11 12:07:38 +0000 | [diff] [blame] | 131 | // Completed actions should not be reported in the progress bar, nor in the |
| 132 | // short progress bar. |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 133 | |
| 134 | String messageFast = "Running quick action"; |
| 135 | String messageSlow = "Running slow action"; |
| 136 | |
Klaus Aehlig | a63d961 | 2016-04-04 12:15:23 +0000 | [diff] [blame] | 137 | ManualClock clock = new ManualClock(); |
| 138 | clock.advanceMillis(120000); |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 139 | Action fastAction = mockAction(messageFast, "foo/fast"); |
| 140 | Action slowAction = mockAction(messageSlow, "bar/slow"); |
Klaus Aehlig | a63d961 | 2016-04-04 12:15:23 +0000 | [diff] [blame] | 141 | ExperimentalStateTracker stateTracker = new ExperimentalStateTracker(clock); |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 142 | stateTracker.actionStarted(new ActionStartedEvent(fastAction, 123456789)); |
| 143 | stateTracker.actionStarted(new ActionStartedEvent(slowAction, 123456999)); |
| 144 | stateTracker.actionCompletion(new ActionCompletionEvent(20, fastAction)); |
| 145 | |
Klaus Aehlig | ed453e0 | 2016-04-15 11:29:05 +0000 | [diff] [blame] | 146 | LoggingTerminalWriter terminalWriter = new LoggingTerminalWriter(/*discardHighlight=*/ true); |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 147 | stateTracker.writeProgressBar(terminalWriter); |
Klaus Aehlig | ed453e0 | 2016-04-15 11:29:05 +0000 | [diff] [blame] | 148 | String output = terminalWriter.getTranscript(); |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 149 | assertFalse( |
| 150 | "Completed action '" + messageFast + "' should not be present in output: " + output, |
| 151 | output.contains(messageFast)); |
| 152 | assertTrue( |
| 153 | "Only running action '" + messageSlow + "' should be present in output: " + output, |
| 154 | output.contains(messageSlow)); |
Klaus Aehlig | c0c8884 | 2016-04-11 12:07:38 +0000 | [diff] [blame] | 155 | |
| 156 | terminalWriter = new LoggingTerminalWriter(); |
| 157 | stateTracker.writeProgressBar(terminalWriter, /* shortVersion=*/ true); |
Klaus Aehlig | ed453e0 | 2016-04-15 11:29:05 +0000 | [diff] [blame] | 158 | output = terminalWriter.getTranscript(); |
Klaus Aehlig | c0c8884 | 2016-04-11 12:07:38 +0000 | [diff] [blame] | 159 | assertFalse( |
| 160 | "Completed action '" + messageFast + "' should not be present in short output: " + output, |
| 161 | output.contains(messageFast)); |
| 162 | assertTrue( |
| 163 | "Only running action '" + messageSlow + "' should be present in short output: " + output, |
| 164 | output.contains(messageSlow)); |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | @Test |
| 168 | public void testOldestActionVisible() throws IOException { |
Klaus Aehlig | c0c8884 | 2016-04-11 12:07:38 +0000 | [diff] [blame] | 169 | // The earliest-started action is always visible somehow in the progress bar |
| 170 | // and its short version. |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 171 | |
| 172 | String messageOld = "Running the first-started action"; |
| 173 | |
Klaus Aehlig | a63d961 | 2016-04-04 12:15:23 +0000 | [diff] [blame] | 174 | ManualClock clock = new ManualClock(); |
| 175 | clock.advanceMillis(120000); |
| 176 | ExperimentalStateTracker stateTracker = new ExperimentalStateTracker(clock); |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 177 | stateTracker.actionStarted( |
| 178 | new ActionStartedEvent(mockAction(messageOld, "bar/foo"), 123456789)); |
| 179 | for (int i = 0; i < 30; i++) { |
| 180 | stateTracker.actionStarted( |
| 181 | new ActionStartedEvent( |
| 182 | mockAction("Other action " + i, "some/other/actions/number" + i), 123456790 + i)); |
| 183 | } |
| 184 | |
Klaus Aehlig | ed453e0 | 2016-04-15 11:29:05 +0000 | [diff] [blame] | 185 | LoggingTerminalWriter terminalWriter = new LoggingTerminalWriter(/*discardHighlight=*/ true); |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 186 | stateTracker.writeProgressBar(terminalWriter); |
Klaus Aehlig | ed453e0 | 2016-04-15 11:29:05 +0000 | [diff] [blame] | 187 | String output = terminalWriter.getTranscript(); |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 188 | assertTrue( |
| 189 | "Longest running action '" + messageOld + "' should be visible in output: " + output, |
| 190 | output.contains(messageOld)); |
Klaus Aehlig | c0c8884 | 2016-04-11 12:07:38 +0000 | [diff] [blame] | 191 | |
Klaus Aehlig | ed453e0 | 2016-04-15 11:29:05 +0000 | [diff] [blame] | 192 | terminalWriter = new LoggingTerminalWriter(/*discardHighlight=*/ true); |
Klaus Aehlig | c0c8884 | 2016-04-11 12:07:38 +0000 | [diff] [blame] | 193 | stateTracker.writeProgressBar(terminalWriter, /* shortVersion=*/ true); |
Klaus Aehlig | ed453e0 | 2016-04-15 11:29:05 +0000 | [diff] [blame] | 194 | output = terminalWriter.getTranscript(); |
Klaus Aehlig | c0c8884 | 2016-04-11 12:07:38 +0000 | [diff] [blame] | 195 | assertTrue( |
| 196 | "Longest running action '" + messageOld + "' should be visible in short output: " + output, |
| 197 | output.contains(messageOld)); |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 198 | } |
Klaus Aehlig | e25642a | 2016-04-04 13:31:28 +0000 | [diff] [blame] | 199 | |
| 200 | @Test |
| 201 | public void testTimesShown() throws IOException { |
| 202 | // For sufficiently long running actions, the time that has passed since their start is shown. |
Klaus Aehlig | c0c8884 | 2016-04-11 12:07:38 +0000 | [diff] [blame] | 203 | // In the short version of the progress bar, this should be true at least for the oldest action. |
Klaus Aehlig | e25642a | 2016-04-04 13:31:28 +0000 | [diff] [blame] | 204 | |
| 205 | ManualClock clock = new ManualClock(); |
| 206 | clock.advanceMillis(TimeUnit.SECONDS.toMillis(123)); |
| 207 | ExperimentalStateTracker stateTracker = new ExperimentalStateTracker(clock); |
| 208 | clock.advanceMillis(TimeUnit.SECONDS.toMillis(2)); |
| 209 | |
| 210 | stateTracker.actionStarted( |
| 211 | new ActionStartedEvent(mockAction("First action", "foo"), clock.nanoTime())); |
| 212 | clock.advanceMillis(TimeUnit.SECONDS.toMillis(7)); |
| 213 | stateTracker.actionStarted( |
| 214 | new ActionStartedEvent(mockAction("Second action", "bar"), clock.nanoTime())); |
| 215 | clock.advanceMillis(TimeUnit.SECONDS.toMillis(20)); |
| 216 | |
Klaus Aehlig | ed453e0 | 2016-04-15 11:29:05 +0000 | [diff] [blame] | 217 | LoggingTerminalWriter terminalWriter = new LoggingTerminalWriter(/*discardHighlight=*/ true); |
Klaus Aehlig | e25642a | 2016-04-04 13:31:28 +0000 | [diff] [blame] | 218 | stateTracker.writeProgressBar(terminalWriter); |
Klaus Aehlig | ed453e0 | 2016-04-15 11:29:05 +0000 | [diff] [blame] | 219 | String output = terminalWriter.getTranscript(); |
Klaus Aehlig | e25642a | 2016-04-04 13:31:28 +0000 | [diff] [blame] | 220 | assertTrue( |
| 221 | "Runtime of first action should be visible in output: " + output, output.contains("27s")); |
| 222 | assertTrue( |
| 223 | "Runtime of second action should be visible in output: " + output, output.contains("20s")); |
Klaus Aehlig | c0c8884 | 2016-04-11 12:07:38 +0000 | [diff] [blame] | 224 | |
Klaus Aehlig | ed453e0 | 2016-04-15 11:29:05 +0000 | [diff] [blame] | 225 | terminalWriter = new LoggingTerminalWriter(/*discardHighlight=*/ true); |
Klaus Aehlig | c0c8884 | 2016-04-11 12:07:38 +0000 | [diff] [blame] | 226 | stateTracker.writeProgressBar(terminalWriter, /* shortVersion=*/ true); |
Klaus Aehlig | ed453e0 | 2016-04-15 11:29:05 +0000 | [diff] [blame] | 227 | output = terminalWriter.getTranscript(); |
Klaus Aehlig | c0c8884 | 2016-04-11 12:07:38 +0000 | [diff] [blame] | 228 | assertTrue( |
| 229 | "Runtime of first action should be visible in short output: " + output, |
| 230 | output.contains("27s")); |
Klaus Aehlig | e25642a | 2016-04-04 13:31:28 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Klaus Aehlig | 2ce24d4 | 2016-04-04 15:54:58 +0000 | [diff] [blame] | 233 | @Test |
| 234 | public void initialProgressBarTimeIndependent() { |
| 235 | ManualClock clock = new ManualClock(); |
| 236 | clock.advanceMillis(TimeUnit.SECONDS.toMillis(123)); |
| 237 | ExperimentalStateTracker stateTracker = new ExperimentalStateTracker(clock); |
| 238 | |
| 239 | assertFalse( |
| 240 | "Initial progress status should be time independent", |
| 241 | stateTracker.progressBarTimeDependent()); |
| 242 | } |
| 243 | |
| 244 | @Test |
| 245 | public void runningActionTimeIndependent() { |
| 246 | ManualClock clock = new ManualClock(); |
| 247 | clock.advanceMillis(TimeUnit.SECONDS.toMillis(123)); |
| 248 | ExperimentalStateTracker stateTracker = new ExperimentalStateTracker(clock); |
| 249 | clock.advanceMillis(TimeUnit.SECONDS.toMillis(1)); |
| 250 | stateTracker.actionStarted( |
| 251 | new ActionStartedEvent(mockAction("Some action", "foo"), clock.nanoTime())); |
| 252 | |
| 253 | assertTrue( |
| 254 | "Progress bar showing a running action should be time dependent", |
| 255 | stateTracker.progressBarTimeDependent()); |
| 256 | } |
Klaus Aehlig | d232d23 | 2016-04-13 09:51:25 +0000 | [diff] [blame] | 257 | |
| 258 | @Test |
Klaus Aehlig | b8d0c6b | 2016-04-13 11:06:41 +0000 | [diff] [blame] | 259 | public void testCountVisible() throws Exception { |
Klaus Aehlig | d232d23 | 2016-04-13 09:51:25 +0000 | [diff] [blame] | 260 | // The test count should be visible in the status bar, as well as the short status bar |
| 261 | ManualClock clock = new ManualClock(); |
| 262 | ExperimentalStateTracker stateTracker = new ExperimentalStateTracker(clock); |
| 263 | TestFilteringCompleteEvent filteringComplete = Mockito.mock(TestFilteringCompleteEvent.class); |
Klaus Aehlig | b8d0c6b | 2016-04-13 11:06:41 +0000 | [diff] [blame] | 264 | Label labelA = Label.parseAbsolute("//foo/bar:baz"); |
Klaus Aehlig | d232d23 | 2016-04-13 09:51:25 +0000 | [diff] [blame] | 265 | ConfiguredTarget targetA = Mockito.mock(ConfiguredTarget.class); |
Klaus Aehlig | b8d0c6b | 2016-04-13 11:06:41 +0000 | [diff] [blame] | 266 | when(targetA.getLabel()).thenReturn(labelA); |
Klaus Aehlig | d232d23 | 2016-04-13 09:51:25 +0000 | [diff] [blame] | 267 | ConfiguredTarget targetB = Mockito.mock(ConfiguredTarget.class); |
| 268 | when(filteringComplete.getTestTargets()).thenReturn(ImmutableSet.of(targetA, targetB)); |
| 269 | TestSummary testSummary = Mockito.mock(TestSummary.class); |
Klaus Aehlig | b8d0c6b | 2016-04-13 11:06:41 +0000 | [diff] [blame] | 270 | when(testSummary.getTarget()).thenReturn(targetA); |
| 271 | |
Klaus Aehlig | d232d23 | 2016-04-13 09:51:25 +0000 | [diff] [blame] | 272 | stateTracker.testFilteringComplete(filteringComplete); |
| 273 | stateTracker.testSummary(testSummary); |
| 274 | |
Klaus Aehlig | ed453e0 | 2016-04-15 11:29:05 +0000 | [diff] [blame] | 275 | LoggingTerminalWriter terminalWriter = new LoggingTerminalWriter(/*discardHighlight=*/ true); |
Klaus Aehlig | d232d23 | 2016-04-13 09:51:25 +0000 | [diff] [blame] | 276 | stateTracker.writeProgressBar(terminalWriter); |
Klaus Aehlig | ed453e0 | 2016-04-15 11:29:05 +0000 | [diff] [blame] | 277 | String output = terminalWriter.getTranscript(); |
Klaus Aehlig | d232d23 | 2016-04-13 09:51:25 +0000 | [diff] [blame] | 278 | assertTrue( |
| 279 | "Test count should be visible in output: " + output, output.contains(" 1 / 2 tests")); |
| 280 | |
Klaus Aehlig | ed453e0 | 2016-04-15 11:29:05 +0000 | [diff] [blame] | 281 | terminalWriter = new LoggingTerminalWriter(/*discardHighlight=*/ true); |
Klaus Aehlig | d232d23 | 2016-04-13 09:51:25 +0000 | [diff] [blame] | 282 | stateTracker.writeProgressBar(terminalWriter, /* shortVersion=*/ true); |
Klaus Aehlig | ed453e0 | 2016-04-15 11:29:05 +0000 | [diff] [blame] | 283 | output = terminalWriter.getTranscript(); |
Klaus Aehlig | d232d23 | 2016-04-13 09:51:25 +0000 | [diff] [blame] | 284 | assertTrue( |
| 285 | "Test count should be visible in short output: " + output, output.contains(" 1 / 2 tests")); |
| 286 | } |
Klaus Aehlig | b8d0c6b | 2016-04-13 11:06:41 +0000 | [diff] [blame] | 287 | |
| 288 | @Test |
| 289 | public void testPassedVisible() throws Exception { |
| 290 | // The last test that passed should still be visible in the long status bar. |
| 291 | ManualClock clock = new ManualClock(); |
| 292 | ExperimentalStateTracker stateTracker = new ExperimentalStateTracker(clock); |
| 293 | TestFilteringCompleteEvent filteringComplete = Mockito.mock(TestFilteringCompleteEvent.class); |
| 294 | Label labelA = Label.parseAbsolute("//foo/bar:baz"); |
| 295 | ConfiguredTarget targetA = Mockito.mock(ConfiguredTarget.class); |
| 296 | when(targetA.getLabel()).thenReturn(labelA); |
| 297 | ConfiguredTarget targetB = Mockito.mock(ConfiguredTarget.class); |
| 298 | when(filteringComplete.getTestTargets()).thenReturn(ImmutableSet.of(targetA, targetB)); |
| 299 | TestSummary testSummary = Mockito.mock(TestSummary.class); |
| 300 | when(testSummary.getStatus()).thenReturn(BlazeTestStatus.PASSED); |
| 301 | when(testSummary.getTarget()).thenReturn(targetA); |
| 302 | |
| 303 | stateTracker.testFilteringComplete(filteringComplete); |
| 304 | stateTracker.testSummary(testSummary); |
| 305 | |
Klaus Aehlig | ed453e0 | 2016-04-15 11:29:05 +0000 | [diff] [blame] | 306 | LoggingTerminalWriter terminalWriter = new LoggingTerminalWriter(/*discardHighlight=*/ true); |
Klaus Aehlig | b8d0c6b | 2016-04-13 11:06:41 +0000 | [diff] [blame] | 307 | stateTracker.writeProgressBar(terminalWriter); |
Klaus Aehlig | ed453e0 | 2016-04-15 11:29:05 +0000 | [diff] [blame] | 308 | String output = terminalWriter.getTranscript(); |
Klaus Aehlig | b8d0c6b | 2016-04-13 11:06:41 +0000 | [diff] [blame] | 309 | |
| 310 | assertTrue( |
| 311 | "Label " + labelA.toString() + " should be present in progress bar: " + output, |
| 312 | output.contains(labelA.toString())); |
| 313 | } |
Klaus Aehlig | 6d0876a | 2016-04-15 14:41:07 +0000 | [diff] [blame] | 314 | |
Klaus Aehlig | a4c7d25 | 2016-05-20 10:44:21 +0000 | [diff] [blame] | 315 | @Test |
| 316 | public void testSensibleShortening() throws Exception { |
| 317 | // Verify that in the typical case, we shorten the progress message by shortening |
| 318 | // the path implicit in it, that can also be extracted from the label. In particular, |
| 319 | // the parts |
| 320 | ManualClock clock = new ManualClock(); |
| 321 | ExperimentalStateTracker stateTracker = new ExperimentalStateTracker(clock, 70); |
| 322 | Action action = mockAction( |
| 323 | "Building some/very/very/long/path/for/some/library/directory/foo.jar (42 source files)", |
| 324 | "/home/user/bazel/out/abcdef/some/very/very/long/path/for/some/library/directory/foo.jar"); |
| 325 | Label label = |
| 326 | Label.parseAbsolute("//some/very/very/long/path/for/some/library/directory:libfoo"); |
| 327 | ActionOwner owner = new ActionOwner(label, null, null, null, "fedcba", null); |
| 328 | when(action.getOwner()).thenReturn(owner); |
| 329 | |
| 330 | clock.advanceMillis(TimeUnit.SECONDS.toMillis(3)); |
| 331 | stateTracker.actionStarted(new ActionStartedEvent(action, clock.nanoTime())); |
| 332 | clock.advanceMillis(TimeUnit.SECONDS.toMillis(5)); |
| 333 | |
| 334 | LoggingTerminalWriter terminalWriter = new LoggingTerminalWriter(/*discardHighlight=*/ true); |
| 335 | stateTracker.writeProgressBar(terminalWriter); |
| 336 | String output = terminalWriter.getTranscript(); |
| 337 | |
| 338 | assertTrue( |
| 339 | "Progress bar should contain 'Building ', but was:\n" + output, |
| 340 | output.contains("Building ")); |
| 341 | assertTrue( |
| 342 | "Progress bar should contain 'foo.jar (42 source files)', but was:\n" + output, |
| 343 | output.contains("foo.jar (42 source files)")); |
| 344 | } |
| 345 | |
Klaus Aehlig | 6d0876a | 2016-04-15 14:41:07 +0000 | [diff] [blame] | 346 | private void doTestOutputLength(boolean withTest, int actions) throws Exception { |
| 347 | // If we target 70 characters, then there should be enough space to both, |
| 348 | // keep the line limit, and show the local part of the running actions and |
| 349 | // the passed test. |
| 350 | ManualClock clock = new ManualClock(); |
| 351 | ExperimentalStateTracker stateTracker = new ExperimentalStateTracker(clock, 70); |
| 352 | |
| 353 | Action foobuildAction = mockAction( |
| 354 | "Building //src/some/very/long/path/long/long/long/long/long/long/long/foo/foobuild.jar", |
| 355 | "//src/some/very/long/path/long/long/long/long/long/long/long/foo:foobuild"); |
| 356 | Action bazbuildAction = mockAction( |
| 357 | "Building //src/some/very/long/path/long/long/long/long/long/long/long/baz/bazbuild.jar", |
| 358 | "//src/some/very/long/path/long/long/long/long/long/long/long/baz:bazbuild"); |
| 359 | |
| 360 | Label bartestLabel = Label.parseAbsolute( |
| 361 | "//src/another/very/long/long/path/long/long/long/long/long/long/long/long/bars:bartest"); |
| 362 | ConfiguredTarget bartestTarget = Mockito.mock(ConfiguredTarget.class); |
| 363 | when(bartestTarget.getLabel()).thenReturn(bartestLabel); |
| 364 | |
| 365 | TestFilteringCompleteEvent filteringComplete = Mockito.mock(TestFilteringCompleteEvent.class); |
| 366 | when(filteringComplete.getTestTargets()).thenReturn(ImmutableSet.of(bartestTarget)); |
| 367 | |
| 368 | TestSummary testSummary = Mockito.mock(TestSummary.class); |
| 369 | when(testSummary.getStatus()).thenReturn(BlazeTestStatus.PASSED); |
| 370 | when(testSummary.getTarget()).thenReturn(bartestTarget); |
| 371 | |
| 372 | if (actions >= 1) { |
| 373 | stateTracker.actionStarted(new ActionStartedEvent(foobuildAction, 123456789)); |
| 374 | } |
| 375 | if (actions >= 2) { |
| 376 | stateTracker.actionStarted(new ActionStartedEvent(bazbuildAction, 123456900)); |
| 377 | } |
| 378 | if (withTest) { |
| 379 | stateTracker.testFilteringComplete(filteringComplete); |
| 380 | stateTracker.testSummary(testSummary); |
| 381 | } |
| 382 | |
| 383 | LoggingTerminalWriter terminalWriter = new LoggingTerminalWriter(/*discardHighlight=*/ true); |
| 384 | stateTracker.writeProgressBar(terminalWriter); |
| 385 | String output = terminalWriter.getTranscript(); |
| 386 | |
| 387 | assertTrue( |
| 388 | "Only lines with at most 70 chars should be present in the output:\n" + output, |
| 389 | longestLine(output) <= 70); |
| 390 | if (actions >= 1) { |
| 391 | assertTrue( |
| 392 | "Running action 'foobuild' should be mentioned in output:\n" + output, |
| 393 | output.contains("foobuild")); |
| 394 | } |
| 395 | if (actions >= 2) { |
| 396 | assertTrue( |
| 397 | "Running action 'bazbuild' should be mentioned in output:\n" + output, |
| 398 | output.contains("bazbuild")); |
| 399 | } |
| 400 | if (withTest) { |
| 401 | assertTrue( |
| 402 | "Passed test ':bartest' should be mentioned in output:\n" + output, |
| 403 | output.contains(":bartest")); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | @Test |
| 408 | public void testOutputLength() throws Exception { |
| 409 | for (int i = 0; i < 3; i++) { |
| 410 | doTestOutputLength(true, i); |
| 411 | doTestOutputLength(false, i); |
| 412 | } |
| 413 | } |
Klaus Aehlig | c6d3ccc | 2016-05-24 12:58:09 +0000 | [diff] [blame] | 414 | |
| 415 | @Test |
| 416 | public void testAggregation() throws Exception { |
| 417 | // Assert that actions for the same test are aggregated so that an action afterwards |
| 418 | // is still shown. |
| 419 | ManualClock clock = new ManualClock(); |
| 420 | clock.advanceMillis(TimeUnit.SECONDS.toMillis(1234)); |
| 421 | ExperimentalStateTracker stateTracker = new ExperimentalStateTracker(clock, 80); |
| 422 | |
| 423 | Label labelFooTest = Label.parseAbsolute("//foo/bar:footest"); |
| 424 | ConfiguredTarget targetFooTest = Mockito.mock(ConfiguredTarget.class); |
| 425 | when(targetFooTest.getLabel()).thenReturn(labelFooTest); |
| 426 | ActionOwner fooOwner = new ActionOwner(labelFooTest, null, null, null, "abcdef", null); |
| 427 | |
| 428 | Label labelBarTest = Label.parseAbsolute("//baz:bartest"); |
| 429 | ConfiguredTarget targetBarTest = Mockito.mock(ConfiguredTarget.class); |
| 430 | when(targetBarTest.getLabel()).thenReturn(labelBarTest); |
| 431 | TestFilteringCompleteEvent filteringComplete = Mockito.mock(TestFilteringCompleteEvent.class); |
| 432 | when(filteringComplete.getTestTargets()) |
| 433 | .thenReturn(ImmutableSet.of(targetFooTest, targetBarTest)); |
| 434 | ActionOwner barOwner = new ActionOwner(labelBarTest, null, null, null, "fedcba", null); |
| 435 | |
| 436 | stateTracker.testFilteringComplete(filteringComplete); |
| 437 | |
| 438 | // First produce 10 actions for footest... |
| 439 | for (int i = 0; i < 10; i++) { |
| 440 | clock.advanceMillis(TimeUnit.SECONDS.toMillis(1)); |
| 441 | Action action = mockAction("Testing foo, shard " + i, "testlog_foo_" + i); |
| 442 | when(action.getOwner()).thenReturn(fooOwner); |
| 443 | stateTracker.actionStarted(new ActionStartedEvent(action, clock.nanoTime())); |
| 444 | } |
| 445 | // ...then produce 10 actions for bartest... |
| 446 | for (int i = 0; i < 10; i++) { |
| 447 | clock.advanceMillis(TimeUnit.SECONDS.toMillis(1)); |
| 448 | Action action = mockAction("Testing bar, shard " + i, "testlog_bar_" + i); |
| 449 | when(action.getOwner()).thenReturn(barOwner); |
| 450 | stateTracker.actionStarted(new ActionStartedEvent(action, clock.nanoTime())); |
| 451 | } |
| 452 | // ...and finally a completely unrelated action |
| 453 | clock.advanceMillis(TimeUnit.SECONDS.toMillis(1)); |
| 454 | stateTracker.actionStarted( |
| 455 | new ActionStartedEvent(mockAction("Other action", "other/action"), clock.nanoTime())); |
| 456 | clock.advanceMillis(TimeUnit.SECONDS.toMillis(1)); |
| 457 | |
| 458 | LoggingTerminalWriter terminalWriter = new LoggingTerminalWriter(/*discardHighlight=*/ true); |
| 459 | stateTracker.writeProgressBar(terminalWriter); |
| 460 | String output = terminalWriter.getTranscript(); |
| 461 | |
| 462 | assertTrue( |
| 463 | "Progress bar should contain ':footest', but was:\n" + output, output.contains(":footest")); |
| 464 | assertTrue( |
| 465 | "Progress bar should contain ':bartest', but was:\n" + output, output.contains(":bartest")); |
| 466 | assertTrue( |
| 467 | "Progress bar should contain 'Other action', but was:\n" + output, |
| 468 | output.contains("Other action")); |
| 469 | } |
Klaus Aehlig | 8cad4bd | 2016-03-14 11:13:58 +0000 | [diff] [blame] | 470 | } |