use of com.thoughtworks.go.domain.PipelineState in project gocd by gocd.
the class PipelineStateDao method pipelineStateFor.
public PipelineState pipelineStateFor(String pipelineName) {
String cacheKey = pipelineLockStateCacheKey(pipelineName);
PipelineState pipelineState = (PipelineState) goCache.get(cacheKey);
if (pipelineState != null) {
return pipelineState.equals(PipelineState.NOT_LOCKED) ? null : pipelineState;
}
synchronized (cacheKey) {
pipelineState = (PipelineState) goCache.get(cacheKey);
if (pipelineState != null) {
return pipelineState.equals(PipelineState.NOT_LOCKED) ? null : pipelineState;
}
pipelineState = (PipelineState) transactionTemplate.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus transactionStatus) {
return sessionFactory.getCurrentSession().createCriteria(PipelineState.class).add(Restrictions.eq("pipelineName", pipelineName)).setCacheable(false).uniqueResult();
}
});
if (pipelineState != null && pipelineState.isLocked()) {
StageIdentifier lockedBy = (StageIdentifier) getSqlMapClientTemplate().queryForObject("lockedPipeline", pipelineState.getLockedByPipelineId());
pipelineState.setLockedBy(lockedBy);
}
goCache.put(cacheKey, pipelineState == null ? PipelineState.NOT_LOCKED : pipelineState);
return pipelineState;
}
}
use of com.thoughtworks.go.domain.PipelineState in project gocd by gocd.
the class PipelineStateDaoCachingTest method unlockPipeline_shouldSavePipelineStateAndInvalidateCache.
@Test
public void unlockPipeline_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());
goCache.put(pipelineStateDao.pipelineLockStateCacheKey(pipeline.getName()), pipelineState);
when(session.load(PipelineState.class, pipeline.getId())).thenReturn(pipelineState);
pipelineStateDao.unlockPipeline(pipeline.getName());
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(false));
assertThat(savedPipelineState.getLockedBy(), is(nullValue()));
}
use of com.thoughtworks.go.domain.PipelineState in project gocd by gocd.
the class PipelineStateDaoCachingTest method lockedPipeline_shouldCacheLockedPipelineStatus.
@Test
public void lockedPipeline_shouldCacheLockedPipelineStatus() throws Exception {
String pipelineName = "mingle";
when(transactionTemplate.execute(any(org.springframework.transaction.support.TransactionCallback.class))).thenReturn(new PipelineState(pipelineName, new StageIdentifier(pipelineName, 1, "1", 1L, "s1", "1")));
PipelineState pipelineState = pipelineStateDao.pipelineStateFor(pipelineName);
assertThat(goCache.get(pipelineStateDao.pipelineLockStateCacheKey(pipelineName)), is(pipelineState));
PipelineState secondAttempt = pipelineStateDao.pipelineStateFor(pipelineName);
assertSame(pipelineState, secondAttempt);
verify(transactionTemplate, times(1)).execute(any(TransactionCallback.class));
}
use of com.thoughtworks.go.domain.PipelineState in project gocd by gocd.
the class PipelineStateDaoTest method shouldNotCorruptCacheIfSaveFailsWhileUnLocking.
@Test
public void shouldNotCorruptCacheIfSaveFailsWhileUnLocking() {
String pipelineName = UUID.randomUUID().toString();
PipelineState pipelineState = new PipelineState(pipelineName);
long lockedByPipelineId = 1;
pipelineState.lock(lockedByPipelineId);
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.unlockPipeline(pipelineName);
fail("save should have thrown an exception!");
} catch (Exception e) {
PipelineState stateFromCache = (PipelineState) goCache.get(pipelineStateDao.pipelineLockStateCacheKey(pipelineName));
assertThat(stateFromCache.isLocked(), is(true));
assertThat(stateFromCache.getLockedByPipelineId(), is(lockedByPipelineId));
}
}
use of com.thoughtworks.go.domain.PipelineState in project gocd by gocd.
the class PipelineLockServiceTest method shouldNotAllowStageFromLockedPipelineToBeScheduled.
@Test
public void shouldNotAllowStageFromLockedPipelineToBeScheduled() throws Exception {
Pipeline pipeline = PipelineMother.firstStageBuildingAndSecondStageScheduled("mingle", asList("dev", "ft"), asList("test"));
PipelineState pipelineState = new PipelineState(pipeline.getName(), new StageIdentifier(pipeline.getName(), 9999, "1.2.9999", "stage", "1"));
pipelineState.lock(1);
when(pipelineStateDao.pipelineStateFor("mingle")).thenReturn(pipelineState);
when(goConfigService.isLockable(pipeline.getName())).thenReturn(true);
pipelineLockService.lockIfNeeded(pipeline);
assertThat(pipelineLockService.canScheduleStageInPipeline(pipeline.getIdentifier()), is(false));
}
Aggregations