1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.job.internal

File DefaultJobExecutorTest.java

 

Code metrics

4
46
5
1
157
89
8
0.17
9.2
5
1.6

Classes

Class Line # Actions
DefaultJobExecutorTest 46 46 0% 8 3
0.9454545494.5%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.job.internal;
21   
22    import java.util.Arrays;
23   
24    import org.junit.jupiter.api.Test;
25    import org.xwiki.component.internal.ContextComponentManagerProvider;
26    import org.xwiki.job.DefaultRequest;
27    import org.xwiki.job.Job;
28    import org.xwiki.job.JobGroupPath;
29    import org.xwiki.job.event.status.JobStatus.State;
30    import org.xwiki.job.test.TestBasicGroupedJob;
31    import org.xwiki.test.annotation.ComponentList;
32    import org.xwiki.test.junit5.mockito.ComponentTest;
33    import org.xwiki.test.junit5.mockito.InjectMockComponents;
34   
35    import static org.junit.jupiter.api.Assertions.assertNull;
36    import static org.junit.jupiter.api.Assertions.assertSame;
37    import static org.junit.jupiter.api.Assertions.fail;
38   
39    /**
40    * Validate {@link DefaultJobExecutor};
41    *
42    * @version $Id: 3e261ba0279317254a5a162161002c4b176386b1 $
43    */
44    @ComponentTest
45    @ComponentList(ContextComponentManagerProvider.class)
 
46    public class DefaultJobExecutorTest
47    {
48    @InjectMockComponents
49    private DefaultJobExecutor executor;
50   
 
51  4 toggle private void waitJobWaiting(Job job)
52    {
53  4 waitJobState(State.WAITING, job);
54    }
55   
 
56  2 toggle private void waitJobFinished(Job job)
57    {
58  2 waitJobState(State.FINISHED, job);
59    }
60   
 
61  6 toggle private void waitJobState(State expected, Job job)
62    {
63  6 int wait = 0;
64   
65  6 do {
66  12 if (expected == job.getStatus().getState()) {
67  6 return;
68    }
69   
70    // Wait a bit
71  6 try {
72  6 Thread.sleep(1);
73    } catch (InterruptedException e) {
74  0 fail("Job state monitor has been interrupted");
75    }
76   
77  6 wait += 1;
78  6 } while (wait < 100);
79   
80  0 fail("Job never reached expected state [" + expected + "]. Still [" + job.getStatus().getState()
81    + "] after 100 milliseconds");
82    }
83   
 
84  1 toggle @Test
85    public void matchingGroupPathAreBlocked()
86    {
87  1 TestBasicGroupedJob jobA = groupedJob("A");
88  1 TestBasicGroupedJob jobAB = groupedJob("A", "B");
89   
90  1 TestBasicGroupedJob job12 = groupedJob("1", "2");
91  1 TestBasicGroupedJob job1 = groupedJob("1");
92   
93    // Pre-lock all jobs
94  1 jobA.lock();
95  1 jobAB.lock();
96  1 job12.lock();
97  1 job1.lock();
98   
99    // Give first jobs to JobExecutor
100  1 this.executor.execute(jobA);
101  1 this.executor.execute(job12);
102   
103    // Give enough time for the jobs to be fully taken into account
104  1 waitJobWaiting(jobA);
105  1 waitJobWaiting(job12);
106   
107    // Give following jobs to JobExecutor (to make sure they are actually after since the grouped job executor queue
108    // is not "fair")
109  1 this.executor.execute(jobAB);
110  1 this.executor.execute(job1);
111   
112    ////////////////////
113    // A and A/B
114   
115  1 assertSame(State.WAITING, jobA.getStatus().getState());
116  1 assertNull(jobAB.getStatus().getState());
117   
118    // Next job
119  1 jobA.unlock();
120  1 waitJobWaiting(jobAB);
121   
122  1 assertSame(State.FINISHED, jobA.getStatus().getState());
123  1 assertSame(State.WAITING, jobAB.getStatus().getState());
124   
125    // Next job
126  1 jobAB.unlock();
127  1 waitJobFinished(jobAB);
128   
129  1 assertSame(State.FINISHED, jobA.getStatus().getState());
130  1 assertSame(State.FINISHED, jobAB.getStatus().getState());
131   
132    ////////////////////
133    // 1/2 and 1
134   
135  1 assertSame(State.WAITING, job12.getStatus().getState());
136  1 assertNull(job1.getStatus().getState());
137   
138    // Next job
139  1 job12.unlock();
140  1 waitJobWaiting(job1);
141   
142  1 assertSame(State.FINISHED, job12.getStatus().getState());
143  1 assertSame(State.WAITING, job1.getStatus().getState());
144   
145    // Next job
146  1 job1.unlock();
147  1 waitJobFinished(job1);
148   
149  1 assertSame(State.FINISHED, job1.getStatus().getState());
150  1 assertSame(State.FINISHED, job1.getStatus().getState());
151    }
152   
 
153  4 toggle private TestBasicGroupedJob groupedJob(String... path)
154    {
155  4 return new TestBasicGroupedJob("type", new JobGroupPath(Arrays.asList(path)), new DefaultRequest());
156    }
157    }