Skip to content

Commit

Permalink
Reuse RadioBrowser.getServerStats() instead of re-implementing it.
Browse files Browse the repository at this point in the history
  • Loading branch information
sfuhrm committed Jan 8, 2024
1 parent a61e80d commit 866012b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import java.io.IOException;
import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Comparator;
Expand Down Expand Up @@ -119,7 +118,14 @@ List<DiscoveryResult> discoverApiUrls(final List<String> apiUrls) {
Callable<DiscoveryResult> discoveryResultCallable = () -> {
long start = System.currentTimeMillis();
log.debug("Starting check for {}", apiUrl);
Stats stats = getStats(apiUrl, DEFAULT_TIMEOUT_MILLIS);
RadioBrowser radioBrowser = new RadioBrowser(
apiUrl,
DEFAULT_TIMEOUT_MILLIS,
userAgent,
proxyUri,
proxyUser,
proxyPassword);
Stats stats = radioBrowser.getServerStats();
long duration = System.currentTimeMillis() - start;
log.debug("Finished check for {}, took {} ms",
apiUrl, duration);
Expand Down Expand Up @@ -149,28 +155,6 @@ List<DiscoveryResult> discoverApiUrls(final List<String> apiUrls) {
}
}

/** Get the stats for a specific API endpoint.
* @param endpoint the API endpoint URI.
* @param timeout the timeout in millis.
* @return the stats object from the server.
*/
Stats getStats(final String endpoint, final int timeout) {
if (timeout <= 0) {
throw new IllegalArgumentException(
"timeout must be > 0, but is "
+ timeout);
}

RestDelegateJaxRsImpl rest = new RestDelegateJaxRsImpl(
URI.create(endpoint),
timeout,
proxyUri,
proxyUser,
proxyPassword,
userAgent);
return rest.get("json/stats", Stats.class);
}

/** Discovers the best performing endpoint.
* @return an optional endpoint address that can be passed to
* the {@link RadioBrowser} constructors.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,4 @@ public void discover(@Mocked Client clientMock,
Optional<String> name = endpointDiscovery.discover();
assertThat(name.isPresent(), is(true));
}

@Test
public void getStats(@Mocked Client clientMock,
@Mocked ClientBuilder clientBuilderMock,
@Mocked WebTarget webTargetMock,
@Mocked Invocation.Builder invocationBuilderMock) {
Stats myStats = new Stats();

new Expectations() {{
ClientBuilder.newBuilder(); result = clientBuilderMock;
clientBuilderMock.build(); result = clientMock;
clientMock.target(URI.create(RadioBrowser.DEFAULT_API_URL)); result = webTargetMock;
invocationBuilderMock.get(Stats.class); result = myStats;
}};

Stats stats = endpointDiscovery.getStats(RadioBrowser.DEFAULT_API_URL, 5000);
assertThat(stats, is(myStats));
}
}

0 comments on commit 866012b

Please sign in to comment.