Search in sources :

Example 6 with PipelineState

use of com.thoughtworks.go.domain.PipelineState in project gocd by gocd.

the class PipelineLockServiceTest method shouldAllowStageFromCurrentPipelineToBeScheduled.

@Test
public void shouldAllowStageFromCurrentPipelineToBeScheduled() throws Exception {
    Pipeline pipeline = PipelineMother.firstStageBuildingAndSecondStageScheduled("mingle", asList("dev", "ft"), asList("test"));
    when(pipelineStateDao.pipelineStateFor("mingle")).thenReturn(new PipelineState(pipeline.getName(), pipeline.getStages().get(0).getIdentifier()));
    when(goConfigService.isLockable(pipeline.getName())).thenReturn(true);
    pipelineLockService.lockIfNeeded(pipeline);
    assertThat(pipelineLockService.canScheduleStageInPipeline(pipeline.getIdentifier()), is(true));
}
Also used : PipelineState(com.thoughtworks.go.domain.PipelineState) Pipeline(com.thoughtworks.go.domain.Pipeline) Test(org.junit.Test)

Example 7 with PipelineState

use of com.thoughtworks.go.domain.PipelineState in project gocd by gocd.

the class PipelineLockServiceTest method shouldAllowStageFromAnotherPipelineIfThePipelineIsNotLockabler.

@Test
public void shouldAllowStageFromAnotherPipelineIfThePipelineIsNotLockabler() throws Exception {
    Pipeline pipeline = PipelineMother.firstStageBuildingAndSecondStageScheduled("mingle", asList("dev", "ft"), asList("test"));
    when(pipelineStateDao.pipelineStateFor("mingle")).thenReturn(new PipelineState(pipeline.getName(), new StageIdentifier(pipeline.getName(), 9999, "1.2.9999", "stage", "1")));
    when(goConfigService.isLockable(pipeline.getName())).thenReturn(false);
    pipelineLockService.lockIfNeeded(pipeline);
    assertThat(pipelineLockService.canScheduleStageInPipeline(pipeline.getIdentifier()), is(true));
}
Also used : PipelineState(com.thoughtworks.go.domain.PipelineState) StageIdentifier(com.thoughtworks.go.domain.StageIdentifier) Pipeline(com.thoughtworks.go.domain.Pipeline) Test(org.junit.Test)

Example 8 with PipelineState

use of com.thoughtworks.go.domain.PipelineState in project gocd by gocd.

the class PipelineLockServiceTest method shouldKnowIfPipelineIsLocked.

@Test
public void shouldKnowIfPipelineIsLocked() throws Exception {
    String pipelineName = "mingle";
    PipelineState pipelineState = new PipelineState(pipelineName, new StageIdentifier(pipelineName, 1, "1", "stage", "1"));
    pipelineState.lock(1);
    when(pipelineStateDao.pipelineStateFor(pipelineName)).thenReturn(pipelineState);
    assertThat(pipelineLockService.isLocked(pipelineName), is(true));
    assertThat(pipelineLockService.isLocked("twist"), is(false));
}
Also used : PipelineState(com.thoughtworks.go.domain.PipelineState) StageIdentifier(com.thoughtworks.go.domain.StageIdentifier) Test(org.junit.Test)

Example 9 with PipelineState

use of com.thoughtworks.go.domain.PipelineState in project gocd by gocd.

the class PipelineStateDaoCachingTest method lockedPipeline_shouldReturnNullIfPipelineIsNotLocked.

@Test
public void lockedPipeline_shouldReturnNullIfPipelineIsNotLocked() throws Exception {
    String pipelineName = UUID.randomUUID().toString();
    pipelineStateDao.pipelineStateFor(pipelineName);
    PipelineState actual = pipelineStateDao.pipelineStateFor(pipelineName);
    assertNull("got " + actual, actual);
    assertThat(goCache.get(pipelineStateDao.pipelineLockStateCacheKey(pipelineName)), is(NOT_LOCKED));
    verify(transactionTemplate, times(1)).execute(any(org.springframework.transaction.support.TransactionCallback.class));
}
Also used : PipelineState(com.thoughtworks.go.domain.PipelineState) TransactionCallback(org.springframework.transaction.support.TransactionCallback) Test(org.junit.Test)

Example 10 with PipelineState

use of com.thoughtworks.go.domain.PipelineState in project gocd by gocd.

the class PipelineStateDaoCachingTest method lockPipeline_ShouldSavePipelineStateAndInvalidateCache.

@Test
public void lockPipeline_ShouldSavePipelineStateAndInvalidateCache() throws Exception {
    final TransactionSynchronizationAdapter[] transactionSynchronizationAdapter = { null };
    when(transactionTemplate.execute(any(org.springframework.transaction.support.TransactionCallbackWithoutResult.class))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            org.springframework.transaction.support.TransactionCallbackWithoutResult callback = (org.springframework.transaction.support.TransactionCallbackWithoutResult) invocation.getArguments()[0];
            callback.doInTransaction(new SimpleTransactionStatus());
            transactionSynchronizationAdapter[0].afterCommit();
            return null;
        }
    });
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            TransactionSynchronizationAdapter adapter = (TransactionSynchronizationAdapter) invocation.getArguments()[0];
            transactionSynchronizationAdapter[0] = adapter;
            return null;
        }
    }).when(transactionSynchronizationManager).registerSynchronization(any(TransactionSynchronization.class));
    final Pipeline pipeline = PipelineMother.pipeline("mingle");
    PipelineState pipelineState = new PipelineState(pipeline.getName(), pipeline.getFirstStage().getIdentifier());
    when(session.load(PipelineState.class, pipeline.getId())).thenReturn(pipelineState);
    goCache.put(pipelineStateDao.pipelineLockStateCacheKey(pipeline.getName()), pipelineState);
    pipelineStateDao.lockPipeline(pipeline);
    assertThat(goCache.get(pipelineStateDao.pipelineLockStateCacheKey(pipeline.getName())), is(nullValue()));
    ArgumentCaptor<PipelineState> pipelineStateArgumentCaptor = ArgumentCaptor.forClass(PipelineState.class);
    verify(session).saveOrUpdate(pipelineStateArgumentCaptor.capture());
    PipelineState savedPipelineState = pipelineStateArgumentCaptor.getValue();
    assertThat(savedPipelineState.isLocked(), is(true));
    assertThat(savedPipelineState.getLockedByPipelineId(), is(pipeline.getId()));
}
Also used : TransactionSynchronization(org.springframework.transaction.support.TransactionSynchronization) Pipeline(com.thoughtworks.go.domain.Pipeline) PipelineState(com.thoughtworks.go.domain.PipelineState) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SimpleTransactionStatus(org.springframework.transaction.support.SimpleTransactionStatus) TransactionSynchronizationAdapter(org.springframework.transaction.support.TransactionSynchronizationAdapter) Test(org.junit.Test)

Aggregations

PipelineState (com.thoughtworks.go.domain.PipelineState)11 Test (org.junit.Test)10 Pipeline (com.thoughtworks.go.domain.Pipeline)6 StageIdentifier (com.thoughtworks.go.domain.StageIdentifier)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 SimpleTransactionStatus (org.springframework.transaction.support.SimpleTransactionStatus)4 TransactionCallback (org.springframework.transaction.support.TransactionCallback)3 Answer (org.mockito.stubbing.Answer)2 TransactionSynchronization (org.springframework.transaction.support.TransactionSynchronization)2 TransactionSynchronizationAdapter (org.springframework.transaction.support.TransactionSynchronizationAdapter)2 Stage (com.thoughtworks.go.domain.Stage)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1