-
Notifications
You must be signed in to change notification settings - Fork 188
/
AbstractTaskManagerTest.java
339 lines (298 loc) · 16.1 KB
/
AbstractTaskManagerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* Copyright (c) 2010-2018 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.task.quartzimpl;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.util.PrismTestUtil;
import com.evolveum.midpoint.repo.api.RepositoryService;
import com.evolveum.midpoint.schema.MidPointPrismContextFactory;
import com.evolveum.midpoint.schema.constants.MidPointConstants;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.task.api.TaskDebugUtil;
import com.evolveum.midpoint.task.api.TaskExecutionStatus;
import com.evolveum.midpoint.test.IntegrationTestTools;
import com.evolveum.midpoint.test.util.TestUtil;
import com.evolveum.midpoint.util.PrettyPrinter;
import com.evolveum.midpoint.util.exception.CommonException;
import com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.BeforeSuite;
import org.xml.sax.SAXException;
import javax.xml.bind.JAXBException;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import static com.evolveum.midpoint.test.IntegrationTestTools.display;
import static com.evolveum.midpoint.test.IntegrationTestTools.waitFor;
import static org.testng.AssertJUnit.*;
/**
* @author mederly
*/
public class AbstractTaskManagerTest extends AbstractTestNGSpringContextTests {
protected static final String CYCLE_TASK_HANDLER_URI = "http://midpoint.evolveum.com/test/cycle-task-handler";
protected static final String CYCLE_FINISHING_TASK_HANDLER_URI = "http://midpoint.evolveum.com/test/cycle-finishing-task-handler";
protected static final String SINGLE_TASK_HANDLER_URI = "http://midpoint.evolveum.com/test/single-task-handler";
protected static final String SINGLE_TASK_HANDLER_2_URI = "http://midpoint.evolveum.com/test/single-task-handler-2";
protected static final String SINGLE_TASK_HANDLER_3_URI = "http://midpoint.evolveum.com/test/single-task-handler-3";
protected static final String SINGLE_WB_TASK_HANDLER_URI = "http://midpoint.evolveum.com/test/single-wb-task-handler";
protected static final String PARTITIONED_WB_TASK_HANDLER_URI = "http://midpoint.evolveum.com/test/partitioned-wb-task-handler";
protected static final String PARTITIONED_WB_TASK_HANDLER_URI_1 = PARTITIONED_WB_TASK_HANDLER_URI + "#1";
protected static final String PARTITIONED_WB_TASK_HANDLER_URI_2 = PARTITIONED_WB_TASK_HANDLER_URI + "#2";
protected static final String PARTITIONED_WB_TASK_HANDLER_URI_3 = PARTITIONED_WB_TASK_HANDLER_URI + "#3";
protected static final String L1_TASK_HANDLER_URI = "http://midpoint.evolveum.com/test/l1-task-handler";
protected static final String L2_TASK_HANDLER_URI = "http://midpoint.evolveum.com/test/l2-task-handler";
protected static final String L3_TASK_HANDLER_URI = "http://midpoint.evolveum.com/test/l3-task-handler";
protected static final String WAIT_FOR_SUBTASKS_TASK_HANDLER_URI = "http://midpoint.evolveum.com/test/wait-for-subtasks-task-handler";
protected static final String PARALLEL_TASK_HANDLER_URI = "http://midpoint.evolveum.com/test/parallel-task-handler";
protected static final String LONG_TASK_HANDLER_URI = "http://midpoint.evolveum.com/test/long-task-handler";
private static final String USER_ADMINISTRATOR_FILE = "src/test/resources/common/user-administrator.xml";
// TODO make configurable. Due to a race condition there can be a small number of unoptimized complete buckets
// (it should not exceed the number of workers ... at least not by much amount :)
private static final int OPTIMIZED_BUCKETS_THRESHOLD = 8;
@Autowired protected RepositoryService repositoryService;
@Autowired protected TaskManagerQuartzImpl taskManager;
@Autowired protected PrismContext prismContext;
protected MockSingleTaskHandler singleHandler1, singleHandler2, singleHandler3;
protected MockWorkBucketsTaskHandler workBucketsTaskHandler;
protected MockWorkBucketsTaskHandler partitionedWorkBucketsTaskHandler;
protected MockSingleTaskHandler l1Handler, l2Handler, l3Handler;
protected MockSingleTaskHandler waitForSubtasksTaskHandler;
protected MockCycleTaskHandler cycleFinishingHandler;
protected MockParallelTaskHandler parallelTaskHandler;
protected MockLongTaskHandler longTaskHandler;
protected static OperationResult createResult(String test, Trace logger) {
TestUtil.displayTestTitle(test);
return new OperationResult(TestQuartzTaskManagerContract.class.getName() + ".test" + test);
}
protected void initHandlers() {
MockCycleTaskHandler cycleHandler = new MockCycleTaskHandler(false); // ordinary recurring task
taskManager.registerHandler(CYCLE_TASK_HANDLER_URI, cycleHandler);
cycleFinishingHandler = new MockCycleTaskHandler(true); // finishes the handler
taskManager.registerHandler(CYCLE_FINISHING_TASK_HANDLER_URI, cycleFinishingHandler);
singleHandler1 = new MockSingleTaskHandler("1", taskManager);
taskManager.registerHandler(SINGLE_TASK_HANDLER_URI, singleHandler1);
singleHandler2 = new MockSingleTaskHandler("2", taskManager);
taskManager.registerHandler(SINGLE_TASK_HANDLER_2_URI, singleHandler2);
singleHandler3 = new MockSingleTaskHandler("3", taskManager);
taskManager.registerHandler(SINGLE_TASK_HANDLER_3_URI, singleHandler3);
workBucketsTaskHandler = new MockWorkBucketsTaskHandler(null, taskManager);
taskManager.registerHandler(SINGLE_WB_TASK_HANDLER_URI, workBucketsTaskHandler);
new PartitionedMockWorkBucketsTaskHandlerCreator(taskManager, prismContext)
.initializeAndRegister(PARTITIONED_WB_TASK_HANDLER_URI);
partitionedWorkBucketsTaskHandler = new MockWorkBucketsTaskHandler("p", taskManager);
taskManager.registerHandler(PARTITIONED_WB_TASK_HANDLER_URI_1, partitionedWorkBucketsTaskHandler);
taskManager.registerHandler(PARTITIONED_WB_TASK_HANDLER_URI_2, partitionedWorkBucketsTaskHandler);
taskManager.registerHandler(PARTITIONED_WB_TASK_HANDLER_URI_3, partitionedWorkBucketsTaskHandler);
l1Handler = new MockSingleTaskHandler("L1", taskManager);
l2Handler = new MockSingleTaskHandler("L2", taskManager);
l3Handler = new MockSingleTaskHandler("L3", taskManager);
taskManager.registerHandler(L1_TASK_HANDLER_URI, l1Handler);
taskManager.registerHandler(L2_TASK_HANDLER_URI, l2Handler);
taskManager.registerHandler(L3_TASK_HANDLER_URI, l3Handler);
waitForSubtasksTaskHandler = new MockSingleTaskHandler("WFS", taskManager);
taskManager.registerHandler(WAIT_FOR_SUBTASKS_TASK_HANDLER_URI, waitForSubtasksTaskHandler);
parallelTaskHandler = new MockParallelTaskHandler("1", taskManager);
taskManager.registerHandler(PARALLEL_TASK_HANDLER_URI, parallelTaskHandler);
longTaskHandler = new MockLongTaskHandler("1", taskManager);
taskManager.registerHandler(LONG_TASK_HANDLER_URI, longTaskHandler);
}
@BeforeSuite
public void setup() throws SchemaException, SAXException, IOException {
PrettyPrinter.setDefaultNamespacePrefix(MidPointConstants.NS_MIDPOINT_PUBLIC_PREFIX);
PrismTestUtil.resetPrismContext(MidPointPrismContextFactory.FACTORY);
}
public void initialize() throws Exception {
initHandlers();
addObjectFromFile(USER_ADMINISTRATOR_FILE);
}
protected <T extends ObjectType> PrismObject<T> unmarshallJaxbFromFile(String filePath) throws IOException, JAXBException, SchemaException {
File file = new File(filePath);
return PrismTestUtil.parseObject(file);
}
protected <T extends ObjectType> PrismObject<T> addObjectFromFile(String filePath) throws Exception {
PrismObject<T> object = unmarshallJaxbFromFile(filePath);
System.out.println("obj: " + object.getElementName());
OperationResult result = new OperationResult(TestQuartzTaskManagerContract.class.getName() + ".addObjectFromFile");
try {
add(object, result);
} catch(ObjectAlreadyExistsException e) {
delete(object, result);
add(object, result);
}
logger.trace("Object from " + filePath + " added to repository.");
return object;
}
protected void add(PrismObject<? extends ObjectType> object, OperationResult result)
throws ObjectAlreadyExistsException, SchemaException {
if (object.canRepresent(TaskType.class)) {
taskManager.addTask((PrismObject)object, result);
} else {
repositoryService.addObject(object, null, result);
}
}
protected void delete(PrismObject<? extends ObjectType> object, OperationResult result) throws ObjectNotFoundException, SchemaException {
if (object.canRepresent(TaskType.class)) {
taskManager.deleteTask(object.getOid(), result);
} else {
repositoryService.deleteObject(ObjectType.class, object.getOid(), result); // correct?
}
}
protected void waitForTaskClose(String taskOid, OperationResult result, long timeoutInterval, long sleepInterval) throws
CommonException {
waitFor("Waiting for task to close", () -> {
Task task = taskManager.getTaskWithResult(taskOid, result);
IntegrationTestTools.display("Task while waiting for it to close", task);
return task.getExecutionStatus() == TaskExecutionStatus.CLOSED;
}, timeoutInterval, sleepInterval);
}
protected void waitForTaskRunnable(String taskOid, OperationResult result, long timeoutInterval, long sleepInterval) throws
CommonException {
waitFor("Waiting for task to become runnable", () -> {
Task task = taskManager.getTaskWithResult(taskOid, result);
IntegrationTestTools.display("Task while waiting for it to become runnable", task);
return task.getExecutionStatus() == TaskExecutionStatus.RUNNABLE;
}, timeoutInterval, sleepInterval);
}
protected void waitForTaskCloseCheckingSubtasks(String taskOid, OperationResult result, long timeoutInterval, long sleepInterval) throws
CommonException {
waitFor("Waiting for task manager to execute the task", () -> {
Task task = taskManager.getTaskWithResult(taskOid, result);
display("Task tree while waiting", TaskDebugUtil.dumpTaskTree(task, result));
if (task.getExecutionStatus() == TaskExecutionStatus.CLOSED) {
display("Task is closed, finishing waiting: " + task);
return true;
}
List<Task> subtasks = task.listSubtasksDeeply(result);
for (Task subtask : subtasks) {
if (subtask.getResultStatus() == OperationResultStatusType.FATAL_ERROR
|| subtask.getResultStatus() == OperationResultStatusType.PARTIAL_ERROR) {
display("Error detected in subtask, finishing waiting: " + subtask);
return true;
}
}
return false;
}, timeoutInterval, sleepInterval);
}
protected void waitForTaskStart(String oid, OperationResult result, long timeoutInterval, long sleepInterval) throws CommonException {
waitFor("Waiting for task manager to start the task", () -> {
Task task = taskManager.getTaskWithResult(oid, result);
IntegrationTestTools.display("Task while waiting for task manager to start the task", task);
return task.getLastRunStartTimestamp() != null && task.getLastRunStartTimestamp() != 0L;
}, timeoutInterval, sleepInterval);
}
protected void waitForTaskProgress(String taskOid, OperationResult result, long timeoutInterval, long sleepInterval,
int threshold) throws CommonException {
waitFor("Waiting for task progress reaching " + threshold, () -> {
Task task = taskManager.getTaskWithResult(taskOid, result);
IntegrationTestTools.display("Task while waiting for progress reaching " + threshold, task);
return task.getProgress() >= threshold;
}, timeoutInterval, sleepInterval);
}
protected void waitForTaskNextRun(String taskOid, OperationResult result, long timeoutInterval, long sleepInterval) throws Exception {
TaskQuartzImpl taskBefore = (TaskQuartzImpl) taskManager.getTaskWithResult(taskOid, result);
waitFor("Waiting for task manager to execute the task", () -> {
Task task = taskManager.getTaskWithResult(taskOid, result);
IntegrationTestTools.display("Task while waiting for task manager to execute the task", task);
return task.getLastRunStartTimestamp() != null &&
(taskBefore.getLastRunStartTimestamp() == null || task.getLastRunStartTimestamp() > taskBefore.getLastRunStartTimestamp());
}, timeoutInterval, sleepInterval);
}
protected void suspendAndDeleteTasks(String... oids) {
taskManager.suspendAndDeleteTasks(Arrays.asList(oids), 20000L, true, new OperationResult("dummy"));
}
protected void sleepChecked(long delay) {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
// nothing to do here
}
}
protected void assertTotalSuccessCount(int expectedCount, Collection<? extends Task> workers) {
int total = 0;
for (Task worker : workers) {
total += worker.getStoredOperationStats().getIterativeTaskInformation().getTotalSuccessCount();
}
assertEquals("Wrong total success count", expectedCount, total);
}
protected void assertNoWorkBuckets(TaskWorkStateType ws) {
assertTrue(ws == null || ws.getBucket().isEmpty());
}
protected void assertNumericBucket(WorkBucketType bucket, WorkBucketStateType state, int seqNumber, Integer start, Integer end) {
assertBucket(bucket, state, seqNumber);
AbstractWorkBucketContentType content = bucket.getContent();
assertEquals("Wrong bucket content class", NumericIntervalWorkBucketContentType.class, content.getClass());
NumericIntervalWorkBucketContentType numContent = (NumericIntervalWorkBucketContentType) content;
assertEquals("Wrong bucket start", toBig(start), numContent.getFrom());
assertEquals("Wrong bucket end", toBig(end), numContent.getTo());
}
protected void assertBucket(WorkBucketType bucket, WorkBucketStateType state, int seqNumber) {
if (state != null) {
assertEquals("Wrong bucket state", state, bucket.getState());
}
assertEquals("Wrong bucket seq number", seqNumber, bucket.getSequentialNumber());
}
protected BigInteger toBig(Integer integer) {
return integer != null ? BigInteger.valueOf(integer) : null;
}
protected void assertOptimizedCompletedBuckets(TaskQuartzImpl task) {
if (task.getWorkState() == null) {
return;
}
long completed = task.getWorkState().getBucket().stream()
.filter(b -> b.getState() == WorkBucketStateType.COMPLETE)
.count();
if (completed > OPTIMIZED_BUCKETS_THRESHOLD) {
display("Task with more than one completed bucket", task);
fail("More than one completed bucket found in task: " + completed + " in " + task);
}
}
protected int getTotalItemsProcessed(String coordinatorTaskOid) {
OperationResult result = new OperationResult("getTotalItemsProcessed");
try {
Task coordinatorTask = taskManager.getTask(coordinatorTaskOid, result);
List<Task> tasks = coordinatorTask.listSubtasks(result);
int total = 0;
for (Task task : tasks) {
OperationStatsType opStat = task.getStoredOperationStats();
if (opStat == null) {
continue;
}
IterativeTaskInformationType iti = opStat.getIterativeTaskInformation();
if (iti == null) {
continue;
}
int count = iti.getTotalSuccessCount();
display("Task " + task + ": " + count + " items processed");
total += count;
}
return total;
} catch (Throwable t) {
throw new AssertionError("Unexpected exception", t);
}
}
protected void assertNumberOfBuckets(TaskQuartzImpl task, Integer expectedNumber) {
assertEquals("Wrong # of expected buckets", expectedNumber, task.getWorkState().getNumberOfBuckets());
}
}