Skip to content

Commit b8408bb

Browse files
committed
add tests for progress package + fixes
1 parent 9eaacde commit b8408bb

File tree

5 files changed

+306
-210
lines changed

5 files changed

+306
-210
lines changed
Lines changed: 64 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,64 @@
1-
package net.lecousin.framework.progress;
2-
3-
import java.util.ArrayList;
4-
import java.util.List;
5-
6-
import net.lecousin.framework.concurrent.synch.JoinPoint;
7-
8-
/** Implementation of WorkProgress, composed of sub-WorkProgress. */
9-
public class MultiTaskProgress extends WorkProgressImpl implements WorkProgress.MultiTask {
10-
11-
/** Constructor. */
12-
public MultiTaskProgress(String text) {
13-
super(0, text);
14-
}
15-
16-
protected ArrayList<WorkProgress> tasks = new ArrayList<>();
17-
protected JoinPoint<Exception> jp = null;
18-
19-
/** Create a sub-progress for the given amount of work (this amount is added to the total amount to be done). */
20-
public WorkProgress createTaskProgress(long amount, String text) {
21-
this.amount += amount;
22-
SubWorkProgress task = new SubWorkProgress(this, amount, amount, text);
23-
synchronized (tasks) {
24-
tasks.add(task);
25-
if (jp != null) jp.addToJoin(task.getSynch());
26-
}
27-
return task;
28-
}
29-
30-
/** Add the given sub-progress as a sub-task for the given amount of work (this amount is added to the total amount to be done). */
31-
public void addTask(WorkProgress task, long amount) {
32-
this.amount += amount;
33-
synchronized (tasks) {
34-
tasks.add(task);
35-
if (jp != null) jp.addToJoin(task.getSynch());
36-
}
37-
WorkProgressUtil.propagateToParent(task, this, amount);
38-
}
39-
40-
/** Signal that no more task will be added, meaning that once all current sub-tasks are done, this WorkProgress is done. */
41-
public void endOfTasks() {
42-
jp.start();
43-
}
44-
45-
@Override
46-
public List<? extends WorkProgress> getTasks() {
47-
synchronized (tasks) {
48-
return new ArrayList<>(tasks);
49-
}
50-
}
51-
52-
/** Automatically call the done or error method of this WorkProgress once all current sub-tasks are done. */
53-
public void doneOnSubTasksDone() {
54-
if (jp != null) return;
55-
synchronized (tasks) {
56-
jp = new JoinPoint<>();
57-
for (WorkProgress task : tasks) jp.addToJoin(task.getSynch());
58-
}
59-
jp.listenInline(new Runnable() {
60-
@Override
61-
public void run() {
62-
if (jp.hasError()) error(jp.getError());
63-
else done();
64-
}
65-
});
66-
jp.start();
67-
}
68-
69-
}
1+
package net.lecousin.framework.progress;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import net.lecousin.framework.concurrent.synch.JoinPoint;
7+
8+
/** Implementation of WorkProgress, composed of sub-WorkProgress. */
9+
public class MultiTaskProgress extends WorkProgressImpl implements WorkProgress.MultiTask {
10+
11+
/** Constructor. */
12+
public MultiTaskProgress(String text) {
13+
super(0, text);
14+
}
15+
16+
protected ArrayList<WorkProgress> tasks = new ArrayList<>();
17+
protected JoinPoint<Exception> jp = null;
18+
19+
/** Create a sub-progress for the given amount of work (this amount is added to the total amount to be done). */
20+
public WorkProgress createTaskProgress(long amount, String text) {
21+
this.amount += amount;
22+
SubWorkProgress task = new SubWorkProgress(this, amount, amount, text);
23+
synchronized (tasks) {
24+
tasks.add(task);
25+
if (jp != null) jp.addToJoin(task.getSynch());
26+
}
27+
return task;
28+
}
29+
30+
/** Add the given sub-progress as a sub-task for the given amount of work (this amount is added to the total amount to be done). */
31+
public void addTask(WorkProgress task, long amount) {
32+
this.amount += amount;
33+
synchronized (tasks) {
34+
tasks.add(task);
35+
if (jp != null) jp.addToJoin(task.getSynch());
36+
}
37+
WorkProgressUtil.propagateToParent(task, this, amount);
38+
}
39+
40+
@Override
41+
public List<? extends WorkProgress> getTasks() {
42+
synchronized (tasks) {
43+
return new ArrayList<>(tasks);
44+
}
45+
}
46+
47+
/** Automatically call the done or error method of this WorkProgress once all current sub-tasks are done. */
48+
public void doneOnSubTasksDone() {
49+
if (jp != null) return;
50+
synchronized (tasks) {
51+
jp = new JoinPoint<>();
52+
for (WorkProgress task : tasks) jp.addToJoin(task.getSynch());
53+
}
54+
jp.listenInline(new Runnable() {
55+
@Override
56+
public void run() {
57+
if (jp.hasError()) error(jp.getError());
58+
else done();
59+
}
60+
});
61+
jp.start();
62+
}
63+
64+
}

net.lecousin.core/src/main/java/net/lecousin/framework/progress/WorkProgress.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,16 @@ public void run() {
9797
progress.error(subTask.getSynch().getError());
9898
else if (subTask.getSynch().isCancelled())
9999
progress.cancel(subTask.getSynch().getCancelEvent());
100-
else
100+
else {
101101
progress.progress(work - sent.get());
102+
sent.set(work);
103+
}
102104
}
103105
});
104106
if (subTask.getSynch().isUnblocked()) return;
105107
Runnable listener = () -> {
106108
long done = subTask.getPosition() * work / subTask.getAmount();
107-
if (sent.get() < done) {
109+
if (sent.get() < done && !progress.getSynch().isUnblocked()) {
108110
progress.progress(done - sent.get());
109111
sent.set(done);
110112
}

0 commit comments

Comments
 (0)