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; |
Klaus Aehlig | 9b3b2e9 | 2017-05-02 11:18:42 +0200 | [diff] [blame] | 18 | import static org.mockito.Mockito.times; |
| 19 | import static org.mockito.Mockito.verify; |
| 20 | import static org.mockito.Mockito.when; |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 21 | |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableList; |
| 23 | import com.google.common.collect.ImmutableMap; |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 24 | import com.google.common.collect.ImmutableSet; |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 25 | import com.google.common.eventbus.Subscribe; |
buchgr | 0b93743 | 2017-04-06 18:54:22 +0000 | [diff] [blame] | 26 | import com.google.common.util.concurrent.Futures; |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 27 | import com.google.common.util.concurrent.ListenableFuture; |
lberki | 8626623 | 2018-04-11 00:33:42 -0700 | [diff] [blame] | 28 | import com.google.devtools.build.lib.actions.ActionEnvironment; |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 29 | import com.google.devtools.build.lib.actions.ActionExecutedEvent; |
| 30 | import com.google.devtools.build.lib.actions.ActionExecutedEvent.ErrorTiming; |
| 31 | import com.google.devtools.build.lib.actions.ActionExecutionException; |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 32 | import com.google.devtools.build.lib.actions.Artifact; |
tomlu | 04e9281 | 2018-08-02 11:49:08 -0700 | [diff] [blame] | 33 | import com.google.devtools.build.lib.actions.ArtifactPathResolver; |
tomlu | 1cdcdf9 | 2018-01-16 11:07:51 -0800 | [diff] [blame] | 34 | import com.google.devtools.build.lib.actions.ArtifactRoot; |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 35 | import com.google.devtools.build.lib.actions.EventReportingArtifacts; |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 36 | import com.google.devtools.build.lib.actions.util.ActionsTestUtil; |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 37 | import com.google.devtools.build.lib.analysis.BlazeDirectories; |
janakr | 3b63a4e | 2017-09-14 09:55:40 +0200 | [diff] [blame] | 38 | import com.google.devtools.build.lib.analysis.ServerDirectories; |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 39 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 40 | import com.google.devtools.build.lib.analysis.config.BuildOptions; |
gregce | e495e6b | 2019-04-30 14:07:06 -0700 | [diff] [blame] | 41 | import com.google.devtools.build.lib.analysis.config.CoreOptions; |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 42 | import com.google.devtools.build.lib.analysis.config.FragmentOptions; |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 43 | import com.google.devtools.build.lib.buildeventstream.AnnounceBuildEventTransportsEvent; |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 44 | import com.google.devtools.build.lib.buildeventstream.ArtifactGroupNamer; |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 45 | import com.google.devtools.build.lib.buildeventstream.BuildEvent; |
felly | 75a422a | 2019-01-17 14:52:49 -0800 | [diff] [blame] | 46 | import com.google.devtools.build.lib.buildeventstream.BuildEventArtifactUploader; |
ulfjack | 26e586d | 2018-05-17 08:42:13 -0700 | [diff] [blame] | 47 | import com.google.devtools.build.lib.buildeventstream.BuildEventContext; |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 48 | import com.google.devtools.build.lib.buildeventstream.BuildEventId; |
ulfjack | fbf2756 | 2018-05-18 12:46:54 -0700 | [diff] [blame] | 49 | import com.google.devtools.build.lib.buildeventstream.BuildEventProtocolOptions; |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 50 | import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos; |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame^] | 51 | import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.Aborted.AbortReason; |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 52 | import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.BuildEventId.NamedSetOfFilesId; |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 53 | import com.google.devtools.build.lib.buildeventstream.BuildEventTransport; |
lberki | d263009 | 2018-11-22 09:12:34 -0800 | [diff] [blame] | 54 | import com.google.devtools.build.lib.buildeventstream.BuildEventTransportClosedEvent; |
Klaus Aehlig | a708a02 | 2017-07-11 12:54:40 +0200 | [diff] [blame] | 55 | import com.google.devtools.build.lib.buildeventstream.BuildEventWithConfiguration; |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 56 | import com.google.devtools.build.lib.buildeventstream.BuildEventWithOrderConstraint; |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 57 | import com.google.devtools.build.lib.buildeventstream.GenericBuildEvent; |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 58 | import com.google.devtools.build.lib.buildeventstream.PathConverter; |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 59 | import com.google.devtools.build.lib.buildeventstream.ProgressEvent; |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 60 | import com.google.devtools.build.lib.buildeventstream.transports.BuildEventStreamOptions; |
Jakob Buchgraber | 8c3a4ef | 2017-02-28 18:49:50 +0000 | [diff] [blame] | 61 | import com.google.devtools.build.lib.buildtool.BuildResult; |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 62 | import com.google.devtools.build.lib.buildtool.buildevent.BuildCompleteEvent; |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame^] | 63 | import com.google.devtools.build.lib.buildtool.buildevent.NoAnalyzeEvent; |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 64 | import com.google.devtools.build.lib.collect.nestedset.NestedSet; |
| 65 | import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; |
| 66 | import com.google.devtools.build.lib.collect.nestedset.NestedSetView; |
| 67 | import com.google.devtools.build.lib.testutil.FoundationTestCase; |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame^] | 68 | import com.google.devtools.build.lib.util.ExitCode; |
felly | 1759476 | 2019-02-07 15:24:32 -0800 | [diff] [blame] | 69 | import com.google.devtools.build.lib.util.Pair; |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 70 | import com.google.devtools.build.lib.vfs.Path; |
| 71 | import com.google.devtools.build.lib.vfs.PathFragment; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 72 | import com.google.devtools.build.lib.vfs.Root; |
ulfjack | fbf2756 | 2018-05-18 12:46:54 -0700 | [diff] [blame] | 73 | import com.google.devtools.common.options.Options; |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 74 | import java.util.ArrayList; |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 75 | import java.util.Collection; |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 76 | import java.util.Collections; |
| 77 | import java.util.HashSet; |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 78 | import java.util.List; |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 79 | import java.util.Set; |
felly | a4488a2 | 2019-04-03 13:05:47 -0700 | [diff] [blame] | 80 | import java.util.concurrent.LinkedBlockingQueue; |
| 81 | import java.util.concurrent.ThreadPoolExecutor; |
lberki | d263009 | 2018-11-22 09:12:34 -0800 | [diff] [blame] | 82 | import java.util.concurrent.TimeUnit; |
felly | a4488a2 | 2019-04-03 13:05:47 -0700 | [diff] [blame] | 83 | import java.util.concurrent.atomic.AtomicInteger; |
lberki | d263009 | 2018-11-22 09:12:34 -0800 | [diff] [blame] | 84 | import java.util.concurrent.locks.LockSupport; |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame^] | 85 | import javax.annotation.Nullable; |
felly | a4488a2 | 2019-04-03 13:05:47 -0700 | [diff] [blame] | 86 | import org.apache.commons.lang.time.StopWatch; |
lpino | 0361ff6 | 2019-02-12 22:52:59 -0800 | [diff] [blame] | 87 | import org.junit.After; |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 88 | import org.junit.Before; |
felly | a4488a2 | 2019-04-03 13:05:47 -0700 | [diff] [blame] | 89 | import org.junit.Ignore; |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 90 | import org.junit.Test; |
| 91 | import org.junit.runner.RunWith; |
| 92 | import org.junit.runners.JUnit4; |
Klaus Aehlig | 9b3b2e9 | 2017-05-02 11:18:42 +0200 | [diff] [blame] | 93 | import org.mockito.Mockito; |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 94 | import org.mockito.MockitoAnnotations; |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 95 | |
| 96 | /** Tests {@link BuildEventStreamer}. */ |
| 97 | @RunWith(JUnit4.class) |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 98 | public class BuildEventStreamerTest extends FoundationTestCase { |
lpino | 0361ff6 | 2019-02-12 22:52:59 -0800 | [diff] [blame] | 99 | private CountingArtifactGroupNamer artifactGroupNamer; |
| 100 | private RecordingBuildEventTransport transport; |
| 101 | private BuildEventStreamer streamer; |
| 102 | |
| 103 | @Before |
| 104 | public void setUp() { |
| 105 | artifactGroupNamer = new CountingArtifactGroupNamer(); |
felly | a4488a2 | 2019-04-03 13:05:47 -0700 | [diff] [blame] | 106 | transport = new RecordingBuildEventTransport(artifactGroupNamer, true); |
lpino | 0361ff6 | 2019-02-12 22:52:59 -0800 | [diff] [blame] | 107 | streamer = |
lpino | 5bec36c | 2019-03-21 09:50:52 -0700 | [diff] [blame] | 108 | new BuildEventStreamer(ImmutableSet.<BuildEventTransport>of(transport), artifactGroupNamer); |
lpino | 0361ff6 | 2019-02-12 22:52:59 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @After |
| 112 | public void tearDown() { |
| 113 | artifactGroupNamer = null; |
| 114 | transport = null; |
| 115 | streamer = null; |
| 116 | } |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 117 | |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 118 | private static final ActionExecutedEvent SUCCESSFUL_ACTION_EXECUTED_EVENT = |
| 119 | new ActionExecutedEvent( |
ulfjack | 5f312dd | 2018-10-02 04:58:53 -0700 | [diff] [blame] | 120 | ActionsTestUtil.DUMMY_ARTIFACT.getExecPath(), |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 121 | new ActionsTestUtil.NullAction(), |
| 122 | /* exception= */ null, |
tomlu | d56a806 | 2018-08-01 13:20:41 -0700 | [diff] [blame] | 123 | ActionsTestUtil.DUMMY_ARTIFACT.getPath(), |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 124 | /* stdout= */ null, |
| 125 | /* stderr= */ null, |
| 126 | ErrorTiming.NO_ERROR); |
| 127 | |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 128 | private static class RecordingBuildEventTransport implements BuildEventTransport { |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 129 | private final List<BuildEvent> events = new ArrayList<>(); |
| 130 | private final List<BuildEventStreamProtos.BuildEvent> eventsAsProtos = new ArrayList<>(); |
lpino | 0361ff6 | 2019-02-12 22:52:59 -0800 | [diff] [blame] | 131 | private ArtifactGroupNamer artifactGroupNamer; |
| 132 | |
felly | a4488a2 | 2019-04-03 13:05:47 -0700 | [diff] [blame] | 133 | RecordingBuildEventTransport(ArtifactGroupNamer namer, boolean recordEvents) { |
lpino | 0361ff6 | 2019-02-12 22:52:59 -0800 | [diff] [blame] | 134 | this.artifactGroupNamer = namer; |
| 135 | } |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 136 | |
| 137 | @Override |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 138 | public String name() { |
| 139 | return this.getClass().getSimpleName(); |
| 140 | } |
| 141 | |
| 142 | @Override |
felly | a4488a2 | 2019-04-03 13:05:47 -0700 | [diff] [blame] | 143 | public synchronized void sendBuildEvent(BuildEvent event) { |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 144 | events.add(event); |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 145 | eventsAsProtos.add( |
| 146 | event.asStreamProto( |
ulfjack | 26e586d | 2018-05-17 08:42:13 -0700 | [diff] [blame] | 147 | new BuildEventContext() { |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 148 | @Override |
| 149 | public ArtifactGroupNamer artifactGroupNamer() { |
lpino | 0361ff6 | 2019-02-12 22:52:59 -0800 | [diff] [blame] | 150 | return artifactGroupNamer; |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public PathConverter pathConverter() { |
| 155 | return new PathConverter() { |
| 156 | @Override |
| 157 | public String apply(Path path) { |
| 158 | return path.toString(); |
| 159 | } |
| 160 | }; |
| 161 | } |
ulfjack | fbf2756 | 2018-05-18 12:46:54 -0700 | [diff] [blame] | 162 | |
| 163 | @Override |
| 164 | public BuildEventProtocolOptions getOptions() { |
| 165 | return Options.getDefaults(BuildEventProtocolOptions.class); |
| 166 | } |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 167 | })); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | @Override |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 171 | public ListenableFuture<Void> close() { |
buchgr | 0b93743 | 2017-04-06 18:54:22 +0000 | [diff] [blame] | 172 | return Futures.immediateFuture(null); |
| 173 | } |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 174 | |
felly | 75a422a | 2019-01-17 14:52:49 -0800 | [diff] [blame] | 175 | @Override |
| 176 | public BuildEventArtifactUploader getUploader() { |
| 177 | throw new IllegalStateException(); |
| 178 | } |
| 179 | |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 180 | List<BuildEvent> getEvents() { |
| 181 | return events; |
| 182 | } |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 183 | |
| 184 | List<BuildEventStreamProtos.BuildEvent> getEventProtos() { |
| 185 | return eventsAsProtos; |
| 186 | } |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 189 | private static class GenericOrderEvent implements BuildEventWithOrderConstraint { |
| 190 | private final BuildEventId id; |
| 191 | private final Collection<BuildEventId> children; |
| 192 | private final Collection<BuildEventId> after; |
| 193 | |
| 194 | GenericOrderEvent( |
| 195 | BuildEventId id, Collection<BuildEventId> children, Collection<BuildEventId> after) { |
| 196 | this.id = id; |
| 197 | this.children = children; |
| 198 | this.after = after; |
| 199 | } |
| 200 | |
| 201 | GenericOrderEvent(BuildEventId id, Collection<BuildEventId> children) { |
| 202 | this(id, children, children); |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public BuildEventId getEventId() { |
| 207 | return id; |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public Collection<BuildEventId> getChildrenEvents() { |
| 212 | return children; |
| 213 | } |
| 214 | |
| 215 | @Override |
ulfjack | 26e586d | 2018-05-17 08:42:13 -0700 | [diff] [blame] | 216 | public BuildEventStreamProtos.BuildEvent asStreamProto(BuildEventContext converters) { |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 217 | return GenericBuildEvent.protoChaining(this).build(); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public Collection<BuildEventId> postedAfter() { |
| 222 | return after; |
| 223 | } |
| 224 | } |
| 225 | |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 226 | private static class GenericArtifactReportingEvent implements EventReportingArtifacts { |
| 227 | private final BuildEventId id; |
| 228 | private final Collection<BuildEventId> children; |
| 229 | private final Collection<NestedSet<Artifact>> artifacts; |
| 230 | |
| 231 | GenericArtifactReportingEvent( |
| 232 | BuildEventId id, |
| 233 | Collection<BuildEventId> children, |
| 234 | Collection<NestedSet<Artifact>> artifacts) { |
| 235 | this.id = id; |
| 236 | this.children = children; |
| 237 | this.artifacts = artifacts; |
| 238 | } |
| 239 | |
| 240 | GenericArtifactReportingEvent(BuildEventId id, Collection<NestedSet<Artifact>> artifacts) { |
| 241 | this(id, ImmutableSet.<BuildEventId>of(), artifacts); |
| 242 | } |
| 243 | |
| 244 | @Override |
| 245 | public BuildEventId getEventId() { |
| 246 | return id; |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public Collection<BuildEventId> getChildrenEvents() { |
| 251 | return children; |
| 252 | } |
| 253 | |
| 254 | @Override |
tomlu | 04e9281 | 2018-08-02 11:49:08 -0700 | [diff] [blame] | 255 | public ReportedArtifacts reportedArtifacts() { |
| 256 | return new ReportedArtifacts(artifacts, ArtifactPathResolver.IDENTITY); |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | @Override |
ulfjack | 26e586d | 2018-05-17 08:42:13 -0700 | [diff] [blame] | 260 | public BuildEventStreamProtos.BuildEvent asStreamProto(BuildEventContext converters) { |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 261 | BuildEventStreamProtos.NamedSetOfFiles.Builder builder = |
| 262 | BuildEventStreamProtos.NamedSetOfFiles.newBuilder(); |
| 263 | for (NestedSet<Artifact> artifactset : artifacts) { |
| 264 | builder.addFileSets( |
| 265 | converters |
| 266 | .artifactGroupNamer() |
| 267 | .apply((new NestedSetView<Artifact>(artifactset)).identifier())); |
| 268 | } |
| 269 | return GenericBuildEvent.protoChaining(this).setNamedSetOfFiles(builder.build()).build(); |
| 270 | } |
| 271 | } |
| 272 | |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 273 | private static class GenericConfigurationEvent implements BuildEventWithConfiguration { |
| 274 | private final BuildEventId id; |
| 275 | private final Collection<BuildEventId> children; |
Klaus Aehlig | a708a02 | 2017-07-11 12:54:40 +0200 | [diff] [blame] | 276 | private final Collection<BuildEvent> configurations; |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 277 | |
| 278 | GenericConfigurationEvent( |
Klaus Aehlig | a708a02 | 2017-07-11 12:54:40 +0200 | [diff] [blame] | 279 | BuildEventId id, Collection<BuildEventId> children, Collection<BuildEvent> configurations) { |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 280 | this.id = id; |
| 281 | this.children = children; |
| 282 | this.configurations = configurations; |
| 283 | } |
| 284 | |
Klaus Aehlig | a708a02 | 2017-07-11 12:54:40 +0200 | [diff] [blame] | 285 | GenericConfigurationEvent(BuildEventId id, BuildEvent configuration) { |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 286 | this(id, ImmutableSet.<BuildEventId>of(), ImmutableSet.of(configuration)); |
| 287 | } |
| 288 | |
| 289 | @Override |
| 290 | public BuildEventId getEventId() { |
| 291 | return id; |
| 292 | } |
| 293 | |
| 294 | @Override |
| 295 | public Collection<BuildEventId> getChildrenEvents() { |
| 296 | return children; |
| 297 | } |
| 298 | |
| 299 | @Override |
Klaus Aehlig | a708a02 | 2017-07-11 12:54:40 +0200 | [diff] [blame] | 300 | public Collection<BuildEvent> getConfigurations() { |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 301 | return configurations; |
| 302 | } |
| 303 | |
| 304 | @Override |
ulfjack | 26e586d | 2018-05-17 08:42:13 -0700 | [diff] [blame] | 305 | public BuildEventStreamProtos.BuildEvent asStreamProto(BuildEventContext converters) { |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 306 | return GenericBuildEvent.protoChaining(this).build(); |
| 307 | } |
| 308 | } |
| 309 | |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 310 | private static BuildEventId testId(String opaque) { |
| 311 | return BuildEventId.unknownBuildEventId(opaque); |
| 312 | } |
| 313 | |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 314 | private static class EventBusHandler { |
| 315 | |
| 316 | Set<BuildEventTransport> transportSet; |
| 317 | |
| 318 | @Subscribe |
| 319 | void transportsAnnounced(AnnounceBuildEventTransportsEvent evt) { |
| 320 | transportSet = Collections.synchronizedSet(new HashSet<>(evt.transports())); |
| 321 | } |
lberki | d263009 | 2018-11-22 09:12:34 -0800 | [diff] [blame] | 322 | |
| 323 | @Subscribe |
| 324 | void transportClosed(BuildEventTransportClosedEvent evt) { |
| 325 | transportSet.remove(evt.transport()); |
| 326 | } |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | @Before |
| 330 | public void setup() { |
| 331 | MockitoAnnotations.initMocks(this); |
| 332 | } |
| 333 | |
| 334 | @Test(timeout = 5000) |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 335 | public void testSimpleStream() { |
| 336 | // 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] | 337 | // build clears the pending progress-update event. However, there is no guarantee |
| 338 | // on the order of the flushed events. |
| 339 | // 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] | 340 | |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 341 | EventBusHandler handler = new EventBusHandler(); |
| 342 | eventBus.register(handler); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 343 | assertThat(handler.transportSet).isNull(); |
buchgr | 9e0308e | 2017-04-25 16:38:45 +0200 | [diff] [blame] | 344 | |
lpino | 5bec36c | 2019-03-21 09:50:52 -0700 | [diff] [blame] | 345 | eventBus.post(new AnnounceBuildEventTransportsEvent(ImmutableSet.of(transport))); |
| 346 | |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 347 | BuildEvent startEvent = |
| 348 | new GenericBuildEvent( |
Jakob Buchgraber | 8c3a4ef | 2017-02-28 18:49:50 +0000 | [diff] [blame] | 349 | testId("Initial"), ImmutableSet.of(ProgressEvent.INITIAL_PROGRESS_UPDATE, |
| 350 | BuildEventId.buildFinished())); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 351 | |
| 352 | streamer.buildEvent(startEvent); |
| 353 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 354 | assertThat(streamer.isClosed()).isFalse(); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 355 | List<BuildEvent> afterFirstEvent = transport.getEvents(); |
| 356 | assertThat(afterFirstEvent).hasSize(1); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 357 | assertThat(afterFirstEvent.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
| 358 | assertThat(handler.transportSet).hasSize(1); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 359 | |
Jakob Buchgraber | 8c3a4ef | 2017-02-28 18:49:50 +0000 | [diff] [blame] | 360 | streamer.buildEvent(new BuildCompleteEvent(new BuildResult(0))); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 361 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 362 | assertThat(streamer.isClosed()).isTrue(); |
lpino | 5bec36c | 2019-03-21 09:50:52 -0700 | [diff] [blame] | 363 | eventBus.post(new BuildEventTransportClosedEvent(transport)); |
| 364 | |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 365 | List<BuildEvent> finalStream = transport.getEvents(); |
Jakob Buchgraber | 8c3a4ef | 2017-02-28 18:49:50 +0000 | [diff] [blame] | 366 | assertThat(finalStream).hasSize(3); |
Klaus Aehlig | fb3c572 | 2017-07-10 13:08:19 +0200 | [diff] [blame] | 367 | assertThat(ImmutableSet.of(finalStream.get(1).getEventId(), finalStream.get(2).getEventId())) |
| 368 | .isEqualTo( |
| 369 | ImmutableSet.of(BuildEventId.buildFinished(), ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
| 370 | |
| 371 | // verify the "last_message" flag. |
| 372 | assertThat(transport.getEventProtos().get(0).getLastMessage()).isFalse(); |
| 373 | assertThat(transport.getEventProtos().get(1).getLastMessage()).isFalse(); |
| 374 | assertThat(transport.getEventProtos().get(2).getLastMessage()).isTrue(); |
lberki | d263009 | 2018-11-22 09:12:34 -0800 | [diff] [blame] | 375 | |
| 376 | while (!handler.transportSet.isEmpty()) { |
| 377 | LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(100)); |
| 378 | } |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | @Test |
| 382 | public void testChaining() { |
| 383 | // Verify that unannounced events are linked in with progress update events, assuming |
| 384 | // a correctly formed initial event. |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 385 | BuildEvent startEvent = |
| 386 | new GenericBuildEvent( |
| 387 | testId("Initial"), ImmutableSet.of(ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
Klaus Aehlig | 2b1eec3 | 2016-10-05 09:21:45 +0000 | [diff] [blame] | 388 | BuildEvent unexpectedEvent = |
| 389 | new GenericBuildEvent(testId("unexpected"), ImmutableSet.<BuildEventId>of()); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 390 | |
| 391 | streamer.buildEvent(startEvent); |
| 392 | streamer.buildEvent(unexpectedEvent); |
| 393 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 394 | assertThat(streamer.isClosed()).isFalse(); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 395 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 396 | assertThat(eventsSeen).hasSize(3); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 397 | assertThat(eventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
| 398 | assertThat(eventsSeen.get(2).getEventId()).isEqualTo(unexpectedEvent.getEventId()); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 399 | BuildEvent linkEvent = eventsSeen.get(1); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 400 | assertThat(linkEvent.getEventId()).isEqualTo(ProgressEvent.INITIAL_PROGRESS_UPDATE); |
| 401 | assertWithMessage("Unexpected events should be linked") |
| 402 | .that(linkEvent.getChildrenEvents().contains(unexpectedEvent.getEventId())) |
| 403 | .isTrue(); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | @Test |
| 407 | public void testBadInitialEvent() { |
| 408 | // Verify that, if the initial event does not announce the initial progress update event, |
| 409 | // the initial progress event is used instead to chain that event; in this way, new |
| 410 | // progress updates can always be chained in. |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 411 | BuildEvent unexpectedStartEvent = |
Klaus Aehlig | 2b1eec3 | 2016-10-05 09:21:45 +0000 | [diff] [blame] | 412 | new GenericBuildEvent(testId("unexpected start"), ImmutableSet.<BuildEventId>of()); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 413 | |
| 414 | streamer.buildEvent(unexpectedStartEvent); |
| 415 | |
| 416 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 417 | assertThat(eventsSeen).hasSize(2); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 418 | assertThat(eventsSeen.get(1).getEventId()).isEqualTo(unexpectedStartEvent.getEventId()); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 419 | BuildEvent initial = eventsSeen.get(0); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 420 | assertThat(initial.getEventId()).isEqualTo(ProgressEvent.INITIAL_PROGRESS_UPDATE); |
| 421 | assertWithMessage("Event should be linked") |
| 422 | .that(initial.getChildrenEvents().contains(unexpectedStartEvent.getEventId())) |
| 423 | .isTrue(); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 424 | |
| 425 | // The initial event should also announce a new progress event; we test this |
| 426 | // by streaming another unannounced event. |
| 427 | |
Klaus Aehlig | 2b1eec3 | 2016-10-05 09:21:45 +0000 | [diff] [blame] | 428 | BuildEvent unexpectedEvent = |
| 429 | new GenericBuildEvent(testId("unexpected"), ImmutableSet.<BuildEventId>of()); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 430 | |
| 431 | streamer.buildEvent(unexpectedEvent); |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 432 | |
| 433 | assertThat(streamer.isClosed()).isFalse(); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 434 | List<BuildEvent> allEventsSeen = transport.getEvents(); |
| 435 | assertThat(allEventsSeen).hasSize(4); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 436 | assertThat(allEventsSeen.get(3).getEventId()).isEqualTo(unexpectedEvent.getEventId()); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 437 | BuildEvent secondLinkEvent = allEventsSeen.get(2); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 438 | assertWithMessage("Progress should have been announced") |
| 439 | .that(initial.getChildrenEvents().contains(secondLinkEvent.getEventId())) |
| 440 | .isTrue(); |
| 441 | assertWithMessage("Second event should be linked") |
| 442 | .that(secondLinkEvent.getChildrenEvents().contains(unexpectedEvent.getEventId())) |
| 443 | .isTrue(); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | @Test |
| 447 | public void testReferPastEvent() { |
| 448 | // Verify that, if an event is refers to a previously done event, that duplicated |
| 449 | // late-referenced event is not expected again. |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 450 | BuildEvent startEvent = |
| 451 | new GenericBuildEvent( |
Klaus Aehlig | 2b1eec3 | 2016-10-05 09:21:45 +0000 | [diff] [blame] | 452 | testId("Initial"), |
Jakob Buchgraber | 8c3a4ef | 2017-02-28 18:49:50 +0000 | [diff] [blame] | 453 | ImmutableSet.<BuildEventId>of(ProgressEvent.INITIAL_PROGRESS_UPDATE, |
| 454 | BuildEventId.buildFinished())); |
Klaus Aehlig | 2b1eec3 | 2016-10-05 09:21:45 +0000 | [diff] [blame] | 455 | BuildEvent earlyEvent = |
| 456 | new GenericBuildEvent(testId("unexpected"), ImmutableSet.<BuildEventId>of()); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 457 | BuildEvent lateReference = |
| 458 | new GenericBuildEvent(testId("late reference"), ImmutableSet.of(earlyEvent.getEventId())); |
| 459 | |
| 460 | streamer.buildEvent(startEvent); |
| 461 | streamer.buildEvent(earlyEvent); |
| 462 | streamer.buildEvent(lateReference); |
Jakob Buchgraber | 8c3a4ef | 2017-02-28 18:49:50 +0000 | [diff] [blame] | 463 | streamer.buildEvent(new BuildCompleteEvent(new BuildResult(0))); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 464 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 465 | assertThat(streamer.isClosed()).isTrue(); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 466 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 467 | int earlyEventCount = 0; |
| 468 | for (BuildEvent event : eventsSeen) { |
| 469 | if (event.getEventId().equals(earlyEvent.getEventId())) { |
| 470 | earlyEventCount++; |
| 471 | } |
| 472 | } |
| 473 | // The early event should be reported precisely once. |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 474 | assertThat(earlyEventCount).isEqualTo(1); |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 475 | } |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 476 | |
| 477 | @Test |
felly | 1759476 | 2019-02-07 15:24:32 -0800 | [diff] [blame] | 478 | public void testReordering() { |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 479 | // 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] | 480 | BuildEventId expectedId = testId("the target"); |
| 481 | BuildEvent startEvent = |
| 482 | new GenericBuildEvent( |
| 483 | testId("Initial"), |
| 484 | ImmutableSet.<BuildEventId>of(ProgressEvent.INITIAL_PROGRESS_UPDATE, expectedId)); |
| 485 | BuildEvent rootCause = |
| 486 | new GenericBuildEvent(testId("failure event"), ImmutableSet.<BuildEventId>of()); |
| 487 | BuildEvent failedTarget = |
| 488 | new GenericOrderEvent(expectedId, ImmutableSet.<BuildEventId>of(rootCause.getEventId())); |
| 489 | |
| 490 | streamer.buildEvent(startEvent); |
| 491 | streamer.buildEvent(failedTarget); |
| 492 | streamer.buildEvent(rootCause); |
| 493 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 494 | assertThat(streamer.isClosed()).isFalse(); |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 495 | List<BuildEvent> allEventsSeen = transport.getEvents(); |
| 496 | assertThat(allEventsSeen).hasSize(4); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 497 | assertThat(allEventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 498 | BuildEvent linkEvent = allEventsSeen.get(1); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 499 | assertThat(linkEvent.getEventId()).isEqualTo(ProgressEvent.INITIAL_PROGRESS_UPDATE); |
| 500 | assertThat(allEventsSeen.get(2).getEventId()).isEqualTo(rootCause.getEventId()); |
| 501 | assertThat(allEventsSeen.get(3).getEventId()).isEqualTo(failedTarget.getEventId()); |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 502 | } |
| 503 | |
felly | a4488a2 | 2019-04-03 13:05:47 -0700 | [diff] [blame] | 504 | private static BuildEvent indexOrderedBuildEvent(int index, int afterIndex) { |
| 505 | return new GenericOrderEvent( |
| 506 | testId("Concurrent-" + index), |
| 507 | ImmutableList.of(), |
| 508 | afterIndex == -1 |
| 509 | ? ImmutableList.of() |
| 510 | : ImmutableList.of(testId("Concurrent-" + afterIndex))); |
| 511 | } |
| 512 | |
| 513 | @Test |
| 514 | public void testConcurrency() throws Exception { |
| 515 | // Verify that we can blast the BuildEventStreamer with many build events in parallel without |
| 516 | // violating internal consistency. The thread-safety under test is primarily sensitive to the |
| 517 | // pendingEvents field constructed when there are ordering constraints, so we make sure to |
| 518 | // include such ordering constraints in this test. |
| 519 | BuildEvent startEvent = |
| 520 | new GenericBuildEvent( |
| 521 | testId("Initial"), |
| 522 | ImmutableSet.of(ProgressEvent.INITIAL_PROGRESS_UPDATE, BuildEventId.buildFinished())); |
| 523 | streamer.buildEvent(startEvent); |
| 524 | |
| 525 | int numThreads = 12; |
| 526 | int numEventsPerThread = 10_000; |
| 527 | int totalEvents = numThreads * numEventsPerThread; |
| 528 | AtomicInteger idIndex = new AtomicInteger(); |
| 529 | ThreadPoolExecutor pool = |
| 530 | new ThreadPoolExecutor( |
| 531 | numThreads, |
| 532 | numThreads, |
| 533 | /* keepAliveTime= */ 0, |
| 534 | TimeUnit.SECONDS, |
| 535 | /* workQueue= */ new LinkedBlockingQueue<>()); |
| 536 | |
| 537 | for (int i = 0; i < numThreads; i++) { |
| 538 | pool.execute( |
| 539 | () -> { |
| 540 | for (int j = 0; j < numEventsPerThread; j++) { |
| 541 | int index = idIndex.getAndIncrement(); |
| 542 | // Arrange for half of the events to have an ordering constraint on the subsequent |
| 543 | // event. The ordering graph must avoid cycles. |
| 544 | int afterIndex = (index % 2 == 0) ? (index + 1) % totalEvents : -1; |
| 545 | streamer.buildEvent(indexOrderedBuildEvent(index, afterIndex)); |
| 546 | } |
| 547 | }); |
| 548 | } |
| 549 | |
| 550 | pool.shutdown(); |
| 551 | pool.awaitTermination(1, TimeUnit.DAYS); |
| 552 | |
| 553 | BuildEventId lateId = testId("late event"); |
| 554 | streamer.buildEvent(new BuildCompleteEvent(new BuildResult(0), ImmutableList.of(lateId))); |
| 555 | assertThat(streamer.isClosed()).isFalse(); |
| 556 | streamer.buildEvent(new GenericBuildEvent(lateId, ImmutableSet.of())); |
| 557 | assertThat(streamer.isClosed()).isTrue(); |
| 558 | |
| 559 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 560 | assertThat(eventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
| 561 | assertThat(eventsSeen).hasSize(4 + totalEvents * 2); |
| 562 | } |
| 563 | |
| 564 | // Re-enable this "test" for ad-hoc benchmarking of many concurrent build events. |
| 565 | @Ignore |
| 566 | public void concurrencyBenchmark() throws Exception { |
| 567 | long time = 0; |
| 568 | for (int iteration = 0; iteration < 3; iteration++) { |
| 569 | StopWatch watch = new StopWatch(); |
| 570 | watch.start(); |
| 571 | |
| 572 | transport = new RecordingBuildEventTransport(artifactGroupNamer, /*recordEvents=*/ false); |
| 573 | streamer = |
| 574 | new BuildEventStreamer( |
| 575 | ImmutableSet.<BuildEventTransport>of(transport), artifactGroupNamer); |
| 576 | BuildEvent startEvent = |
| 577 | new GenericBuildEvent( |
| 578 | testId("Initial"), |
| 579 | ImmutableSet.of(ProgressEvent.INITIAL_PROGRESS_UPDATE, BuildEventId.buildFinished())); |
| 580 | streamer.buildEvent(startEvent); |
| 581 | |
| 582 | int numThreads = 12; |
| 583 | int numEventsPerThread = 100_000; |
| 584 | int totalEvents = numThreads * numEventsPerThread; |
| 585 | AtomicInteger idIndex = new AtomicInteger(); |
| 586 | ThreadPoolExecutor pool = |
| 587 | new ThreadPoolExecutor( |
| 588 | numThreads, numThreads, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<>()); |
| 589 | |
| 590 | for (int i = 0; i < numThreads; i++) { |
| 591 | pool.execute( |
| 592 | () -> { |
| 593 | for (int j = 0; j < numEventsPerThread; j++) { |
| 594 | int index = idIndex.getAndIncrement(); |
| 595 | // Arrange for half of the events to have an ordering constraint on the subsequent |
| 596 | // event. The ordering graph must avoid cycles. |
| 597 | int afterIndex = (index % 2 == 0) ? (index + 1) % totalEvents : -1; |
| 598 | streamer.buildEvent(indexOrderedBuildEvent(index, afterIndex)); |
| 599 | } |
| 600 | }); |
| 601 | } |
| 602 | |
| 603 | pool.shutdown(); |
| 604 | pool.awaitTermination(1, TimeUnit.DAYS); |
| 605 | watch.stop(); |
| 606 | |
| 607 | time += watch.getTime(); |
| 608 | |
| 609 | BuildEventId lateId = testId("late event"); |
| 610 | streamer.buildEvent(new BuildCompleteEvent(new BuildResult(0), ImmutableList.of(lateId))); |
| 611 | assertThat(streamer.isClosed()).isFalse(); |
| 612 | streamer.buildEvent(new GenericBuildEvent(lateId, ImmutableSet.of())); |
| 613 | assertThat(streamer.isClosed()).isTrue(); |
| 614 | } |
| 615 | |
| 616 | System.err.println(); |
| 617 | System.err.println("============================================================="); |
| 618 | System.err.println("Concurrent performance of BEP build event processing: " + time + "ms"); |
| 619 | System.err.println("============================================================="); |
| 620 | } |
| 621 | |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 622 | @Test |
felly | 21c2582 | 2018-12-28 13:29:52 -0800 | [diff] [blame] | 623 | public void testMissingPrerequisites() { |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 624 | // Verify that an event where the prerequisite is never coming till the end of |
| 625 | // the build still gets posted, with the prerequisite aborted. |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 626 | BuildEventId expectedId = testId("the target"); |
| 627 | BuildEvent startEvent = |
| 628 | new GenericBuildEvent( |
| 629 | testId("Initial"), |
Jakob Buchgraber | 8c3a4ef | 2017-02-28 18:49:50 +0000 | [diff] [blame] | 630 | ImmutableSet.<BuildEventId>of(ProgressEvent.INITIAL_PROGRESS_UPDATE, expectedId, |
| 631 | BuildEventId.buildFinished())); |
Klaus Aehlig | f75878d | 2016-11-21 13:41:16 +0000 | [diff] [blame] | 632 | BuildEventId rootCauseId = testId("failure event"); |
| 633 | BuildEvent failedTarget = |
| 634 | new GenericOrderEvent(expectedId, ImmutableSet.<BuildEventId>of(rootCauseId)); |
| 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()); |
| 644 | assertThat(allEventsSeen.get(1).getEventId()).isEqualTo(BuildEventId.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( |
| 658 | initialId, |
| 659 | ImmutableSet.<BuildEventId>of(ProgressEvent.INITIAL_PROGRESS_UPDATE, waitId)); |
| 660 | BuildEvent waitingForStart = |
| 661 | new GenericOrderEvent(waitId, ImmutableSet.<BuildEventId>of(), ImmutableSet.of(initialId)); |
| 662 | |
| 663 | streamer.buildEvent(waitingForStart); |
| 664 | streamer.buildEvent(startEvent); |
| 665 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 666 | assertThat(streamer.isClosed()).isFalse(); |
Klaus Aehlig | ceb0f62 | 2017-03-20 13:52:06 +0000 | [diff] [blame] | 667 | List<BuildEvent> allEventsSeen = transport.getEvents(); |
| 668 | assertThat(allEventsSeen).hasSize(2); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 669 | assertThat(allEventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
| 670 | assertThat(allEventsSeen.get(1).getEventId()).isEqualTo(waitingForStart.getEventId()); |
Klaus Aehlig | ceb0f62 | 2017-03-20 13:52:06 +0000 | [diff] [blame] | 671 | } |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 672 | |
| 673 | private Artifact makeArtifact(String pathString) { |
| 674 | Path path = outputBase.getRelative(PathFragment.create(pathString)); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 675 | return new Artifact(path, ArtifactRoot.asSourceRoot(Root.fromPath(outputBase))); |
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( |
| 683 | testId("Initial"), |
| 684 | ImmutableSet.<BuildEventId>of(ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
| 685 | |
| 686 | Artifact a = makeArtifact("path/a"); |
| 687 | Artifact b = makeArtifact("path/b"); |
| 688 | Artifact c = makeArtifact("path/c"); |
| 689 | NestedSet<Artifact> innerGroup = NestedSetBuilder.<Artifact>stableOrder().add(a).add(b).build(); |
| 690 | NestedSet<Artifact> group = |
| 691 | NestedSetBuilder.<Artifact>stableOrder().addTransitive(innerGroup).add(c).build(); |
| 692 | BuildEvent reportingArtifacts = |
| 693 | new GenericArtifactReportingEvent(testId("reporting"), ImmutableSet.of(group)); |
| 694 | |
| 695 | streamer.buildEvent(startEvent); |
| 696 | streamer.buildEvent(reportingArtifacts); |
| 697 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 698 | assertThat(streamer.isClosed()).isFalse(); |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 699 | List<BuildEvent> allEventsSeen = transport.getEvents(); |
| 700 | List<BuildEventStreamProtos.BuildEvent> eventProtos = transport.getEventProtos(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 701 | assertThat(allEventsSeen).hasSize(7); |
| 702 | assertThat(allEventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
| 703 | assertThat(allEventsSeen.get(1).getEventId()).isEqualTo(ProgressEvent.INITIAL_PROGRESS_UPDATE); |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 704 | List<BuildEventStreamProtos.File> firstSetDirects = |
| 705 | eventProtos.get(2).getNamedSetOfFiles().getFilesList(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 706 | assertThat(firstSetDirects).hasSize(2); |
| 707 | assertThat(ImmutableSet.of(firstSetDirects.get(0).getUri(), firstSetDirects.get(1).getUri())) |
| 708 | .isEqualTo(ImmutableSet.of(a.getPath().toString(), b.getPath().toString())); |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 709 | List<NamedSetOfFilesId> secondSetTransitives = |
| 710 | eventProtos.get(4).getNamedSetOfFiles().getFileSetsList(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 711 | assertThat(secondSetTransitives).hasSize(1); |
| 712 | assertThat(secondSetTransitives.get(0)).isEqualTo(eventProtos.get(2).getId().getNamedSet()); |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 713 | List<NamedSetOfFilesId> reportedArtifactSets = |
| 714 | eventProtos.get(6).getNamedSetOfFiles().getFileSetsList(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 715 | assertThat(reportedArtifactSets).hasSize(1); |
| 716 | assertThat(reportedArtifactSets.get(0)).isEqualTo(eventProtos.get(4).getId().getNamedSet()); |
Klaus Aehlig | ee3e192 | 2017-04-07 14:25:27 +0000 | [diff] [blame] | 717 | } |
Klaus Aehlig | 9b3b2e9 | 2017-05-02 11:18:42 +0200 | [diff] [blame] | 718 | |
| 719 | @Test |
| 720 | public void testStdoutReported() { |
| 721 | // Verify that stdout and stderr are reported in the build-event stream on progress |
| 722 | // events. |
Klaus Aehlig | 9b3b2e9 | 2017-05-02 11:18:42 +0200 | [diff] [blame] | 723 | BuildEventStreamer.OutErrProvider outErr = |
| 724 | Mockito.mock(BuildEventStreamer.OutErrProvider.class); |
| 725 | String stdoutMsg = "Some text that was written to stdout."; |
| 726 | String stderrMsg = "The UI text that bazel wrote to stderr."; |
felly | 1759476 | 2019-02-07 15:24:32 -0800 | [diff] [blame] | 727 | when(outErr.getOut()).thenReturn(ImmutableList.of(stdoutMsg)); |
| 728 | when(outErr.getErr()).thenReturn(ImmutableList.of(stderrMsg)); |
Klaus Aehlig | 9b3b2e9 | 2017-05-02 11:18:42 +0200 | [diff] [blame] | 729 | BuildEvent startEvent = |
| 730 | new GenericBuildEvent( |
| 731 | testId("Initial"), |
| 732 | ImmutableSet.<BuildEventId>of(ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
| 733 | BuildEvent unexpectedEvent = |
| 734 | new GenericBuildEvent(testId("unexpected"), ImmutableSet.<BuildEventId>of()); |
| 735 | |
| 736 | streamer.registerOutErrProvider(outErr); |
| 737 | streamer.buildEvent(startEvent); |
| 738 | streamer.buildEvent(unexpectedEvent); |
| 739 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 740 | assertThat(streamer.isClosed()).isFalse(); |
Klaus Aehlig | 9b3b2e9 | 2017-05-02 11:18:42 +0200 | [diff] [blame] | 741 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 742 | assertThat(eventsSeen).hasSize(3); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 743 | assertThat(eventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
| 744 | assertThat(eventsSeen.get(2).getEventId()).isEqualTo(unexpectedEvent.getEventId()); |
Klaus Aehlig | 9b3b2e9 | 2017-05-02 11:18:42 +0200 | [diff] [blame] | 745 | BuildEvent linkEvent = eventsSeen.get(1); |
| 746 | BuildEventStreamProtos.BuildEvent linkEventProto = transport.getEventProtos().get(1); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 747 | assertThat(linkEvent.getEventId()).isEqualTo(ProgressEvent.INITIAL_PROGRESS_UPDATE); |
| 748 | assertWithMessage("Unexpected events should be linked") |
| 749 | .that(linkEvent.getChildrenEvents().contains(unexpectedEvent.getEventId())) |
| 750 | .isTrue(); |
| 751 | assertThat(linkEventProto.getProgress().getStdout()).isEqualTo(stdoutMsg); |
| 752 | assertThat(linkEventProto.getProgress().getStderr()).isEqualTo(stderrMsg); |
Klaus Aehlig | 9b3b2e9 | 2017-05-02 11:18:42 +0200 | [diff] [blame] | 753 | |
| 754 | // As there is only one progress event, the OutErrProvider should be queried |
| 755 | // only once for stdout and stderr. |
| 756 | verify(outErr, times(1)).getOut(); |
| 757 | verify(outErr, times(1)).getErr(); |
| 758 | } |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 759 | |
| 760 | @Test |
felly | 21c2582 | 2018-12-28 13:29:52 -0800 | [diff] [blame] | 761 | public void testStdoutReportedAfterCrash() { |
| 762 | // Verify that stdout and stderr are reported in the build-event stream on progress |
| 763 | // events. |
felly | 21c2582 | 2018-12-28 13:29:52 -0800 | [diff] [blame] | 764 | BuildEventStreamer.OutErrProvider outErr = |
| 765 | Mockito.mock(BuildEventStreamer.OutErrProvider.class); |
| 766 | String stdoutMsg = "Some text that was written to stdout."; |
| 767 | String stderrMsg = "The UI text that bazel wrote to stderr."; |
felly | 1759476 | 2019-02-07 15:24:32 -0800 | [diff] [blame] | 768 | when(outErr.getOut()).thenReturn(ImmutableList.of(stdoutMsg)); |
| 769 | when(outErr.getErr()).thenReturn(ImmutableList.of(stderrMsg)); |
felly | 21c2582 | 2018-12-28 13:29:52 -0800 | [diff] [blame] | 770 | BuildEvent startEvent = |
| 771 | new GenericBuildEvent( |
| 772 | testId("Initial"), |
| 773 | ImmutableSet.<BuildEventId>of(ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
| 774 | |
| 775 | streamer.registerOutErrProvider(outErr); |
| 776 | streamer.buildEvent(startEvent); |
| 777 | // Simulate a crash with an abrupt call to #close(). |
| 778 | streamer.close(); |
| 779 | assertThat(streamer.isClosed()).isTrue(); |
| 780 | |
| 781 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 782 | assertThat(eventsSeen).hasSize(2); |
| 783 | assertThat(eventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
| 784 | BuildEvent linkEvent = eventsSeen.get(1); |
| 785 | BuildEventStreamProtos.BuildEvent linkEventProto = transport.getEventProtos().get(1); |
| 786 | assertThat(linkEvent.getEventId()).isEqualTo(ProgressEvent.INITIAL_PROGRESS_UPDATE); |
| 787 | assertThat(linkEventProto.getProgress().getStdout()).isEqualTo(stdoutMsg); |
| 788 | assertThat(linkEventProto.getProgress().getStderr()).isEqualTo(stderrMsg); |
| 789 | |
| 790 | // As there is only one progress event, the OutErrProvider should be queried |
| 791 | // only once for stdout and stderr. |
| 792 | verify(outErr, times(1)).getOut(); |
| 793 | verify(outErr, times(1)).getErr(); |
| 794 | } |
| 795 | |
felly | 1759476 | 2019-02-07 15:24:32 -0800 | [diff] [blame] | 796 | private static <T> ImmutableList<ImmutableList<Pair<T, T>>> consumeToLists( |
| 797 | Iterable<T> left, Iterable<T> right) { |
| 798 | ImmutableList.Builder<Pair<T, T>> consumerBuilder = ImmutableList.builder(); |
| 799 | ImmutableList.Builder<Pair<T, T>> lastConsumerBuilder = ImmutableList.builder(); |
| 800 | |
| 801 | BuildEventStreamer.consumeAsPairs( |
| 802 | left, |
| 803 | right, |
| 804 | (t1, t2) -> consumerBuilder.add(Pair.of(t1, t2)), |
| 805 | (t1, t2) -> lastConsumerBuilder.add(Pair.of(t1, t2))); |
| 806 | |
| 807 | return ImmutableList.of(consumerBuilder.build(), lastConsumerBuilder.build()); |
| 808 | } |
| 809 | |
| 810 | @Test |
| 811 | public void testConsumeAsPairs() { |
| 812 | assertThat(consumeToLists(ImmutableList.of(1, 2, 3), ImmutableList.of(4, 5, 6))) |
| 813 | .containsExactly( |
| 814 | ImmutableList.of(Pair.of(1, null), Pair.of(2, null), Pair.of(3, 4), Pair.of(null, 5)), |
| 815 | ImmutableList.of(Pair.of(null, 6))) |
| 816 | .inOrder(); |
| 817 | |
| 818 | assertThat(consumeToLists(ImmutableList.of(), ImmutableList.of())) |
| 819 | .containsExactly(ImmutableList.of(), ImmutableList.of(Pair.of(null, null))) |
| 820 | .inOrder(); |
| 821 | |
| 822 | assertThat(consumeToLists(ImmutableList.of(1), ImmutableList.of(2))) |
| 823 | .containsExactly(ImmutableList.of(), ImmutableList.of(Pair.of(1, 2))) |
| 824 | .inOrder(); |
| 825 | |
| 826 | assertThat(consumeToLists(ImmutableList.of(1), ImmutableList.of(2, 3))) |
| 827 | .containsExactly(ImmutableList.of(Pair.of(1, 2)), ImmutableList.of(Pair.of(null, 3))) |
| 828 | .inOrder(); |
| 829 | |
| 830 | assertThat(consumeToLists(ImmutableList.of(1, 2), ImmutableList.of())) |
| 831 | .containsExactly(ImmutableList.of(Pair.of(1, null)), ImmutableList.of(Pair.of(2, null))) |
| 832 | .inOrder(); |
| 833 | |
| 834 | assertThat(consumeToLists(ImmutableList.of(), ImmutableList.of(1))) |
| 835 | .containsExactly(ImmutableList.of(), ImmutableList.of(Pair.of(null, 1))) |
| 836 | .inOrder(); |
| 837 | } |
| 838 | |
felly | 21c2582 | 2018-12-28 13:29:52 -0800 | [diff] [blame] | 839 | @Test |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 840 | public void testReportedConfigurations() throws Exception { |
| 841 | // Verify that configuration events are posted, but only once. |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 842 | BuildOptions defaultBuildOptions = |
gregce | e495e6b | 2019-04-30 14:07:06 -0700 | [diff] [blame] | 843 | BuildOptions.of(ImmutableList.<Class<? extends FragmentOptions>>of(CoreOptions.class)); |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 844 | BuildEvent startEvent = |
| 845 | new GenericBuildEvent( |
| 846 | testId("Initial"), |
| 847 | ImmutableSet.<BuildEventId>of(ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
| 848 | BuildConfiguration configuration = |
| 849 | new BuildConfiguration( |
janakr | 3b63a4e | 2017-09-14 09:55:40 +0200 | [diff] [blame] | 850 | new BlazeDirectories( |
Klaus Aehlig | c2499c4 | 2018-02-27 05:47:21 -0800 | [diff] [blame] | 851 | new ServerDirectories(outputBase, outputBase, outputBase), |
| 852 | rootDirectory, |
cushon | 849df36 | 2018-05-14 01:51:45 -0700 | [diff] [blame] | 853 | /* defaultSystemJavabase= */ null, |
Klaus Aehlig | c2499c4 | 2018-02-27 05:47:21 -0800 | [diff] [blame] | 854 | "productName"), |
lberki | 406199f | 2018-04-09 03:16:19 -0700 | [diff] [blame] | 855 | /* fragmentsMap= */ ImmutableMap |
janakr | 3b63a4e | 2017-09-14 09:55:40 +0200 | [diff] [blame] | 856 | .<Class<? extends BuildConfiguration.Fragment>, BuildConfiguration.Fragment>of(), |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 857 | defaultBuildOptions, |
| 858 | BuildOptions.diffForReconstruction(defaultBuildOptions, defaultBuildOptions), |
lberki | 406199f | 2018-04-09 03:16:19 -0700 | [diff] [blame] | 859 | /* reservedActionMnemonics= */ ImmutableSet.of(), |
lberki | 8626623 | 2018-04-11 00:33:42 -0700 | [diff] [blame] | 860 | ActionEnvironment.EMPTY, |
gregce | 8529746 | 2017-08-18 21:20:29 +0200 | [diff] [blame] | 861 | "workspace"); |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 862 | BuildEvent firstWithConfiguration = |
shahan | 50f99d5 | 2018-03-10 05:14:09 -0800 | [diff] [blame] | 863 | new GenericConfigurationEvent(testId("first"), configuration.toBuildEvent()); |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 864 | BuildEvent secondWithConfiguration = |
shahan | 50f99d5 | 2018-03-10 05:14:09 -0800 | [diff] [blame] | 865 | new GenericConfigurationEvent(testId("second"), configuration.toBuildEvent()); |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 866 | |
| 867 | streamer.buildEvent(startEvent); |
| 868 | streamer.buildEvent(firstWithConfiguration); |
| 869 | streamer.buildEvent(secondWithConfiguration); |
| 870 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 871 | assertThat(streamer.isClosed()).isFalse(); |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 872 | List<BuildEvent> allEventsSeen = transport.getEvents(); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 873 | assertThat(allEventsSeen).hasSize(7); |
| 874 | assertThat(allEventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
| 875 | assertThat(allEventsSeen.get(1).getEventId()).isEqualTo(ProgressEvent.INITIAL_PROGRESS_UPDATE); |
shahan | 50f99d5 | 2018-03-10 05:14:09 -0800 | [diff] [blame] | 876 | assertThat(allEventsSeen.get(2)).isEqualTo(configuration.toBuildEvent()); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 877 | assertThat(allEventsSeen.get(3).getEventId()).isEqualTo(BuildEventId.progressId(1)); |
| 878 | assertThat(allEventsSeen.get(4)).isEqualTo(firstWithConfiguration); |
| 879 | assertThat(allEventsSeen.get(5).getEventId()).isEqualTo(BuildEventId.progressId(2)); |
| 880 | assertThat(allEventsSeen.get(6)).isEqualTo(secondWithConfiguration); |
Klaus Aehlig | 0cce00c | 2017-05-08 08:26:03 -0400 | [diff] [blame] | 881 | } |
Klaus Aehlig | 8f6549f | 2017-06-26 15:30:55 +0200 | [diff] [blame] | 882 | |
| 883 | @Test |
| 884 | public void testEarlyFlush() throws Exception { |
| 885 | // Verify that the streamer can handle early calls to flush() and still correctly |
| 886 | // reports stdout and stderr in the build-event stream. |
Klaus Aehlig | 8f6549f | 2017-06-26 15:30:55 +0200 | [diff] [blame] | 887 | BuildEventStreamer.OutErrProvider outErr = |
| 888 | Mockito.mock(BuildEventStreamer.OutErrProvider.class); |
| 889 | String firstStdoutMsg = "Some text that was written to stdout."; |
| 890 | String firstStderrMsg = "The UI text that bazel wrote to stderr."; |
| 891 | String secondStdoutMsg = "More text that was written to stdout, still before the start event."; |
| 892 | String secondStderrMsg = "More text written to stderr, still before the start event."; |
felly | 1759476 | 2019-02-07 15:24:32 -0800 | [diff] [blame] | 893 | when(outErr.getOut()) |
| 894 | .thenReturn(ImmutableList.of(firstStdoutMsg)) |
| 895 | .thenReturn(ImmutableList.of(secondStdoutMsg)); |
| 896 | when(outErr.getErr()) |
| 897 | .thenReturn(ImmutableList.of(firstStderrMsg)) |
| 898 | .thenReturn(ImmutableList.of(secondStderrMsg)); |
Klaus Aehlig | 8f6549f | 2017-06-26 15:30:55 +0200 | [diff] [blame] | 899 | BuildEvent startEvent = |
| 900 | new GenericBuildEvent( |
| 901 | testId("Initial"), |
| 902 | ImmutableSet.<BuildEventId>of(ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
| 903 | |
| 904 | streamer.registerOutErrProvider(outErr); |
| 905 | streamer.flush(); |
| 906 | streamer.flush(); |
| 907 | streamer.buildEvent(startEvent); |
| 908 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 909 | assertThat(streamer.isClosed()).isFalse(); |
Klaus Aehlig | 8f6549f | 2017-06-26 15:30:55 +0200 | [diff] [blame] | 910 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 911 | assertThat(eventsSeen).hasSize(3); |
| 912 | assertThat(eventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
| 913 | BuildEvent progressEvent = eventsSeen.get(1); |
| 914 | assertThat(progressEvent.getEventId()).isEqualTo(ProgressEvent.INITIAL_PROGRESS_UPDATE); |
| 915 | BuildEventStreamProtos.BuildEvent progressEventProto = transport.getEventProtos().get(1); |
| 916 | assertThat(progressEventProto.getProgress().getStdout()).isEqualTo(firstStdoutMsg); |
| 917 | assertThat(progressEventProto.getProgress().getStderr()).isEqualTo(firstStderrMsg); |
| 918 | BuildEventStreamProtos.BuildEvent secondProgressEventProto = transport.getEventProtos().get(2); |
| 919 | assertThat(secondProgressEventProto.getProgress().getStdout()).isEqualTo(secondStdoutMsg); |
| 920 | assertThat(secondProgressEventProto.getProgress().getStderr()).isEqualTo(secondStderrMsg); |
| 921 | |
| 922 | // As there is only one progress event, the OutErrProvider should be queried |
| 923 | // only once per flush() for stdout and stderr. |
| 924 | verify(outErr, times(2)).getOut(); |
| 925 | verify(outErr, times(2)).getErr(); |
| 926 | } |
| 927 | |
| 928 | @Test |
felly | 1759476 | 2019-02-07 15:24:32 -0800 | [diff] [blame] | 929 | public void testChunkedFlush() throws Exception { |
| 930 | // Verify that the streamer calls to flush() that return multiple chunked buffers. |
felly | 1759476 | 2019-02-07 15:24:32 -0800 | [diff] [blame] | 931 | BuildEventStreamer.OutErrProvider outErr = |
| 932 | Mockito.mock(BuildEventStreamer.OutErrProvider.class); |
| 933 | String firstStdoutMsg = "Some text that was written to stdout."; |
| 934 | String firstStderrMsg = "The UI text that bazel wrote to stderr."; |
| 935 | String secondStdoutMsg = "More text that was written to stdout, still before the start event."; |
| 936 | String secondStderrMsg = "More text written to stderr, still before the start event."; |
| 937 | when(outErr.getOut()).thenReturn(ImmutableList.of(firstStdoutMsg, secondStdoutMsg)); |
| 938 | when(outErr.getErr()).thenReturn(ImmutableList.of(firstStderrMsg, secondStderrMsg)); |
| 939 | BuildEvent startEvent = |
| 940 | new GenericBuildEvent( |
| 941 | testId("Initial"), |
| 942 | ImmutableSet.<BuildEventId>of(ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
| 943 | |
| 944 | streamer.registerOutErrProvider(outErr); |
| 945 | streamer.buildEvent(startEvent); |
| 946 | streamer.flush(); |
| 947 | |
| 948 | assertThat(streamer.isClosed()).isFalse(); |
| 949 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 950 | assertThat(eventsSeen).hasSize(4); |
| 951 | assertThat(eventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
| 952 | |
| 953 | // Expect to find 3 progress messages: (firstStdout, ""), (secondStdout, firstStderr), |
| 954 | // ("", secondStdErr). Assuming UIs display stdout first, this maintains ordering. |
| 955 | BuildEvent progressEvent = eventsSeen.get(1); |
| 956 | assertThat(progressEvent.getEventId()).isEqualTo(ProgressEvent.INITIAL_PROGRESS_UPDATE); |
| 957 | BuildEventStreamProtos.BuildEvent progressEventProto = transport.getEventProtos().get(1); |
| 958 | assertThat(progressEventProto.getProgress().getStdout()).isEqualTo(firstStdoutMsg); |
| 959 | assertThat(progressEventProto.getProgress().getStderr()).isEmpty(); |
| 960 | |
| 961 | BuildEventStreamProtos.BuildEvent secondProgressEventProto = transport.getEventProtos().get(2); |
| 962 | assertThat(secondProgressEventProto.getProgress().getStdout()).isEqualTo(secondStdoutMsg); |
| 963 | assertThat(secondProgressEventProto.getProgress().getStderr()).isEqualTo(firstStderrMsg); |
| 964 | |
| 965 | BuildEventStreamProtos.BuildEvent thirdProgressEventProto = transport.getEventProtos().get(3); |
| 966 | assertThat(thirdProgressEventProto.getProgress().getStdout()).isEmpty(); |
| 967 | assertThat(thirdProgressEventProto.getProgress().getStderr()).isEqualTo(secondStderrMsg); |
| 968 | |
| 969 | // The OutErrProvider should be queried only once per flush(). |
| 970 | verify(outErr, times(1)).getOut(); |
| 971 | verify(outErr, times(1)).getErr(); |
| 972 | } |
| 973 | |
| 974 | @Test |
Klaus Aehlig | 28221ff | 2018-01-11 04:28:00 -0800 | [diff] [blame] | 975 | public void testNoopFlush() throws Exception { |
| 976 | // Verify that the streamer ignores a flush, if neither stream produces any output. |
Klaus Aehlig | 28221ff | 2018-01-11 04:28:00 -0800 | [diff] [blame] | 977 | BuildEventStreamer.OutErrProvider outErr = |
| 978 | Mockito.mock(BuildEventStreamer.OutErrProvider.class); |
| 979 | String stdoutMsg = "Some text that was written to stdout."; |
| 980 | String stderrMsg = "The UI text that bazel wrote to stderr."; |
felly | 1759476 | 2019-02-07 15:24:32 -0800 | [diff] [blame] | 981 | when(outErr.getOut()).thenReturn(ImmutableList.of(stdoutMsg)).thenReturn(ImmutableList.of()); |
| 982 | when(outErr.getErr()).thenReturn(ImmutableList.of(stderrMsg)).thenReturn(ImmutableList.of()); |
Klaus Aehlig | 28221ff | 2018-01-11 04:28:00 -0800 | [diff] [blame] | 983 | BuildEvent startEvent = |
| 984 | new GenericBuildEvent( |
| 985 | testId("Initial"), |
| 986 | ImmutableSet.<BuildEventId>of(ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
| 987 | |
| 988 | streamer.registerOutErrProvider(outErr); |
| 989 | streamer.buildEvent(startEvent); |
| 990 | assertThat(transport.getEvents()).hasSize(1); |
| 991 | streamer.flush(); // Output, so a new progress event has to be added |
| 992 | assertThat(transport.getEvents()).hasSize(2); |
| 993 | streamer.flush(); // No further output, so no additional event should be generated. |
| 994 | assertThat(transport.getEvents()).hasSize(2); |
| 995 | |
| 996 | assertThat(transport.getEvents().get(0)).isEqualTo(startEvent); |
| 997 | assertThat(transport.getEventProtos().get(1).getProgress().getStdout()).isEqualTo(stdoutMsg); |
| 998 | assertThat(transport.getEventProtos().get(1).getProgress().getStderr()).isEqualTo(stderrMsg); |
| 999 | } |
| 1000 | |
| 1001 | @Test |
Klaus Aehlig | 8f6549f | 2017-06-26 15:30:55 +0200 | [diff] [blame] | 1002 | public void testEarlyFlushBadInitialEvent() throws Exception { |
| 1003 | // Verify that an early flush works correctly with an unusual start event. |
| 1004 | // In this case, we expect 3 events in the stream, in that order: |
jingwen | 68c57f0 | 2018-11-21 16:17:17 -0800 | [diff] [blame] | 1005 | // - an artificial progress event as initial event, to properly link in |
Klaus Aehlig | 8f6549f | 2017-06-26 15:30:55 +0200 | [diff] [blame] | 1006 | // all events |
| 1007 | // - the unusal first event we have seen, and |
| 1008 | // - a progress event reporting the flushed messages. |
Klaus Aehlig | 8f6549f | 2017-06-26 15:30:55 +0200 | [diff] [blame] | 1009 | BuildEventStreamer.OutErrProvider outErr = |
| 1010 | Mockito.mock(BuildEventStreamer.OutErrProvider.class); |
| 1011 | String stdoutMsg = "Some text that was written to stdout."; |
| 1012 | String stderrMsg = "The UI text that bazel wrote to stderr."; |
felly | 1759476 | 2019-02-07 15:24:32 -0800 | [diff] [blame] | 1013 | when(outErr.getOut()).thenReturn(ImmutableList.of(stdoutMsg)); |
| 1014 | when(outErr.getErr()).thenReturn(ImmutableList.of(stderrMsg)); |
Klaus Aehlig | 8f6549f | 2017-06-26 15:30:55 +0200 | [diff] [blame] | 1015 | |
| 1016 | BuildEvent unexpectedStartEvent = |
| 1017 | new GenericBuildEvent(testId("unexpected start"), ImmutableSet.<BuildEventId>of()); |
| 1018 | |
| 1019 | streamer.registerOutErrProvider(outErr); |
| 1020 | streamer.flush(); |
| 1021 | streamer.buildEvent(unexpectedStartEvent); |
| 1022 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 1023 | assertThat(streamer.isClosed()).isFalse(); |
| 1024 | |
Klaus Aehlig | 8f6549f | 2017-06-26 15:30:55 +0200 | [diff] [blame] | 1025 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 1026 | assertThat(eventsSeen).hasSize(3); |
| 1027 | |
| 1028 | BuildEvent initial = eventsSeen.get(0); |
| 1029 | assertThat(initial.getEventId()).isEqualTo(ProgressEvent.INITIAL_PROGRESS_UPDATE); |
| 1030 | BuildEventStreamProtos.BuildEvent initialProto = transport.getEventProtos().get(0); |
| 1031 | assertThat(initialProto.getProgress().getStdout()).isEmpty(); |
| 1032 | assertThat(initialProto.getProgress().getStderr()).isEmpty(); |
| 1033 | |
| 1034 | assertThat(eventsSeen.get(1).getEventId()).isEqualTo(unexpectedStartEvent.getEventId()); |
| 1035 | assertWithMessage("Unexpected event should be linked") |
| 1036 | .that(initial.getChildrenEvents().contains(unexpectedStartEvent.getEventId())) |
| 1037 | .isTrue(); |
| 1038 | |
| 1039 | BuildEventStreamProtos.BuildEvent progressProto = transport.getEventProtos().get(2); |
| 1040 | assertThat(progressProto.getProgress().getStdout()).isEqualTo(stdoutMsg); |
| 1041 | assertThat(progressProto.getProgress().getStderr()).isEqualTo(stderrMsg); |
| 1042 | assertWithMessage("flushed progress should be linked") |
| 1043 | .that(initial.getChildrenEvents().contains(eventsSeen.get(2).getEventId())) |
| 1044 | .isTrue(); |
| 1045 | |
| 1046 | verify(outErr, times(1)).getOut(); |
| 1047 | verify(outErr, times(1)).getErr(); |
| 1048 | } |
Klaus Aehlig | deea6b0 | 2017-10-16 12:32:20 +0200 | [diff] [blame] | 1049 | |
| 1050 | @Test |
| 1051 | public void testEarlyAbort() throws Exception { |
| 1052 | // For a build that is aborted before a build-started event is generated, |
| 1053 | // we still expect that, if a build-started event is forced by some order |
| 1054 | // constraint (e.g., CommandLine wants to come after build started), then |
| 1055 | // that gets sorted to the beginning. |
Klaus Aehlig | deea6b0 | 2017-10-16 12:32:20 +0200 | [diff] [blame] | 1056 | BuildEvent orderEvent = |
| 1057 | new GenericOrderEvent( |
| 1058 | testId("event depending on start"), |
| 1059 | ImmutableList.of(), |
| 1060 | ImmutableList.of(BuildEventId.buildStartedId())); |
| 1061 | |
| 1062 | streamer.buildEvent(orderEvent); |
| 1063 | streamer.buildEvent(new BuildCompleteEvent(new BuildResult(0))); |
| 1064 | |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 1065 | assertThat(streamer.isClosed()).isTrue(); |
Klaus Aehlig | deea6b0 | 2017-10-16 12:32:20 +0200 | [diff] [blame] | 1066 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 1067 | assertThat(eventsSeen).hasSize(4); |
| 1068 | assertThat(eventsSeen.get(0).getEventId()).isEqualTo(BuildEventId.buildStartedId()); |
| 1069 | assertThat(eventsSeen.get(1).getEventId()).isEqualTo(orderEvent.getEventId()); |
| 1070 | assertThat(ImmutableSet.of(eventsSeen.get(2).getEventId(), eventsSeen.get(3).getEventId())) |
| 1071 | .isEqualTo( |
| 1072 | ImmutableSet.of(BuildEventId.buildFinished(), ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
| 1073 | assertThat(transport.getEventProtos().get(3).getLastMessage()).isTrue(); |
| 1074 | } |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 1075 | |
| 1076 | @Test |
| 1077 | public void testFinalEventsLate() throws Exception { |
| 1078 | // Verify that we correctly handle late events (i.e., events coming only after the |
| 1079 | // BuildCompleteEvent) that are sent to the streamer after the BuildCompleteEvent. |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 1080 | BuildEvent startEvent = |
| 1081 | new GenericBuildEvent( |
| 1082 | testId("Initial"), |
| 1083 | ImmutableSet.of(ProgressEvent.INITIAL_PROGRESS_UPDATE, BuildEventId.buildFinished())); |
| 1084 | BuildEventId lateId = testId("late event"); |
| 1085 | BuildEvent finishedEvent = new BuildCompleteEvent(new BuildResult(0), ImmutableList.of(lateId)); |
| 1086 | |
| 1087 | streamer.buildEvent(startEvent); |
| 1088 | streamer.buildEvent(finishedEvent); |
| 1089 | assertThat(streamer.isClosed()).isFalse(); |
| 1090 | streamer.buildEvent(new GenericBuildEvent(lateId, ImmutableSet.of())); |
| 1091 | assertThat(streamer.isClosed()).isTrue(); |
| 1092 | |
| 1093 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 1094 | assertThat(eventsSeen).hasSize(4); |
| 1095 | assertThat(eventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
| 1096 | assertThat(eventsSeen.get(1).getEventId()).isEqualTo(BuildEventId.buildFinished()); |
| 1097 | assertThat(ImmutableSet.of(eventsSeen.get(2).getEventId(), eventsSeen.get(3).getEventId())) |
| 1098 | .isEqualTo(ImmutableSet.of(lateId, ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
| 1099 | } |
| 1100 | |
| 1101 | @Test |
| 1102 | public void testFinalEventsEarly() throws Exception { |
| 1103 | // Verify that we correctly handle late events (i.e., events coming only after the |
| 1104 | // BuildCompleteEvent) that are sent to the streamer before the BuildCompleteEvent, |
| 1105 | // but with an order constraint to come afterwards. |
aehlig | 03d55f3 | 2017-10-26 11:45:35 +0200 | [diff] [blame] | 1106 | BuildEvent startEvent = |
| 1107 | new GenericBuildEvent( |
| 1108 | testId("Initial"), |
| 1109 | ImmutableSet.of(ProgressEvent.INITIAL_PROGRESS_UPDATE, BuildEventId.buildFinished())); |
| 1110 | BuildEventId lateId = testId("late event"); |
| 1111 | BuildEvent finishedEvent = new BuildCompleteEvent(new BuildResult(0), ImmutableList.of(lateId)); |
| 1112 | |
| 1113 | streamer.buildEvent(startEvent); |
| 1114 | streamer.buildEvent( |
| 1115 | new GenericOrderEvent( |
| 1116 | lateId, ImmutableSet.of(), ImmutableList.of(BuildEventId.buildFinished()))); |
| 1117 | streamer.buildEvent(finishedEvent); |
| 1118 | assertThat(streamer.isClosed()).isTrue(); |
| 1119 | |
| 1120 | List<BuildEvent> eventsSeen = transport.getEvents(); |
| 1121 | assertThat(eventsSeen).hasSize(4); |
| 1122 | assertThat(eventsSeen.get(0).getEventId()).isEqualTo(startEvent.getEventId()); |
| 1123 | assertThat(eventsSeen.get(1).getEventId()).isEqualTo(BuildEventId.buildFinished()); |
| 1124 | assertThat(ImmutableSet.of(eventsSeen.get(2).getEventId(), eventsSeen.get(3).getEventId())) |
| 1125 | .isEqualTo(ImmutableSet.of(lateId, ProgressEvent.INITIAL_PROGRESS_UPDATE)); |
| 1126 | } |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 1127 | |
| 1128 | @Test |
| 1129 | public void testSuccessfulActionsAreNotPublishedByDefault() { |
| 1130 | EventBusHandler handler = new EventBusHandler(); |
| 1131 | eventBus.register(handler); |
| 1132 | |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 1133 | ActionExecutedEvent failedActionExecutedEvent = |
| 1134 | new ActionExecutedEvent( |
ulfjack | 5f312dd | 2018-10-02 04:58:53 -0700 | [diff] [blame] | 1135 | ActionsTestUtil.DUMMY_ARTIFACT.getExecPath(), |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 1136 | new ActionsTestUtil.NullAction(), |
| 1137 | new ActionExecutionException("Exception", /* action= */ null, /* catastrophe= */ false), |
tomlu | d56a806 | 2018-08-01 13:20:41 -0700 | [diff] [blame] | 1138 | ActionsTestUtil.DUMMY_ARTIFACT.getPath(), |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 1139 | /* stdout= */ null, |
| 1140 | /* stderr= */ null, |
| 1141 | ErrorTiming.BEFORE_EXECUTION); |
| 1142 | |
| 1143 | streamer.buildEvent(SUCCESSFUL_ACTION_EXECUTED_EVENT); |
| 1144 | streamer.buildEvent(failedActionExecutedEvent); |
| 1145 | |
| 1146 | List<BuildEvent> transportedEvents = transport.getEvents(); |
| 1147 | |
| 1148 | assertThat(transportedEvents).doesNotContain(SUCCESSFUL_ACTION_EXECUTED_EVENT); |
| 1149 | assertThat(transportedEvents).contains(failedActionExecutedEvent); |
| 1150 | } |
| 1151 | |
| 1152 | @Test |
| 1153 | public void testSuccessfulActionsCanBePublished() { |
| 1154 | EventBusHandler handler = new EventBusHandler(); |
| 1155 | eventBus.register(handler); |
| 1156 | |
| 1157 | BuildEventStreamOptions options = new BuildEventStreamOptions(); |
| 1158 | options.publishAllActions = true; |
| 1159 | |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 1160 | BuildEventStreamer streamer = |
lpino | 2bf8906 | 2019-02-21 03:24:49 -0800 | [diff] [blame] | 1161 | new BuildEventStreamer.Builder() |
| 1162 | .artifactGroupNamer(artifactGroupNamer) |
| 1163 | .besStreamOptions(options) |
lpino | 2bf8906 | 2019-02-21 03:24:49 -0800 | [diff] [blame] | 1164 | .buildEventTransports(ImmutableSet.of(transport)) |
| 1165 | .build(); |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 1166 | |
| 1167 | ActionExecutedEvent failedActionExecutedEvent = |
| 1168 | new ActionExecutedEvent( |
ulfjack | 5f312dd | 2018-10-02 04:58:53 -0700 | [diff] [blame] | 1169 | ActionsTestUtil.DUMMY_ARTIFACT.getExecPath(), |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 1170 | new ActionsTestUtil.NullAction(), |
| 1171 | new ActionExecutionException("Exception", /* action= */ null, /* catastrophe= */ false), |
tomlu | d56a806 | 2018-08-01 13:20:41 -0700 | [diff] [blame] | 1172 | ActionsTestUtil.DUMMY_ARTIFACT.getPath(), |
ruperts | 76de73b | 2018-03-02 16:10:43 -0800 | [diff] [blame] | 1173 | /* stdout= */ null, |
| 1174 | /* stderr= */ null, |
| 1175 | ErrorTiming.BEFORE_EXECUTION); |
| 1176 | |
| 1177 | streamer.buildEvent(SUCCESSFUL_ACTION_EXECUTED_EVENT); |
| 1178 | streamer.buildEvent(failedActionExecutedEvent); |
| 1179 | |
| 1180 | List<BuildEvent> transportedEvents = transport.getEvents(); |
| 1181 | |
| 1182 | assertThat(transportedEvents).contains(SUCCESSFUL_ACTION_EXECUTED_EVENT); |
| 1183 | assertThat(transportedEvents).contains(failedActionExecutedEvent); |
| 1184 | } |
Googler | d990a0a | 2019-05-13 16:46:42 -0700 | [diff] [blame^] | 1185 | |
| 1186 | @Test |
| 1187 | public void testBuildIncomplete() { |
| 1188 | BuildEventId buildEventId = testId("abort_expected"); |
| 1189 | BuildEvent startEvent = |
| 1190 | new GenericBuildEvent( |
| 1191 | BuildEventId.buildStartedId(), |
| 1192 | ImmutableSet.of( |
| 1193 | buildEventId, ProgressEvent.INITIAL_PROGRESS_UPDATE, BuildEventId.buildFinished())); |
| 1194 | BuildCompleteEvent buildCompleteEvent = |
| 1195 | buildCompleteEvent(ExitCode.BUILD_FAILURE, true, null, false); |
| 1196 | |
| 1197 | streamer.buildEvent(startEvent); |
| 1198 | streamer.buildEvent(buildCompleteEvent); |
| 1199 | streamer.close(); |
| 1200 | |
| 1201 | BuildEventStreamProtos.BuildEvent aborted = getBepEvent(buildEventId); |
| 1202 | assertThat(aborted).isNotNull(); |
| 1203 | assertThat(aborted.hasAborted()).isNotNull(); |
| 1204 | assertThat(aborted.getAborted().getReason()).isEqualTo(AbortReason.INCOMPLETE); |
| 1205 | assertThat(aborted.getAborted().getDescription()).isEmpty(); |
| 1206 | } |
| 1207 | |
| 1208 | @Test |
| 1209 | public void testBuildCrash() { |
| 1210 | BuildEventId buildEventId = testId("abort_expected"); |
| 1211 | BuildEvent startEvent = |
| 1212 | new GenericBuildEvent( |
| 1213 | BuildEventId.buildStartedId(), |
| 1214 | ImmutableSet.of( |
| 1215 | buildEventId, ProgressEvent.INITIAL_PROGRESS_UPDATE, BuildEventId.buildFinished())); |
| 1216 | BuildCompleteEvent buildCompleteEvent = |
| 1217 | buildCompleteEvent(ExitCode.BUILD_FAILURE, true, new RuntimeException(), false); |
| 1218 | |
| 1219 | streamer.buildEvent(startEvent); |
| 1220 | streamer.buildEvent(buildCompleteEvent); |
| 1221 | streamer.close(); |
| 1222 | |
| 1223 | BuildEventStreamProtos.BuildEvent aborted = getBepEvent(buildEventId); |
| 1224 | assertThat(aborted).isNotNull(); |
| 1225 | assertThat(aborted.hasAborted()).isNotNull(); |
| 1226 | assertThat(aborted.getAborted().getReason()).isEqualTo(AbortReason.INTERNAL); |
| 1227 | assertThat(aborted.getAborted().getDescription()).isEmpty(); |
| 1228 | } |
| 1229 | |
| 1230 | @Test |
| 1231 | public void testBuildCatastrophe() { |
| 1232 | BuildEventId buildEventId = testId("abort_expected"); |
| 1233 | BuildEvent startEvent = |
| 1234 | new GenericBuildEvent( |
| 1235 | BuildEventId.buildStartedId(), |
| 1236 | ImmutableSet.of( |
| 1237 | buildEventId, ProgressEvent.INITIAL_PROGRESS_UPDATE, BuildEventId.buildFinished())); |
| 1238 | BuildCompleteEvent buildCompleteEvent = |
| 1239 | buildCompleteEvent(ExitCode.BUILD_FAILURE, true, null, true); |
| 1240 | |
| 1241 | streamer.buildEvent(startEvent); |
| 1242 | streamer.buildEvent(buildCompleteEvent); |
| 1243 | streamer.close(); |
| 1244 | |
| 1245 | BuildEventStreamProtos.BuildEvent aborted = getBepEvent(buildEventId); |
| 1246 | assertThat(aborted).isNotNull(); |
| 1247 | assertThat(aborted.hasAborted()).isNotNull(); |
| 1248 | assertThat(aborted.getAborted().getReason()).isEqualTo(AbortReason.INTERNAL); |
| 1249 | assertThat(aborted.getAborted().getDescription()).isEmpty(); |
| 1250 | } |
| 1251 | |
| 1252 | @Test |
| 1253 | public void testStreamAbortedWithTimeout() { |
| 1254 | BuildEventId buildEventId = testId("abort_expected"); |
| 1255 | BuildEvent startEvent = |
| 1256 | new GenericBuildEvent( |
| 1257 | BuildEventId.buildStartedId(), |
| 1258 | ImmutableSet.of( |
| 1259 | buildEventId, ProgressEvent.INITIAL_PROGRESS_UPDATE, BuildEventId.buildFinished())); |
| 1260 | |
| 1261 | streamer.buildEvent(startEvent); |
| 1262 | streamer.close(AbortReason.TIME_OUT); |
| 1263 | |
| 1264 | BuildEventStreamProtos.BuildEvent aborted0 = getBepEvent(buildEventId); |
| 1265 | assertThat(aborted0).isNotNull(); |
| 1266 | assertThat(aborted0.hasAborted()).isNotNull(); |
| 1267 | assertThat(aborted0.getAborted().getReason()).isEqualTo(AbortReason.TIME_OUT); |
| 1268 | assertThat(aborted0.getAborted().getDescription()).isEmpty(); |
| 1269 | |
| 1270 | BuildEventStreamProtos.BuildEvent aborted1 = getBepEvent(BuildEventId.buildFinished()); |
| 1271 | assertThat(aborted1).isNotNull(); |
| 1272 | assertThat(aborted1.hasAborted()).isNotNull(); |
| 1273 | assertThat(aborted1.getAborted().getReason()).isEqualTo(AbortReason.TIME_OUT); |
| 1274 | assertThat(aborted1.getAborted().getDescription()).isEmpty(); |
| 1275 | } |
| 1276 | |
| 1277 | @Test |
| 1278 | public void testBuildFailureMultipleReasons() { |
| 1279 | BuildEventId buildEventId = testId("abort_expected"); |
| 1280 | BuildEvent startEvent = |
| 1281 | new GenericBuildEvent( |
| 1282 | BuildEventId.buildStartedId(), |
| 1283 | ImmutableSet.of( |
| 1284 | buildEventId, ProgressEvent.INITIAL_PROGRESS_UPDATE, BuildEventId.buildFinished())); |
| 1285 | BuildCompleteEvent buildCompleteEvent = |
| 1286 | buildCompleteEvent(ExitCode.BUILD_FAILURE, false, new RuntimeException(), false); |
| 1287 | |
| 1288 | streamer.buildEvent(startEvent); |
| 1289 | streamer.noAnalyze(new NoAnalyzeEvent()); |
| 1290 | streamer.buildEvent(buildCompleteEvent); |
| 1291 | streamer.close(); |
| 1292 | |
| 1293 | BuildEventStreamProtos.BuildEvent aborted = getBepEvent(buildEventId); |
| 1294 | assertThat(aborted).isNotNull(); |
| 1295 | assertThat(aborted.hasAborted()).isNotNull(); |
| 1296 | assertThat(aborted.getAborted().getReason()).isEqualTo(AbortReason.INTERNAL); |
| 1297 | assertThat(aborted.getAborted().getDescription()) |
| 1298 | .isEqualTo("Multiple abort reasons reported: [NO_ANALYZE, INTERNAL]"); |
| 1299 | } |
| 1300 | |
| 1301 | @Nullable |
| 1302 | private BuildEventStreamProtos.BuildEvent getBepEvent(BuildEventId buildEventId) { |
| 1303 | return transport.getEventProtos().stream() |
| 1304 | .filter(e -> e.getId().equals(buildEventId.asStreamProto())) |
| 1305 | .findFirst() |
| 1306 | .orElse(null); |
| 1307 | } |
| 1308 | |
| 1309 | private BuildCompleteEvent buildCompleteEvent( |
| 1310 | ExitCode exitCode, boolean stopOnFailure, Throwable crash, boolean catastrophe) { |
| 1311 | BuildResult result = new BuildResult(0); |
| 1312 | result.setExitCondition(exitCode); |
| 1313 | result.setStopOnFirstFailure(stopOnFailure); |
| 1314 | if (catastrophe) { |
| 1315 | result.setCatastrophe(); |
| 1316 | } |
| 1317 | if (crash != null) { |
| 1318 | result.setUnhandledThrowable(crash); |
| 1319 | } |
| 1320 | return new BuildCompleteEvent(result); |
| 1321 | } |
Klaus Aehlig | 17325a1 | 2016-09-30 15:45:27 +0000 | [diff] [blame] | 1322 | } |