Search in sources :

Example 6 with TestTransactionTemplate

use of com.thoughtworks.go.server.transaction.TestTransactionTemplate 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));
}
Also used : HttpOperationResult(com.thoughtworks.go.server.service.result.HttpOperationResult) TestTransactionTemplate(com.thoughtworks.go.server.transaction.TestTransactionTemplate) HttpOperationResult(com.thoughtworks.go.server.service.result.HttpOperationResult) OperationResult(com.thoughtworks.go.server.service.result.OperationResult) Semaphore(java.util.concurrent.Semaphore) TestTransactionSynchronizationManager(com.thoughtworks.go.server.transaction.TestTransactionSynchronizationManager) TransactionCallback(org.springframework.transaction.support.TransactionCallback) Test(org.junit.Test)

Example 7 with TestTransactionTemplate

use of com.thoughtworks.go.server.transaction.TestTransactionTemplate in project gocd by gocd.

the class StageServiceTest method setUp.

@Before
public void setUp() throws Exception {
    stageDao = mock(StageDao.class);
    pipelineDao = mock(PipelineDao.class);
    jobInstanceService = mock(JobInstanceService.class);
    securityService = mock(SecurityService.class);
    pipelineNames = asList(new CaseInsensitiveString("blah-pipeline"));
    user = new Username(new CaseInsensitiveString("poovan"));
    operationResult = new HttpLocalizedOperationResult();
    cruiseConfig = mock(BasicCruiseConfig.class);
    goConfigService = mock(GoConfigService.class);
    changesetService = mock(ChangesetService.class);
    goCache = mock(GoCache.class);
    transactionSynchronizationManager = new TestTransactionSynchronizationManager();
    transactionTemplate = new TestTransactionTemplate(transactionSynchronizationManager);
}
Also used : StageDao(com.thoughtworks.go.server.dao.StageDao) TestTransactionTemplate(com.thoughtworks.go.server.transaction.TestTransactionTemplate) TestTransactionSynchronizationManager(com.thoughtworks.go.server.transaction.TestTransactionSynchronizationManager) HttpLocalizedOperationResult(com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult) Username(com.thoughtworks.go.server.domain.Username) GoCache(com.thoughtworks.go.server.cache.GoCache) PipelineDao(com.thoughtworks.go.server.dao.PipelineDao) Before(org.junit.Before)

Aggregations

TestTransactionSynchronizationManager (com.thoughtworks.go.server.transaction.TestTransactionSynchronizationManager)7 TestTransactionTemplate (com.thoughtworks.go.server.transaction.TestTransactionTemplate)7 Before (org.junit.Before)6 PipelineSqlMapDao (com.thoughtworks.go.server.dao.PipelineSqlMapDao)2 PipelineTimeline (com.thoughtworks.go.server.domain.PipelineTimeline)2 SchedulingPerformanceLogger (com.thoughtworks.go.server.perf.SchedulingPerformanceLogger)2 MaterialRepository (com.thoughtworks.go.server.persistence.MaterialRepository)2 TransactionTemplate (com.thoughtworks.go.server.transaction.TransactionTemplate)2 ServerHealthService (com.thoughtworks.go.serverhealth.ServerHealthService)2 TimeProvider (com.thoughtworks.go.util.TimeProvider)2 JobStatusCache (com.thoughtworks.go.domain.activity.JobStatusCache)1 Localizer (com.thoughtworks.go.i18n.Localizer)1 GoCache (com.thoughtworks.go.server.cache.GoCache)1 PipelineDao (com.thoughtworks.go.server.dao.PipelineDao)1 StageDao (com.thoughtworks.go.server.dao.StageDao)1 Username (com.thoughtworks.go.server.domain.Username)1 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)1 HttpOperationResult (com.thoughtworks.go.server.service.result.HttpOperationResult)1 OperationResult (com.thoughtworks.go.server.service.result.OperationResult)1 Semaphore (java.util.concurrent.Semaphore)1