Skip to content

Commit e784ab7

Browse files
committed
Add tests for Java built-in schedulers
1 parent 655f417 commit e784ab7

File tree

2 files changed

+62
-13
lines changed

2 files changed

+62
-13
lines changed

JavaReleases/src/test/java/pl/mperor/lab/java/Java3.java

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
import javax.sound.sampled.*;
1111
import java.io.File;
1212
import java.io.IOException;
13+
import java.util.Timer;
14+
import java.util.TimerTask;
1315
import java.util.concurrent.CountDownLatch;
16+
import java.util.concurrent.TimeUnit;
17+
import java.util.concurrent.atomic.AtomicInteger;
1418

1519
/**
1620
* Java 1.3 (May 2000)
@@ -36,19 +40,43 @@ public void testJavaNamingAndDirectoryInterfaceAkaJNDILookup() throws NamingExce
3640

3741
@Test
3842
public void testJavaSoundAPI() throws UnsupportedAudioFileException, IOException, LineUnavailableException, InterruptedException {
39-
if (AudioSystem.getMixerInfo().length != 0)
40-
try (AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("src/test/resources/beep.wav"))) {
41-
Clip clip = AudioSystem.getClip();
42-
clip.open(audioInputStream);
43-
CountDownLatch latch = new CountDownLatch(1);
44-
clip.addLineListener(event -> {
45-
if (event.getType() == LineEvent.Type.STOP) {
46-
clip.close();
47-
latch.countDown();
48-
}
49-
});
50-
clip.start();
51-
latch.await();
43+
if (AudioSystem.getMixerInfo().length == 0) {
44+
return;
45+
}
46+
47+
try (AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("src/test/resources/beep.wav"))) {
48+
Clip clip = AudioSystem.getClip();
49+
clip.open(audioInputStream);
50+
CountDownLatch latch = new CountDownLatch(1);
51+
clip.addLineListener(event -> {
52+
if (event.getType() == LineEvent.Type.STOP) {
53+
clip.close();
54+
latch.countDown();
55+
}
56+
});
57+
clip.start();
58+
latch.await();
59+
}
60+
}
61+
62+
@Test
63+
public void testScheduledTask() throws InterruptedException {
64+
var schedulerCounter = new AtomicInteger(0);
65+
var latch = new CountDownLatch(2);
66+
67+
var scheduler = new TimerTask() {
68+
@Override
69+
public void run() {
70+
schedulerCounter.getAndIncrement();
71+
latch.countDown();
5272
}
73+
};
74+
var timer = new Timer();
75+
timer.scheduleAtFixedRate(scheduler, 0, 100);
76+
77+
boolean completed = latch.await(300, TimeUnit.MILLISECONDS);
78+
Assertions.assertTrue(completed, "Scheduler did not run twice in time");
79+
Assertions.assertEquals(2, schedulerCounter.get());
80+
timer.cancel();
5381
}
5482
}

JavaReleases/src/test/java/pl/mperor/lab/java/Java8.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.lang.reflect.Modifier;
1010
import java.time.*;
1111
import java.util.*;
12+
import java.util.concurrent.*;
13+
import java.util.concurrent.atomic.AtomicInteger;
1214
import java.util.function.*;
1315
import java.util.stream.Collectors;
1416
import java.util.stream.IntStream;
@@ -211,4 +213,23 @@ public void testNewDateAndTimeAPI() {
211213
Assertions.assertEquals(60, initialInstant.plusSeconds(60).getEpochSecond());
212214
}
213215

216+
@Test
217+
public void testScheduledExecutor() throws InterruptedException {
218+
var counter = new AtomicInteger(0);
219+
var latch = new CountDownLatch(2);
220+
221+
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
222+
ScheduledFuture<?> future = executor.scheduleAtFixedRate(() -> {
223+
counter.getAndIncrement();
224+
latch.countDown();
225+
}, 0, 100, TimeUnit.MILLISECONDS);
226+
227+
boolean completed = latch.await(300, TimeUnit.MILLISECONDS);
228+
Assertions.assertTrue(completed, "Task did not execute twice in time");
229+
Assertions.assertEquals(2, counter.get());
230+
231+
future.cancel(true);
232+
executor.shutdownNow();
233+
}
234+
214235
}

0 commit comments

Comments
 (0)