blob: ae6ec139cf68f2bd36279e3572ad84a71f5814dc [file] [log] [blame]
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +00001// 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.
14package com.google.devtools.build.lib.runtime;
15
16import static org.junit.Assert.assertFalse;
17import static org.junit.Assert.assertTrue;
18import static org.mockito.Mockito.when;
19
Klaus Aehligd232d232016-04-13 09:51:25 +000020import com.google.common.collect.ImmutableSet;
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +000021import com.google.devtools.build.lib.actions.Action;
22import com.google.devtools.build.lib.actions.ActionCompletionEvent;
Klaus Aehliga4c7d252016-05-20 10:44:21 +000023import com.google.devtools.build.lib.actions.ActionOwner;
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +000024import com.google.devtools.build.lib.actions.ActionStartedEvent;
25import com.google.devtools.build.lib.actions.Artifact;
26import com.google.devtools.build.lib.actions.Root;
Klaus Aehligd232d232016-04-13 09:51:25 +000027import com.google.devtools.build.lib.analysis.ConfiguredTarget;
28import com.google.devtools.build.lib.buildtool.buildevent.TestFilteringCompleteEvent;
Klaus Aehligb8d0c6b2016-04-13 11:06:41 +000029import com.google.devtools.build.lib.cmdline.Label;
Klaus Aehligc6fd6bb2016-05-27 11:42:32 +000030import com.google.devtools.build.lib.skyframe.LoadingPhaseStartedEvent;
31import com.google.devtools.build.lib.skyframe.PackageProgressReceiver;
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +000032import com.google.devtools.build.lib.testutil.FoundationTestCase;
Klaus Aehliged453e02016-04-15 11:29:05 +000033import com.google.devtools.build.lib.testutil.LoggingTerminalWriter;
Klaus Aehliga63d9612016-04-04 12:15:23 +000034import com.google.devtools.build.lib.testutil.ManualClock;
Klaus Aehligc6fd6bb2016-05-27 11:42:32 +000035import com.google.devtools.build.lib.util.Pair;
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +000036import com.google.devtools.build.lib.vfs.Path;
37import com.google.devtools.build.lib.vfs.PathFragment;
Klaus Aehligb8d0c6b2016-04-13 11:06:41 +000038import com.google.devtools.build.lib.view.test.TestStatus.BlazeTestStatus;
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +000039
40import org.junit.Test;
41import org.junit.runner.RunWith;
42import org.junit.runners.JUnit4;
43import org.mockito.Mockito;
44
45import java.io.IOException;
Klaus Aehlige25642a2016-04-04 13:31:28 +000046import java.util.concurrent.TimeUnit;
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +000047
48/**
Klaus Aehligc23ba452016-04-06 12:16:39 +000049 * Tests {@link ExperimentalStateTracker}.
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +000050 */
51@RunWith(JUnit4.class)
52public 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 Aehlig6d0876a2016-04-15 14:41:07 +000064 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 Aehlig8cad4bd2016-03-14 11:13:58 +000072 @Test
Klaus Aehligc6fd6bb2016-05-27 11:42:32 +000073 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 Aehlig8cad4bd2016-03-14 11:13:58 +0000102 public void testActionVisible() throws IOException {
103 // If there is only one action running, it should be visible
Klaus Aehligc0c88842016-04-11 12:07:38 +0000104 // somewhere in the progress bar, and also the short version thereof.
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +0000105
106 String message = "Building foo";
Klaus Aehliga63d9612016-04-04 12:15:23 +0000107 ManualClock clock = new ManualClock();
108 clock.advanceMillis(120000);
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +0000109
Klaus Aehliga63d9612016-04-04 12:15:23 +0000110 ExperimentalStateTracker stateTracker = new ExperimentalStateTracker(clock);
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +0000111 stateTracker.actionStarted(new ActionStartedEvent(mockAction(message, "bar/foo"), 123456789));
Klaus Aehligc0c88842016-04-11 12:07:38 +0000112
Klaus Aehliged453e02016-04-15 11:29:05 +0000113 LoggingTerminalWriter terminalWriter = new LoggingTerminalWriter(/*discardHighlight=*/ true);
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +0000114 stateTracker.writeProgressBar(terminalWriter);
Klaus Aehliged453e02016-04-15 11:29:05 +0000115 String output = terminalWriter.getTranscript();
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +0000116 assertTrue(
117 "Action message '" + message + "' should be present in output: " + output,
118 output.contains(message));
Klaus Aehligc0c88842016-04-11 12:07:38 +0000119
120 terminalWriter = new LoggingTerminalWriter();
121 stateTracker.writeProgressBar(terminalWriter, /* shortVersion=*/ true);
Klaus Aehliged453e02016-04-15 11:29:05 +0000122 output = terminalWriter.getTranscript();
Klaus Aehligc0c88842016-04-11 12:07:38 +0000123 assertTrue(
124 "Action message '" + message + "' should be present in short output: " + output,
125 output.contains(message));
126
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +0000127 }
128
129 @Test
130 public void testCompletedActionNotShown() throws IOException {
Klaus Aehligc0c88842016-04-11 12:07:38 +0000131 // Completed actions should not be reported in the progress bar, nor in the
132 // short progress bar.
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +0000133
134 String messageFast = "Running quick action";
135 String messageSlow = "Running slow action";
136
Klaus Aehliga63d9612016-04-04 12:15:23 +0000137 ManualClock clock = new ManualClock();
138 clock.advanceMillis(120000);
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +0000139 Action fastAction = mockAction(messageFast, "foo/fast");
140 Action slowAction = mockAction(messageSlow, "bar/slow");
Klaus Aehliga63d9612016-04-04 12:15:23 +0000141 ExperimentalStateTracker stateTracker = new ExperimentalStateTracker(clock);
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +0000142 stateTracker.actionStarted(new ActionStartedEvent(fastAction, 123456789));
143 stateTracker.actionStarted(new ActionStartedEvent(slowAction, 123456999));
144 stateTracker.actionCompletion(new ActionCompletionEvent(20, fastAction));
145
Klaus Aehliged453e02016-04-15 11:29:05 +0000146 LoggingTerminalWriter terminalWriter = new LoggingTerminalWriter(/*discardHighlight=*/ true);
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +0000147 stateTracker.writeProgressBar(terminalWriter);
Klaus Aehliged453e02016-04-15 11:29:05 +0000148 String output = terminalWriter.getTranscript();
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +0000149 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 Aehligc0c88842016-04-11 12:07:38 +0000155
156 terminalWriter = new LoggingTerminalWriter();
157 stateTracker.writeProgressBar(terminalWriter, /* shortVersion=*/ true);
Klaus Aehliged453e02016-04-15 11:29:05 +0000158 output = terminalWriter.getTranscript();
Klaus Aehligc0c88842016-04-11 12:07:38 +0000159 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 Aehlig8cad4bd2016-03-14 11:13:58 +0000165 }
166
167 @Test
168 public void testOldestActionVisible() throws IOException {
Klaus Aehligc0c88842016-04-11 12:07:38 +0000169 // The earliest-started action is always visible somehow in the progress bar
170 // and its short version.
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +0000171
172 String messageOld = "Running the first-started action";
173
Klaus Aehliga63d9612016-04-04 12:15:23 +0000174 ManualClock clock = new ManualClock();
175 clock.advanceMillis(120000);
176 ExperimentalStateTracker stateTracker = new ExperimentalStateTracker(clock);
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +0000177 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 Aehliged453e02016-04-15 11:29:05 +0000185 LoggingTerminalWriter terminalWriter = new LoggingTerminalWriter(/*discardHighlight=*/ true);
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +0000186 stateTracker.writeProgressBar(terminalWriter);
Klaus Aehliged453e02016-04-15 11:29:05 +0000187 String output = terminalWriter.getTranscript();
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +0000188 assertTrue(
189 "Longest running action '" + messageOld + "' should be visible in output: " + output,
190 output.contains(messageOld));
Klaus Aehligc0c88842016-04-11 12:07:38 +0000191
Klaus Aehliged453e02016-04-15 11:29:05 +0000192 terminalWriter = new LoggingTerminalWriter(/*discardHighlight=*/ true);
Klaus Aehligc0c88842016-04-11 12:07:38 +0000193 stateTracker.writeProgressBar(terminalWriter, /* shortVersion=*/ true);
Klaus Aehliged453e02016-04-15 11:29:05 +0000194 output = terminalWriter.getTranscript();
Klaus Aehligc0c88842016-04-11 12:07:38 +0000195 assertTrue(
196 "Longest running action '" + messageOld + "' should be visible in short output: " + output,
197 output.contains(messageOld));
Klaus Aehlig8cad4bd2016-03-14 11:13:58 +0000198 }
Klaus Aehlige25642a2016-04-04 13:31:28 +0000199
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 Aehligc0c88842016-04-11 12:07:38 +0000203 // In the short version of the progress bar, this should be true at least for the oldest action.
Klaus Aehlige25642a2016-04-04 13:31:28 +0000204
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 Aehliged453e02016-04-15 11:29:05 +0000217 LoggingTerminalWriter terminalWriter = new LoggingTerminalWriter(/*discardHighlight=*/ true);
Klaus Aehlige25642a2016-04-04 13:31:28 +0000218 stateTracker.writeProgressBar(terminalWriter);
Klaus Aehliged453e02016-04-15 11:29:05 +0000219 String output = terminalWriter.getTranscript();
Klaus Aehlige25642a2016-04-04 13:31:28 +0000220 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 Aehligc0c88842016-04-11 12:07:38 +0000224
Klaus Aehliged453e02016-04-15 11:29:05 +0000225 terminalWriter = new LoggingTerminalWriter(/*discardHighlight=*/ true);
Klaus Aehligc0c88842016-04-11 12:07:38 +0000226 stateTracker.writeProgressBar(terminalWriter, /* shortVersion=*/ true);
Klaus Aehliged453e02016-04-15 11:29:05 +0000227 output = terminalWriter.getTranscript();
Klaus Aehligc0c88842016-04-11 12:07:38 +0000228 assertTrue(
229 "Runtime of first action should be visible in short output: " + output,
230 output.contains("27s"));
Klaus Aehlige25642a2016-04-04 13:31:28 +0000231 }
232
Klaus Aehlig2ce24d42016-04-04 15:54:58 +0000233 @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 Aehligd232d232016-04-13 09:51:25 +0000257
258 @Test
Klaus Aehligb8d0c6b2016-04-13 11:06:41 +0000259 public void testCountVisible() throws Exception {
Klaus Aehligd232d232016-04-13 09:51:25 +0000260 // 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 Aehligb8d0c6b2016-04-13 11:06:41 +0000264 Label labelA = Label.parseAbsolute("//foo/bar:baz");
Klaus Aehligd232d232016-04-13 09:51:25 +0000265 ConfiguredTarget targetA = Mockito.mock(ConfiguredTarget.class);
Klaus Aehligb8d0c6b2016-04-13 11:06:41 +0000266 when(targetA.getLabel()).thenReturn(labelA);
Klaus Aehligd232d232016-04-13 09:51:25 +0000267 ConfiguredTarget targetB = Mockito.mock(ConfiguredTarget.class);
268 when(filteringComplete.getTestTargets()).thenReturn(ImmutableSet.of(targetA, targetB));
269 TestSummary testSummary = Mockito.mock(TestSummary.class);
Klaus Aehligb8d0c6b2016-04-13 11:06:41 +0000270 when(testSummary.getTarget()).thenReturn(targetA);
271
Klaus Aehligd232d232016-04-13 09:51:25 +0000272 stateTracker.testFilteringComplete(filteringComplete);
273 stateTracker.testSummary(testSummary);
274
Klaus Aehliged453e02016-04-15 11:29:05 +0000275 LoggingTerminalWriter terminalWriter = new LoggingTerminalWriter(/*discardHighlight=*/ true);
Klaus Aehligd232d232016-04-13 09:51:25 +0000276 stateTracker.writeProgressBar(terminalWriter);
Klaus Aehliged453e02016-04-15 11:29:05 +0000277 String output = terminalWriter.getTranscript();
Klaus Aehligd232d232016-04-13 09:51:25 +0000278 assertTrue(
279 "Test count should be visible in output: " + output, output.contains(" 1 / 2 tests"));
280
Klaus Aehliged453e02016-04-15 11:29:05 +0000281 terminalWriter = new LoggingTerminalWriter(/*discardHighlight=*/ true);
Klaus Aehligd232d232016-04-13 09:51:25 +0000282 stateTracker.writeProgressBar(terminalWriter, /* shortVersion=*/ true);
Klaus Aehliged453e02016-04-15 11:29:05 +0000283 output = terminalWriter.getTranscript();
Klaus Aehligd232d232016-04-13 09:51:25 +0000284 assertTrue(
285 "Test count should be visible in short output: " + output, output.contains(" 1 / 2 tests"));
286 }
Klaus Aehligb8d0c6b2016-04-13 11:06:41 +0000287
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 Aehliged453e02016-04-15 11:29:05 +0000306 LoggingTerminalWriter terminalWriter = new LoggingTerminalWriter(/*discardHighlight=*/ true);
Klaus Aehligb8d0c6b2016-04-13 11:06:41 +0000307 stateTracker.writeProgressBar(terminalWriter);
Klaus Aehliged453e02016-04-15 11:29:05 +0000308 String output = terminalWriter.getTranscript();
Klaus Aehligb8d0c6b2016-04-13 11:06:41 +0000309
310 assertTrue(
311 "Label " + labelA.toString() + " should be present in progress bar: " + output,
312 output.contains(labelA.toString()));
313 }
Klaus Aehlig6d0876a2016-04-15 14:41:07 +0000314
Klaus Aehliga4c7d252016-05-20 10:44:21 +0000315 @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 Aehlig6d0876a2016-04-15 14:41:07 +0000346 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 Aehligc6d3ccc2016-05-24 12:58:09 +0000414
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 Aehlig8cad4bd2016-03-14 11:13:58 +0000470}