10
10
import javax .sound .sampled .*;
11
11
import java .io .File ;
12
12
import java .io .IOException ;
13
+ import java .util .Timer ;
14
+ import java .util .TimerTask ;
13
15
import java .util .concurrent .CountDownLatch ;
16
+ import java .util .concurrent .TimeUnit ;
17
+ import java .util .concurrent .atomic .AtomicInteger ;
14
18
15
19
/**
16
20
* Java 1.3 (May 2000)
@@ -36,19 +40,43 @@ public void testJavaNamingAndDirectoryInterfaceAkaJNDILookup() throws NamingExce
36
40
37
41
@ Test
38
42
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 ();
52
72
}
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 ();
53
81
}
54
82
}
0 commit comments