use of com.thoughtworks.go.server.transaction.TestTransactionSynchronizationManager in project gocd by gocd.
the class FeatureToggleServiceTest method shouldInvalidateCacheWhenAFeatureTogglesValueIsChanged.
@Test
public void shouldInvalidateCacheWhenAFeatureTogglesValueIsChanged() throws Exception {
when(repository.availableToggles()).thenReturn(new FeatureToggles(new FeatureToggle("key1", "desc1", true)));
when(repository.userToggles()).thenReturn(new FeatureToggles());
FeatureToggleService service = new FeatureToggleService(repository, new StubGoCache(new TestTransactionSynchronizationManager()));
service.allToggles();
verify(repository, times(1)).availableToggles();
service.changeValueOfToggle("key1", false);
verify(repository, times(1)).availableToggles();
service.allToggles();
verify(repository, times(2)).availableToggles();
}
use of com.thoughtworks.go.server.transaction.TestTransactionSynchronizationManager in project gocd by gocd.
the class FeatureToggleServiceTest method shouldCacheFeatureToggleStatus.
@Test
public void shouldCacheFeatureToggleStatus() throws Exception {
when(repository.availableToggles()).thenReturn(new FeatureToggles(new FeatureToggle("key1", "desc1", true)));
when(repository.userToggles()).thenReturn(new FeatureToggles());
FeatureToggleService service = new FeatureToggleService(repository, new StubGoCache(new TestTransactionSynchronizationManager()));
service.allToggles();
service.allToggles();
service.isToggleOn("key1");
service.isToggleOn("someOtherKey");
verify(repository, times(1)).availableToggles();
}
use of com.thoughtworks.go.server.transaction.TestTransactionSynchronizationManager in project gocd by gocd.
the class PipelineServiceTest method setUp.
@Before
public void setUp() throws Exception {
pipelineTimeline = mock(PipelineTimeline.class);
pipelineDao = mock(PipelineSqlMapDao.class);
materialRepository = mock(MaterialRepository.class);
TestTransactionSynchronizationManager mockTransactionSynchronizationManager = new TestTransactionSynchronizationManager();
TransactionTemplate mockTransactionTemplate = new TestTransactionTemplate(mockTransactionSynchronizationManager);
service = new PipelineService(pipelineDao, mock(StageService.class), mock(PipelineLockService.class), pipelineTimeline, materialRepository, mockTransactionTemplate, systemEnvironment, null, materialConfigConverter);
first = oneModifiedFile("1");
third = oneModifiedFile("3");
second = oneModifiedFile("2");
first.setId(1);
third.setId(3);
second.setId(2);
}
use of com.thoughtworks.go.server.transaction.TestTransactionSynchronizationManager in project gocd by gocd.
the class StageSqlMapDaoTest method setUp.
@Before
public void setUp() {
goCache = new StubGoCache(new TestTransactionSynchronizationManager());
sqlMapClientTemplate = mock(SqlMapClientTemplate.class);
stageSqlMapDao = new StageSqlMapDao(mock(JobInstanceSqlMapDao.class), new Cache(true, false, false), mock(TransactionTemplate.class), mock(SqlMapClient.class), goCache, mock(TransactionSynchronizationManager.class), mock(SystemEnvironment.class), null);
stageSqlMapDao.setSqlMapClientTemplate(sqlMapClientTemplate);
cloner = mock(Cloner.class);
ReflectionUtil.setField(stageSqlMapDao, "cloner", cloner);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
return invocationOnMock.getArguments()[0];
}
}).when(cloner).deepClone(anyObject());
}
use of com.thoughtworks.go.server.transaction.TestTransactionSynchronizationManager in project gocd by gocd.
the class JobRerunScheduleServiceTest method shouldSynchronizeAroundRerunJobsFlow.
@Test
public void shouldSynchronizeAroundRerunJobsFlow() throws InterruptedException {
PipelineConfig mingleConfig = PipelineConfigMother.createPipelineConfig("mingle", "build", "unit", "functional");
Pipeline pipeline = PipelineMother.passedPipelineInstance("mingle", "build", "unit");
final Stage firstStage = pipeline.getFirstStage();
stub(mingleConfig, pipeline, firstStage);
stubConfigMd5Cal("latest-md5");
final Semaphore sem = new Semaphore(1);
sem.acquire();
final ThreadLocal<Integer> requestNumber = new ThreadLocal<>();
final boolean[] firstRequestFinished = new boolean[] { false };
final boolean[] secondReqGotInAfterFirstFinished = new boolean[] { false };
schedulingChecker = new SchedulingCheckerService(null, null, null, null, null, null, null, null, null, null) {
@Override
public boolean canSchedule(OperationResult result) {
if (requestNumber.get() == 0) {
//is first request, and has lock
//now we are in the locked section, so let the other request try
sem.release();
}
if (requestNumber.get() == 1) {
//this is the second req
//was the first thread done with last bit of useful work before second came in?
secondReqGotInAfterFirstFinished[0] = firstRequestFinished[0];
}
return true;
}
@Override
public boolean canRerunStage(PipelineIdentifier pipelineIdentifier, String stageName, String username, OperationResult result) {
return true;
}
};
TestTransactionTemplate template = new TestTransactionTemplate(new TestTransactionSynchronizationManager()) {
@Override
public Object execute(TransactionCallback action) {
if (requestNumber.get() == 0) {
try {
//let the other thread try for 5 seconds
Thread.sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException();
}
}
return super.execute(action);
}
};
service = new ScheduleService(goConfigService, pipelineService, stageService, schedulingChecker, mock(PipelineScheduledTopic.class), mock(PipelineDao.class), mock(StageDao.class), mock(StageOrderService.class), securityService, pipelineScheduleQueue, jobInstanceService, mock(JobInstanceDao.class), mock(AgentAssignment.class), environmentConfigService, lockService, serverHealthService, template, mock(AgentService.class), null, timeProvider, null, null, mock(InstanceFactory.class), schedulingPerformanceLogger, elasticProfileService) {
@Override
public Stage scheduleStage(Pipeline pipeline, String stageName, String username, StageInstanceCreator creator, ErrorConditionHandler errorHandler) {
Stage stage = super.scheduleStage(pipeline, stageName, username, creator, errorHandler);
if (requestNumber.get() == 0) {
firstRequestFinished[0] = true;
}
return stage;
}
};
Thread firstReq = new Thread(new Runnable() {
public void run() {
requestNumber.set(0);
service.rerunJobs(firstStage, a("unit"), new HttpOperationResult());
}
});
Thread secondReq = new Thread(new Runnable() {
public void run() {
try {
requestNumber.set(1);
sem.acquire();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
service.rerunJobs(firstStage, a("unit"), new HttpOperationResult());
}
});
firstReq.start();
secondReq.start();
firstReq.join();
secondReq.join();
assertThat("second request should have gone-in only after first is out", secondReqGotInAfterFirstFinished[0], is(true));
}
Aggregations