use of org.jboss.pnc.spi.events.BuildStatusChangedEvent in project pnc by project-ncl.
the class CancelledBuildByBpmTest method buildSingleProjectTestCase.
@Test(timeout = 5_000)
public void buildSingleProjectTestCase() throws Exception {
// given
DatastoreMock datastoreMock = new DatastoreMock();
TestProjectConfigurationBuilder configurationBuilder = new TestProjectConfigurationBuilder(datastoreMock);
DatastoreAdapter datastoreAdapter = new DatastoreAdapter(datastoreMock);
SystemConfig systemConfig = createConfiguration();
BuildQueue queue = new BuildQueue(systemConfig);
BlockingQueue<BuildStatusChangedEvent> receivedStatuses = new ArrayBlockingQueue<>(5);
Consumer<BuildStatusChangedEvent> onStatusUpdate = receivedStatuses::add;
EventListener buildStatusChangedEventNotifier = new EventListener(onStatusUpdate);
BlockingQueue<BpmTask> task = new ArrayBlockingQueue<>(5);
Consumer<BpmTask> onBpmTaskCreated = task::add;
BuildSchedulerFactory buildSchedulerFactory = new BuildSchedulerFactory(onBpmTaskCreated);
BuildCoordinator coordinator = new DefaultBuildCoordinator(datastoreAdapter, buildStatusChangedEventNotifier, null, buildSchedulerFactory, queue, systemConfig, groupBuildMapper, buildMapper);
coordinator.start();
queue.initSemaphore();
coordinator.build(configurationBuilder.buildConfigurationToCancel(1, "c1-bpm"), MockUser.newTestUser(1), new BuildOptions());
waitForStatus(receivedStatuses, BuildStatus.BUILDING);
BpmTask bpmTask = task.poll(1, TimeUnit.SECONDS);
BuildResultRest result = new BuildResultRest();
result.setCompletionStatus(CompletionStatus.CANCELLED);
// when
bpmTask.notify(BUILD_COMPLETE, result);
waitForStatus(receivedStatuses, BuildStatus.CANCELLED);
// expect
List<BuildRecord> buildRecords = datastoreMock.getBuildRecords();
Assert.assertEquals("Too many build records in datastore: " + buildRecords, 1, buildRecords.size());
BuildRecord buildRecord = buildRecords.get(0);
Assert.assertNotNull(buildRecord.getSubmitTime());
Assert.assertNotNull(buildRecord.getStartTime());
Assert.assertNotNull(buildRecord.getEndTime());
Assert.assertEquals(BuildStatus.CANCELLED, buildRecord.getStatus());
}
use of org.jboss.pnc.spi.events.BuildStatusChangedEvent in project pnc by project-ncl.
the class CancelledBuildByBpmTest method waitForStatus.
private void waitForStatus(BlockingQueue<BuildStatusChangedEvent> receivedStatuses, BuildStatus status) throws InterruptedException, TimeoutException {
BuildStatusChangedEvent statusChangedEvent = receivedStatuses.poll(1, TimeUnit.SECONDS);
if (statusChangedEvent == null) {
throw new TimeoutException("Did not received status update: " + status);
}
if (statusChangedEvent.getNewStatus().equals(status)) {
return;
}
waitForStatus(receivedStatuses, status);
}
use of org.jboss.pnc.spi.events.BuildStatusChangedEvent in project pnc by project-ncl.
the class CancelledBuildTest method buildSingleProjectTestCase.
@Test(timeout = 5_000)
public void buildSingleProjectTestCase() throws Exception {
// given
TestProjectConfigurationBuilder configurationBuilder = new TestProjectConfigurationBuilder(datastore);
BuildCoordinator coordinator = buildCoordinatorFactory.createBuildCoordinator(datastore).coordinator;
List<BuildStatusChangedEvent> receivedStatuses = new ArrayList<>();
Consumer<BuildStatusChangedEvent> onStatusUpdate = (event) -> {
receivedStatuses.add(event);
if (event.getNewStatus().equals(BuildStatus.BUILDING)) {
CompletableFuture.runAsync(() -> {
try {
// wait a bit for build execution to start
Thread.sleep(250);
coordinator.cancel(event.getBuild().getId());
} catch (CoreException | InterruptedException e) {
log.error("Unable to cancel the build.", e);
Assert.fail("Unable to cancel the build.");
}
});
}
};
// when
BuildTask buildTask = buildProject(configurationBuilder.buildConfigurationToCancel(1, "c1-java"), coordinator, onStatusUpdate);
// expect
List<BuildRecord> buildRecords = datastore.getBuildRecords();
Assert.assertEquals("Too many build records in datastore: " + buildRecords, 1, buildRecords.size());
BuildRecord buildRecord = buildRecords.get(0);
Assert.assertNotNull(buildRecord.getSubmitTime());
Assert.assertNotNull(buildRecord.getStartTime());
Assert.assertNotNull(buildRecord.getEndTime());
Assert.assertEquals(BuildStatus.CANCELLED, buildRecord.getStatus());
String buildTaskId = buildTask.getId();
assertStatusUpdateReceived(receivedStatuses, BuildStatus.BUILDING, buildTaskId);
assertStatusUpdateReceived(receivedStatuses, BuildStatus.CANCELLED, buildTaskId);
}
use of org.jboss.pnc.spi.events.BuildStatusChangedEvent in project pnc by project-ncl.
the class StatusUpdatesTest method BuildTaskCallbacksShouldBeCalled.
@Test
@InSequence(30)
public void BuildTaskCallbacksShouldBeCalled() throws DatastoreException, CoreException {
User user = User.Builder.newBuilder().id(3).username("test-user-3").build();
Set<BuildTask> buildTasks = initializeBuildTaskSet(configurationBuilder, user, (buildConfigSetRecord) -> {
}).getBuildTasks();
Set<String> tasksIds = buildTasks.stream().map((BuildTask::getId)).collect(Collectors.toSet());
Set<String> receivedUpdatesForId = new HashSet<>();
Consumer<BuildStatusChangedEvent> statusChangeEventConsumer = (statusChangedEvent) -> {
receivedUpdatesForId.add(statusChangedEvent.getBuild().getId());
};
tasksIds.forEach((id) -> buildStatusNotifications.subscribe(new BuildCallBack(id, statusChangeEventConsumer)));
buildTasks.forEach((bt) -> buildCoordinator.updateBuildTaskStatus(bt, BuildCoordinationStatus.DONE));
System.out.println("Received updates: " + receivedUpdatesForId);
tasksIds.forEach((id) -> Assert.assertTrue("Did not receive update for task " + id, receivedUpdatesForId.contains(id)));
}
use of org.jboss.pnc.spi.events.BuildStatusChangedEvent in project pnc by project-ncl.
the class ProjectBuilder method buildProjectsAndWaitForUpdates.
private BuildSetTask buildProjectsAndWaitForUpdates(BuildConfigurationSet buildConfigurationSet, BuildCoordinator buildCoordinator, int nStatusUpdates, Consumer<BuildStatusChangedEvent> onStatusUpdate, List<BuildStatusChangedEvent> receivedStatuses, List<BuildSetStatusChangedEvent> receivedSetStatuses) throws InterruptedException, CoreException {
Consumer<BuildStatusChangedEvent> onStatusUpdateInternal = (statusUpdate) -> {
log.debug("Received status change event [" + (i++) + "]: {}.", statusUpdate);
receivedStatuses.add(statusUpdate);
onStatusUpdate.accept(statusUpdate);
};
final Semaphore semaphore = registerReleaseListenersAndAcquireSemaphore(onStatusUpdateInternal, nStatusUpdates);
final Semaphore buildSetSemaphore = registerBuildSetListeners(receivedSetStatuses, BUILD_SET_STATUS_UPDATES);
BuildOptions buildOptions = new BuildOptions();
buildOptions.setRebuildMode(RebuildMode.FORCE);
BuildSetTask buildSetTask = buildCoordinator.build(buildConfigurationSet, MockUser.newTestUser(1), buildOptions);
assertBuildStartedSuccessfully(buildSetTask);
log.info("Waiting to receive all {} status updates...", nStatusUpdates);
waitForStatusUpdates(nStatusUpdates, semaphore, "");
log.debug("All status updates should be received. Semaphore has {} free entries.", semaphore.availablePermits());
log.info("Waiting to receive all {} build set status updates...", BUILD_SET_STATUS_UPDATES);
waitForStatusUpdates(BUILD_SET_STATUS_UPDATES, buildSetSemaphore, "build set task: " + buildSetTask);
log.debug("All status updates should be received. Semaphore has {} free entries.", semaphore.availablePermits());
return buildSetTask;
}
Aggregations