Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +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 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; |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 18 | import static org.mockito.Mockito.mock; |
Klaus Aehlig | 9b3b2e9 | 2017-05-02 11:18:42 +0200 | [diff] [blame] | 19 | import static org.mockito.Mockito.times; |
| 20 | import static org.mockito.Mockito.verify; |
| 21 | import static org.mockito.Mockito.when; |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 22 | |
philwo | 052e5f8 | 2021-01-19 10:47:41 -0800 | [diff] [blame] | 23 | import com.google.common.base.Stopwatch; |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 24 | import com.google.common.collect.ImmutableList; |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 25 | import com.google.common.collect.ImmutableSet; |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 26 | import com.google.common.eventbus.Subscribe; |
buchgr | 0b93743 | 2017-04-06 18:54:22 +0000 | [diff] [blame] | 27 | import com.google.common.util.concurrent.Futures; |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 28 | import com.google.common.util.concurrent.ListenableFuture; |
lberki | 8626623 | 2018-04-11 00:33:42 -0700 | [diff] [blame] | 29 | import com.google.devtools.build.lib.actions.ActionEnvironment; |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 30 | import com.google.devtools.build.lib.actions.ActionExecutedEvent; |
| 31 | import com.google.devtools.build.lib.actions.ActionExecutedEvent.ErrorTiming; |
| 32 | import com.google.devtools.build.lib.actions.ActionExecutionException; |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.actions.Artifact; |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 34 | import com.google.devtools.build.lib.actions.ArtifactRoot; |
felly | 9b91cb4 | 2019-06-04 09:35:21 -0700 | [diff] [blame] | 35 | import com.google.devtools.build.lib.actions.CompletionContext; |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 36 | import com.google.devtools.build.lib.actions.EventReportingArtifacts; |
Googler | 17e28df | 2019-09-10 12:07:05 -0700 | [diff] [blame] | 37 | import com.google.devtools.build.lib.actions.SpawnResult.MetadataLog; |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 38 | import com.google.devtools.build.lib.actions.util.ActionsTestUtil; |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 39 | import com.google.devtools.build.lib.analysis.BlazeDirectories; |
janakr | 3b63a4e | 2017-09-14 09:55:40 +0200 | [diff] [blame] | 40 | import com.google.devtools.build.lib.analysis.ServerDirectories; |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 41 | import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue; |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 42 | import com.google.devtools.build.lib.analysis.config.BuildOptions; |
gregce | e495e6b | 2019-04-30 14:07:06 -0700 | [diff] [blame] | 43 | import com.google.devtools.build.lib.analysis.config.CoreOptions; |
twigg | 44c30ac | 2021-12-08 15:57:11 -0800 | [diff] [blame] | 44 | import com.google.devtools.build.lib.analysis.config.FragmentFactory; |
| 45 | import com.google.devtools.build.lib.analysis.config.FragmentRegistry; |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 46 | import com.google.devtools.build.lib.bugreport.BugReport; |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 47 | import com.google.devtools.build.lib.buildeventstream.AnnounceBuildEventTransportsEvent; |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 48 | import com.google.devtools.build.lib.buildeventstream.ArtifactGroupNamer; |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 49 | import com.google.devtools.build.lib.buildeventstream.BuildEvent; |
felly | 75a422a | 2019-01-17 14:52:49 -0800 | [diff] [blame] | 50 | import com.google.devtools.build.lib.buildeventstream.BuildEventArtifactUploader; |
ulfjack | 26e586d | 2018-05-17 08:42:13 -0700 | [diff] [blame] | 51 | import com.google.devtools.build.lib.buildeventstream.BuildEventContext; |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 52 | import com.google.devtools.build.lib.buildeventstream.BuildEventIdUtil; |
ulfjack | fbf2756 | 2018-05-18 12:46:54 -0700 | [diff] [blame] | 53 | import com.google.devtools.build.lib.buildeventstream.BuildEventProtocolOptions; |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 54 | import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos; |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 55 | import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.Aborted; |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 56 | import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.Aborted.AbortReason; |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 57 | import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.BuildEventId; |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 58 | import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.BuildEventId.NamedSetOfFilesId; |
Googler | 17e28df | 2019-09-10 12:07:05 -0700 | [diff] [blame] | 59 | import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.File; |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 60 | import com.google.devtools.build.lib.buildeventstream.BuildEventTransport; |
lberki | d263009 | 2018-11-22 09:12:34 -0800 | [diff] [blame] | 61 | import com.google.devtools.build.lib.buildeventstream.BuildEventTransportClosedEvent; |
Klaus Aehlig | a708a02 | 2017-07-11 12:54:40 +0200 | [diff] [blame] | 62 | import com.google.devtools.build.lib.buildeventstream.BuildEventWithConfiguration; |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 63 | import com.google.devtools.build.lib.buildeventstream.BuildEventWithOrderConstraint; |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 64 | import com.google.devtools.build.lib.buildeventstream.GenericBuildEvent; |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 65 | import com.google.devtools.build.lib.buildeventstream.PathConverter; |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 66 | import com.google.devtools.build.lib.buildeventstream.ProgressEvent; |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 67 | import com.google.devtools.build.lib.buildeventstream.transports.BuildEventStreamOptions; |
Jakob Buchgraber | 8c3a4ef | 2017-02-28 18:49:50 +0000 | [diff] [blame] | 68 | import com.google.devtools.build.lib.buildtool.BuildResult; |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 69 | import com.google.devtools.build.lib.buildtool.buildevent.BuildCompleteEvent; |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 70 | import com.google.devtools.build.lib.buildtool.buildevent.NoAnalyzeEvent; |
jhorvitz | 6380c289 | 2021-05-03 10:13:52 -0700 | [diff] [blame] | 71 | import com.google.devtools.build.lib.cmdline.RepositoryName; |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 72 | import com.google.devtools.build.lib.collect.nestedset.NestedSet; |
| 73 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
adgar | fcf9dd5 | 2021-03-09 09:04:47 -0800 | [diff] [blame] | 74 | import com.google.devtools.build.lib.collect.nestedset.Order; |
mschaller | 6bf7c51 | 2020-06-02 21:34:02 -0700 | [diff] [blame] | 75 | import com.google.devtools.build.lib.server.FailureDetails.FailureDetail; |
| 76 | import com.google.devtools.build.lib.server.FailureDetails.Spawn; |
| 77 | import com.google.devtools.build.lib.server.FailureDetails.Spawn.Code; |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 78 | import com.google.devtools.build.lib.testutil.FoundationTestCase; |
mschaller | 854bb90 | 2020-03-03 10:56:14 -0800 | [diff] [blame] | 79 | import com.google.devtools.build.lib.util.DetailedExitCode; |
michajlo | d6d4ee2 | 2020-04-16 13:48:06 -0700 | [diff] [blame] | 80 | import com.google.devtools.build.lib.util.Pair; |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 81 | import com.google.devtools.build.lib.vfs.Path; |
| 82 | import com.google.devtools.build.lib.vfs.PathFragment; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 83 | import com.google.devtools.build.lib.vfs.Root; |
Googler | 17e28df | 2019-09-10 12:07:05 -0700 | [diff] [blame] | 84 | import com.google.devtools.build.lib.vfs.util.FileSystems; |
ulfjack | fbf2756 | 2018-05-18 12:46:54 -0700 | [diff] [blame] | 85 | import com.google.devtools.common.options.Options; |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 86 | import java.util.ArrayList; |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 87 | import java.util.Collection; |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 88 | import java.util.Collections; |
| 89 | import java.util.HashSet; |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 90 | import java.util.List; |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 91 | import java.util.Set; |
adgar | fcf9dd5 | 2021-03-09 09:04:47 -0800 | [diff] [blame] | 92 | import java.util.concurrent.CountDownLatch; |
felly | a4488a2 | 2019-04-03 13:05:47 -0700 | [diff] [blame] | 93 | import java.util.concurrent.LinkedBlockingQueue; |
| 94 | import java.util.concurrent.ThreadPoolExecutor; |
lberki | d263009 | 2018-11-22 09:12:34 -0800 | [diff] [blame] | 95 | import java.util.concurrent.TimeUnit; |
felly | a4488a2 | 2019-04-03 13:05:47 -0700 | [diff] [blame] | 96 | import java.util.concurrent.atomic.AtomicInteger; |
lberki | d263009 | 2018-11-22 09:12:34 -0800 | [diff] [blame] | 97 | import java.util.concurrent.locks.LockSupport; |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 98 | import javax.annotation.Nullable; |
felly | a4488a2 | 2019-04-03 13:05:47 -0700 | [diff] [blame] | 99 | import org.junit.Ignore; |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 100 | import org.junit.Test; |
| 101 | import org.junit.runner.RunWith; |
| 102 | import org.junit.runners.JUnit4; |
| 103 | |
| 104 | /** Tests {@link BuildEventStreamer}. */ |
| 105 | @RunWith(JUnit4.class) |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 106 | public final class BuildEventStreamerTest extends FoundationTestCase { |
lpino | 0361ff6 | 2019-02-12 22:52:59 -0800 | [diff] [blame] | 107 | |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 108 | private static final String OOM_MESSAGE = "Please build fewer targets."; |
lpino | 0361ff6 | 2019-02-12 22:52:59 -0800 | [diff] [blame] | 109 | |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 110 | private final CountingArtifactGroupNamer artifactGroupNamer = new CountingArtifactGroupNamer(); |
| 111 | private final RecordingBuildEventTransport transport = |
| 112 | new RecordingBuildEventTransport(artifactGroupNamer); |
| 113 | |
| 114 | private final BuildEventStreamer streamer = |
| 115 | new BuildEventStreamer.Builder() |
| 116 | .artifactGroupNamer(artifactGroupNamer) |
| 117 | .buildEventTransports(ImmutableSet.of(transport)) |
| 118 | .besStreamOptions(new BuildEventStreamOptions()) |
| 119 | .oomMessage(OOM_MESSAGE) |
| 120 | .build(); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 121 | |
Googler | 17e28df | 2019-09-10 12:07:05 -0700 | [diff] [blame] | 122 | private static BuildEventContext getTestBuildEventContext(ArtifactGroupNamer artifactGroupNamer) { |
| 123 | return new BuildEventContext() { |
| 124 | @Override |
| 125 | public ArtifactGroupNamer artifactGroupNamer() { |
| 126 | return artifactGroupNamer; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public PathConverter pathConverter() { |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 131 | return Path::toString; |
Googler | 17e28df | 2019-09-10 12:07:05 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public BuildEventProtocolOptions getOptions() { |
| 136 | return Options.getDefaults(BuildEventProtocolOptions.class); |
| 137 | } |
| 138 | }; |
| 139 | } |
| 140 | |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 141 | private static final ActionExecutedEvent SUCCESSFUL_ACTION_EXECUTED_EVENT = |
| 142 | new ActionExecutedEvent( |
ulfjack | 5f312dd | 2018-10-02 04:58:53 -0700 | [diff] [blame] | 143 | ActionsTestUtil.DUMMY_ARTIFACT.getExecPath(), |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 144 | new ActionsTestUtil.NullAction(), |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 145 | /*exception=*/ null, |
tomlu | d56a806 | 2018-08-01 13:20:41 -0700 | [diff] [blame] | 146 | ActionsTestUtil.DUMMY_ARTIFACT.getPath(), |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 147 | /*stdout=*/ null, |
| 148 | /*stderr=*/ null, |
| 149 | /*actionMetadataLogs=*/ ImmutableList.of(), |
felly | 3b050f6 | 2020-01-23 08:57:31 -0800 | [diff] [blame] | 150 | ErrorTiming.NO_ERROR, |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 151 | /*isInMemoryFs=*/ false); |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 152 | |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 153 | private static final class RecordingBuildEventTransport implements BuildEventTransport { |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 154 | private final List<BuildEvent> events = new ArrayList<>(); |
| 155 | private final List<BuildEventStreamProtos.BuildEvent> eventsAsProtos = new ArrayList<>(); |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 156 | private final ArtifactGroupNamer artifactGroupNamer; |
lpino | 0361ff6 | 2019-02-12 22:52:59 -0800 | [diff] [blame] | 157 | |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 158 | RecordingBuildEventTransport(ArtifactGroupNamer namer) { |
lpino | 0361ff6 | 2019-02-12 22:52:59 -0800 | [diff] [blame] | 159 | this.artifactGroupNamer = namer; |
| 160 | } |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 161 | |
| 162 | @Override |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 163 | public String name() { |
| 164 | return this.getClass().getSimpleName(); |
| 165 | } |
| 166 | |
| 167 | @Override |
felly | 6a8759f | 2019-07-09 11:27:03 -0700 | [diff] [blame] | 168 | public boolean mayBeSlow() { |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | @Override |
felly | a4488a2 | 2019-04-03 13:05:47 -0700 | [diff] [blame] | 173 | public synchronized void sendBuildEvent(BuildEvent event) { |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 174 | events.add(event); |
michajlo | 99725e1 | 2020-11-18 14:30:05 -0800 | [diff] [blame] | 175 | try { |
| 176 | eventsAsProtos.add(event.asStreamProto(getTestBuildEventContext(this.artifactGroupNamer))); |
| 177 | } catch (InterruptedException e) { |
| 178 | Thread.currentThread().interrupt(); |
| 179 | throw new IllegalStateException("interrupts not supported in test instance"); |
| 180 | } |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | @Override |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 184 | public ListenableFuture<Void> close() { |
buchgr | 0b93743 | 2017-04-06 18:54:22 +0000 | [diff] [blame] | 185 | return Futures.immediateFuture(null); |
| 186 | } |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 187 | |
felly | 75a422a | 2019-01-17 14:52:49 -0800 | [diff] [blame] | 188 | @Override |
| 189 | public BuildEventArtifactUploader getUploader() { |
| 190 | throw new IllegalStateException(); |
| 191 | } |
| 192 | |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 193 | List<BuildEvent> getEvents() { |
| 194 | return events; |
| 195 | } |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 196 | |
| 197 | List<BuildEventStreamProtos.BuildEvent> getEventProtos() { |
| 198 | return eventsAsProtos; |
| 199 | } |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 202 | private static class GenericOrderEvent implements BuildEventWithOrderConstraint { |
| 203 | private final BuildEventId id; |
| 204 | private final Collection<BuildEventId> children; |
| 205 | private final Collection<BuildEventId> after; |
| 206 | |
| 207 | GenericOrderEvent( |
| 208 | BuildEventId id, Collection<BuildEventId> children, Collection<BuildEventId> after) { |
| 209 | this.id = id; |
| 210 | this.children = children; |
| 211 | this.after = after; |
| 212 | } |
| 213 | |
| 214 | GenericOrderEvent(BuildEventId id, Collection<BuildEventId> children) { |
| 215 | this(id, children, children); |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public BuildEventId getEventId() { |
| 220 | return id; |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public Collection<BuildEventId> getChildrenEvents() { |
| 225 | return children; |
| 226 | } |
| 227 | |
| 228 | @Override |
ulfjack | 26e586d | 2018-05-17 08:42:13 -0700 | [diff] [blame] | 229 | public BuildEventStreamProtos.BuildEvent asStreamProto(BuildEventContext converters) { |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 230 | return GenericBuildEvent.protoChaining(this).build(); |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public Collection<BuildEventId> postedAfter() { |
| 235 | return after; |
| 236 | } |
| 237 | } |
| 238 | |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 239 | private static final class GenericArtifactReportingEvent implements EventReportingArtifacts { |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 240 | private final BuildEventId id; |
| 241 | private final Collection<BuildEventId> children; |
| 242 | private final Collection<NestedSet<Artifact>> artifacts; |
| 243 | |
| 244 | GenericArtifactReportingEvent( |
| 245 | BuildEventId id, |
| 246 | Collection<BuildEventId> children, |
| 247 | Collection<NestedSet<Artifact>> artifacts) { |
| 248 | this.id = id; |
| 249 | this.children = children; |
| 250 | this.artifacts = artifacts; |
| 251 | } |
| 252 | |
| 253 | GenericArtifactReportingEvent(BuildEventId id, Collection<NestedSet<Artifact>> artifacts) { |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 254 | this(id, ImmutableSet.of(), artifacts); |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public BuildEventId getEventId() { |
| 259 | return id; |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public Collection<BuildEventId> getChildrenEvents() { |
| 264 | return children; |
| 265 | } |
| 266 | |
| 267 | @Override |
tomlu | 04e9281 | 2018-08-02 11:49:08 -0700 | [diff] [blame] | 268 | public ReportedArtifacts reportedArtifacts() { |
felly | 9b91cb4 | 2019-06-04 09:35:21 -0700 | [diff] [blame] | 269 | return new ReportedArtifacts(artifacts, CompletionContext.FAILED_COMPLETION_CTX); |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | @Override |
ulfjack | 26e586d | 2018-05-17 08:42:13 -0700 | [diff] [blame] | 273 | public BuildEventStreamProtos.BuildEvent asStreamProto(BuildEventContext converters) { |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 274 | BuildEventStreamProtos.NamedSetOfFiles.Builder builder = |
| 275 | BuildEventStreamProtos.NamedSetOfFiles.newBuilder(); |
| 276 | for (NestedSet<Artifact> artifactset : artifacts) { |
adonovan | 841ac61 | 2020-06-06 07:18:58 -0700 | [diff] [blame] | 277 | builder.addFileSets(converters.artifactGroupNamer().apply(artifactset.toNode())); |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 278 | } |
| 279 | return GenericBuildEvent.protoChaining(this).setNamedSetOfFiles(builder.build()).build(); |
| 280 | } |
| 281 | } |
| 282 | |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 283 | private static class GenericConfigurationEvent implements BuildEventWithConfiguration { |
| 284 | private final BuildEventId id; |
| 285 | private final Collection<BuildEventId> children; |
Klaus Aehlig | a708a02 | 2017-07-11 12:54:40 +0200 | [diff] [blame] | 286 | private final Collection<BuildEvent> configurations; |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 287 | |
| 288 | GenericConfigurationEvent( |
Klaus Aehlig | a708a02 | 2017-07-11 12:54:40 +0200 | [diff] [blame] | 289 | BuildEventId id, Collection<BuildEventId> children, Collection<BuildEvent> configurations) { |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 290 | this.id = id; |
| 291 | this.children = children; |
| 292 | this.configurations = configurations; |
| 293 | } |
| 294 | |
Klaus Aehlig | a708a02 | 2017-07-11 12:54:40 +0200 | [diff] [blame] | 295 | GenericConfigurationEvent(BuildEventId id, BuildEvent configuration) { |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 296 | this(id, ImmutableSet.of(), ImmutableSet.of(configuration)); |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | @Override |
| 300 | public BuildEventId getEventId() { |
| 301 | return id; |
| 302 | } |
| 303 | |
| 304 | @Override |
| 305 | public Collection<BuildEventId> getChildrenEvents() { |
| 306 | return children; |
| 307 | } |
| 308 | |
| 309 | @Override |
Klaus Aehlig | a708a02 | 2017-07-11 12:54:40 +0200 | [diff] [blame] | 310 | public Collection<BuildEvent> getConfigurations() { |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 311 | return configurations; |
| 312 | } |
| 313 | |
| 314 | @Override |
ulfjack | 26e586d | 2018-05-17 08:42:13 -0700 | [diff] [blame] | 315 | public BuildEventStreamProtos.BuildEvent asStreamProto(BuildEventContext converters) { |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 316 | return GenericBuildEvent.protoChaining(this).build(); |
| 317 | } |
| 318 | } |
| 319 | |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 320 | private static BuildEventId testId(String opaque) { |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 321 | return BuildEventIdUtil.unknownBuildEventId(opaque); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 322 | } |
| 323 | |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 324 | private static class EventBusHandler { |
| 325 | |
| 326 | Set<BuildEventTransport> transportSet; |
| 327 | |
| 328 | @Subscribe |
| 329 | void transportsAnnounced(AnnounceBuildEventTransportsEvent evt) { |
| 330 | transportSet = Collections.synchronizedSet(new HashSet<>(evt.transports())); |
| 331 | } |
lberki | d263009 | 2018-11-22 09:12:34 -0800 | [diff] [blame] | 332 | |
| 333 | @Subscribe |
| 334 | void transportClosed(BuildEventTransportClosedEvent evt) { |
| 335 | transportSet.remove(evt.transport()); |
| 336 | } |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 337 | } |
| 338 | |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 339 | @Test(timeout = 5000) |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 340 | public void testSimpleStream() { |
| 341 | // Verify that a well-formed event is passed through and that completion of the |
Klaus Aehlig | fb3c572 | 2017-07-10 13:08:19 +0200 | [diff] [blame] | 342 | // build clears the pending progress-update event. However, there is no guarantee |
| 343 | // on the order of the flushed events. |
| 344 | // Additionally, assert that the actual last event has the last_message flag set. |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 345 | |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 346 | EventBusHandler handler = new EventBusHandler(); |
| 347 | eventBus.register(handler); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 348 | assertThat(handler.transportSet).isNull(); |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 349 | |
lpino | 5bec36c | 2019-03-21 09:50:52 -0700 | [diff] [blame] | 350 | eventBus.post(new AnnounceBuildEventTransportsEvent(ImmutableSet.of(transport))); |
| 351 | |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 352 | BuildEvent startEvent = |
| 353 | new GenericBuildEvent( |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 354 | testId("Initial"), |
| 355 | ImmutableSet.of( |
| 356 | ProgressEvent.INITIAL_PROGRESS_UPDATE, BuildEventIdUtil.buildFinished())); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 357 | |
| 358 | streamer.buildEvent(startEvent); |
| 359 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 360 | assertThat(streamer.isClosed()).isFalse(); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 361 | List<BuildEvent> afterFirstEvent = transport.getEvents(); |
| 362 | assertThat(afterFirstEvent).hasSize(1); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 363 | assertThat(afterFirstEvent.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
| 364 | assertThat(handler.transportSet).hasSize(1); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 365 | |
Jakob Buchgraber | 8c3a4ef | 2017-02-28 18:49:50 +0000 | [diff] [blame] | 366 | streamer.buildEvent(new BuildCompleteEvent(new BuildResult(0))); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 367 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 368 | assertThat(streamer.isClosed()).isTrue(); |
lpino | 5bec36c | 2019-03-21 09:50:52 -0700 | [diff] [blame] | 369 | eventBus.post(new BuildEventTransportClosedEvent(transport)); |
| 370 | |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 371 | List<BuildEvent> finalStream = transport.getEvents(); |
Jakob Buchgraber | 8c3a4ef | 2017-02-28 18:49:50 +0000 | [diff] [blame] | 372 | assertThat(finalStream).hasSize(3); |
Klaus Aehlig | fb3c572 | 2017-07-10 13:08:19 +0200 | [diff] [blame] | 373 | assertThat(ImmutableSet.of(finalStream.get(1).getEventId(), finalStream.get(2).getEventId())) |
| 374 | .isEqualTo( |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 375 | ImmutableSet.of( |
| 376 | BuildEventIdUtil.buildFinished(), ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
Klaus Aehlig | fb3c572 | 2017-07-10 13:08:19 +0200 | [diff] [blame] | 377 | |
| 378 | // verify the "last_message" flag. |
| 379 | assertThat(transport.getEventProtos().get(0).getLastMessage()).isFalse(); |
| 380 | assertThat(transport.getEventProtos().get(1).getLastMessage()).isFalse(); |
| 381 | assertThat(transport.getEventProtos().get(2).getLastMessage()).isTrue(); |
lberki | d263009 | 2018-11-22 09:12:34 -0800 | [diff] [blame] | 382 | |
| 383 | while (!handler.transportSet.isEmpty()) { |
| 384 | LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(100)); |
| 385 | } |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | @Test |
| 389 | public void testChaining() { |
| 390 | // Verify that unannounced events are linked in with progress update events, assuming |
| 391 | // a correctly formed initial event. |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 392 | BuildEvent startEvent = |
| 393 | new GenericBuildEvent( |
| 394 | testId("Initial"), ImmutableSet.of(ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 395 | BuildEvent unexpectedEvent = new GenericBuildEvent(testId("unexpected"), ImmutableSet.of()); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 396 | |
| 397 | streamer.buildEvent(startEvent); |
| 398 | streamer.buildEvent(unexpectedEvent); |
| 399 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 400 | assertThat(streamer.isClosed()).isFalse(); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 401 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 402 | assertThat(eventsSeen).hasSize(3); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 403 | assertThat(eventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
| 404 | assertThat(eventsSeen.get(2).getEventId()).isEqualTo(unexpectedEvent.getEventId()); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 405 | BuildEvent linkEvent = eventsSeen.get(1); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 406 | assertThat(linkEvent.getEventId()).isEqualTo(ProgressEvent.INITIAL_PROGRESS_UPDATE); |
| 407 | assertWithMessage("Unexpected events should be linked") |
| 408 | .that(linkEvent.getChildrenEvents().contains(unexpectedEvent.getEventId())) |
| 409 | .isTrue(); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | @Test |
| 413 | public void testBadInitialEvent() { |
| 414 | // Verify that, if the initial event does not announce the initial progress update event, |
| 415 | // the initial progress event is used instead to chain that event; in this way, new |
| 416 | // progress updates can always be chained in. |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 417 | BuildEvent unexpectedStartEvent = |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 418 | new GenericBuildEvent(testId("unexpected start"), ImmutableSet.of()); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 419 | |
| 420 | streamer.buildEvent(unexpectedStartEvent); |
| 421 | |
| 422 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 423 | assertThat(eventsSeen).hasSize(2); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 424 | assertThat(eventsSeen.get(1).getEventId()).isEqualTo(unexpectedStartEvent.getEventId()); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 425 | BuildEvent initial = eventsSeen.get(0); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 426 | assertThat(initial.getEventId()).isEqualTo(ProgressEvent.INITIAL_PROGRESS_UPDATE); |
| 427 | assertWithMessage("Event should be linked") |
| 428 | .that(initial.getChildrenEvents().contains(unexpectedStartEvent.getEventId())) |
| 429 | .isTrue(); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 430 | |
| 431 | // The initial event should also announce a new progress event; we test this |
| 432 | // by streaming another unannounced event. |
| 433 | |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 434 | BuildEvent unexpectedEvent = new GenericBuildEvent(testId("unexpected"), ImmutableSet.of()); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 435 | |
| 436 | streamer.buildEvent(unexpectedEvent); |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 437 | |
| 438 | assertThat(streamer.isClosed()).isFalse(); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 439 | List<BuildEvent> allEventsSeen = transport.getEvents(); |
| 440 | assertThat(allEventsSeen).hasSize(4); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 441 | assertThat(allEventsSeen.get(3).getEventId()).isEqualTo(unexpectedEvent.getEventId()); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 442 | BuildEvent secondLinkEvent = allEventsSeen.get(2); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 443 | assertWithMessage("Progress should have been announced") |
| 444 | .that(initial.getChildrenEvents().contains(secondLinkEvent.getEventId())) |
| 445 | .isTrue(); |
| 446 | assertWithMessage("Second event should be linked") |
| 447 | .that(secondLinkEvent.getChildrenEvents().contains(unexpectedEvent.getEventId())) |
| 448 | .isTrue(); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | @Test |
| 452 | public void testReferPastEvent() { |
| 453 | // Verify that, if an event is refers to a previously done event, that duplicated |
| 454 | // late-referenced event is not expected again. |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 455 | BuildEvent startEvent = |
| 456 | new GenericBuildEvent( |
Klaus Aehlig | 2b1eec3 | 2016-10-05 09:21:45 +0000 | [diff] [blame] | 457 | testId("Initial"), |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 458 | ImmutableSet.of( |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 459 | ProgressEvent.INITIAL_PROGRESS_UPDATE, BuildEventIdUtil.buildFinished())); |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 460 | BuildEvent earlyEvent = new GenericBuildEvent(testId("unexpected"), ImmutableSet.of()); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 461 | BuildEvent lateReference = |
| 462 | new GenericBuildEvent(testId("late reference"), ImmutableSet.of(earlyEvent.getEventId())); |
| 463 | |
| 464 | streamer.buildEvent(startEvent); |
| 465 | streamer.buildEvent(earlyEvent); |
| 466 | streamer.buildEvent(lateReference); |
Jakob Buchgraber | 8c3a4ef | 2017-02-28 18:49:50 +0000 | [diff] [blame] | 467 | streamer.buildEvent(new BuildCompleteEvent(new BuildResult(0))); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 468 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 469 | assertThat(streamer.isClosed()).isTrue(); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 470 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 471 | int earlyEventCount = 0; |
| 472 | for (BuildEvent event : eventsSeen) { |
| 473 | if (event.getEventId().equals(earlyEvent.getEventId())) { |
| 474 | earlyEventCount++; |
| 475 | } |
| 476 | } |
| 477 | // The early event should be reported precisely once. |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 478 | assertThat(earlyEventCount).isEqualTo(1); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 479 | } |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 480 | |
| 481 | @Test |
felly | 1759476 | 2019-02-07 15:24:32 -0800 | [diff] [blame] | 482 | public void testReordering() { |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 483 | // Verify that an event requiring to be posted after another one is indeed. |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 484 | BuildEventId expectedId = testId("the target"); |
| 485 | BuildEvent startEvent = |
| 486 | new GenericBuildEvent( |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 487 | testId("Initial"), ImmutableSet.of(ProgressEvent.INITIAL_PROGRESS_UPDATE, expectedId)); |
| 488 | BuildEvent rootCause = new GenericBuildEvent(testId("failure event"), ImmutableSet.of()); |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 489 | BuildEvent failedTarget = |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 490 | new GenericOrderEvent(expectedId, ImmutableSet.of(rootCause.getEventId())); |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 491 | |
| 492 | streamer.buildEvent(startEvent); |
| 493 | streamer.buildEvent(failedTarget); |
| 494 | streamer.buildEvent(rootCause); |
| 495 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 496 | assertThat(streamer.isClosed()).isFalse(); |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 497 | List<BuildEvent> allEventsSeen = transport.getEvents(); |
| 498 | assertThat(allEventsSeen).hasSize(4); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 499 | assertThat(allEventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 500 | BuildEvent linkEvent = allEventsSeen.get(1); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 501 | assertThat(linkEvent.getEventId()).isEqualTo(ProgressEvent.INITIAL_PROGRESS_UPDATE); |
| 502 | assertThat(allEventsSeen.get(2).getEventId()).isEqualTo(rootCause.getEventId()); |
| 503 | assertThat(allEventsSeen.get(3).getEventId()).isEqualTo(failedTarget.getEventId()); |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 504 | } |
| 505 | |
felly | a4488a2 | 2019-04-03 13:05:47 -0700 | [diff] [blame] | 506 | private static BuildEvent indexOrderedBuildEvent(int index, int afterIndex) { |
| 507 | return new GenericOrderEvent( |
| 508 | testId("Concurrent-" + index), |
| 509 | ImmutableList.of(), |
| 510 | afterIndex == -1 |
| 511 | ? ImmutableList.of() |
| 512 | : ImmutableList.of(testId("Concurrent-" + afterIndex))); |
| 513 | } |
| 514 | |
| 515 | @Test |
| 516 | public void testConcurrency() throws Exception { |
| 517 | // Verify that we can blast the BuildEventStreamer with many build events in parallel without |
| 518 | // violating internal consistency. The thread-safety under test is primarily sensitive to the |
| 519 | // pendingEvents field constructed when there are ordering constraints, so we make sure to |
| 520 | // include such ordering constraints in this test. |
| 521 | BuildEvent startEvent = |
| 522 | new GenericBuildEvent( |
| 523 | testId("Initial"), |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 524 | ImmutableSet.of( |
| 525 | ProgressEvent.INITIAL_PROGRESS_UPDATE, BuildEventIdUtil.buildFinished())); |
felly | a4488a2 | 2019-04-03 13:05:47 -0700 | [diff] [blame] | 526 | streamer.buildEvent(startEvent); |
| 527 | |
| 528 | int numThreads = 12; |
| 529 | int numEventsPerThread = 10_000; |
| 530 | int totalEvents = numThreads * numEventsPerThread; |
| 531 | AtomicInteger idIndex = new AtomicInteger(); |
| 532 | ThreadPoolExecutor pool = |
| 533 | new ThreadPoolExecutor( |
| 534 | numThreads, |
| 535 | numThreads, |
| 536 | /* keepAliveTime= */ 0, |
| 537 | TimeUnit.SECONDS, |
| 538 | /* workQueue= */ new LinkedBlockingQueue<>()); |
| 539 | |
| 540 | for (int i = 0; i < numThreads; i++) { |
| 541 | pool.execute( |
| 542 | () -> { |
| 543 | for (int j = 0; j < numEventsPerThread; j++) { |
| 544 | int index = idIndex.getAndIncrement(); |
| 545 | // Arrange for half of the events to have an ordering constraint on the subsequent |
| 546 | // event. The ordering graph must avoid cycles. |
| 547 | int afterIndex = (index % 2 == 0) ? (index + 1) % totalEvents : -1; |
| 548 | streamer.buildEvent(indexOrderedBuildEvent(index, afterIndex)); |
| 549 | } |
| 550 | }); |
| 551 | } |
| 552 | |
| 553 | pool.shutdown(); |
| 554 | pool.awaitTermination(1, TimeUnit.DAYS); |
| 555 | |
| 556 | BuildEventId lateId = testId("late event"); |
| 557 | streamer.buildEvent(new BuildCompleteEvent(new BuildResult(0), ImmutableList.of(lateId))); |
| 558 | assertThat(streamer.isClosed()).isFalse(); |
| 559 | streamer.buildEvent(new GenericBuildEvent(lateId, ImmutableSet.of())); |
| 560 | assertThat(streamer.isClosed()).isTrue(); |
| 561 | |
| 562 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 563 | assertThat(eventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
| 564 | assertThat(eventsSeen).hasSize(4 + totalEvents * 2); |
| 565 | } |
| 566 | |
| 567 | // Re-enable this "test" for ad-hoc benchmarking of many concurrent build events. |
| 568 | @Ignore |
| 569 | public void concurrencyBenchmark() throws Exception { |
| 570 | long time = 0; |
| 571 | for (int iteration = 0; iteration < 3; iteration++) { |
philwo | 052e5f8 | 2021-01-19 10:47:41 -0800 | [diff] [blame] | 572 | Stopwatch watch = Stopwatch.createStarted(); |
felly | a4488a2 | 2019-04-03 13:05:47 -0700 | [diff] [blame] | 573 | |
felly | a4488a2 | 2019-04-03 13:05:47 -0700 | [diff] [blame] | 574 | BuildEvent startEvent = |
| 575 | new GenericBuildEvent( |
| 576 | testId("Initial"), |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 577 | ImmutableSet.of( |
| 578 | ProgressEvent.INITIAL_PROGRESS_UPDATE, BuildEventIdUtil.buildFinished())); |
felly | a4488a2 | 2019-04-03 13:05:47 -0700 | [diff] [blame] | 579 | streamer.buildEvent(startEvent); |
| 580 | |
| 581 | int numThreads = 12; |
| 582 | int numEventsPerThread = 100_000; |
| 583 | int totalEvents = numThreads * numEventsPerThread; |
| 584 | AtomicInteger idIndex = new AtomicInteger(); |
| 585 | ThreadPoolExecutor pool = |
| 586 | new ThreadPoolExecutor( |
| 587 | numThreads, numThreads, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<>()); |
| 588 | |
| 589 | for (int i = 0; i < numThreads; i++) { |
| 590 | pool.execute( |
| 591 | () -> { |
| 592 | for (int j = 0; j < numEventsPerThread; j++) { |
| 593 | int index = idIndex.getAndIncrement(); |
| 594 | // Arrange for half of the events to have an ordering constraint on the subsequent |
| 595 | // event. The ordering graph must avoid cycles. |
| 596 | int afterIndex = (index % 2 == 0) ? (index + 1) % totalEvents : -1; |
| 597 | streamer.buildEvent(indexOrderedBuildEvent(index, afterIndex)); |
| 598 | } |
| 599 | }); |
| 600 | } |
| 601 | |
| 602 | pool.shutdown(); |
| 603 | pool.awaitTermination(1, TimeUnit.DAYS); |
| 604 | watch.stop(); |
| 605 | |
philwo | 052e5f8 | 2021-01-19 10:47:41 -0800 | [diff] [blame] | 606 | time += watch.elapsed().toMillis(); |
felly | a4488a2 | 2019-04-03 13:05:47 -0700 | [diff] [blame] | 607 | |
| 608 | BuildEventId lateId = testId("late event"); |
| 609 | streamer.buildEvent(new BuildCompleteEvent(new BuildResult(0), ImmutableList.of(lateId))); |
| 610 | assertThat(streamer.isClosed()).isFalse(); |
| 611 | streamer.buildEvent(new GenericBuildEvent(lateId, ImmutableSet.of())); |
| 612 | assertThat(streamer.isClosed()).isTrue(); |
| 613 | } |
| 614 | |
| 615 | System.err.println(); |
| 616 | System.err.println("============================================================="); |
| 617 | System.err.println("Concurrent performance of BEP build event processing: " + time + "ms"); |
| 618 | System.err.println("============================================================="); |
| 619 | } |
| 620 | |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 621 | @Test |
felly | 21c2582 | 2018-12-28 13:29:52 -0800 | [diff] [blame] | 622 | public void testMissingPrerequisites() { |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 623 | // Verify that an event where the prerequisite is never coming till the end of |
| 624 | // the build still gets posted, with the prerequisite aborted. |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 625 | BuildEventId expectedId = testId("the target"); |
| 626 | BuildEvent startEvent = |
| 627 | new GenericBuildEvent( |
| 628 | testId("Initial"), |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 629 | ImmutableSet.of( |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 630 | ProgressEvent.INITIAL_PROGRESS_UPDATE, |
| 631 | expectedId, |
| 632 | BuildEventIdUtil.buildFinished())); |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 633 | BuildEventId rootCauseId = testId("failure event"); |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 634 | BuildEvent failedTarget = new GenericOrderEvent(expectedId, ImmutableSet.of(rootCauseId)); |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 635 | |
| 636 | streamer.buildEvent(startEvent); |
| 637 | streamer.buildEvent(failedTarget); |
Jakob Buchgraber | 8c3a4ef | 2017-02-28 18:49:50 +0000 | [diff] [blame] | 638 | streamer.buildEvent(new BuildCompleteEvent(new BuildResult(0))); |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 639 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 640 | assertThat(streamer.isClosed()).isTrue(); |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 641 | List<BuildEvent> allEventsSeen = transport.getEvents(); |
Jakob Buchgraber | 8c3a4ef | 2017-02-28 18:49:50 +0000 | [diff] [blame] | 642 | assertThat(allEventsSeen).hasSize(6); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 643 | assertThat(allEventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 644 | assertThat(allEventsSeen.get(1).getEventId()).isEqualTo(BuildEventIdUtil.buildFinished()); |
Jakob Buchgraber | 8c3a4ef | 2017-02-28 18:49:50 +0000 | [diff] [blame] | 645 | BuildEvent linkEvent = allEventsSeen.get(2); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 646 | assertThat(linkEvent.getEventId()).isEqualTo(ProgressEvent.INITIAL_PROGRESS_UPDATE); |
| 647 | assertThat(allEventsSeen.get(3).getEventId()).isEqualTo(rootCauseId); |
| 648 | assertThat(allEventsSeen.get(4).getEventId()).isEqualTo(failedTarget.getEventId()); |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 649 | } |
Klaus Aehlig | ceb0f62 | 2017-03-20 13:52:06 +0000 | [diff] [blame] | 650 | |
| 651 | @Test |
| 652 | public void testVeryFirstEventNeedsToWait() { |
| 653 | // Verify that we can handle an first event waiting for another event. |
Klaus Aehlig | ceb0f62 | 2017-03-20 13:52:06 +0000 | [diff] [blame] | 654 | BuildEventId initialId = testId("Initial"); |
| 655 | BuildEventId waitId = testId("Waiting for initial event"); |
| 656 | BuildEvent startEvent = |
| 657 | new GenericBuildEvent( |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 658 | initialId, ImmutableSet.of(ProgressEvent.INITIAL_PROGRESS_UPDATE, waitId)); |
Klaus Aehlig | ceb0f62 | 2017-03-20 13:52:06 +0000 | [diff] [blame] | 659 | BuildEvent waitingForStart = |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 660 | new GenericOrderEvent(waitId, ImmutableSet.of(), ImmutableSet.of(initialId)); |
Klaus Aehlig | ceb0f62 | 2017-03-20 13:52:06 +0000 | [diff] [blame] | 661 | |
| 662 | streamer.buildEvent(waitingForStart); |
| 663 | streamer.buildEvent(startEvent); |
| 664 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 665 | assertThat(streamer.isClosed()).isFalse(); |
Klaus Aehlig | ceb0f62 | 2017-03-20 13:52:06 +0000 | [diff] [blame] | 666 | List<BuildEvent> allEventsSeen = transport.getEvents(); |
| 667 | assertThat(allEventsSeen).hasSize(2); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 668 | assertThat(allEventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
| 669 | assertThat(allEventsSeen.get(1).getEventId()).isEqualTo(waitingForStart.getEventId()); |
Klaus Aehlig | ceb0f62 | 2017-03-20 13:52:06 +0000 | [diff] [blame] | 670 | } |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 671 | |
| 672 | private Artifact makeArtifact(String pathString) { |
| 673 | Path path = outputBase.getRelative(PathFragment.create(pathString)); |
janakr | aea0560 | 2019-05-22 15:41:29 -0700 | [diff] [blame] | 674 | return ActionsTestUtil.createArtifact( |
| 675 | ArtifactRoot.asSourceRoot(Root.fromPath(outputBase)), path); |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | @Test |
| 679 | public void testReportedArtifacts() { |
| 680 | // Verify that reported artifacts are correctly unfolded into the stream |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 681 | BuildEvent startEvent = |
| 682 | new GenericBuildEvent( |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 683 | testId("Initial"), ImmutableSet.of(ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 684 | |
| 685 | Artifact a = makeArtifact("path/a"); |
| 686 | Artifact b = makeArtifact("path/b"); |
| 687 | Artifact c = makeArtifact("path/c"); |
| 688 | NestedSet<Artifact> innerGroup = NestedSetBuilder.<Artifact>stableOrder().add(a).add(b).build(); |
| 689 | NestedSet<Artifact> group = |
| 690 | NestedSetBuilder.<Artifact>stableOrder().addTransitive(innerGroup).add(c).build(); |
| 691 | BuildEvent reportingArtifacts = |
| 692 | new GenericArtifactReportingEvent(testId("reporting"), ImmutableSet.of(group)); |
| 693 | |
| 694 | streamer.buildEvent(startEvent); |
| 695 | streamer.buildEvent(reportingArtifacts); |
| 696 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 697 | assertThat(streamer.isClosed()).isFalse(); |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 698 | List<BuildEvent> allEventsSeen = transport.getEvents(); |
| 699 | List<BuildEventStreamProtos.BuildEvent> eventProtos = transport.getEventProtos(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 700 | assertThat(allEventsSeen).hasSize(7); |
| 701 | assertThat(allEventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
| 702 | assertThat(allEventsSeen.get(1).getEventId()).isEqualTo(ProgressEvent.INITIAL_PROGRESS_UPDATE); |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 703 | List<BuildEventStreamProtos.File> firstSetDirects = |
| 704 | eventProtos.get(2).getNamedSetOfFiles().getFilesList(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 705 | assertThat(firstSetDirects).hasSize(2); |
| 706 | assertThat(ImmutableSet.of(firstSetDirects.get(0).getUri(), firstSetDirects.get(1).getUri())) |
| 707 | .isEqualTo(ImmutableSet.of(a.getPath().toString(), b.getPath().toString())); |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 708 | List<NamedSetOfFilesId> secondSetTransitives = |
| 709 | eventProtos.get(4).getNamedSetOfFiles().getFileSetsList(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 710 | assertThat(secondSetTransitives).hasSize(1); |
| 711 | assertThat(secondSetTransitives.get(0)).isEqualTo(eventProtos.get(2).getId().getNamedSet()); |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 712 | List<NamedSetOfFilesId> reportedArtifactSets = |
| 713 | eventProtos.get(6).getNamedSetOfFiles().getFileSetsList(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 714 | assertThat(reportedArtifactSets).hasSize(1); |
| 715 | assertThat(reportedArtifactSets.get(0)).isEqualTo(eventProtos.get(4).getId().getNamedSet()); |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 716 | } |
Klaus Aehlig | 9b3b2e9 | 2017-05-02 11:18:42 +0200 | [diff] [blame] | 717 | |
| 718 | @Test |
adgar | fcf9dd5 | 2021-03-09 09:04:47 -0800 | [diff] [blame] | 719 | public void testArtifactSetsPrecedeReportingEvent() throws InterruptedException { |
| 720 | // Verify that reported artifacts appear as named_set_of_files before their ID is referenced by |
| 721 | // a reporting event. |
| 722 | BuildEvent startEvent = |
| 723 | new GenericBuildEvent( |
| 724 | testId("Initial"), ImmutableSet.of(ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
| 725 | |
| 726 | // Prepare a dense NestedSet DAG with lots of shared references. |
| 727 | List<NestedSet<Artifact>> baseSets = new ArrayList<>(); |
| 728 | baseSets.add(NestedSetBuilder.create(Order.STABLE_ORDER, makeArtifact("path/a"))); |
| 729 | baseSets.add(NestedSetBuilder.create(Order.STABLE_ORDER, makeArtifact("path/b"))); |
| 730 | baseSets.add(NestedSetBuilder.create(Order.STABLE_ORDER, makeArtifact("path/c"))); |
| 731 | baseSets.add(NestedSetBuilder.create(Order.STABLE_ORDER, makeArtifact("path/d"))); |
| 732 | List<NestedSet<Artifact>> depth2Sets = new ArrayList<>(); |
| 733 | for (int i = 0; i < baseSets.size(); i++) { |
| 734 | depth2Sets.add( |
| 735 | NestedSetBuilder.<Artifact>stableOrder() |
| 736 | .addTransitive(baseSets.get(i)) |
| 737 | .addTransitive(baseSets.get((i + 1) % baseSets.size())) |
| 738 | .build()); |
| 739 | } |
| 740 | List<NestedSet<Artifact>> depth3Sets = new ArrayList<>(); |
| 741 | for (int i = 0; i < depth2Sets.size(); i++) { |
| 742 | depth3Sets.add( |
| 743 | NestedSetBuilder.<Artifact>stableOrder() |
| 744 | .addTransitive(depth2Sets.get(i)) |
| 745 | .addTransitive(depth2Sets.get((i + 1) % depth2Sets.size())) |
| 746 | .build()); |
| 747 | } |
| 748 | List<NestedSet<Artifact>> depth4Sets = new ArrayList<>(); |
| 749 | for (int i = 0; i < depth3Sets.size(); i++) { |
| 750 | depth4Sets.add( |
| 751 | NestedSetBuilder.<Artifact>stableOrder() |
| 752 | .addTransitive(depth3Sets.get(i)) |
| 753 | .addTransitive(depth3Sets.get((i + 1) % depth3Sets.size())) |
| 754 | .build()); |
| 755 | } |
| 756 | int numEvents = 20; |
| 757 | List<BuildEvent> eventsToPost = new ArrayList<>(); |
| 758 | for (int i = 0; i < numEvents; i++) { |
| 759 | eventsToPost.add( |
| 760 | new GenericArtifactReportingEvent( |
| 761 | testId("reporting" + i), ImmutableSet.of(depth4Sets.get(i % depth4Sets.size())))); |
| 762 | } |
| 763 | |
| 764 | streamer.buildEvent(startEvent); |
| 765 | // Publish `numEvents` different events that all report the same NamedSet of artifacts on |
| 766 | // `numEvents` different threads. Use latches to ensure: |
| 767 | // |
| 768 | // 1. all threads have started, before: |
| 769 | // 2. all threads send their event, before: |
| 770 | // 3. verifying the recorded events. |
| 771 | CountDownLatch readyToPublishLatch = new CountDownLatch(numEvents); |
| 772 | CountDownLatch startPublishingLatch = new CountDownLatch(1); |
| 773 | CountDownLatch donePublishingLatch = new CountDownLatch(numEvents); |
| 774 | for (int i = 0; i < numEvents; i++) { |
| 775 | int num = i; |
| 776 | new Thread( |
| 777 | () -> { |
| 778 | try { |
| 779 | BuildEvent reportingArtifacts = eventsToPost.get(num); |
| 780 | readyToPublishLatch.countDown(); |
| 781 | startPublishingLatch.await(); |
| 782 | streamer.buildEvent(reportingArtifacts); |
| 783 | } catch (InterruptedException e) { |
| 784 | throw new RuntimeException(e); |
| 785 | } |
| 786 | donePublishingLatch.countDown(); |
| 787 | }) |
| 788 | .start(); |
| 789 | } |
| 790 | readyToPublishLatch.await(); |
| 791 | startPublishingLatch.countDown(); |
| 792 | donePublishingLatch.await(); |
| 793 | |
| 794 | assertThat(streamer.isClosed()).isFalse(); |
| 795 | List<BuildEvent> allEventsSeen = transport.getEvents(); |
| 796 | List<BuildEventStreamProtos.BuildEvent> eventProtos = transport.getEventProtos(); |
| 797 | // Each GenericArtifactReportingEvent and NamedArtifactGroup event has a corresponding Progress |
| 798 | // event posted immediately before. |
| 799 | assertThat(allEventsSeen) |
| 800 | .hasSize(1 + ((numEvents + baseSets.size() + depth2Sets.size() + depth3Sets.size()) * 2)); |
| 801 | assertThat(allEventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
| 802 | // Verify that each named_set_of_files event is sent before all of the events that report that |
| 803 | // named_set. |
| 804 | Set<String> seenFileSets = new HashSet<>(); |
| 805 | for (int i = 1; i < eventProtos.size(); i++) { |
| 806 | BuildEventStreamProtos.BuildEvent buildEvent = eventProtos.get(i); |
| 807 | if (buildEvent.getId().hasNamedSet()) { |
| 808 | // These are the separately-posted contents of reported artifacts. |
| 809 | seenFileSets.add(buildEvent.getId().getNamedSet().getId()); |
| 810 | for (NamedSetOfFilesId nestedSetId : buildEvent.getNamedSetOfFiles().getFileSetsList()) { |
| 811 | assertThat(seenFileSets).contains(nestedSetId.getId()); |
| 812 | } |
| 813 | } else if (buildEvent.getId().hasUnknown()) { |
| 814 | // These are the GenericArtifactReportingEvent that report artifacts. |
| 815 | for (NamedSetOfFilesId nestedSetId : buildEvent.getNamedSetOfFiles().getFileSetsList()) { |
| 816 | assertThat(seenFileSets).contains(nestedSetId.getId()); |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | @Test |
Klaus Aehlig | 9b3b2e9 | 2017-05-02 11:18:42 +0200 | [diff] [blame] | 823 | public void testStdoutReported() { |
| 824 | // Verify that stdout and stderr are reported in the build-event stream on progress |
| 825 | // events. |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 826 | BuildEventStreamer.OutErrProvider outErr = mock(BuildEventStreamer.OutErrProvider.class); |
Klaus Aehlig | 9b3b2e9 | 2017-05-02 11:18:42 +0200 | [diff] [blame] | 827 | String stdoutMsg = "Some text that was written to stdout."; |
| 828 | String stderrMsg = "The UI text that bazel wrote to stderr."; |
michajlo | d6d4ee2 | 2020-04-16 13:48:06 -0700 | [diff] [blame] | 829 | when(outErr.getOut()).thenReturn(ImmutableList.of(stdoutMsg)); |
| 830 | when(outErr.getErr()).thenReturn(ImmutableList.of(stderrMsg)); |
Klaus Aehlig | 9b3b2e9 | 2017-05-02 11:18:42 +0200 | [diff] [blame] | 831 | BuildEvent startEvent = |
| 832 | new GenericBuildEvent( |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 833 | testId("Initial"), ImmutableSet.of(ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
| 834 | BuildEvent unexpectedEvent = new GenericBuildEvent(testId("unexpected"), ImmutableSet.of()); |
Klaus Aehlig | 9b3b2e9 | 2017-05-02 11:18:42 +0200 | [diff] [blame] | 835 | |
| 836 | streamer.registerOutErrProvider(outErr); |
| 837 | streamer.buildEvent(startEvent); |
| 838 | streamer.buildEvent(unexpectedEvent); |
| 839 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 840 | assertThat(streamer.isClosed()).isFalse(); |
Klaus Aehlig | 9b3b2e9 | 2017-05-02 11:18:42 +0200 | [diff] [blame] | 841 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 842 | assertThat(eventsSeen).hasSize(3); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 843 | assertThat(eventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
| 844 | assertThat(eventsSeen.get(2).getEventId()).isEqualTo(unexpectedEvent.getEventId()); |
Klaus Aehlig | 9b3b2e9 | 2017-05-02 11:18:42 +0200 | [diff] [blame] | 845 | BuildEvent linkEvent = eventsSeen.get(1); |
| 846 | BuildEventStreamProtos.BuildEvent linkEventProto = transport.getEventProtos().get(1); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 847 | assertThat(linkEvent.getEventId()).isEqualTo(ProgressEvent.INITIAL_PROGRESS_UPDATE); |
| 848 | assertWithMessage("Unexpected events should be linked") |
| 849 | .that(linkEvent.getChildrenEvents().contains(unexpectedEvent.getEventId())) |
| 850 | .isTrue(); |
| 851 | assertThat(linkEventProto.getProgress().getStdout()).isEqualTo(stdoutMsg); |
| 852 | assertThat(linkEventProto.getProgress().getStderr()).isEqualTo(stderrMsg); |
Klaus Aehlig | 9b3b2e9 | 2017-05-02 11:18:42 +0200 | [diff] [blame] | 853 | |
| 854 | // As there is only one progress event, the OutErrProvider should be queried |
| 855 | // only once for stdout and stderr. |
| 856 | verify(outErr, times(1)).getOut(); |
| 857 | verify(outErr, times(1)).getErr(); |
| 858 | } |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 859 | |
| 860 | @Test |
felly | 21c2582 | 2018-12-28 13:29:52 -0800 | [diff] [blame] | 861 | public void testStdoutReportedAfterCrash() { |
| 862 | // Verify that stdout and stderr are reported in the build-event stream on progress |
| 863 | // events. |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 864 | BuildEventStreamer.OutErrProvider outErr = mock(BuildEventStreamer.OutErrProvider.class); |
felly | 21c2582 | 2018-12-28 13:29:52 -0800 | [diff] [blame] | 865 | String stdoutMsg = "Some text that was written to stdout."; |
| 866 | String stderrMsg = "The UI text that bazel wrote to stderr."; |
michajlo | d6d4ee2 | 2020-04-16 13:48:06 -0700 | [diff] [blame] | 867 | when(outErr.getOut()).thenReturn(ImmutableList.of(stdoutMsg)); |
| 868 | when(outErr.getErr()).thenReturn(ImmutableList.of(stderrMsg)); |
felly | 21c2582 | 2018-12-28 13:29:52 -0800 | [diff] [blame] | 869 | BuildEvent startEvent = |
| 870 | new GenericBuildEvent( |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 871 | testId("Initial"), ImmutableSet.of(ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
felly | 21c2582 | 2018-12-28 13:29:52 -0800 | [diff] [blame] | 872 | |
| 873 | streamer.registerOutErrProvider(outErr); |
| 874 | streamer.buildEvent(startEvent); |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 875 | // Simulate a crash with an abrupt call to #closeOnAbort(). |
| 876 | streamer.closeOnAbort(AbortReason.INTERNAL); |
felly | 21c2582 | 2018-12-28 13:29:52 -0800 | [diff] [blame] | 877 | assertThat(streamer.isClosed()).isTrue(); |
| 878 | |
| 879 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 880 | assertThat(eventsSeen).hasSize(2); |
| 881 | assertThat(eventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
| 882 | BuildEvent linkEvent = eventsSeen.get(1); |
| 883 | BuildEventStreamProtos.BuildEvent linkEventProto = transport.getEventProtos().get(1); |
| 884 | assertThat(linkEvent.getEventId()).isEqualTo(ProgressEvent.INITIAL_PROGRESS_UPDATE); |
| 885 | assertThat(linkEventProto.getProgress().getStdout()).isEqualTo(stdoutMsg); |
| 886 | assertThat(linkEventProto.getProgress().getStderr()).isEqualTo(stderrMsg); |
| 887 | |
| 888 | // As there is only one progress event, the OutErrProvider should be queried |
| 889 | // only once for stdout and stderr. |
| 890 | verify(outErr, times(1)).getOut(); |
| 891 | verify(outErr, times(1)).getErr(); |
| 892 | } |
| 893 | |
michajlo | d6d4ee2 | 2020-04-16 13:48:06 -0700 | [diff] [blame] | 894 | private static <T> ImmutableList<ImmutableList<Pair<T, T>>> consumeToLists( |
| 895 | Iterable<T> left, Iterable<T> right) { |
| 896 | ImmutableList.Builder<Pair<T, T>> consumerBuilder = ImmutableList.builder(); |
| 897 | ImmutableList.Builder<Pair<T, T>> lastConsumerBuilder = ImmutableList.builder(); |
| 898 | |
| 899 | BuildEventStreamer.consumeAsPairs( |
| 900 | left, |
| 901 | right, |
| 902 | (t1, t2) -> consumerBuilder.add(Pair.of(t1, t2)), |
| 903 | (t1, t2) -> lastConsumerBuilder.add(Pair.of(t1, t2))); |
| 904 | |
| 905 | return ImmutableList.of(consumerBuilder.build(), lastConsumerBuilder.build()); |
| 906 | } |
| 907 | |
| 908 | @Test |
| 909 | public void testConsumeAsPairs() { |
| 910 | assertThat(consumeToLists(ImmutableList.of(1, 2, 3), ImmutableList.of(4, 5, 6))) |
| 911 | .containsExactly( |
| 912 | ImmutableList.of(Pair.of(1, null), Pair.of(2, null), Pair.of(3, 4), Pair.of(null, 5)), |
| 913 | ImmutableList.of(Pair.of(null, 6))) |
| 914 | .inOrder(); |
| 915 | |
| 916 | assertThat(consumeToLists(ImmutableList.of(), ImmutableList.of())) |
| 917 | .containsExactly(ImmutableList.of(), ImmutableList.of(Pair.of(null, null))) |
| 918 | .inOrder(); |
| 919 | |
| 920 | assertThat(consumeToLists(ImmutableList.of(1), ImmutableList.of(2))) |
| 921 | .containsExactly(ImmutableList.of(), ImmutableList.of(Pair.of(1, 2))) |
| 922 | .inOrder(); |
| 923 | |
| 924 | assertThat(consumeToLists(ImmutableList.of(1), ImmutableList.of(2, 3))) |
| 925 | .containsExactly(ImmutableList.of(Pair.of(1, 2)), ImmutableList.of(Pair.of(null, 3))) |
| 926 | .inOrder(); |
| 927 | |
| 928 | assertThat(consumeToLists(ImmutableList.of(1, 2), ImmutableList.of())) |
| 929 | .containsExactly(ImmutableList.of(Pair.of(1, null)), ImmutableList.of(Pair.of(2, null))) |
| 930 | .inOrder(); |
| 931 | |
| 932 | assertThat(consumeToLists(ImmutableList.of(), ImmutableList.of(1))) |
| 933 | .containsExactly(ImmutableList.of(), ImmutableList.of(Pair.of(null, 1))) |
| 934 | .inOrder(); |
| 935 | } |
felly | 1759476 | 2019-02-07 15:24:32 -0800 | [diff] [blame] | 936 | |
felly | 21c2582 | 2018-12-28 13:29:52 -0800 | [diff] [blame] | 937 | @Test |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 938 | public void testReportedConfigurations() throws Exception { |
| 939 | // Verify that configuration events are posted, but only once. |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 940 | BuildOptions defaultBuildOptions = BuildOptions.of(ImmutableList.of(CoreOptions.class)); |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 941 | BuildEvent startEvent = |
| 942 | new GenericBuildEvent( |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 943 | testId("Initial"), ImmutableSet.of(ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 944 | BuildConfigurationValue configuration = |
twigg | 44c30ac | 2021-12-08 15:57:11 -0800 | [diff] [blame] | 945 | BuildConfigurationValue.create( |
| 946 | defaultBuildOptions, |
Googler | 80ada0f | 2021-12-22 07:15:59 -0800 | [diff] [blame] | 947 | RepositoryName.createFromValidStrippedName("workspace"), |
twigg | 44c30ac | 2021-12-08 15:57:11 -0800 | [diff] [blame] | 948 | /*siblingRepositoryLayout=*/ false, |
janakr | 3b63a4e | 2017-09-14 09:55:40 +0200 | [diff] [blame] | 949 | new BlazeDirectories( |
Klaus Aehlig | c2499c4 | 2018-02-27 05:47:21 -0800 | [diff] [blame] | 950 | new ServerDirectories(outputBase, outputBase, outputBase), |
| 951 | rootDirectory, |
jhorvitz | 9ad5aed | 2021-04-23 14:32:33 -0700 | [diff] [blame] | 952 | /*defaultSystemJavabase=*/ null, |
Klaus Aehlig | c2499c4 | 2018-02-27 05:47:21 -0800 | [diff] [blame] | 953 | "productName"), |
twigg | 44c30ac | 2021-12-08 15:57:11 -0800 | [diff] [blame] | 954 | new BuildConfigurationValue.GlobalStateProvider() { |
| 955 | @Override |
| 956 | public ActionEnvironment getActionEnvironment(BuildOptions buildOptions) { |
| 957 | return ActionEnvironment.EMPTY; |
| 958 | } |
| 959 | |
| 960 | @Override |
| 961 | public FragmentRegistry getFragmentRegistry() { |
| 962 | return FragmentRegistry.create( |
| 963 | ImmutableList.of(), ImmutableList.of(), ImmutableList.of()); |
| 964 | } |
| 965 | |
| 966 | @Override |
| 967 | public ImmutableSet<String> getReservedActionMnemonics() { |
| 968 | return ImmutableSet.of(); |
| 969 | } |
| 970 | }, |
| 971 | new FragmentFactory()); |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 972 | BuildEvent firstWithConfiguration = |
shahan | 50f99d5 | 2018-03-10 05:14:09 -0800 | [diff] [blame] | 973 | new GenericConfigurationEvent(testId("first"), configuration.toBuildEvent()); |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 974 | BuildEvent secondWithConfiguration = |
shahan | 50f99d5 | 2018-03-10 05:14:09 -0800 | [diff] [blame] | 975 | new GenericConfigurationEvent(testId("second"), configuration.toBuildEvent()); |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 976 | |
| 977 | streamer.buildEvent(startEvent); |
| 978 | streamer.buildEvent(firstWithConfiguration); |
| 979 | streamer.buildEvent(secondWithConfiguration); |
| 980 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 981 | assertThat(streamer.isClosed()).isFalse(); |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 982 | List<BuildEvent> allEventsSeen = transport.getEvents(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 983 | assertThat(allEventsSeen).hasSize(7); |
| 984 | assertThat(allEventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
| 985 | assertThat(allEventsSeen.get(1).getEventId()).isEqualTo(ProgressEvent.INITIAL_PROGRESS_UPDATE); |
shahan | 50f99d5 | 2018-03-10 05:14:09 -0800 | [diff] [blame] | 986 | assertThat(allEventsSeen.get(2)).isEqualTo(configuration.toBuildEvent()); |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 987 | assertThat(allEventsSeen.get(3).getEventId()).isEqualTo(BuildEventIdUtil.progressId(1)); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 988 | assertThat(allEventsSeen.get(4)).isEqualTo(firstWithConfiguration); |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 989 | assertThat(allEventsSeen.get(5).getEventId()).isEqualTo(BuildEventIdUtil.progressId(2)); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 990 | assertThat(allEventsSeen.get(6)).isEqualTo(secondWithConfiguration); |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 991 | } |
Klaus Aehlig | 8f6549f | 2017-06-26 15:30:55 +0200 | [diff] [blame] | 992 | |
| 993 | @Test |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 994 | public void testEarlyFlush() { |
Klaus Aehlig | 8f6549f | 2017-06-26 15:30:55 +0200 | [diff] [blame] | 995 | // Verify that the streamer can handle early calls to flush() and still correctly |
| 996 | // reports stdout and stderr in the build-event stream. |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 997 | BuildEventStreamer.OutErrProvider outErr = mock(BuildEventStreamer.OutErrProvider.class); |
Klaus Aehlig | 8f6549f | 2017-06-26 15:30:55 +0200 | [diff] [blame] | 998 | String firstStdoutMsg = "Some text that was written to stdout."; |
| 999 | String firstStderrMsg = "The UI text that bazel wrote to stderr."; |
| 1000 | String secondStdoutMsg = "More text that was written to stdout, still before the start event."; |
| 1001 | String secondStderrMsg = "More text written to stderr, still before the start event."; |
felly | 1759476 | 2019-02-07 15:24:32 -0800 | [diff] [blame] | 1002 | when(outErr.getOut()) |
michajlo | d6d4ee2 | 2020-04-16 13:48:06 -0700 | [diff] [blame] | 1003 | .thenReturn(ImmutableList.of(firstStdoutMsg)) |
| 1004 | .thenReturn(ImmutableList.of(secondStdoutMsg)); |
felly | 1759476 | 2019-02-07 15:24:32 -0800 | [diff] [blame] | 1005 | when(outErr.getErr()) |
michajlo | d6d4ee2 | 2020-04-16 13:48:06 -0700 | [diff] [blame] | 1006 | .thenReturn(ImmutableList.of(firstStderrMsg)) |
| 1007 | .thenReturn(ImmutableList.of(secondStderrMsg)); |
Klaus Aehlig | 8f6549f | 2017-06-26 15:30:55 +0200 | [diff] [blame] | 1008 | BuildEvent startEvent = |
| 1009 | new GenericBuildEvent( |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1010 | testId("Initial"), ImmutableSet.of(ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
Klaus Aehlig | 8f6549f | 2017-06-26 15:30:55 +0200 | [diff] [blame] | 1011 | |
| 1012 | streamer.registerOutErrProvider(outErr); |
| 1013 | streamer.flush(); |
| 1014 | streamer.flush(); |
| 1015 | streamer.buildEvent(startEvent); |
| 1016 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 1017 | assertThat(streamer.isClosed()).isFalse(); |
Klaus Aehlig | 8f6549f | 2017-06-26 15:30:55 +0200 | [diff] [blame] | 1018 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 1019 | assertThat(eventsSeen).hasSize(3); |
| 1020 | assertThat(eventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
| 1021 | BuildEvent progressEvent = eventsSeen.get(1); |
| 1022 | assertThat(progressEvent.getEventId()).isEqualTo(ProgressEvent.INITIAL_PROGRESS_UPDATE); |
| 1023 | BuildEventStreamProtos.BuildEvent progressEventProto = transport.getEventProtos().get(1); |
| 1024 | assertThat(progressEventProto.getProgress().getStdout()).isEqualTo(firstStdoutMsg); |
| 1025 | assertThat(progressEventProto.getProgress().getStderr()).isEqualTo(firstStderrMsg); |
| 1026 | BuildEventStreamProtos.BuildEvent secondProgressEventProto = transport.getEventProtos().get(2); |
| 1027 | assertThat(secondProgressEventProto.getProgress().getStdout()).isEqualTo(secondStdoutMsg); |
| 1028 | assertThat(secondProgressEventProto.getProgress().getStderr()).isEqualTo(secondStderrMsg); |
| 1029 | |
| 1030 | // As there is only one progress event, the OutErrProvider should be queried |
| 1031 | // only once per flush() for stdout and stderr. |
| 1032 | verify(outErr, times(2)).getOut(); |
| 1033 | verify(outErr, times(2)).getErr(); |
| 1034 | } |
| 1035 | |
| 1036 | @Test |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1037 | public void testChunkedFlush() { |
felly | 1759476 | 2019-02-07 15:24:32 -0800 | [diff] [blame] | 1038 | // Verify that the streamer calls to flush() that return multiple chunked buffers. |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1039 | BuildEventStreamer.OutErrProvider outErr = mock(BuildEventStreamer.OutErrProvider.class); |
felly | 1759476 | 2019-02-07 15:24:32 -0800 | [diff] [blame] | 1040 | String firstStdoutMsg = "Some text that was written to stdout."; |
| 1041 | String firstStderrMsg = "The UI text that bazel wrote to stderr."; |
| 1042 | String secondStdoutMsg = "More text that was written to stdout, still before the start event."; |
| 1043 | String secondStderrMsg = "More text written to stderr, still before the start event."; |
michajlo | d6d4ee2 | 2020-04-16 13:48:06 -0700 | [diff] [blame] | 1044 | when(outErr.getOut()).thenReturn(ImmutableList.of(firstStdoutMsg, secondStdoutMsg)); |
| 1045 | when(outErr.getErr()).thenReturn(ImmutableList.of(firstStderrMsg, secondStderrMsg)); |
felly | 1759476 | 2019-02-07 15:24:32 -0800 | [diff] [blame] | 1046 | BuildEvent startEvent = |
| 1047 | new GenericBuildEvent( |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1048 | testId("Initial"), ImmutableSet.of(ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
felly | 1759476 | 2019-02-07 15:24:32 -0800 | [diff] [blame] | 1049 | |
| 1050 | streamer.registerOutErrProvider(outErr); |
| 1051 | streamer.buildEvent(startEvent); |
| 1052 | streamer.flush(); |
| 1053 | |
| 1054 | assertThat(streamer.isClosed()).isFalse(); |
| 1055 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 1056 | assertThat(eventsSeen).hasSize(4); |
| 1057 | assertThat(eventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
| 1058 | |
| 1059 | // Expect to find 3 progress messages: (firstStdout, ""), (secondStdout, firstStderr), |
| 1060 | // ("", secondStdErr). Assuming UIs display stdout first, this maintains ordering. |
| 1061 | BuildEvent progressEvent = eventsSeen.get(1); |
| 1062 | assertThat(progressEvent.getEventId()).isEqualTo(ProgressEvent.INITIAL_PROGRESS_UPDATE); |
| 1063 | BuildEventStreamProtos.BuildEvent progressEventProto = transport.getEventProtos().get(1); |
| 1064 | assertThat(progressEventProto.getProgress().getStdout()).isEqualTo(firstStdoutMsg); |
| 1065 | assertThat(progressEventProto.getProgress().getStderr()).isEmpty(); |
| 1066 | |
| 1067 | BuildEventStreamProtos.BuildEvent secondProgressEventProto = transport.getEventProtos().get(2); |
| 1068 | assertThat(secondProgressEventProto.getProgress().getStdout()).isEqualTo(secondStdoutMsg); |
| 1069 | assertThat(secondProgressEventProto.getProgress().getStderr()).isEqualTo(firstStderrMsg); |
| 1070 | |
| 1071 | BuildEventStreamProtos.BuildEvent thirdProgressEventProto = transport.getEventProtos().get(3); |
| 1072 | assertThat(thirdProgressEventProto.getProgress().getStdout()).isEmpty(); |
| 1073 | assertThat(thirdProgressEventProto.getProgress().getStderr()).isEqualTo(secondStderrMsg); |
| 1074 | |
| 1075 | // The OutErrProvider should be queried only once per flush(). |
| 1076 | verify(outErr, times(1)).getOut(); |
| 1077 | verify(outErr, times(1)).getErr(); |
| 1078 | } |
| 1079 | |
| 1080 | @Test |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1081 | public void testNoopFlush() { |
Klaus Aehlig | 28221ff | 2018-01-11 04:28:00 -0800 | [diff] [blame] | 1082 | // Verify that the streamer ignores a flush, if neither stream produces any output. |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1083 | BuildEventStreamer.OutErrProvider outErr = mock(BuildEventStreamer.OutErrProvider.class); |
Klaus Aehlig | 28221ff | 2018-01-11 04:28:00 -0800 | [diff] [blame] | 1084 | String stdoutMsg = "Some text that was written to stdout."; |
| 1085 | String stderrMsg = "The UI text that bazel wrote to stderr."; |
michajlo | d6d4ee2 | 2020-04-16 13:48:06 -0700 | [diff] [blame] | 1086 | when(outErr.getOut()).thenReturn(ImmutableList.of(stdoutMsg)).thenReturn(ImmutableList.of()); |
| 1087 | when(outErr.getErr()).thenReturn(ImmutableList.of(stderrMsg)).thenReturn(ImmutableList.of()); |
Klaus Aehlig | 28221ff | 2018-01-11 04:28:00 -0800 | [diff] [blame] | 1088 | BuildEvent startEvent = |
| 1089 | new GenericBuildEvent( |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1090 | testId("Initial"), ImmutableSet.of(ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
Klaus Aehlig | 28221ff | 2018-01-11 04:28:00 -0800 | [diff] [blame] | 1091 | |
| 1092 | streamer.registerOutErrProvider(outErr); |
| 1093 | streamer.buildEvent(startEvent); |
| 1094 | assertThat(transport.getEvents()).hasSize(1); |
| 1095 | streamer.flush(); // Output, so a new progress event has to be added |
| 1096 | assertThat(transport.getEvents()).hasSize(2); |
| 1097 | streamer.flush(); // No further output, so no additional event should be generated. |
| 1098 | assertThat(transport.getEvents()).hasSize(2); |
| 1099 | |
| 1100 | assertThat(transport.getEvents().get(0)).isEqualTo(startEvent); |
| 1101 | assertThat(transport.getEventProtos().get(1).getProgress().getStdout()).isEqualTo(stdoutMsg); |
| 1102 | assertThat(transport.getEventProtos().get(1).getProgress().getStderr()).isEqualTo(stderrMsg); |
| 1103 | } |
| 1104 | |
| 1105 | @Test |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1106 | public void testEarlyFlushBadInitialEvent() { |
Klaus Aehlig | 8f6549f | 2017-06-26 15:30:55 +0200 | [diff] [blame] | 1107 | // Verify that an early flush works correctly with an unusual start event. |
| 1108 | // In this case, we expect 3 events in the stream, in that order: |
jingwen | 68c57f0 | 2018-11-21 16:17:17 -0800 | [diff] [blame] | 1109 | // - an artificial progress event as initial event, to properly link in |
Klaus Aehlig | 8f6549f | 2017-06-26 15:30:55 +0200 | [diff] [blame] | 1110 | // all events |
| 1111 | // - the unusal first event we have seen, and |
| 1112 | // - a progress event reporting the flushed messages. |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1113 | BuildEventStreamer.OutErrProvider outErr = mock(BuildEventStreamer.OutErrProvider.class); |
Klaus Aehlig | 8f6549f | 2017-06-26 15:30:55 +0200 | [diff] [blame] | 1114 | String stdoutMsg = "Some text that was written to stdout."; |
| 1115 | String stderrMsg = "The UI text that bazel wrote to stderr."; |
michajlo | d6d4ee2 | 2020-04-16 13:48:06 -0700 | [diff] [blame] | 1116 | when(outErr.getOut()).thenReturn(ImmutableList.of(stdoutMsg)); |
| 1117 | when(outErr.getErr()).thenReturn(ImmutableList.of(stderrMsg)); |
Klaus Aehlig | 8f6549f | 2017-06-26 15:30:55 +0200 | [diff] [blame] | 1118 | |
| 1119 | BuildEvent unexpectedStartEvent = |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1120 | new GenericBuildEvent(testId("unexpected start"), ImmutableSet.of()); |
Klaus Aehlig | 8f6549f | 2017-06-26 15:30:55 +0200 | [diff] [blame] | 1121 | |
| 1122 | streamer.registerOutErrProvider(outErr); |
| 1123 | streamer.flush(); |
| 1124 | streamer.buildEvent(unexpectedStartEvent); |
| 1125 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 1126 | assertThat(streamer.isClosed()).isFalse(); |
| 1127 | |
Klaus Aehlig | 8f6549f | 2017-06-26 15:30:55 +0200 | [diff] [blame] | 1128 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 1129 | assertThat(eventsSeen).hasSize(3); |
| 1130 | |
| 1131 | BuildEvent initial = eventsSeen.get(0); |
| 1132 | assertThat(initial.getEventId()).isEqualTo(ProgressEvent.INITIAL_PROGRESS_UPDATE); |
| 1133 | BuildEventStreamProtos.BuildEvent initialProto = transport.getEventProtos().get(0); |
| 1134 | assertThat(initialProto.getProgress().getStdout()).isEmpty(); |
| 1135 | assertThat(initialProto.getProgress().getStderr()).isEmpty(); |
| 1136 | |
| 1137 | assertThat(eventsSeen.get(1).getEventId()).isEqualTo(unexpectedStartEvent.getEventId()); |
| 1138 | assertWithMessage("Unexpected event should be linked") |
| 1139 | .that(initial.getChildrenEvents().contains(unexpectedStartEvent.getEventId())) |
| 1140 | .isTrue(); |
| 1141 | |
| 1142 | BuildEventStreamProtos.BuildEvent progressProto = transport.getEventProtos().get(2); |
| 1143 | assertThat(progressProto.getProgress().getStdout()).isEqualTo(stdoutMsg); |
| 1144 | assertThat(progressProto.getProgress().getStderr()).isEqualTo(stderrMsg); |
| 1145 | assertWithMessage("flushed progress should be linked") |
| 1146 | .that(initial.getChildrenEvents().contains(eventsSeen.get(2).getEventId())) |
| 1147 | .isTrue(); |
| 1148 | |
| 1149 | verify(outErr, times(1)).getOut(); |
| 1150 | verify(outErr, times(1)).getErr(); |
| 1151 | } |
Klaus Aehlig | deea6b0 | 2017-10-16 12:32:20 +0200 | [diff] [blame] | 1152 | |
| 1153 | @Test |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1154 | public void testEarlyAbort() { |
Klaus Aehlig | deea6b0 | 2017-10-16 12:32:20 +0200 | [diff] [blame] | 1155 | // For a build that is aborted before a build-started event is generated, |
| 1156 | // we still expect that, if a build-started event is forced by some order |
| 1157 | // constraint (e.g., CommandLine wants to come after build started), then |
| 1158 | // that gets sorted to the beginning. |
Klaus Aehlig | deea6b0 | 2017-10-16 12:32:20 +0200 | [diff] [blame] | 1159 | BuildEvent orderEvent = |
| 1160 | new GenericOrderEvent( |
| 1161 | testId("event depending on start"), |
| 1162 | ImmutableList.of(), |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 1163 | ImmutableList.of(BuildEventIdUtil.buildStartedId())); |
Klaus Aehlig | deea6b0 | 2017-10-16 12:32:20 +0200 | [diff] [blame] | 1164 | |
| 1165 | streamer.buildEvent(orderEvent); |
| 1166 | streamer.buildEvent(new BuildCompleteEvent(new BuildResult(0))); |
| 1167 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 1168 | assertThat(streamer.isClosed()).isTrue(); |
Klaus Aehlig | deea6b0 | 2017-10-16 12:32:20 +0200 | [diff] [blame] | 1169 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 1170 | assertThat(eventsSeen).hasSize(4); |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 1171 | assertThat(eventsSeen.get(0).getEventId()).isEqualTo(BuildEventIdUtil.buildStartedId()); |
Klaus Aehlig | deea6b0 | 2017-10-16 12:32:20 +0200 | [diff] [blame] | 1172 | assertThat(eventsSeen.get(1).getEventId()).isEqualTo(orderEvent.getEventId()); |
| 1173 | assertThat(ImmutableSet.of(eventsSeen.get(2).getEventId(), eventsSeen.get(3).getEventId())) |
| 1174 | .isEqualTo( |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 1175 | ImmutableSet.of( |
| 1176 | BuildEventIdUtil.buildFinished(), ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
Klaus Aehlig | deea6b0 | 2017-10-16 12:32:20 +0200 | [diff] [blame] | 1177 | assertThat(transport.getEventProtos().get(3).getLastMessage()).isTrue(); |
| 1178 | } |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 1179 | |
| 1180 | @Test |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1181 | public void testFinalEventsLate() { |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 1182 | // Verify that we correctly handle late events (i.e., events coming only after the |
| 1183 | // BuildCompleteEvent) that are sent to the streamer after the BuildCompleteEvent. |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 1184 | BuildEvent startEvent = |
| 1185 | new GenericBuildEvent( |
| 1186 | testId("Initial"), |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 1187 | ImmutableSet.of( |
| 1188 | ProgressEvent.INITIAL_PROGRESS_UPDATE, BuildEventIdUtil.buildFinished())); |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 1189 | BuildEventId lateId = testId("late event"); |
| 1190 | BuildEvent finishedEvent = new BuildCompleteEvent(new BuildResult(0), ImmutableList.of(lateId)); |
| 1191 | |
| 1192 | streamer.buildEvent(startEvent); |
| 1193 | streamer.buildEvent(finishedEvent); |
| 1194 | assertThat(streamer.isClosed()).isFalse(); |
| 1195 | streamer.buildEvent(new GenericBuildEvent(lateId, ImmutableSet.of())); |
| 1196 | assertThat(streamer.isClosed()).isTrue(); |
| 1197 | |
| 1198 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 1199 | assertThat(eventsSeen).hasSize(4); |
| 1200 | assertThat(eventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 1201 | assertThat(eventsSeen.get(1).getEventId()).isEqualTo(BuildEventIdUtil.buildFinished()); |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 1202 | assertThat(ImmutableSet.of(eventsSeen.get(2).getEventId(), eventsSeen.get(3).getEventId())) |
| 1203 | .isEqualTo(ImmutableSet.of(lateId, ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
| 1204 | } |
| 1205 | |
| 1206 | @Test |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1207 | public void testFinalEventsEarly() { |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 1208 | // Verify that we correctly handle late events (i.e., events coming only after the |
| 1209 | // BuildCompleteEvent) that are sent to the streamer before the BuildCompleteEvent, |
| 1210 | // but with an order constraint to come afterwards. |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 1211 | BuildEvent startEvent = |
| 1212 | new GenericBuildEvent( |
| 1213 | testId("Initial"), |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 1214 | ImmutableSet.of( |
| 1215 | ProgressEvent.INITIAL_PROGRESS_UPDATE, BuildEventIdUtil.buildFinished())); |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 1216 | BuildEventId lateId = testId("late event"); |
| 1217 | BuildEvent finishedEvent = new BuildCompleteEvent(new BuildResult(0), ImmutableList.of(lateId)); |
| 1218 | |
| 1219 | streamer.buildEvent(startEvent); |
| 1220 | streamer.buildEvent( |
| 1221 | new GenericOrderEvent( |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 1222 | lateId, ImmutableSet.of(), ImmutableList.of(BuildEventIdUtil.buildFinished()))); |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 1223 | streamer.buildEvent(finishedEvent); |
| 1224 | assertThat(streamer.isClosed()).isTrue(); |
| 1225 | |
| 1226 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 1227 | assertThat(eventsSeen).hasSize(4); |
| 1228 | assertThat(eventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 1229 | assertThat(eventsSeen.get(1).getEventId()).isEqualTo(BuildEventIdUtil.buildFinished()); |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 1230 | assertThat(ImmutableSet.of(eventsSeen.get(2).getEventId(), eventsSeen.get(3).getEventId())) |
| 1231 | .isEqualTo(ImmutableSet.of(lateId, ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
| 1232 | } |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 1233 | |
| 1234 | @Test |
| 1235 | public void testSuccessfulActionsAreNotPublishedByDefault() { |
| 1236 | EventBusHandler handler = new EventBusHandler(); |
| 1237 | eventBus.register(handler); |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 1238 | ActionExecutedEvent failedActionExecutedEvent = |
| 1239 | new ActionExecutedEvent( |
ulfjack | 5f312dd | 2018-10-02 04:58:53 -0700 | [diff] [blame] | 1240 | ActionsTestUtil.DUMMY_ARTIFACT.getExecPath(), |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 1241 | new ActionsTestUtil.NullAction(), |
mschaller | 2902018 | 2020-06-10 23:16:07 -0700 | [diff] [blame] | 1242 | new ActionExecutionException( |
| 1243 | "Exception", |
| 1244 | /* action= */ null, |
| 1245 | /* catastrophe= */ false, |
| 1246 | DetailedExitCode.of( |
| 1247 | FailureDetail.newBuilder() |
| 1248 | .setSpawn(Spawn.newBuilder().setCode(Code.EXECUTION_DENIED)) |
| 1249 | .build())), |
tomlu | d56a806 | 2018-08-01 13:20:41 -0700 | [diff] [blame] | 1250 | ActionsTestUtil.DUMMY_ARTIFACT.getPath(), |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1251 | /*stdout=*/ null, |
| 1252 | /*stderr=*/ null, |
| 1253 | /*actionMetadataLogs=*/ ImmutableList.of(), |
felly | 3b050f6 | 2020-01-23 08:57:31 -0800 | [diff] [blame] | 1254 | ErrorTiming.BEFORE_EXECUTION, |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1255 | /*isInMemoryFs=*/ false); |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 1256 | |
| 1257 | streamer.buildEvent(SUCCESSFUL_ACTION_EXECUTED_EVENT); |
| 1258 | streamer.buildEvent(failedActionExecutedEvent); |
| 1259 | |
| 1260 | List<BuildEvent> transportedEvents = transport.getEvents(); |
| 1261 | |
| 1262 | assertThat(transportedEvents).doesNotContain(SUCCESSFUL_ACTION_EXECUTED_EVENT); |
| 1263 | assertThat(transportedEvents).contains(failedActionExecutedEvent); |
| 1264 | } |
| 1265 | |
| 1266 | @Test |
| 1267 | public void testSuccessfulActionsCanBePublished() { |
| 1268 | EventBusHandler handler = new EventBusHandler(); |
| 1269 | eventBus.register(handler); |
| 1270 | |
| 1271 | BuildEventStreamOptions options = new BuildEventStreamOptions(); |
| 1272 | options.publishAllActions = true; |
| 1273 | |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 1274 | BuildEventStreamer streamer = |
lpino | 2bf8906 | 2019-02-21 03:24:49 -0800 | [diff] [blame] | 1275 | new BuildEventStreamer.Builder() |
| 1276 | .artifactGroupNamer(artifactGroupNamer) |
| 1277 | .besStreamOptions(options) |
lpino | 2bf8906 | 2019-02-21 03:24:49 -0800 | [diff] [blame] | 1278 | .buildEventTransports(ImmutableSet.of(transport)) |
| 1279 | .build(); |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 1280 | |
| 1281 | ActionExecutedEvent failedActionExecutedEvent = |
| 1282 | new ActionExecutedEvent( |
ulfjack | 5f312dd | 2018-10-02 04:58:53 -0700 | [diff] [blame] | 1283 | ActionsTestUtil.DUMMY_ARTIFACT.getExecPath(), |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 1284 | new ActionsTestUtil.NullAction(), |
mschaller | 2902018 | 2020-06-10 23:16:07 -0700 | [diff] [blame] | 1285 | new ActionExecutionException( |
| 1286 | "Exception", |
| 1287 | /* action= */ null, |
| 1288 | /* catastrophe= */ false, |
| 1289 | DetailedExitCode.of( |
| 1290 | FailureDetail.newBuilder() |
| 1291 | .setSpawn(Spawn.newBuilder().setCode(Code.EXECUTION_DENIED)) |
| 1292 | .build())), |
tomlu | d56a806 | 2018-08-01 13:20:41 -0700 | [diff] [blame] | 1293 | ActionsTestUtil.DUMMY_ARTIFACT.getPath(), |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 1294 | /* stdout= */ null, |
| 1295 | /* stderr= */ null, |
Googler | 17e28df | 2019-09-10 12:07:05 -0700 | [diff] [blame] | 1296 | /* actionMetadataLogs= */ ImmutableList.of(), |
felly | 3b050f6 | 2020-01-23 08:57:31 -0800 | [diff] [blame] | 1297 | ErrorTiming.BEFORE_EXECUTION, |
| 1298 | /* isInMemoryFs= */ false); |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 1299 | |
| 1300 | streamer.buildEvent(SUCCESSFUL_ACTION_EXECUTED_EVENT); |
| 1301 | streamer.buildEvent(failedActionExecutedEvent); |
| 1302 | |
| 1303 | List<BuildEvent> transportedEvents = transport.getEvents(); |
| 1304 | |
| 1305 | assertThat(transportedEvents).contains(SUCCESSFUL_ACTION_EXECUTED_EVENT); |
| 1306 | assertThat(transportedEvents).contains(failedActionExecutedEvent); |
| 1307 | } |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1308 | |
| 1309 | @Test |
| 1310 | public void testBuildIncomplete() { |
| 1311 | BuildEventId buildEventId = testId("abort_expected"); |
| 1312 | BuildEvent startEvent = |
| 1313 | new GenericBuildEvent( |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 1314 | BuildEventIdUtil.buildStartedId(), |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1315 | ImmutableSet.of( |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 1316 | buildEventId, |
| 1317 | ProgressEvent.INITIAL_PROGRESS_UPDATE, |
| 1318 | BuildEventIdUtil.buildFinished())); |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1319 | BuildCompleteEvent buildCompleteEvent = |
mschaller | 6bf7c51 | 2020-06-02 21:34:02 -0700 | [diff] [blame] | 1320 | buildCompleteEvent(createGenericDetailedExitCode(), true, null, false); |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1321 | |
| 1322 | streamer.buildEvent(startEvent); |
| 1323 | streamer.buildEvent(buildCompleteEvent); |
| 1324 | streamer.close(); |
| 1325 | |
| 1326 | BuildEventStreamProtos.BuildEvent aborted = getBepEvent(buildEventId); |
| 1327 | assertThat(aborted).isNotNull(); |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1328 | assertThat(aborted.hasAborted()).isTrue(); |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1329 | assertThat(aborted.getAborted().getReason()).isEqualTo(AbortReason.INCOMPLETE); |
| 1330 | assertThat(aborted.getAborted().getDescription()).isEmpty(); |
| 1331 | } |
| 1332 | |
| 1333 | @Test |
| 1334 | public void testBuildCrash() { |
| 1335 | BuildEventId buildEventId = testId("abort_expected"); |
| 1336 | BuildEvent startEvent = |
| 1337 | new GenericBuildEvent( |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 1338 | BuildEventIdUtil.buildStartedId(), |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1339 | ImmutableSet.of( |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 1340 | buildEventId, |
| 1341 | ProgressEvent.INITIAL_PROGRESS_UPDATE, |
| 1342 | BuildEventIdUtil.buildFinished())); |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1343 | BuildCompleteEvent buildCompleteEvent = |
mschaller | 6bf7c51 | 2020-06-02 21:34:02 -0700 | [diff] [blame] | 1344 | buildCompleteEvent(createGenericDetailedExitCode(), true, new RuntimeException(), false); |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1345 | |
| 1346 | streamer.buildEvent(startEvent); |
| 1347 | streamer.buildEvent(buildCompleteEvent); |
| 1348 | streamer.close(); |
| 1349 | |
| 1350 | BuildEventStreamProtos.BuildEvent aborted = getBepEvent(buildEventId); |
| 1351 | assertThat(aborted).isNotNull(); |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1352 | assertThat(aborted.hasAborted()).isTrue(); |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1353 | assertThat(aborted.getAborted().getReason()).isEqualTo(AbortReason.INTERNAL); |
| 1354 | assertThat(aborted.getAborted().getDescription()).isEmpty(); |
| 1355 | } |
| 1356 | |
| 1357 | @Test |
| 1358 | public void testBuildCatastrophe() { |
| 1359 | BuildEventId buildEventId = testId("abort_expected"); |
| 1360 | BuildEvent startEvent = |
| 1361 | new GenericBuildEvent( |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 1362 | BuildEventIdUtil.buildStartedId(), |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1363 | ImmutableSet.of( |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 1364 | buildEventId, |
| 1365 | ProgressEvent.INITIAL_PROGRESS_UPDATE, |
| 1366 | BuildEventIdUtil.buildFinished())); |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1367 | BuildCompleteEvent buildCompleteEvent = |
mschaller | 6bf7c51 | 2020-06-02 21:34:02 -0700 | [diff] [blame] | 1368 | buildCompleteEvent(createGenericDetailedExitCode(), true, null, true); |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1369 | |
| 1370 | streamer.buildEvent(startEvent); |
| 1371 | streamer.buildEvent(buildCompleteEvent); |
| 1372 | streamer.close(); |
| 1373 | |
| 1374 | BuildEventStreamProtos.BuildEvent aborted = getBepEvent(buildEventId); |
| 1375 | assertThat(aborted).isNotNull(); |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1376 | assertThat(aborted.hasAborted()).isTrue(); |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1377 | assertThat(aborted.getAborted().getReason()).isEqualTo(AbortReason.INTERNAL); |
| 1378 | assertThat(aborted.getAborted().getDescription()).isEmpty(); |
| 1379 | } |
| 1380 | |
| 1381 | @Test |
| 1382 | public void testStreamAbortedWithTimeout() { |
| 1383 | BuildEventId buildEventId = testId("abort_expected"); |
| 1384 | BuildEvent startEvent = |
| 1385 | new GenericBuildEvent( |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 1386 | BuildEventIdUtil.buildStartedId(), |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1387 | ImmutableSet.of( |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 1388 | buildEventId, |
| 1389 | ProgressEvent.INITIAL_PROGRESS_UPDATE, |
| 1390 | BuildEventIdUtil.buildFinished())); |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1391 | |
| 1392 | streamer.buildEvent(startEvent); |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1393 | streamer.closeOnAbort(AbortReason.TIME_OUT); |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1394 | |
| 1395 | BuildEventStreamProtos.BuildEvent aborted0 = getBepEvent(buildEventId); |
| 1396 | assertThat(aborted0).isNotNull(); |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1397 | assertThat(aborted0.hasAborted()).isTrue(); |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1398 | assertThat(aborted0.getAborted().getReason()).isEqualTo(AbortReason.TIME_OUT); |
| 1399 | assertThat(aborted0.getAborted().getDescription()).isEmpty(); |
| 1400 | |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 1401 | BuildEventStreamProtos.BuildEvent aborted1 = getBepEvent(BuildEventIdUtil.buildFinished()); |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1402 | assertThat(aborted1).isNotNull(); |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1403 | assertThat(aborted1.hasAborted()).isTrue(); |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1404 | assertThat(aborted1.getAborted().getReason()).isEqualTo(AbortReason.TIME_OUT); |
| 1405 | assertThat(aborted1.getAborted().getDescription()).isEmpty(); |
| 1406 | } |
| 1407 | |
| 1408 | @Test |
| 1409 | public void testBuildFailureMultipleReasons() { |
| 1410 | BuildEventId buildEventId = testId("abort_expected"); |
| 1411 | BuildEvent startEvent = |
| 1412 | new GenericBuildEvent( |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 1413 | BuildEventIdUtil.buildStartedId(), |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1414 | ImmutableSet.of( |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 1415 | buildEventId, |
| 1416 | ProgressEvent.INITIAL_PROGRESS_UPDATE, |
| 1417 | BuildEventIdUtil.buildFinished())); |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1418 | BuildCompleteEvent buildCompleteEvent = |
mschaller | 6bf7c51 | 2020-06-02 21:34:02 -0700 | [diff] [blame] | 1419 | buildCompleteEvent(createGenericDetailedExitCode(), false, new RuntimeException(), false); |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1420 | |
| 1421 | streamer.buildEvent(startEvent); |
| 1422 | streamer.noAnalyze(new NoAnalyzeEvent()); |
| 1423 | streamer.buildEvent(buildCompleteEvent); |
| 1424 | streamer.close(); |
| 1425 | |
| 1426 | BuildEventStreamProtos.BuildEvent aborted = getBepEvent(buildEventId); |
| 1427 | assertThat(aborted).isNotNull(); |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1428 | assertThat(aborted.hasAborted()).isTrue(); |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1429 | assertThat(aborted.getAborted().getReason()).isEqualTo(AbortReason.INTERNAL); |
| 1430 | assertThat(aborted.getAborted().getDescription()) |
| 1431 | .isEqualTo("Multiple abort reasons reported: [NO_ANALYZE, INTERNAL]"); |
| 1432 | } |
| 1433 | |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1434 | @Test |
| 1435 | public void nonOomAbortReason_doesNotIncludeOomMessage() { |
| 1436 | BuildEventId buildEventId = testId("abort_expected"); |
| 1437 | BuildEvent startEvent = |
| 1438 | new GenericBuildEvent( |
| 1439 | BuildEventIdUtil.buildStartedId(), |
| 1440 | ImmutableSet.of( |
| 1441 | buildEventId, |
| 1442 | ProgressEvent.INITIAL_PROGRESS_UPDATE, |
| 1443 | BuildEventIdUtil.buildFinished())); |
| 1444 | |
| 1445 | streamer.buildEvent(startEvent); |
| 1446 | streamer.closeOnAbort(AbortReason.INTERNAL); |
| 1447 | |
| 1448 | assertThat(getBepEvent(buildEventId).getAborted()) |
| 1449 | .isEqualTo(Aborted.newBuilder().setReason(AbortReason.INTERNAL).build()); |
| 1450 | } |
| 1451 | |
| 1452 | @Test |
| 1453 | public void oomAbortReason_includesOomMessage() { |
| 1454 | BuildEventId buildEventId = testId("abort_expected"); |
| 1455 | BuildEvent startEvent = |
| 1456 | new GenericBuildEvent( |
| 1457 | BuildEventIdUtil.buildStartedId(), |
| 1458 | ImmutableSet.of( |
| 1459 | buildEventId, |
| 1460 | ProgressEvent.INITIAL_PROGRESS_UPDATE, |
| 1461 | BuildEventIdUtil.buildFinished())); |
| 1462 | |
| 1463 | streamer.buildEvent(startEvent); |
| 1464 | streamer.closeOnAbort(AbortReason.OUT_OF_MEMORY); |
| 1465 | |
| 1466 | assertThat(getBepEvent(buildEventId).getAborted()) |
| 1467 | .isEqualTo( |
| 1468 | Aborted.newBuilder() |
| 1469 | .setReason(AbortReason.OUT_OF_MEMORY) |
| 1470 | .setDescription(BugReport.constructOomExitMessage(OOM_MESSAGE)) |
| 1471 | .build()); |
| 1472 | } |
| 1473 | |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1474 | @Nullable |
| 1475 | private BuildEventStreamProtos.BuildEvent getBepEvent(BuildEventId buildEventId) { |
| 1476 | return transport.getEventProtos().stream() |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame] | 1477 | .filter(e -> e.getId().equals(buildEventId)) |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1478 | .findFirst() |
| 1479 | .orElse(null); |
| 1480 | } |
| 1481 | |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1482 | private static BuildCompleteEvent buildCompleteEvent( |
mschaller | 6bf7c51 | 2020-06-02 21:34:02 -0700 | [diff] [blame] | 1483 | DetailedExitCode detailedExitCode, |
| 1484 | boolean stopOnFailure, |
| 1485 | Throwable crash, |
| 1486 | boolean catastrophe) { |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1487 | BuildResult result = new BuildResult(0); |
mschaller | 6bf7c51 | 2020-06-02 21:34:02 -0700 | [diff] [blame] | 1488 | result.setDetailedExitCode(detailedExitCode); |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame] | 1489 | result.setStopOnFirstFailure(stopOnFailure); |
| 1490 | if (catastrophe) { |
| 1491 | result.setCatastrophe(); |
| 1492 | } |
| 1493 | if (crash != null) { |
| 1494 | result.setUnhandledThrowable(crash); |
| 1495 | } |
| 1496 | return new BuildCompleteEvent(result); |
| 1497 | } |
Googler | 17e28df | 2019-09-10 12:07:05 -0700 | [diff] [blame] | 1498 | |
| 1499 | private static ActionExecutedEvent createActionExecutedEventWithLogs( |
| 1500 | ImmutableList<MetadataLog> metadataLogs) { |
| 1501 | return new ActionExecutedEvent( |
| 1502 | ActionsTestUtil.DUMMY_ARTIFACT.getExecPath(), |
| 1503 | new ActionsTestUtil.NullAction(), |
| 1504 | /* exception= */ null, |
| 1505 | ActionsTestUtil.DUMMY_ARTIFACT.getPath(), |
| 1506 | /* stdout= */ null, |
| 1507 | /* stderr= */ null, |
| 1508 | metadataLogs, |
felly | 3b050f6 | 2020-01-23 08:57:31 -0800 | [diff] [blame] | 1509 | ErrorTiming.NO_ERROR, |
| 1510 | /* isInMemoryFs= */ false); |
Googler | 17e28df | 2019-09-10 12:07:05 -0700 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | @Test |
| 1514 | public void testActionExecutedEventLogsConstructor() { |
| 1515 | String metadataLogName = "action_metadata"; |
| 1516 | Path testPath1 = FileSystems.getJavaIoFileSystem().getPath("/path/to/logs-1"); |
| 1517 | Path testPath2 = FileSystems.getJavaIoFileSystem().getPath("/path/to/logs-2"); |
| 1518 | MetadataLog testMetadataLog1 = new MetadataLog(metadataLogName, testPath1); |
| 1519 | MetadataLog testMetadataLog2 = new MetadataLog(metadataLogName, testPath2); |
| 1520 | |
| 1521 | ActionExecutedEvent withLogsEvent = |
| 1522 | createActionExecutedEventWithLogs(ImmutableList.of(testMetadataLog1, testMetadataLog2)); |
Googler | 17e28df | 2019-09-10 12:07:05 -0700 | [diff] [blame] | 1523 | |
| 1524 | assertWithMessage("List parameter should return list of log path values") |
| 1525 | .that(withLogsEvent.getActionMetadataLogs()) |
| 1526 | .containsExactly(testMetadataLog1, testMetadataLog2); |
| 1527 | assertWithMessage("Null logs parameter should return empty list.") |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1528 | .that(SUCCESSFUL_ACTION_EXECUTED_EVENT.getActionMetadataLogs()) |
Googler | 17e28df | 2019-09-10 12:07:05 -0700 | [diff] [blame] | 1529 | .isEmpty(); |
| 1530 | } |
| 1531 | |
| 1532 | @Test |
michajlo | 99725e1 | 2020-11-18 14:30:05 -0800 | [diff] [blame] | 1533 | public void testActionExcutedEventProtoLogs() throws Exception { |
Googler | 17e28df | 2019-09-10 12:07:05 -0700 | [diff] [blame] | 1534 | String metadataLogName = "action_metadata"; |
| 1535 | Path testPath1 = FileSystems.getJavaIoFileSystem().getPath("/path/to/logs-1"); |
| 1536 | Path testPath2 = FileSystems.getJavaIoFileSystem().getPath("/path/to/logs-2"); |
| 1537 | |
| 1538 | ActionExecutedEvent withLogsEvent = |
| 1539 | createActionExecutedEventWithLogs( |
| 1540 | ImmutableList.of( |
| 1541 | new MetadataLog(metadataLogName, testPath1), |
| 1542 | new MetadataLog(metadataLogName, testPath2))); |
Googler | 17e28df | 2019-09-10 12:07:05 -0700 | [diff] [blame] | 1543 | |
| 1544 | BuildEventStreamProtos.BuildEvent buildEventLogs = |
| 1545 | withLogsEvent.asStreamProto(getTestBuildEventContext(artifactGroupNamer)); |
| 1546 | BuildEventStreamProtos.BuildEvent buildEventNoLogs = |
jhorvitz | d72c3b229 | 2020-10-20 10:29:38 -0700 | [diff] [blame] | 1547 | SUCCESSFUL_ACTION_EXECUTED_EVENT.asStreamProto( |
| 1548 | getTestBuildEventContext(artifactGroupNamer)); |
Googler | 17e28df | 2019-09-10 12:07:05 -0700 | [diff] [blame] | 1549 | |
| 1550 | assertWithMessage("With logs build event action should contain 2 log files") |
| 1551 | .that(buildEventLogs.getAction().getActionMetadataLogsCount()) |
| 1552 | .isEqualTo(2); |
| 1553 | assertWithMessage("No logs build event action should contain 0 log files") |
| 1554 | .that(buildEventNoLogs.getAction().getActionMetadataLogsCount()) |
| 1555 | .isEqualTo(0); |
| 1556 | assertWithMessage("Event action should contains the two paths") |
| 1557 | .that( |
| 1558 | buildEventLogs.getAction().getActionMetadataLogsList().stream() |
| 1559 | .map(File::getUri) |
| 1560 | .collect(ImmutableList.toImmutableList())) |
| 1561 | .containsExactly(testPath1.toString(), testPath2.toString()); |
| 1562 | } |
mschaller | 6bf7c51 | 2020-06-02 21:34:02 -0700 | [diff] [blame] | 1563 | |
| 1564 | private static DetailedExitCode createGenericDetailedExitCode() { |
| 1565 | return DetailedExitCode.of( |
| 1566 | FailureDetail.newBuilder() |
| 1567 | .setSpawn(Spawn.newBuilder().setCode(Code.NON_ZERO_EXIT)) |
| 1568 | .build()); |
| 1569 | } |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 1570 | } |