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