diff --git a/JavaReleases/src/test/java/pl/mperor/lab/java/Java3.java b/JavaReleases/src/test/java/pl/mperor/lab/java/Java3.java index 911f174..58ca3df 100644 --- a/JavaReleases/src/test/java/pl/mperor/lab/java/Java3.java +++ b/JavaReleases/src/test/java/pl/mperor/lab/java/Java3.java @@ -10,7 +10,11 @@ import javax.sound.sampled.*; import java.io.File; import java.io.IOException; +import java.util.Timer; +import java.util.TimerTask; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; /** * Java 1.3 (May 2000) @@ -36,19 +40,43 @@ public void testJavaNamingAndDirectoryInterfaceAkaJNDILookup() throws NamingExce @Test public void testJavaSoundAPI() throws UnsupportedAudioFileException, IOException, LineUnavailableException, InterruptedException { - if (AudioSystem.getMixerInfo().length != 0) - try (AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("src/test/resources/beep.wav"))) { - Clip clip = AudioSystem.getClip(); - clip.open(audioInputStream); - CountDownLatch latch = new CountDownLatch(1); - clip.addLineListener(event -> { - if (event.getType() == LineEvent.Type.STOP) { - clip.close(); - latch.countDown(); - } - }); - clip.start(); - latch.await(); + if (AudioSystem.getMixerInfo().length == 0) { + return; + } + + try (AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("src/test/resources/beep.wav"))) { + Clip clip = AudioSystem.getClip(); + clip.open(audioInputStream); + CountDownLatch latch = new CountDownLatch(1); + clip.addLineListener(event -> { + if (event.getType() == LineEvent.Type.STOP) { + clip.close(); + latch.countDown(); + } + }); + clip.start(); + latch.await(); + } + } + + @Test + public void testScheduledTask() throws InterruptedException { + var schedulerCounter = new AtomicInteger(0); + var latch = new CountDownLatch(2); + + var scheduler = new TimerTask() { + @Override + public void run() { + schedulerCounter.getAndIncrement(); + latch.countDown(); } + }; + var timer = new Timer(); + timer.scheduleAtFixedRate(scheduler, 0, 100); + + boolean completed = latch.await(300, TimeUnit.MILLISECONDS); + Assertions.assertTrue(completed, "Scheduler did not run twice in time"); + Assertions.assertEquals(2, schedulerCounter.get()); + timer.cancel(); } } \ No newline at end of file diff --git a/JavaReleases/src/test/java/pl/mperor/lab/java/Java5.java b/JavaReleases/src/test/java/pl/mperor/lab/java/Java5.java index f334b2e..3b42320 100644 --- a/JavaReleases/src/test/java/pl/mperor/lab/java/Java5.java +++ b/JavaReleases/src/test/java/pl/mperor/lab/java/Java5.java @@ -9,8 +9,8 @@ import java.lang.reflect.Method; import java.util.Arrays; import java.util.List; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; +import java.util.concurrent.*; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.IntStream; import static java.lang.Math.max; @@ -113,6 +113,25 @@ public void testConcurrencyAPI() { TestUtils.resetSystemOut(); } + @Test + public void testScheduledExecutor() throws InterruptedException { + var counter = new AtomicInteger(0); + var latch = new CountDownLatch(2); + + ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); + ScheduledFuture future = executor.scheduleAtFixedRate(() -> { + counter.getAndIncrement(); + latch.countDown(); + }, 0, 100, TimeUnit.MILLISECONDS); + + boolean completed = latch.await(300, TimeUnit.MILLISECONDS); + Assertions.assertTrue(completed, "Task did not execute twice in time"); + Assertions.assertEquals(2, counter.get()); + + future.cancel(true); + executor.shutdownNow(); + } + @Test public void testStaticImports() { Assertions.assertEquals(4, sqrt(16)); // Math.sqrt(16);