X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Ftest%2FProgressTest.java;h=be8b46a0103ac2d9b6741d2d3741b40a55362951;hb=0924c45dd84be115c6c622b075445855b13951e5;hp=ff46bd6944d6df4721858c2d012c3a3b2ea3fb40;hpb=32ae20790403c7bce40bf278db8ca0258623945c;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/test/ProgressTest.java b/src/be/nikiroo/utils/test/ProgressTest.java index ff46bd6..be8b46a 100644 --- a/src/be/nikiroo/utils/test/ProgressTest.java +++ b/src/be/nikiroo/utils/test/ProgressTest.java @@ -95,7 +95,7 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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()); + } + }); } }); }