Search in sources :

Example 6 with BuildStatusChangedEvent

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());
}
Also used : BuildQueue(org.jboss.pnc.coordinator.builder.BuildQueue) BuildCoordinator(org.jboss.pnc.spi.coordinator.BuildCoordinator) DefaultBuildCoordinator(org.jboss.pnc.coordinator.builder.DefaultBuildCoordinator) SystemConfig(org.jboss.pnc.common.json.moduleconfig.SystemConfig) BpmTask(org.jboss.pnc.bpm.BpmTask) TestProjectConfigurationBuilder(org.jboss.pnc.mock.model.builders.TestProjectConfigurationBuilder) BuildStatusChangedEvent(org.jboss.pnc.spi.events.BuildStatusChangedEvent) DatastoreMock(org.jboss.pnc.mock.datastore.DatastoreMock) BuildRecord(org.jboss.pnc.model.BuildRecord) BuildResultRest(org.jboss.pnc.bpm.model.BuildResultRest) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) BuildOptions(org.jboss.pnc.spi.BuildOptions) DatastoreAdapter(org.jboss.pnc.coordinator.builder.datastore.DatastoreAdapter) DefaultBuildCoordinator(org.jboss.pnc.coordinator.builder.DefaultBuildCoordinator) Test(org.junit.Test)

Example 7 with BuildStatusChangedEvent

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);
}
Also used : BuildStatusChangedEvent(org.jboss.pnc.spi.events.BuildStatusChangedEvent) TimeoutException(java.util.concurrent.TimeoutException)

Example 8 with BuildStatusChangedEvent

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);
}
Also used : BuildCoordinator(org.jboss.pnc.spi.coordinator.BuildCoordinator) Logger(org.slf4j.Logger) Arquillian(org.jboss.arquillian.junit.Arquillian) BuildCoordinator(org.jboss.pnc.spi.coordinator.BuildCoordinator) RunWith(org.junit.runner.RunWith) LoggerFactory(org.slf4j.LoggerFactory) BuildConfigurationSet(org.jboss.pnc.model.BuildConfigurationSet) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) BuildRecord(org.jboss.pnc.model.BuildRecord) BuildTask(org.jboss.pnc.spi.coordinator.BuildTask) BuildStatus(org.jboss.pnc.enums.BuildStatus) TestProjectConfigurationBuilder(org.jboss.pnc.mock.model.builders.TestProjectConfigurationBuilder) DatastoreMock(org.jboss.pnc.mock.datastore.DatastoreMock) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Consumer(java.util.function.Consumer) List(java.util.List) CoreException(org.jboss.pnc.spi.exception.CoreException) Deployment(org.jboss.arquillian.container.test.api.Deployment) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) BuildStatusChangedEvent(org.jboss.pnc.spi.events.BuildStatusChangedEvent) BuildSetTask(org.jboss.pnc.spi.coordinator.BuildSetTask) Assert(org.junit.Assert) BuildTask(org.jboss.pnc.spi.coordinator.BuildTask) TestProjectConfigurationBuilder(org.jboss.pnc.mock.model.builders.TestProjectConfigurationBuilder) BuildStatusChangedEvent(org.jboss.pnc.spi.events.BuildStatusChangedEvent) ArrayList(java.util.ArrayList) BuildRecord(org.jboss.pnc.model.BuildRecord) Test(org.junit.Test)

Example 9 with BuildStatusChangedEvent

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)));
}
Also used : BuildOptions(org.jboss.pnc.spi.BuildOptions) BuildStatusNotifications(org.jboss.pnc.coordinator.notifications.buildTask.BuildStatusNotifications) Arquillian(org.jboss.arquillian.junit.Arquillian) BuildCoordinationStatus(org.jboss.pnc.enums.BuildCoordinationStatus) LoggerFactory(org.slf4j.LoggerFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) User(org.jboss.pnc.model.User) DatastoreAdapter(org.jboss.pnc.coordinator.builder.datastore.DatastoreAdapter) BuildCoordinator(org.jboss.pnc.spi.coordinator.BuildCoordinator) BuildResult(org.jboss.pnc.spi.BuildResult) DatastoreException(org.jboss.pnc.spi.datastore.DatastoreException) Set(java.util.Set) BuildStatus(org.jboss.pnc.enums.BuildStatus) Collectors(java.util.stream.Collectors) TestProjectConfigurationBuilder(org.jboss.pnc.mock.model.builders.TestProjectConfigurationBuilder) BuildDriverResult(org.jboss.pnc.spi.builddriver.BuildDriverResult) CoreException(org.jboss.pnc.spi.exception.CoreException) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) BuildStatusChangedEvent(org.jboss.pnc.spi.events.BuildStatusChangedEvent) BuildSetStatusChangedEvent(org.jboss.pnc.spi.events.BuildSetStatusChangedEvent) Optional(java.util.Optional) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) IndyRepositoryManagerResult(org.jboss.pnc.indyrepositorymanager.IndyRepositoryManagerResult) BuildConfigSetRecord(org.jboss.pnc.model.BuildConfigSetRecord) BuildSetStatus(org.jboss.pnc.spi.BuildSetStatus) RunWith(org.junit.runner.RunWith) BuildConfigurationSet(org.jboss.pnc.model.BuildConfigurationSet) BuildSetStatusNotifications(org.jboss.pnc.coordinator.notifications.buildSetTask.BuildSetStatusNotifications) BuildCallBack(org.jboss.pnc.coordinator.notifications.buildTask.BuildCallBack) RebuildMode(org.jboss.pnc.enums.RebuildMode) BuildTask(org.jboss.pnc.spi.coordinator.BuildTask) Supplier(java.util.function.Supplier) Inject(javax.inject.Inject) HashSet(java.util.HashSet) BuildCoordinatorDeployments(org.jboss.pnc.coordinator.test.BuildCoordinatorDeployments) CompletionStatus(org.jboss.pnc.spi.coordinator.CompletionStatus) BuildTasksInitializer(org.jboss.pnc.coordinator.builder.BuildTasksInitializer) BuildSetTask(org.jboss.pnc.spi.coordinator.BuildSetTask) BuildQueue(org.jboss.pnc.coordinator.builder.BuildQueue) Event(javax.enterprise.event.Event) Logger(org.slf4j.Logger) Test(org.junit.Test) InSequence(org.jboss.arquillian.junit.InSequence) Consumer(java.util.function.Consumer) ObjectWrapper(org.jboss.pnc.common.util.ObjectWrapper) Deployment(org.jboss.arquillian.container.test.api.Deployment) Assert(org.junit.Assert) Sequence(org.jboss.pnc.common.concurrent.Sequence) Collections(java.util.Collections) User(org.jboss.pnc.model.User) BuildTask(org.jboss.pnc.spi.coordinator.BuildTask) BuildStatusChangedEvent(org.jboss.pnc.spi.events.BuildStatusChangedEvent) BuildCallBack(org.jboss.pnc.coordinator.notifications.buildTask.BuildCallBack) HashSet(java.util.HashSet) Test(org.junit.Test) InSequence(org.jboss.arquillian.junit.InSequence)

Example 10 with BuildStatusChangedEvent

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;
}
Also used : BuildOptions(org.jboss.pnc.spi.BuildOptions) BuildConfiguration(org.jboss.pnc.model.BuildConfiguration) Arrays(java.util.Arrays) BuildSetStatus(org.jboss.pnc.spi.BuildSetStatus) BuildCoordinationStatus(org.jboss.pnc.enums.BuildCoordinationStatus) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LoggerFactory(org.slf4j.LoggerFactory) BuildConfigurationSet(org.jboss.pnc.model.BuildConfigurationSet) RebuildMode(org.jboss.pnc.enums.RebuildMode) BuildTask(org.jboss.pnc.spi.coordinator.BuildTask) DatastoreMock(org.jboss.pnc.mock.datastore.DatastoreMock) Inject(javax.inject.Inject) TestCDIBuildSetStatusChangedReceiver(org.jboss.pnc.coordinator.test.event.TestCDIBuildSetStatusChangedReceiver) Assert.fail(org.junit.Assert.fail) BuildSetTask(org.jboss.pnc.spi.coordinator.BuildSetTask) BuildConflictException(org.jboss.pnc.spi.exception.BuildConflictException) Before(org.junit.Before) Logger(org.slf4j.Logger) Semaphore(java.util.concurrent.Semaphore) TestCDIBuildStatusChangedReceiver(org.jboss.pnc.coordinator.test.event.TestCDIBuildStatusChangedReceiver) Artifact(org.jboss.pnc.model.Artifact) BuildCoordinator(org.jboss.pnc.spi.coordinator.BuildCoordinator) DatastoreException(org.jboss.pnc.spi.datastore.DatastoreException) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) BuildStatus(org.jboss.pnc.enums.BuildStatus) TestProjectConfigurationBuilder(org.jboss.pnc.mock.model.builders.TestProjectConfigurationBuilder) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) List(java.util.List) MockUser(org.jboss.pnc.mock.model.MockUser) CoreException(org.jboss.pnc.spi.exception.CoreException) Dependent(javax.enterprise.context.Dependent) BuildSetStatusChangedEvent(org.jboss.pnc.spi.events.BuildSetStatusChangedEvent) BuildStatusChangedEvent(org.jboss.pnc.spi.events.BuildStatusChangedEvent) ArtifactBuilder(org.jboss.pnc.mock.model.builders.ArtifactBuilder) Collections(java.util.Collections) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) BuildOptions(org.jboss.pnc.spi.BuildOptions) BuildSetTask(org.jboss.pnc.spi.coordinator.BuildSetTask) BuildStatusChangedEvent(org.jboss.pnc.spi.events.BuildStatusChangedEvent) Semaphore(java.util.concurrent.Semaphore)

Aggregations

BuildStatusChangedEvent (org.jboss.pnc.spi.events.BuildStatusChangedEvent)17 TestProjectConfigurationBuilder (org.jboss.pnc.mock.model.builders.TestProjectConfigurationBuilder)9 BuildCoordinator (org.jboss.pnc.spi.coordinator.BuildCoordinator)9 BuildTask (org.jboss.pnc.spi.coordinator.BuildTask)9 Test (org.junit.Test)9 DatastoreMock (org.jboss.pnc.mock.datastore.DatastoreMock)8 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)7 BuildOptions (org.jboss.pnc.spi.BuildOptions)7 Inject (javax.inject.Inject)6 BuildStatus (org.jboss.pnc.enums.BuildStatus)6 BuildRecord (org.jboss.pnc.model.BuildRecord)6 BuildSetTask (org.jboss.pnc.spi.coordinator.BuildSetTask)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 List (java.util.List)5 Consumer (java.util.function.Consumer)5 BuildConfigurationSet (org.jboss.pnc.model.BuildConfigurationSet)5 CoreException (org.jboss.pnc.spi.exception.CoreException)5 Set (java.util.Set)4 Deployment (org.jboss.arquillian.container.test.api.Deployment)4