X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Ftest%2FProgressTest.java;h=be8b46a0103ac2d9b6741d2d3741b40a55362951;hb=0924c45dd84be115c6c622b075445855b13951e5;hp=2509735fbbac45f2ecad20594b1f7abd2bfb4302;hpb=86057589d5211fbad4b7cdbcd4dd0f1e3777d4c1;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/test/ProgressTest.java b/src/be/nikiroo/utils/test/ProgressTest.java index 2509735..be8b46a 100644 --- a/src/be/nikiroo/utils/test/ProgressTest.java +++ b/src/be/nikiroo/utils/test/ProgressTest.java @@ -1,8 +1,8 @@ package be.nikiroo.utils.test; -import be.nikiroo.utils.ui.Progress; +import be.nikiroo.utils.Progress; -public class ProgressTest extends TestLauncher { +class ProgressTest extends TestLauncher { public ProgressTest(String[] args) { super("Progress reporting", args); @@ -95,7 +95,7 @@ public class ProgressTest extends TestLauncher { @Override public void test() throws Exception { - Progress p = new Progress(); + final Progress p = new Progress(); Progress child1 = new Progress(); Progress child2 = new Progress(); p.addProgress(child1, 50); @@ -103,7 +103,7 @@ public class ProgressTest extends TestLauncher { p.addProgressListener(new Progress.ProgressListener() { public void progress(Progress progress, String name) { - pg = progress.getProgress(); + pg = p.getProgress(); } }); @@ -121,7 +121,7 @@ public class ProgressTest extends TestLauncher { @Override public void test() throws Exception { - Progress p = new Progress(); + final Progress p = new Progress(); p.setMax(1000); Progress child1 = new Progress(); @@ -133,7 +133,7 @@ public class ProgressTest extends TestLauncher { p.addProgressListener(new Progress.ProgressListener() { public void progress(Progress progress, String name) { - pg = progress.getProgress(); + pg = p.getProgress(); } }); @@ -152,7 +152,7 @@ public class ProgressTest extends TestLauncher { @Override public void test() throws Exception { - Progress p = new Progress(); + final Progress p = new Progress(); p.setMax(1000); Progress child1 = new Progress(); @@ -165,7 +165,7 @@ public class ProgressTest extends TestLauncher { p.addProgressListener(new Progress.ProgressListener() { public void progress(Progress progress, String name) { - pg = progress.getProgress(); + pg = p.getProgress(); } }); @@ -181,6 +181,80 @@ public class ProgressTest extends TestLauncher { assertEquals(1000, pg); } }); + + addTest(new TestCase("Listeners with children, multi-thread") { + int pg; + boolean decrease; + Object lock1 = new Object(); + Object lock2 = new Object(); + int currentStep1; + int currentStep2; + + @Override + public void test() throws Exception { + final Progress p = new Progress(0, 200); + + final Progress child1 = new Progress(); + final Progress child2 = new Progress(); + p.addProgress(child1, 100); + p.addProgress(child2, 100); + + p.addProgressListener(new Progress.ProgressListener() { + public void progress(Progress progress, String name) { + int now = p.getProgress(); + if (now < pg) { + decrease = true; + } + pg = now; + } + }); + + // Run 200 concurrent threads, 2 at a time allowed to + // make progress (each on a different child) + for (int i = 0; i <= 100; i++) { + final int step = i; + new Thread(new Runnable() { + public void run() { + synchronized (lock1) { + if (step > currentStep1) { + currentStep1 = step; + child1.setProgress(step); + } + } + } + }).start(); + + new Thread(new Runnable() { + public void run() { + synchronized (lock2) { + if (step > currentStep2) { + currentStep2 = step; + child2.setProgress(step); + } + } + } + }).start(); + } + + int i; + int timeout = 20; // in 1/10th of seconds + for (i = 0; i < timeout + && (currentStep1 + currentStep2) < 200; i++) { + Thread.sleep(100); + } + + assertEquals("The test froze at step " + currentStep1 + + " + " + currentStep2, true, i < timeout); + assertEquals( + "There should not have any decresing steps", + decrease, false); + assertEquals("The progress should have reached 200", + 200, p.getProgress()); + assertEquals( + "The progress should have reached completion", + true, p.isDone()); + } + }); } }); }