Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkoebert committed Dec 12, 2024
1 parent e645187 commit fc148b4
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,46 @@
@Service
@RequiredArgsConstructor
@Slf4j
public class ResilientStreamPlayer {
class ResilientStreamPlayer {

private final Mp3StreamPlayer mp3StreamPlayer;
private InputStream in;
private URL audioStreamUrl;

public void playStream(final URL audioStreamUrl) {
void playStream(final URL audioStreamUrl) {
if (audioStreamUrl == null) {
log.warn("Expect not null audioStreamUrl but was null.");
return;
}
this.audioStreamUrl = audioStreamUrl;
openNewPlayerWithNewStream();
}

@Scheduled(fixedRate = 2000)
void watchDog() {
final Status playerStatus = mp3StreamPlayer.getStatus();
switch (playerStatus) {
case PLAYING, PAUSED -> {
try {
if (in == null) {
log.warn("In stream is null");
} else if (in.available() > 0) {
log.debug("In stream is ok: {}", in.available());
// TODO use a 'magic eye tube' input
} else {
log.warn("In stream is not ok");
openNewPlayerWithNewStream();
try {
final Status playerStatus = mp3StreamPlayer.getStatus();
switch (playerStatus) {
case PLAYING, PAUSED -> {
try {
if (in == null) {
log.warn("In stream is null");
} else if (in.available() > 0) {
log.debug("In stream is ok: {}", in.available());
// TODO use a 'magic eye tube' input
} else {
log.warn("In stream is not ok");
openNewPlayerWithNewStream();
}
} catch (IOException e) {
log.warn("Can't determine stream {}", e.getMessage());
}
} catch (IOException e) {
log.warn("Can't determine stream {}", e.getMessage());
}
case STOPPED, NOT_SPECIFIED -> log.debug("Player state {}, nothing to do", playerStatus);
default -> log.info("Unsupported player state {}", playerStatus);
}
case STOPPED, NOT_SPECIFIED -> log.debug("Player state {}, nothing to do", playerStatus);
default -> log.info("Unsupported player state {}", playerStatus);
} catch (Exception e) {
log.error("Watchdog runs into an error", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.mirkoebert.simplejavaradioplayer.player;

import com.goxr3plus.streamplayer.enums.Status;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.net.URL;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@ExtendWith(SpringExtension.class)
@Import(ResilientStreamPlayer.class)
class ResilientStreamPlayerTest {

@MockitoBean
private Mp3StreamPlayer mp3StreamPlayer;
@Autowired
private ResilientStreamPlayer cut;

@SneakyThrows
@Test
void playStream() {
cut.playStream(null);

//cut.playStream(new URL("malformed url"));
cut.playStream(new URL("http://ebert-p.com"));
verify(mp3StreamPlayer).playStream(any());
}

@Test
void watchDogPlayerStatusNull() {
cut.watchDog();
}

@Test
void watchDog() {
when(mp3StreamPlayer.getStatus()).thenReturn(Status.PLAYING);
cut.watchDog();
}

@Test
void stop() {
}
}

0 comments on commit fc148b4

Please sign in to comment.