Search in sources :

Example 6 with TestTransactionSynchronizationManager

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();
}
Also used : FeatureToggles(com.thoughtworks.go.server.domain.support.toggle.FeatureToggles) FeatureToggle(com.thoughtworks.go.server.domain.support.toggle.FeatureToggle) StubGoCache(com.thoughtworks.go.server.service.StubGoCache) TestTransactionSynchronizationManager(com.thoughtworks.go.server.transaction.TestTransactionSynchronizationManager) Test(org.junit.Test)

Example 7 with TestTransactionSynchronizationManager

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();
}
Also used : FeatureToggles(com.thoughtworks.go.server.domain.support.toggle.FeatureToggles) FeatureToggle(com.thoughtworks.go.server.domain.support.toggle.FeatureToggle) StubGoCache(com.thoughtworks.go.server.service.StubGoCache) TestTransactionSynchronizationManager(com.thoughtworks.go.server.transaction.TestTransactionSynchronizationManager) Test(org.junit.Test)

Example 8 with TestTransactionSynchronizationManager

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);
}
Also used : PipelineSqlMapDao(com.thoughtworks.go.server.dao.PipelineSqlMapDao) TestTransactionTemplate(com.thoughtworks.go.server.transaction.TestTransactionTemplate) TestTransactionTemplate(com.thoughtworks.go.server.transaction.TestTransactionTemplate) TransactionTemplate(com.thoughtworks.go.server.transaction.TransactionTemplate) TestTransactionSynchronizationManager(com.thoughtworks.go.server.transaction.TestTransactionSynchronizationManager) PipelineTimeline(com.thoughtworks.go.server.domain.PipelineTimeline) MaterialRepository(com.thoughtworks.go.server.persistence.MaterialRepository) Before(org.junit.Before)

Example 9 with TestTransactionSynchronizationManager

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());
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) StubGoCache(com.thoughtworks.go.server.service.StubGoCache) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SqlMapClientTemplate(org.springframework.orm.ibatis.SqlMapClientTemplate) Matchers.anyObject(org.mockito.Matchers.anyObject) TestTransactionSynchronizationManager(com.thoughtworks.go.server.transaction.TestTransactionSynchronizationManager) GoCache(com.thoughtworks.go.server.cache.GoCache) StubGoCache(com.thoughtworks.go.server.service.StubGoCache) Cache(com.opensymphony.oscache.base.Cache) Cloner(com.rits.cloning.Cloner) Before(org.junit.Before)

Example 10 with TestTransactionSynchronizationManager

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

Aggregations

TestTransactionSynchronizationManager (com.thoughtworks.go.server.transaction.TestTransactionSynchronizationManager)11 Before (org.junit.Before)8 TestTransactionTemplate (com.thoughtworks.go.server.transaction.TestTransactionTemplate)7 StubGoCache (com.thoughtworks.go.server.service.StubGoCache)4 Test (org.junit.Test)3 GoCache (com.thoughtworks.go.server.cache.GoCache)2 PipelineSqlMapDao (com.thoughtworks.go.server.dao.PipelineSqlMapDao)2 PipelineTimeline (com.thoughtworks.go.server.domain.PipelineTimeline)2 FeatureToggle (com.thoughtworks.go.server.domain.support.toggle.FeatureToggle)2 FeatureToggles (com.thoughtworks.go.server.domain.support.toggle.FeatureToggles)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 Cache (com.opensymphony.oscache.base.Cache)1 Cloner (com.rits.cloning.Cloner)1 JobStatusCache (com.thoughtworks.go.domain.activity.JobStatusCache)1 Localizer (com.thoughtworks.go.i18n.Localizer)1 PipelineDao (com.thoughtworks.go.server.dao.PipelineDao)1