35
35
import net .dv8tion .jda .core .JDABuilder ;
36
36
import net .dv8tion .jda .core .entities .VoiceChannel ;
37
37
import org .json .JSONArray ;
38
+ import org .json .JSONObject ;
38
39
import org .junit .jupiter .api .AfterAll ;
39
- import org .junit .jupiter .api .Assertions ;
40
40
import org .junit .jupiter .api .BeforeAll ;
41
41
import org .junit .jupiter .api .Test ;
42
42
import org .slf4j .Logger ;
50
50
import java .util .concurrent .CountDownLatch ;
51
51
import java .util .concurrent .TimeUnit ;
52
52
53
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
54
+ import static org .junit .jupiter .api .Assertions .assertFalse ;
55
+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
56
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
57
+
58
+ @ RequireSystemProperty ({
59
+ LavalinkTest .PROPERTY_TOKEN ,
60
+ LavalinkTest .PROPERTY_CHANNEL ,
61
+ })
53
62
class LavalinkTest {
54
63
55
64
private static final Logger log = LoggerFactory .getLogger (LavalinkTest .class );
56
65
66
+ public static final String PROPERTY_TOKEN = "TEST_TOKEN" ;
67
+ public static final String PROPERTY_CHANNEL = "TEST_VOICE_CHANNEL" ;
68
+
57
69
private static JDA jda = null ;
58
70
private static Lavalink lavalink = null ;
59
71
private static final String [] BILL_WURTZ_JINGLES = {
@@ -67,37 +79,40 @@ class LavalinkTest {
67
79
};
68
80
69
81
@ BeforeAll
70
- static void setUp () {
71
- try {
72
- jda = new JDABuilder (AccountType .BOT )
73
- .setToken (System .getenv ("TEST_TOKEN" ))
74
- .addEventListener (lavalink )
75
- .buildBlocking ();
76
-
77
- lavalink = new Lavalink ("152691313123393536" , 1 , integer -> jda );
78
- lavalink .addNode (new URI ("ws://localhost" ), "youshallnotpass" );
79
- } catch (Exception e ) {
80
- throw new RuntimeException (e );
81
- }
82
+ static void setUp () throws Exception {
83
+ JDABuilder jdaBuilder = new JDABuilder (AccountType .BOT )
84
+ .setToken (getSystemProperty (PROPERTY_TOKEN ));
85
+
86
+ JDA selfId = jdaBuilder .buildAsync ();
87
+ lavalink = new Lavalink (selfId .asBot ().getApplicationInfo ().submit ().get (30 , TimeUnit .SECONDS ).getId (), 1 , integer -> jda );
88
+ selfId .shutdown ();
89
+
90
+ lavalink .addNode (new URI ("ws://localhost:5555" ), "youshallnotpass" );
91
+
92
+ jda = jdaBuilder
93
+ .addEventListener (lavalink )
94
+ .buildAsync ();
95
+
96
+ Thread .sleep (2000 );
97
+ assertTrue (lavalink .getNodes ().get (0 ).isAvailable (), "Could not connect to lavalink server" );
82
98
}
83
99
84
100
@ AfterAll
85
101
static void tearDown () {
86
- lavalink .shutdown ();
87
- jda .shutdown ();
102
+ if (lavalink != null ) {
103
+ lavalink .shutdown ();
104
+ }
105
+ if (jda != null ) {
106
+ jda .shutdown ();
107
+ }
88
108
}
89
109
90
110
@ Test
91
111
void vcJoinTest () {
92
- VoiceChannel vc = jda .getVoiceChannelById (System .getenv ("TEST_VOICE_CHANNEL" ));
93
- lavalink .getLink (vc .getGuild ()).connect (vc );
94
- try {
95
- Thread .sleep (1000 );
96
- } catch (InterruptedException e ) {
97
- throw new RuntimeException (e );
98
- }
99
-
100
- lavalink .getLink (vc .getGuild ()).disconnect ();
112
+ VoiceChannel vc = fetchVoiceChannel (jda , getTestVoiceChannelId ());
113
+ ensureConnected (lavalink , vc );
114
+ assertEquals (Link .State .CONNECTED , lavalink .getLink (vc .getGuild ()).getState (), "Failed to connect to voice channel" );
115
+ ensureNotConnected (lavalink , vc );
101
116
}
102
117
103
118
private List <AudioTrack > loadAudioTracks (String identifier ) {
@@ -106,13 +121,12 @@ private List<AudioTrack> loadAudioTracks(String identifier) {
106
121
.header ("Authorization" , "youshallnotpass" )
107
122
.asJson ()
108
123
.getBody ()
109
- .getObject ()
110
- .getJSONArray ("tracks" );
124
+ .getArray ();
111
125
112
126
ArrayList <AudioTrack > list = new ArrayList <>();
113
127
trackData .forEach (o -> {
114
128
try {
115
- list .add (LavalinkUtil .toAudioTrack ((String ) o ));
129
+ list .add (LavalinkUtil .toAudioTrack ((( JSONObject ) o ). getString ( "track" ) ));
116
130
} catch (IOException e ) {
117
131
throw new RuntimeException (e );
118
132
}
@@ -125,8 +139,8 @@ private List<AudioTrack> loadAudioTracks(String identifier) {
125
139
}
126
140
127
141
private void connectAndPlay (AudioTrack track ) throws InterruptedException {
128
- VoiceChannel vc = jda . getVoiceChannelById ( System . getenv ( "TEST_VOICE_CHANNEL" ));
129
- lavalink . getLink ( vc . getGuild ()). connect ( vc );
142
+ VoiceChannel vc = fetchVoiceChannel ( jda , getTestVoiceChannelId ( ));
143
+ ensureConnected ( lavalink , vc );
130
144
131
145
IPlayer player = lavalink .getLink (vc .getGuild ()).getPlayer ();
132
146
CountDownLatch latch = new CountDownLatch (1 );
@@ -141,11 +155,11 @@ public void onTrackStart(IPlayer player, AudioTrack track) {
141
155
player .playTrack (track );
142
156
143
157
latch .await (5 , TimeUnit .SECONDS );
144
- lavalink . getLink ( vc . getGuild ()). disconnect ( );
158
+ ensureNotConnected ( lavalink , vc );
145
159
player .removeListener (listener );
146
160
player .stopTrack ();
147
161
148
- Assertions . assertEquals (0 , latch .getCount ());
162
+ assertEquals (0 , latch .getCount ());
149
163
}
150
164
151
165
@ Test
@@ -160,8 +174,8 @@ void vcStreamTest() throws InterruptedException {
160
174
161
175
@ Test
162
176
void stopTest () throws InterruptedException {
163
- VoiceChannel vc = jda . getVoiceChannelById ( System . getenv ( "TEST_VOICE_CHANNEL" ));
164
- lavalink . getLink ( vc . getGuild ()). connect ( vc );
177
+ VoiceChannel vc = fetchVoiceChannel ( jda , getTestVoiceChannelId ( ));
178
+ ensureConnected ( lavalink , vc );
165
179
166
180
IPlayer player = lavalink .getLink (vc .getGuild ()).getPlayer ();
167
181
CountDownLatch latch = new CountDownLatch (1 );
@@ -185,25 +199,26 @@ public void onTrackEnd(IPlayer player, AudioTrack track, AudioTrackEndReason end
185
199
player .playTrack (loadAudioTracks ("aGOFOP2BIhI" ).get (0 ));
186
200
187
201
latch .await (5 , TimeUnit .SECONDS );
188
- lavalink . getLink ( vc . getGuild ()). disconnect ( );
202
+ ensureNotConnected ( lavalink , vc );
189
203
player .removeListener (listener );
190
204
player .stopTrack ();
191
205
192
- Assertions . assertEquals (0 , latch .getCount ());
206
+ assertEquals (0 , latch .getCount ());
193
207
}
194
208
195
209
@ Test
196
210
void testPlayback () throws InterruptedException {
197
- VoiceChannel vc = jda . getVoiceChannelById ( System . getenv ( "TEST_VOICE_CHANNEL" ));
211
+ VoiceChannel vc = fetchVoiceChannel ( jda , getTestVoiceChannelId ( ));
198
212
Link link = lavalink .getLink (vc .getGuild ());
199
- link . connect ( vc );
213
+ ensureConnected ( lavalink , vc );
200
214
201
215
IPlayer player = link .getPlayer ();
202
216
CountDownLatch latch = new CountDownLatch (1 );
203
217
204
218
PlayerEventListenerAdapter listener = new PlayerEventListenerAdapter () {
205
219
@ Override
206
220
public void onTrackEnd (IPlayer player , AudioTrack track , AudioTrackEndReason endReason ) {
221
+ log .info (endReason .name ());
207
222
if (endReason == AudioTrackEndReason .FINISHED ) {
208
223
latch .countDown ();
209
224
}
@@ -217,12 +232,82 @@ public void onTrackEnd(IPlayer player, AudioTrack track, AudioTrackEndReason end
217
232
player .playTrack (loadAudioTracks (jingle ).get (0 ));
218
233
219
234
latch .await (20 , TimeUnit .SECONDS );
220
- link . disconnect ( );
235
+ ensureNotConnected ( lavalink , vc );
221
236
player .removeListener (listener );
222
237
223
238
player .stopTrack ();
224
239
225
- Assertions .assertEquals (0 , latch .getCount ());
240
+ assertEquals (0 , latch .getCount ());
241
+ }
242
+
243
+ private static String getSystemProperty (String key ) {
244
+ String value = System .getProperty (key );
245
+
246
+ assertNotNull (value , "Missing system property " + key );
247
+ assertFalse (value .isEmpty (), "System property " + key + " is empty" );
248
+
249
+ return value ;
226
250
}
227
251
252
+ private static long getTestVoiceChannelId () {
253
+ return Long .parseUnsignedLong (getSystemProperty (PROPERTY_CHANNEL ));
254
+ }
255
+
256
+ private static VoiceChannel fetchVoiceChannel (JDA jda , long voiceChannelId ) {
257
+ long started = System .currentTimeMillis ();
258
+ while (jda .getStatus () != JDA .Status .CONNECTED
259
+ && System .currentTimeMillis () - started < 10000 //wait 10 sec max
260
+ && !Thread .currentThread ().isInterrupted ()) {
261
+ try {
262
+ Thread .sleep (100 );
263
+ } catch (InterruptedException e ) {
264
+ Thread .currentThread ().interrupt ();
265
+ }
266
+ }
267
+ assertEquals (JDA .Status .CONNECTED , jda .getStatus (), "Failed to connect to Discord in a reasonable amount of time" );
268
+
269
+ VoiceChannel voiceChannel = jda .getVoiceChannelById (voiceChannelId );
270
+ assertNotNull (voiceChannel , "Configured VoiceChannel not found on the configured Discord bot account" );
271
+
272
+ return voiceChannel ;
273
+ }
274
+
275
+
276
+ private static void ensureConnected (Lavalink lavalink , VoiceChannel voiceChannel ) {
277
+
278
+ Link link = lavalink .getLink (voiceChannel .getGuild ());
279
+ link .connect (voiceChannel );
280
+ long started = System .currentTimeMillis ();
281
+ while (link .getState () != Link .State .CONNECTED
282
+ && System .currentTimeMillis () - started < 10000 //wait 10 sec max
283
+ && !Thread .currentThread ().isInterrupted ()) {
284
+ try {
285
+ Thread .sleep (100 );
286
+ } catch (InterruptedException e ) {
287
+ Thread .currentThread ().interrupt ();
288
+ }
289
+ link .connect (voiceChannel );
290
+ }
291
+
292
+ assertEquals (Link .State .CONNECTED , link .getState (), "Failed to connect to voice channel in a reasonable amount of time" );
293
+ }
294
+
295
+ private static void ensureNotConnected (Lavalink lavalink , VoiceChannel voiceChannel ) {
296
+ Link link = lavalink .getLink (voiceChannel .getGuild ());
297
+ link .disconnect ();
298
+ long started = System .currentTimeMillis ();
299
+ while (link .getState () != Link .State .NOT_CONNECTED && link .getState () != Link .State .DISCONNECTING
300
+ && System .currentTimeMillis () - started < 10000 //wait 10 sec max
301
+ && !Thread .currentThread ().isInterrupted ()) {
302
+ try {
303
+ Thread .sleep (100 );
304
+ } catch (InterruptedException e ) {
305
+ Thread .currentThread ().interrupt ();
306
+ }
307
+ link .disconnect ();
308
+ }
309
+
310
+ assertTrue (link .getState () == Link .State .NOT_CONNECTED
311
+ || link .getState () == Link .State .DISCONNECTING , "Failed to disconnect from voice channel in a reasonable amount of time" );
312
+ }
228
313
}
0 commit comments