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()));
}
}
Aggregations