use of org.alfresco.heartbeat.jobs.LockingJob in project alfresco-repository by Alfresco.
the class LockingJobTest method testJobInClusterNotLocked.
@Test
public void testJobInClusterNotLocked() throws Exception {
// mock the job context
JobExecutionContext mockJobExecutionContext = mock(JobExecutionContext.class);
// create the hb collector
SimpleHBDataCollector simpleCollector = spy(new SimpleHBDataCollector("simpleCollector"));
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("collector", simpleCollector);
jobDataMap.put("hbDataSenderService", mockDataSenderService);
jobDataMap.put("jobLockService", mockJobLockService);
JobDetail jobDetail = JobBuilder.newJob().setJobData(jobDataMap).ofType(LockingJob.class).build();
when(mockJobExecutionContext.getJobDetail()).thenReturn(jobDetail);
// collector job is not locked from an other collector
String lockToken = "locked";
Runnable r1 = () -> {
// if a second job tries to get the lock before we finished that will raise the exception
when(mockJobLockService.getLock(isA(QName.class), anyLong())).thenReturn(lockToken).thenThrow(new LockAcquisitionException("", ""));
try {
new LockingJob().execute(mockJobExecutionContext);
} catch (JobExecutionException e) {
//
} finally {
// when we are finished an other job can have the lock
when(mockJobLockService.getLock(isA(QName.class), anyLong())).thenReturn(lockToken);
}
};
Runnable r2 = () -> {
try {
new LockingJob().execute(mockJobExecutionContext);
} catch (JobExecutionException e) {
//
}
};
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1.start();
Thread.sleep(500);
t2.start();
// Wait for threads to finish before testing
Thread.sleep(1000);
// verify that we collected and send data but just one time
verify(simpleCollector, Mockito.times(2)).collectData();
verify(mockDataSenderService, Mockito.times(2)).sendData(any(List.class));
verify(mockDataSenderService, Mockito.times(0)).sendData(any(HBData.class));
verify(mockJobLockService, Mockito.times(2)).getLock(any(QName.class), anyLong());
verify(mockJobLockService, Mockito.times(2)).refreshLock(eq(lockToken), any(QName.class), anyLong(), any(JobLockService.JobLockRefreshCallback.class));
}
use of org.alfresco.heartbeat.jobs.LockingJob in project alfresco-repository by Alfresco.
the class LockingJobTest method testJobLocking.
@Test
public void testJobLocking() throws Exception {
HBBaseDataCollector simpleCollector = mock(HBBaseDataCollector.class);
when(simpleCollector.getCollectorId()).thenReturn("c1");
when(simpleCollector.getCronExpression()).thenReturn("0 0 0 ? * *");
// mock the job context
JobExecutionContext mockJobExecutionContext = mock(JobExecutionContext.class);
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("collector", simpleCollector);
jobDataMap.put("hbDataSenderService", mockDataSenderService);
jobDataMap.put("jobLockService", mockJobLockService);
JobDetail jobDetail = JobBuilder.newJob().setJobData(jobDataMap).ofType(LockingJob.class).build();
when(mockJobExecutionContext.getJobDetail()).thenReturn(jobDetail);
// Simulate job lock service
String lockToken = "token";
when(mockJobLockService.getLock(isA(QName.class), anyLong())).thenReturn(// first job gets the lock
lockToken).thenThrow(// second job doesn't get the lock
new LockAcquisitionException("", ""));
// Run two heart beat jobs
new LockingJob().execute(mockJobExecutionContext);
new LockingJob().execute(mockJobExecutionContext);
// Verify that the collector only collects data once, since only one job got the lock
verify(simpleCollector, Mockito.times(1)).collectData();
// Verify that data was passed to data sender
verify(mockDataSenderService, Mockito.times(1)).sendData(any(List.class));
verify(mockDataSenderService, Mockito.times(0)).sendData(any(HBData.class));
// Verify that both jobs tried to get the lock
verify(mockJobLockService, Mockito.times(2)).getLock(any(QName.class), anyLong());
// Verify that a callback was registered once
verify(mockJobLockService, Mockito.times(1)).refreshLock(eq(lockToken), any(QName.class), anyLong(), any(JobLockService.JobLockRefreshCallback.class));
}
Aggregations