Search in sources :

Example 1 with GoCache

use of com.thoughtworks.go.server.cache.GoCache in project gocd by gocd.

the class MaterialRepositoryIntegrationTest method hasPipelineEverRunWith_shouldCacheResultsForPipelineNameMaterialIdAndModificationId.

@Test
public void hasPipelineEverRunWith_shouldCacheResultsForPipelineNameMaterialIdAndModificationId() {
    HgMaterial hgMaterial = MaterialsMother.hgMaterial("hgUrl", "dest");
    MaterialRevision materialRevision = saveOneScmModification(hgMaterial, "user", "file");
    PipelineConfig pipelineConfig = PipelineMother.createPipelineConfig("mingle", new MaterialConfigs(hgMaterial.config()), "dev");
    MaterialRevisions materialRevisions = new MaterialRevisions(materialRevision);
    Pipeline pipeline = instanceFactory.createPipelineInstance(pipelineConfig, BuildCause.createManualForced(materialRevisions, Username.ANONYMOUS), new DefaultSchedulingContext(DEFAULT_APPROVED_BY), md5, new TimeProvider());
    GoCache spyGoCache = spy(goCache);
    when(spyGoCache.get(any(String.class))).thenCallRealMethod();
    Mockito.doCallRealMethod().when(spyGoCache).put(any(String.class), any(Object.class));
    repo = new MaterialRepository(sessionFactory, spyGoCache, 2, transactionSynchronizationManager, materialConfigConverter, materialExpansionService, databaseStrategy);
    pipelineSqlMapDao.save(pipeline);
    MaterialRevisions revisions = new MaterialRevisions(new MaterialRevision(hgMaterial, materialRevision.getLatestModification()));
    assertThat("should hit the db and cache it", repo.hasPipelineEverRunWith("mingle", revisions), is(true));
    assertThat("should have cached the result on the previous call", repo.hasPipelineEverRunWith("mingle", revisions), is(true));
    verify(spyGoCache, times(1)).put(any(String.class), eq(Boolean.TRUE));
}
Also used : MaterialConfigs(com.thoughtworks.go.config.materials.MaterialConfigs) PipelineConfig(com.thoughtworks.go.config.PipelineConfig) HgMaterial(com.thoughtworks.go.config.materials.mercurial.HgMaterial) GoCache(com.thoughtworks.go.server.cache.GoCache) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) Test(org.junit.Test)

Example 2 with GoCache

use of com.thoughtworks.go.server.cache.GoCache in project gocd by gocd.

the class MaterialRepositoryIntegrationTest method findModificationsSince_shouldHandleConcurrentModificationToCache.

@Test
public void findModificationsSince_shouldHandleConcurrentModificationToCache() throws InterruptedException {
    final SvnMaterial svn = MaterialsMother.svnMaterial();
    final MaterialRevision first = saveOneScmModification(svn, "user1", "file1");
    final MaterialRevision second = saveOneScmModification(svn, "user2", "file2");
    final MaterialRevision third = saveOneScmModification(svn, "user2", "file3");
    repo = new MaterialRepository(sessionFactory, goCache = new GoCache(goCache) {

        @Override
        public Object get(String key) {
            Object value = super.get(key);
            // sleep so we can have multiple threads enter the critical section
            TestUtils.sleepQuietly(200);
            return value;
        }
    }, 200, transactionSynchronizationManager, materialConfigConverter, materialExpansionService, databaseStrategy);
    Thread thread1 = new Thread(new Runnable() {

        public void run() {
            repo.findModificationsSince(svn, first);
        }
    });
    thread1.start();
    TestUtils.sleepQuietly(50);
    Thread thread2 = new Thread(new Runnable() {

        public void run() {
            repo.findModificationsSince(svn, second);
        }
    });
    thread2.start();
    thread1.join();
    thread2.join();
    assertThat(repo.cachedModifications(repo.findMaterialInstance(svn)).size(), is(3));
}
Also used : SvnMaterial(com.thoughtworks.go.config.materials.svn.SvnMaterial) GoCache(com.thoughtworks.go.server.cache.GoCache) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) Test(org.junit.Test)

Example 3 with GoCache

use of com.thoughtworks.go.server.cache.GoCache in project gocd by gocd.

the class UserSqlMapDaoTest method shouldDoADoubleCheckOfCacheBeforeLoadingFromTheDB.

@Test
public void shouldDoADoubleCheckOfCacheBeforeLoadingFromTheDB() throws Exception {
    GoCache cache = mock(GoCache.class);
    UserSqlMapDao userSqlMapDaoSpy = spy(new UserSqlMapDao(sessionFactory, transactionTemplate, cache, transactionSynchronizationManager));
    doReturn(mockHibernateTemplate).when(userSqlMapDaoSpy).hibernateTemplate();
    doReturn(10).when(mockHibernateTemplate).execute(Matchers.<HibernateCallback<Object>>any());
    Integer firstEnabledUserCount = userSqlMapDaoSpy.enabledUserCount();
    assertThat(firstEnabledUserCount, is(10));
    verify(mockHibernateTemplate, times(1)).execute(any(HibernateCallback.class));
    verify(cache, times(2)).get(UserSqlMapDao.ENABLED_USER_COUNT_CACHE_KEY);
    verify(cache, times(1)).put(UserSqlMapDao.ENABLED_USER_COUNT_CACHE_KEY, 10);
}
Also used : GoCache(com.thoughtworks.go.server.cache.GoCache) StubGoCache(com.thoughtworks.go.server.service.StubGoCache) HibernateCallback(org.springframework.orm.hibernate3.HibernateCallback) Test(org.junit.Test)

Example 4 with GoCache

use of com.thoughtworks.go.server.cache.GoCache in project gocd by gocd.

the class BaseUrlChangeListenerTest method shouldNotFlushCacheWhenBaseUrlConfigIsNotChanged.

@Test
public void shouldNotFlushCacheWhenBaseUrlConfigIsNotChanged() {
    GoCache cache = mock(GoCache.class);
    BaseUrlChangeListener listener = new BaseUrlChangeListener(new ServerSiteUrlConfig(""), new ServerSiteUrlConfig(""), cache);
    CruiseConfig newCruiseConfig = new BasicCruiseConfig();
    newCruiseConfig.setServerConfig(serverConfigWith("", ""));
    listener.onConfigChange(newCruiseConfig);
    verifyZeroInteractions(cache);
}
Also used : GoCache(com.thoughtworks.go.server.cache.GoCache) BasicCruiseConfig(com.thoughtworks.go.config.BasicCruiseConfig) CruiseConfig(com.thoughtworks.go.config.CruiseConfig) BasicCruiseConfig(com.thoughtworks.go.config.BasicCruiseConfig) ServerSiteUrlConfig(com.thoughtworks.go.domain.ServerSiteUrlConfig) Test(org.junit.Test)

Example 5 with GoCache

use of com.thoughtworks.go.server.cache.GoCache in project gocd by gocd.

the class PipelineSqlMapDaoCachingTest method findLastSuccessfulStageIdentifier_shouldEnsureOnlyOneThreadCanUpdateCacheAtATime.

@Test
public void findLastSuccessfulStageIdentifier_shouldEnsureOnlyOneThreadCanUpdateCacheAtATime() throws Exception {
    Pipeline pipelineWithOlderStage = PipelineMother.passedPipelineInstance("pipeline-name", "stage", "job");
    Stage stage = pipelineWithOlderStage.findStage("stage");
    stage.setIdentifier(new StageIdentifier("pipeline-name", 1, "stage", "1"));
    stage.setPipelineId(1L);
    when(mockTemplate.queryForObject(eq("getLastSuccessfulStageInPipeline"), any())).thenReturn(pipelineWithOlderStage);
    goCache = new GoCache(goCache) {

        @Override
        public void put(String key, Object value) {
            // sleep so we can have multiple threads try to update the cache with a value from the db
            TestUtils.sleepQuietly(1000);
            super.put(key, value);
        }
    };
    pipelineDao = new PipelineSqlMapDao(null, null, goCache, null, null, null, transactionSynchronizationManager, systemEnvironment, configFileDao, databaseStrategy);
    pipelineDao.setSqlMapClientTemplate(mockTemplate);
    final Stage newerStage = StageMother.passedStageInstance("stage", "job", "pipeline-name");
    newerStage.setPipelineId(1L);
    final StageIdentifier newerIdentifer = new StageIdentifier("pipeline-name", 1, "stage", "999999");
    newerStage.setIdentifier(newerIdentifer);
    new Thread(new Runnable() {

        public void run() {
            pipelineDao.stageStatusChanged(newerStage);
        }
    }).start();
    TestUtils.sleepQuietly(200);
    List<Thread> threads = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        final Pipeline pipeline = PipelineMother.pipeline("mingle");
        pipeline.setCounter(i + 1);
        Thread thread = new Thread(new Runnable() {

            public void run() {
                assertEquals(newerIdentifer, pipelineDao.findLastSuccessfulStageIdentifier("pipeline-name", "stage"));
            }
        }, "thread-" + i);
        threads.add(thread);
        thread.start();
    }
    for (Thread thread : threads) {
        thread.join();
    }
    verify(mockTemplate, never()).queryForObject(eq("getLastSuccessfulStageInPipeline"), any());
}
Also used : CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString) GoCache(com.thoughtworks.go.server.cache.GoCache) Test(org.junit.Test)

Aggregations

GoCache (com.thoughtworks.go.server.cache.GoCache)11 Test (org.junit.Test)7 Before (org.junit.Before)4 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)3 BasicCruiseConfig (com.thoughtworks.go.config.BasicCruiseConfig)2 CruiseConfig (com.thoughtworks.go.config.CruiseConfig)2 ServerSiteUrlConfig (com.thoughtworks.go.domain.ServerSiteUrlConfig)2 StubGoCache (com.thoughtworks.go.server.service.StubGoCache)2 TestTransactionSynchronizationManager (com.thoughtworks.go.server.transaction.TestTransactionSynchronizationManager)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 Cache (com.opensymphony.oscache.base.Cache)1 Cloner (com.rits.cloning.Cloner)1 PipelineConfig (com.thoughtworks.go.config.PipelineConfig)1 MaterialConfigs (com.thoughtworks.go.config.materials.MaterialConfigs)1 HgMaterial (com.thoughtworks.go.config.materials.mercurial.HgMaterial)1 SvnMaterial (com.thoughtworks.go.config.materials.svn.SvnMaterial)1 ConfigElementImplementationRegistry (com.thoughtworks.go.config.registry.ConfigElementImplementationRegistry)1 PipelineDao (com.thoughtworks.go.server.dao.PipelineDao)1 StageDao (com.thoughtworks.go.server.dao.StageDao)1