Skip to content

Commit ab42078

Browse files
authored
Add tests for Java built-in schedulers (#70)
1 parent 655f417 commit ab42078

File tree

2 files changed

+62
-15
lines changed

2 files changed

+62
-15
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/Java5.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import java.lang.reflect.Method;
1010
import java.util.Arrays;
1111
import java.util.List;
12-
import java.util.concurrent.ExecutorService;
13-
import java.util.concurrent.Executors;
12+
import java.util.concurrent.*;
13+
import java.util.concurrent.atomic.AtomicInteger;
1414
import java.util.stream.IntStream;
1515

1616
import static java.lang.Math.max;
@@ -113,6 +113,25 @@ public void testConcurrencyAPI() {
113113
TestUtils.resetSystemOut();
114114
}
115115

116+
@Test
117+
public void testScheduledExecutor() throws InterruptedException {
118+
var counter = new AtomicInteger(0);
119+
var latch = new CountDownLatch(2);
120+
121+
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
122+
ScheduledFuture<?> future = executor.scheduleAtFixedRate(() -> {
123+
counter.getAndIncrement();
124+
latch.countDown();
125+
}, 0, 100, TimeUnit.MILLISECONDS);
126+
127+
boolean completed = latch.await(300, TimeUnit.MILLISECONDS);
128+
Assertions.assertTrue(completed, "Task did not execute twice in time");
129+
Assertions.assertEquals(2, counter.get());
130+
131+
future.cancel(true);
132+
executor.shutdownNow();
133+
}
134+
116135
@Test
117136
public void testStaticImports() {
118137
Assertions.assertEquals(4, sqrt(16)); // Math.sqrt(16);

0 commit comments

Comments
 (0)