Skip to content

Commit 4f7428d

Browse files
schnapsterfreyacodes
authored andcommitted
Bump dependencies and gradle (lavalink-devs#85)
* Bump deps * Bump Spring Boot to v2 * Bump gradle to v4.6 * Move publishing to lavalink client only * Adjust archive name of task that builds the jar * Fix and pimp client tests
1 parent c613929 commit 4f7428d

File tree

9 files changed

+214
-70
lines changed

9 files changed

+214
-70
lines changed

LavalinkClient/build.gradle

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,34 @@ version System.getenv('dev') == 'true' ? '-SNAPSHOT' : '2.0'
33
ext {
44
moduleName = 'Lavalink-Client'
55
}
6+
7+
apply plugin: 'maven-publish'
8+
9+
publishing {
10+
publications {
11+
mavenJava(MavenPublication) {
12+
groupId rootProject.group
13+
artifactId moduleName
14+
15+
from components.java
16+
17+
artifact sourceJar {
18+
classifier "sources"
19+
}
20+
}
21+
}
22+
}
23+
24+
task install(dependsOn: 'publishToMavenLocal')
25+
publishToMavenLocal.dependsOn 'jar'
26+
27+
test {
28+
useJUnitPlatform()
29+
30+
systemProperty("TEST_TOKEN", System.getProperty("TEST_TOKEN"))
31+
systemProperty("TEST_VOICE_CHANNEL", System.getProperty("TEST_VOICE_CHANNEL"))
32+
}
33+
634
dependencies {
735
compile group: 'com.sedmelluq', name: 'lavaplayer', version: lavaplayerVersion
836
compile group: 'org.java-websocket', name: 'Java-WebSocket', version: javaWebSocketVersion

LavalinkClient/src/test/java/lavalink/client/LavalinkTest.java

Lines changed: 124 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
import net.dv8tion.jda.core.JDABuilder;
3636
import net.dv8tion.jda.core.entities.VoiceChannel;
3737
import org.json.JSONArray;
38+
import org.json.JSONObject;
3839
import org.junit.jupiter.api.AfterAll;
39-
import org.junit.jupiter.api.Assertions;
4040
import org.junit.jupiter.api.BeforeAll;
4141
import org.junit.jupiter.api.Test;
4242
import org.slf4j.Logger;
@@ -50,10 +50,22 @@
5050
import java.util.concurrent.CountDownLatch;
5151
import java.util.concurrent.TimeUnit;
5252

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+
})
5362
class LavalinkTest {
5463

5564
private static final Logger log = LoggerFactory.getLogger(LavalinkTest.class);
5665

66+
public static final String PROPERTY_TOKEN = "TEST_TOKEN";
67+
public static final String PROPERTY_CHANNEL = "TEST_VOICE_CHANNEL";
68+
5769
private static JDA jda = null;
5870
private static Lavalink lavalink = null;
5971
private static final String[] BILL_WURTZ_JINGLES = {
@@ -67,37 +79,40 @@ class LavalinkTest {
6779
};
6880

6981
@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");
8298
}
8399

84100
@AfterAll
85101
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+
}
88108
}
89109

90110
@Test
91111
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);
101116
}
102117

103118
private List<AudioTrack> loadAudioTracks(String identifier) {
@@ -106,13 +121,12 @@ private List<AudioTrack> loadAudioTracks(String identifier) {
106121
.header("Authorization", "youshallnotpass")
107122
.asJson()
108123
.getBody()
109-
.getObject()
110-
.getJSONArray("tracks");
124+
.getArray();
111125

112126
ArrayList<AudioTrack> list = new ArrayList<>();
113127
trackData.forEach(o -> {
114128
try {
115-
list.add(LavalinkUtil.toAudioTrack((String) o));
129+
list.add(LavalinkUtil.toAudioTrack(((JSONObject) o).getString("track")));
116130
} catch (IOException e) {
117131
throw new RuntimeException(e);
118132
}
@@ -125,8 +139,8 @@ private List<AudioTrack> loadAudioTracks(String identifier) {
125139
}
126140

127141
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);
130144

131145
IPlayer player = lavalink.getLink(vc.getGuild()).getPlayer();
132146
CountDownLatch latch = new CountDownLatch(1);
@@ -141,11 +155,11 @@ public void onTrackStart(IPlayer player, AudioTrack track) {
141155
player.playTrack(track);
142156

143157
latch.await(5, TimeUnit.SECONDS);
144-
lavalink.getLink(vc.getGuild()).disconnect();
158+
ensureNotConnected(lavalink, vc);
145159
player.removeListener(listener);
146160
player.stopTrack();
147161

148-
Assertions.assertEquals(0, latch.getCount());
162+
assertEquals(0, latch.getCount());
149163
}
150164

151165
@Test
@@ -160,8 +174,8 @@ void vcStreamTest() throws InterruptedException {
160174

161175
@Test
162176
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);
165179

166180
IPlayer player = lavalink.getLink(vc.getGuild()).getPlayer();
167181
CountDownLatch latch = new CountDownLatch(1);
@@ -185,25 +199,26 @@ public void onTrackEnd(IPlayer player, AudioTrack track, AudioTrackEndReason end
185199
player.playTrack(loadAudioTracks("aGOFOP2BIhI").get(0));
186200

187201
latch.await(5, TimeUnit.SECONDS);
188-
lavalink.getLink(vc.getGuild()).disconnect();
202+
ensureNotConnected(lavalink, vc);
189203
player.removeListener(listener);
190204
player.stopTrack();
191205

192-
Assertions.assertEquals(0, latch.getCount());
206+
assertEquals(0, latch.getCount());
193207
}
194208

195209
@Test
196210
void testPlayback() throws InterruptedException {
197-
VoiceChannel vc = jda.getVoiceChannelById(System.getenv("TEST_VOICE_CHANNEL"));
211+
VoiceChannel vc = fetchVoiceChannel(jda, getTestVoiceChannelId());
198212
Link link = lavalink.getLink(vc.getGuild());
199-
link.connect(vc);
213+
ensureConnected(lavalink, vc);
200214

201215
IPlayer player = link.getPlayer();
202216
CountDownLatch latch = new CountDownLatch(1);
203217

204218
PlayerEventListenerAdapter listener = new PlayerEventListenerAdapter() {
205219
@Override
206220
public void onTrackEnd(IPlayer player, AudioTrack track, AudioTrackEndReason endReason) {
221+
log.info(endReason.name());
207222
if (endReason == AudioTrackEndReason.FINISHED) {
208223
latch.countDown();
209224
}
@@ -217,12 +232,82 @@ public void onTrackEnd(IPlayer player, AudioTrack track, AudioTrackEndReason end
217232
player.playTrack(loadAudioTracks(jingle).get(0));
218233

219234
latch.await(20, TimeUnit.SECONDS);
220-
link.disconnect();
235+
ensureNotConnected(lavalink, vc);
221236
player.removeListener(listener);
222237

223238
player.stopTrack();
224239

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;
226250
}
227251

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+
}
228313
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package lavalink.client;
2+
3+
import org.junit.jupiter.api.extension.ExtendWith;
4+
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
8+
/**
9+
* Created by napster on 06.03.18.
10+
*/
11+
@Retention(RetentionPolicy.RUNTIME)
12+
@ExtendWith(RequireSystemPropertyExists.class)
13+
public @interface RequireSystemProperty {
14+
15+
/**
16+
* @return an array of System property keys
17+
*/
18+
String[] value();
19+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package lavalink.client;
2+
3+
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
4+
import org.junit.jupiter.api.extension.ExecutionCondition;
5+
import org.junit.jupiter.api.extension.ExtensionContext;
6+
import org.junit.platform.commons.support.AnnotationSupport;
7+
8+
import java.util.Optional;
9+
10+
/**
11+
* Created by napster on 06.03.18.
12+
* <p>
13+
* Checks whether the required system properties have been set
14+
*/
15+
public class RequireSystemPropertyExists implements ExecutionCondition {
16+
17+
@Override
18+
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
19+
Optional<RequireSystemProperty> annotation = AnnotationSupport.findAnnotation(context.getElement(), RequireSystemProperty.class);
20+
if (annotation.isPresent()) {
21+
for (String propertyKey : annotation.get().value()) {
22+
String propertyValue = System.getProperty(propertyKey);
23+
if (propertyValue == null || propertyValue.isEmpty()) {
24+
return ConditionEvaluationResult.disabled(String.format("System property '%s' not set. Skipping test.", propertyKey));
25+
}
26+
}
27+
return ConditionEvaluationResult.enabled("All required system properties present. Continuing test.");
28+
}
29+
return ConditionEvaluationResult.enabled("No RequireSystemProperty annotation found. Continuing test.");
30+
}
31+
}

LavalinkServer/build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ ext {
99
moduleName = 'Lavalink-Server'
1010
}
1111

12-
jar {
12+
bootJar {
1313
archiveName = "Lavalink.jar"
1414
}
15-
publishToMavenLocal.dependsOn 'bootRepackage'
1615

1716
bootRun {
1817
//compiling tests during bootRun increases the likelyhood of catching broken tests locally instead of on the CI

LavalinkServer/src/main/java/lavalink/server/Launcher.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.beans.factory.annotation.Autowired;
3636
import org.springframework.beans.factory.annotation.Value;
3737
import org.springframework.boot.SpringApplication;
38+
import org.springframework.boot.WebApplicationType;
3839
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3940
import org.springframework.context.annotation.Bean;
4041
import org.springframework.context.annotation.ComponentScan;
@@ -112,7 +113,7 @@ private void turnOffSentry() {
112113

113114
public static void main(String[] args) {
114115
SpringApplication sa = new SpringApplication(Launcher.class);
115-
sa.setWebEnvironment(true);
116+
sa.setWebApplicationType(WebApplicationType.SERVLET);
116117
sa.run(args);
117118

118119
String os = System.getProperty("os.name");

0 commit comments

Comments
 (0)