Search in sources :

Example 11 with PipelineState

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

the class PipelineStateDaoTest method shouldNotCorruptCacheIfSaveFailsWhileLocking.

@Test
public void shouldNotCorruptCacheIfSaveFailsWhileLocking() {
    String pipelineName = UUID.randomUUID().toString();
    Pipeline pipeline = PipelineMother.pipeline(pipelineName, new Stage());
    PipelineState pipelineState = new PipelineState(pipelineName);
    goCache.put(pipelineStateDao.pipelineLockStateCacheKey(pipelineName), pipelineState);
    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());
            return null;
        }
    });
    doThrow(new RuntimeException("could not save!")).when(session).saveOrUpdate(any(PipelineState.class));
    try {
        pipelineStateDao.lockPipeline(pipeline);
        fail("save should have thrown an exception!");
    } catch (Exception e) {
        PipelineState stateFromCache = (PipelineState) goCache.get(pipelineStateDao.pipelineLockStateCacheKey(pipelineName));
        assertThat(stateFromCache.isLocked(), is(false));
        assertThat(stateFromCache.getLockedByPipelineId(), is(0L));
        assertThat(stateFromCache.getLockedBy(), is(nullValue()));
    }
}
Also used : Pipeline(com.thoughtworks.go.domain.Pipeline) PipelineState(com.thoughtworks.go.domain.PipelineState) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Stage(com.thoughtworks.go.domain.Stage) SimpleTransactionStatus(org.springframework.transaction.support.SimpleTransactionStatus) 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