Progress: fix synchro issues + tests
[nikiroo-utils.git] / src / be / nikiroo / utils / test / ProgressTest.java
CommitLineData
86057589
NR
1package be.nikiroo.utils.test;
2
b3aad1f9 3import be.nikiroo.utils.Progress;
86057589 4
32ae2079 5class ProgressTest extends TestLauncher {
86057589
NR
6 public ProgressTest(String[] args) {
7 super("Progress reporting", args);
8
9 addSeries(new TestLauncher("Simple progress", args) {
10 {
11 addTest(new TestCase("Relative values and direct values") {
12 @Override
13 public void test() throws Exception {
14 Progress p = new Progress();
15 assertEquals(0, p.getProgress());
16 assertEquals(0, p.getRelativeProgress());
17 p.setProgress(33);
18 assertEquals(33, p.getProgress());
19 assertEquals(0.33, p.getRelativeProgress());
20 p.setMax(3);
21 p.setProgress(1);
22 assertEquals(1, p.getProgress());
23 assertEquals(
24 generateAssertMessage("0.33..",
25 p.getRelativeProgress()), true,
26 p.getRelativeProgress() >= 0.332);
27 assertEquals(
28 generateAssertMessage("0.33..",
29 p.getRelativeProgress()), true,
30 p.getRelativeProgress() <= 0.334);
31 }
32 });
33
34 addTest(new TestCase("Listeners at first level") {
35 int pg;
36
37 @Override
38 public void test() throws Exception {
39 Progress p = new Progress();
40 p.addProgressListener(new Progress.ProgressListener() {
41 public void progress(Progress progress, String name) {
42 pg = progress.getProgress();
43 }
44 });
45
46 p.setProgress(42);
47 assertEquals(42, pg);
48 p.setProgress(0);
49 assertEquals(0, pg);
50 }
51 });
52 }
53 });
54
55 addSeries(new TestLauncher("Progress with children", args) {
56 {
57 addTest(new TestCase("One child") {
58 @Override
59 public void test() throws Exception {
60 Progress p = new Progress();
61 Progress child = new Progress();
62
63 p.addProgress(child, 100);
64
65 child.setProgress(42);
66 assertEquals(42, p.getProgress());
67 }
68 });
69
70 addTest(new TestCase("Multiple children") {
71 @Override
72 public void test() throws Exception {
73 Progress p = new Progress();
74 Progress child1 = new Progress();
75 Progress child2 = new Progress();
76 Progress child3 = new Progress();
77
78 p.addProgress(child1, 20);
79 p.addProgress(child2, 60);
80 p.addProgress(child3, 20);
81
82 child1.setProgress(50);
83 assertEquals(10, p.getProgress());
84 child2.setProgress(100);
85 assertEquals(70, p.getProgress());
86 child3.setProgress(100);
87 assertEquals(90, p.getProgress());
88 child1.setProgress(100);
89 assertEquals(100, p.getProgress());
90 }
91 });
92
93 addTest(new TestCase("Listeners with children") {
94 int pg;
95
96 @Override
97 public void test() throws Exception {
da5bfa48 98 final Progress p = new Progress();
86057589
NR
99 Progress child1 = new Progress();
100 Progress child2 = new Progress();
101 p.addProgress(child1, 50);
102 p.addProgress(child2, 50);
103
104 p.addProgressListener(new Progress.ProgressListener() {
105 public void progress(Progress progress, String name) {
da5bfa48 106 pg = p.getProgress();
86057589
NR
107 }
108 });
109
110 child1.setProgress(50);
111 assertEquals(25, pg);
112 child2.setProgress(100);
113 assertEquals(75, pg);
114 child1.setProgress(100);
115 assertEquals(100, pg);
116 }
117 });
118
119 addTest(new TestCase("Listeners with children, not 1-100") {
120 int pg;
121
122 @Override
123 public void test() throws Exception {
da5bfa48 124 final Progress p = new Progress();
86057589
NR
125 p.setMax(1000);
126
127 Progress child1 = new Progress();
128 child1.setMax(2);
129
130 Progress child2 = new Progress();
131 p.addProgress(child1, 500);
132 p.addProgress(child2, 500);
133
134 p.addProgressListener(new Progress.ProgressListener() {
135 public void progress(Progress progress, String name) {
da5bfa48 136 pg = p.getProgress();
86057589
NR
137 }
138 });
139
140 child1.setProgress(1);
141 assertEquals(250, pg);
142 child2.setProgress(100);
143 assertEquals(750, pg);
144 child1.setProgress(2);
145 assertEquals(1000, pg);
146 }
147 });
148
149 addTest(new TestCase(
150 "Listeners with children, not 1-100, local progress") {
151 int pg;
152
153 @Override
154 public void test() throws Exception {
da5bfa48 155 final Progress p = new Progress();
86057589
NR
156 p.setMax(1000);
157
158 Progress child1 = new Progress();
159 child1.setMax(2);
160
161 Progress child2 = new Progress();
162 p.addProgress(child1, 400);
163 p.addProgress(child2, 400);
164 // 200 = local progress
165
166 p.addProgressListener(new Progress.ProgressListener() {
167 public void progress(Progress progress, String name) {
da5bfa48 168 pg = p.getProgress();
86057589
NR
169 }
170 });
171
172 child1.setProgress(1);
173 assertEquals(200, pg);
174 child2.setProgress(100);
175 assertEquals(600, pg);
176 p.setProgress(100);
177 assertEquals(700, pg);
178 child1.setProgress(2);
179 assertEquals(900, pg);
180 p.setProgress(200);
181 assertEquals(1000, pg);
182 }
183 });
0924c45d
NR
184
185 addTest(new TestCase("Listeners with children, multi-thread") {
186 int pg;
187 boolean decrease;
188 Object lock1 = new Object();
189 Object lock2 = new Object();
190 int currentStep1;
191 int currentStep2;
192
193 @Override
194 public void test() throws Exception {
195 final Progress p = new Progress(0, 200);
196
197 final Progress child1 = new Progress();
198 final Progress child2 = new Progress();
199 p.addProgress(child1, 100);
200 p.addProgress(child2, 100);
201
202 p.addProgressListener(new Progress.ProgressListener() {
203 public void progress(Progress progress, String name) {
204 int now = p.getProgress();
205 if (now < pg) {
206 decrease = true;
207 }
208 pg = now;
209 }
210 });
211
212 // Run 200 concurrent threads, 2 at a time allowed to
213 // make progress (each on a different child)
214 for (int i = 0; i <= 100; i++) {
215 final int step = i;
216 new Thread(new Runnable() {
217 public void run() {
218 synchronized (lock1) {
219 if (step > currentStep1) {
220 currentStep1 = step;
221 child1.setProgress(step);
222 }
223 }
224 }
225 }).start();
226
227 new Thread(new Runnable() {
228 public void run() {
229 synchronized (lock2) {
230 if (step > currentStep2) {
231 currentStep2 = step;
232 child2.setProgress(step);
233 }
234 }
235 }
236 }).start();
237 }
238
239 int i;
240 int timeout = 20; // in 1/10th of seconds
241 for (i = 0; i < timeout
242 && (currentStep1 + currentStep2) < 200; i++) {
243 Thread.sleep(100);
244 }
245
246 assertEquals("The test froze at step " + currentStep1
247 + " + " + currentStep2, true, i < timeout);
248 assertEquals(
249 "There should not have any decresing steps",
250 decrease, false);
251 assertEquals("The progress should have reached 200",
252 200, p.getProgress());
253 assertEquals(
254 "The progress should have reached completion",
255 true, p.isDone());
256 }
257 });
86057589
NR
258 }
259 });
260 }
261}