Search in sources :

Example 1 with LockingJob

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));
}
Also used : JobDataMap(org.quartz.JobDataMap) QName(org.alfresco.service.namespace.QName) JobDetail(org.quartz.JobDetail) JobExecutionException(org.quartz.JobExecutionException) LockingJob(org.alfresco.heartbeat.jobs.LockingJob) JobExecutionContext(org.quartz.JobExecutionContext) LinkedList(java.util.LinkedList) List(java.util.List) HBData(org.alfresco.heartbeat.datasender.HBData) LockAcquisitionException(org.alfresco.repo.lock.LockAcquisitionException) Test(org.junit.Test)

Example 2 with LockingJob

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));
}
Also used : JobDataMap(org.quartz.JobDataMap) JobDetail(org.quartz.JobDetail) LockingJob(org.alfresco.heartbeat.jobs.LockingJob) QName(org.alfresco.service.namespace.QName) HBBaseDataCollector(org.alfresco.heartbeat.HBBaseDataCollector) JobExecutionContext(org.quartz.JobExecutionContext) LinkedList(java.util.LinkedList) List(java.util.List) HBData(org.alfresco.heartbeat.datasender.HBData) LockAcquisitionException(org.alfresco.repo.lock.LockAcquisitionException) Test(org.junit.Test)

Aggregations

LinkedList (java.util.LinkedList)2 List (java.util.List)2 HBData (org.alfresco.heartbeat.datasender.HBData)2 LockingJob (org.alfresco.heartbeat.jobs.LockingJob)2 LockAcquisitionException (org.alfresco.repo.lock.LockAcquisitionException)2 QName (org.alfresco.service.namespace.QName)2 Test (org.junit.Test)2 JobDataMap (org.quartz.JobDataMap)2 JobDetail (org.quartz.JobDetail)2 JobExecutionContext (org.quartz.JobExecutionContext)2 HBBaseDataCollector (org.alfresco.heartbeat.HBBaseDataCollector)1 JobExecutionException (org.quartz.JobExecutionException)1