Skip to content

Commit

Permalink
reuse default configs in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ortex committed May 20, 2023
1 parent 100128e commit ed5d19c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,8 @@ public static void beforeAll() {
void senderAutoClosingTest() throws Exception {
RocContext context = new RocContext();

RocSenderConfig config = RocSenderConfig.builder()
.frameSampleRate(44100)
.frameChannels(ChannelSet.STEREO)
.frameEncoding(FrameEncoding.PCM_FLOAT)
.build();
@SuppressWarnings("unused")
RocSender sender = new RocSender(context, config);
RocSender sender = new RocSender(context, RocSenderTest.CONFIG);

Exception exception = assertThrows(Exception.class, context::close);
assertEquals("Error closing context", exception.getMessage()); // sender still using context
Expand All @@ -52,13 +47,8 @@ void senderAutoClosingTest() throws Exception {
@Test
void receiverAutoClosingTest() throws Exception {
RocContext context = new RocContext();
RocReceiverConfig config = RocReceiverConfig.builder()
.frameSampleRate(44100)
.frameChannels(ChannelSet.STEREO)
.frameEncoding(FrameEncoding.PCM_FLOAT)
.build();
@SuppressWarnings("unused")
RocReceiver receiver = new RocReceiver(context, config);
RocReceiver receiver = new RocReceiver(context, RocReceiverTest.CONFIG);

Exception exception = assertThrows(Exception.class, context::close);
assertEquals("Error closing context", exception.getMessage()); // receiver still using context
Expand Down
14 changes: 2 additions & 12 deletions src/test/java/org/rocstreaming/roctoolkit/RocContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ public void testCloseWithAttachedSender() {
assertThrows(Exception.class, () -> {
RocSender sender = null;
try (RocContext context = new RocContext()) {
RocSenderConfig config = RocSenderConfig.builder()
.frameSampleRate(44100)
.frameChannels(ChannelSet.STEREO)
.frameEncoding(FrameEncoding.PCM_FLOAT)
.build();
sender = new RocSender(context, config);
sender = new RocSender(context, RocSenderTest.CONFIG);
} finally {
if (sender != null) sender.close();
}
Expand All @@ -49,12 +44,7 @@ public void testCloseWithAttachedReceiver() {
assertThrows(Exception.class, () -> {
RocReceiver receiver = null;
try (RocContext context = new RocContext()) {
RocReceiverConfig config = RocReceiverConfig.builder()
.frameSampleRate(44100)
.frameChannels(ChannelSet.STEREO)
.frameEncoding(FrameEncoding.PCM_FLOAT)
.build();
receiver = new RocReceiver(context, config);
receiver = new RocReceiver(context, RocReceiverTest.CONFIG);
} finally {
if (receiver != null) receiver.close();
}
Expand Down
20 changes: 10 additions & 10 deletions src/test/java/org/rocstreaming/roctoolkit/RocReceiverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
public class RocReceiverTest {

private static final int SAMPLE_RATE = 44100;
private static final RocReceiverConfig config = RocReceiverConfig.builder()
public static final RocReceiverConfig CONFIG = RocReceiverConfig.builder()
.frameSampleRate(SAMPLE_RATE)
.frameChannels(ChannelSet.STEREO)
.frameEncoding(FrameEncoding.PCM_FLOAT)
Expand All @@ -41,7 +41,7 @@ public void afterEach() throws Exception {
public void testCreationAndDeinitialization() {
assertDoesNotThrow(() -> {
//noinspection EmptyTryBlock
try (RocReceiver ignored = new RocReceiver(context, config)) {
try (RocReceiver ignored = new RocReceiver(context, CONFIG)) {
}
});
}
Expand Down Expand Up @@ -75,7 +75,7 @@ private static Stream<Arguments> testInvalidCreationArguments() throws Exception
"context must not be null",
IllegalArgumentException.class,
null,
config),
CONFIG),
Arguments.of(
"config must not be null",
IllegalArgumentException.class,
Expand Down Expand Up @@ -114,7 +114,7 @@ private static Stream<Arguments> testInvalidSetMulticastGroupArguments() {
@ParameterizedTest
@MethodSource("testInvalidSetMulticastGroupArguments")
public void testInvalidSetMulticastGroup(String errorMessage, Slot slot, Interface iface, String ip) throws Exception {
try (RocReceiver receiver = new RocReceiver(context, config)) {
try (RocReceiver receiver = new RocReceiver(context, CONFIG)) {
IllegalArgumentException exception = assertThrows(
IllegalArgumentException.class,
() -> receiver.setMulticastGroup(slot, iface, ip)
Expand All @@ -125,7 +125,7 @@ public void testInvalidSetMulticastGroup(String errorMessage, Slot slot, Interfa

@Test
public void testSetMulticastGroup() throws Exception {
try (RocReceiver receiver = new RocReceiver(context, config)) {
try (RocReceiver receiver = new RocReceiver(context, CONFIG)) {
assertDoesNotThrow(() -> {
receiver.setMulticastGroup(Slot.DEFAULT, Interface.AUDIO_SOURCE, "0.0.0.0");
receiver.bind(Slot.DEFAULT, Interface.AUDIO_SOURCE, new Endpoint("rtp+rs8m://224.0.0.1:0"));
Expand All @@ -135,15 +135,15 @@ public void testSetMulticastGroup() throws Exception {

@Test
public void testBind() throws Exception {
try (RocReceiver receiver = new RocReceiver(context, config)) {
try (RocReceiver receiver = new RocReceiver(context, CONFIG)) {
assertDoesNotThrow(() -> receiver.bind(Slot.DEFAULT, Interface.AUDIO_SOURCE, new Endpoint("rtp+rs8m://0.0.0.0:0")));
assertDoesNotThrow(() -> receiver.bind(Slot.DEFAULT, Interface.AUDIO_REPAIR, new Endpoint("rs8m://0.0.0.0:0")));
}
}

@Test
public void testBindEphemeralPort() throws Exception {
try (RocReceiver receiver = new RocReceiver(context, config)) {
try (RocReceiver receiver = new RocReceiver(context, CONFIG)) {
Endpoint sourceEndpoint = new Endpoint("rtp+rs8m://0.0.0.0:0");
Endpoint repairEndpoint = new Endpoint("rs8m://0.0.0.0:0");
receiver.bind(Slot.DEFAULT, Interface.AUDIO_SOURCE, sourceEndpoint);
Expand Down Expand Up @@ -181,7 +181,7 @@ private static Stream<Arguments> testInvalidBindArguments() {
@ParameterizedTest
@MethodSource("testInvalidBindArguments")
public void testInvalidBind(String errorMessage, Slot slot, Interface iface, Endpoint endpoint) throws Exception {
try (RocReceiver receiver = new RocReceiver(context, config)) {
try (RocReceiver receiver = new RocReceiver(context, CONFIG)) {
IllegalArgumentException exception = assertThrows(
IllegalArgumentException.class,
() -> receiver.bind(slot, iface, endpoint)
Expand All @@ -192,7 +192,7 @@ public void testInvalidBind(String errorMessage, Slot slot, Interface iface, End

@Test
public void testInvalidRead() throws Exception {
try (RocReceiver receiver = new RocReceiver(context, config)) {
try (RocReceiver receiver = new RocReceiver(context, CONFIG)) {
receiver.bind(Slot.DEFAULT, Interface.AUDIO_SOURCE, new Endpoint("rtp+rs8m://127.0.0.1:0"));
receiver.bind(Slot.DEFAULT, Interface.AUDIO_REPAIR, new Endpoint("rs8m://127.0.0.1:0"));
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> receiver.read(null));
Expand All @@ -203,7 +203,7 @@ public void testInvalidRead() throws Exception {

@Test
public void testReadZeroizedArray() throws Exception {
try (RocReceiver receiver = new RocReceiver(context, config)) {
try (RocReceiver receiver = new RocReceiver(context, CONFIG)) {
receiver.bind(Slot.DEFAULT, Interface.AUDIO_SOURCE, new Endpoint("rtp+rs8m://0.0.0.0:0"));
receiver.bind(Slot.DEFAULT, Interface.AUDIO_REPAIR, new Endpoint("rs8m://0.0.0.0:0"));
float[] samples = {1.0f, 1.0f};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,6 @@

public class RocSenderReceiverTest {

private static final int SAMPLE_RATE = 44100;

private final RocSenderConfig senderConfig;
private final RocReceiverConfig receiverConfig;

public RocSenderReceiverTest() {
this.senderConfig = RocSenderConfig.builder()
.frameSampleRate(SAMPLE_RATE)
.frameChannels(ChannelSet.STEREO)
.frameEncoding(FrameEncoding.PCM_FLOAT)
.build();

this.receiverConfig = RocReceiverConfig.builder()
.frameSampleRate(SAMPLE_RATE)
.frameChannels(ChannelSet.STEREO)
.frameEncoding(FrameEncoding.PCM_FLOAT)
.build();
}

@BeforeAll
public static void beforeAll() {
RocLogger.setLevel(RocLogLevel.INFO);
Expand All @@ -43,8 +24,8 @@ public static void beforeAll() {
void testWriteRead() throws Exception {
try (
RocContext context = new RocContext();
RocSender sender = new RocSender(context, senderConfig);
RocReceiver receiver = new RocReceiver(context, receiverConfig)
RocSender sender = new RocSender(context, RocSenderTest.CONFIG);
RocReceiver receiver = new RocReceiver(context, RocReceiverTest.CONFIG)
) {

Endpoint sourceEndpoint = new Endpoint("rtp+rs8m://127.0.0.1:10001");
Expand Down
26 changes: 13 additions & 13 deletions src/test/java/org/rocstreaming/roctoolkit/RocSenderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class RocSenderTest {

private static final int SAMPLE_RATE = 44100;
private static final RocSenderConfig config = RocSenderConfig.builder()
public static final RocSenderConfig CONFIG = RocSenderConfig.builder()
.frameSampleRate(SAMPLE_RATE)
.frameChannels(ChannelSet.STEREO)
.frameEncoding(FrameEncoding.PCM_FLOAT)
Expand Down Expand Up @@ -60,7 +60,7 @@ public void afterEach() throws Exception {
public void testCreationAndDeinitialization() {
assertDoesNotThrow(() -> {
//noinspection EmptyTryBlock
try (RocSender ignored = new RocSender(context, config)) {}
try (RocSender ignored = new RocSender(context, CONFIG)) {}
});
}

Expand Down Expand Up @@ -95,7 +95,7 @@ private static Stream<Arguments> testInvalidCreationArguments() throws Exception
"context must not be null",
IllegalArgumentException.class,
null,
config),
CONFIG),
Arguments.of(
"config must not be null",
IllegalArgumentException.class,
Expand Down Expand Up @@ -134,7 +134,7 @@ private static Stream<Arguments> testInvalidSetOutgoingAddressArguments() {
@ParameterizedTest
@MethodSource("testInvalidSetOutgoingAddressArguments")
public void testInvalidSetOutgoingAddress(String errorMessage, Slot slot, Interface iface, String ip) throws Exception {
try (RocSender receiver = new RocSender(context, config)) {
try (RocSender receiver = new RocSender(context, CONFIG)) {
IllegalArgumentException exception = assertThrows(
IllegalArgumentException.class,
() -> receiver.setOutgoingAddress(slot, iface, ip)
Expand All @@ -145,7 +145,7 @@ public void testInvalidSetOutgoingAddress(String errorMessage, Slot slot, Interf

@Test
void testSetOutgoingAddressAfterConnect() throws Exception {
try (RocSender sender = new RocSender(context, config)) {
try (RocSender sender = new RocSender(context, CONFIG)) {
sender.connect(Slot.DEFAULT, Interface.AUDIO_SOURCE, new Endpoint("rtp+rs8m://0.0.0.0:10001"));
sender.connect(Slot.DEFAULT, Interface.AUDIO_REPAIR, new Endpoint("rs8m://0.0.0.0:10002"));
Exception exception = assertThrows(Exception.class, () -> sender.setOutgoingAddress(Slot.DEFAULT, Interface.AUDIO_SOURCE, "127.0.0.1"));
Expand All @@ -156,15 +156,15 @@ void testSetOutgoingAddressAfterConnect() throws Exception {
@Disabled("bind not implemented in roc 0.2.x yet")
@Test
public void testBind() throws Exception {
try (RocSender sender = new RocSender(context, config)) {
try (RocSender sender = new RocSender(context, CONFIG)) {
assertDoesNotThrow(() -> sender.bind(new Endpoint("rtp+rs8m://127.0.0.1:0")));
}
}

@Disabled("bind not implemented in roc 0.2.x yet")
@Test
public void testBindEphemeralPort() throws Exception {
try (RocSender sender = new RocSender(context, config)) {
try (RocSender sender = new RocSender(context, CONFIG)) {
Endpoint senderEndpoint = new Endpoint("rtp+rs8m://127.0.0.1:0");
sender.bind(senderEndpoint);
assertNotEquals(0, senderEndpoint.getPort());
Expand All @@ -174,7 +174,7 @@ public void testBindEphemeralPort() throws Exception {
@Disabled("bind not implemented in roc 0.2.x yet")
@Test
public void testInvalidBind() throws Exception {
try (RocSender sender = new RocSender(context, config)) {
try (RocSender sender = new RocSender(context, CONFIG)) {
assertThrows(IllegalArgumentException.class, () -> sender.bind(null));
sender.bind(new Endpoint("rtp+rs8m://127.0.0.1:0"));
assertThrows(IOException.class, () -> {
Expand All @@ -185,7 +185,7 @@ public void testInvalidBind() throws Exception {

@Test
public void testConnect() throws Exception {
try (RocSender sender = new RocSender(context, config)) {
try (RocSender sender = new RocSender(context, CONFIG)) {
assertDoesNotThrow(() -> {
sender.connect(Slot.DEFAULT, Interface.AUDIO_SOURCE, new Endpoint("rtp+rs8m://127.0.0.1:10001"));
sender.connect(Slot.DEFAULT, Interface.AUDIO_REPAIR, new Endpoint("rs8m://127.0.0.1:10002"));
Expand Down Expand Up @@ -216,7 +216,7 @@ private static Stream<Arguments> testInvalidConnectArguments() {
@ParameterizedTest
@MethodSource("testInvalidConnectArguments")
public void testInvalidConnect(String errorMessage, Slot slot, Interface iface, Endpoint endpoint) throws Exception {
try (RocSender sender = new RocSender(context, config)) {
try (RocSender sender = new RocSender(context, CONFIG)) {
IllegalArgumentException exception = assertThrows(
IllegalArgumentException.class,
() -> sender.connect(slot, iface, endpoint)
Expand All @@ -227,7 +227,7 @@ public void testInvalidConnect(String errorMessage, Slot slot, Interface iface,

@Test
public void testWrite() throws Exception {
try (RocSender sender = new RocSender(context, config)) {
try (RocSender sender = new RocSender(context, CONFIG)) {
sender.connect(Slot.DEFAULT, Interface.AUDIO_SOURCE, new Endpoint("rtp+rs8m://0.0.0.0:10001"));
sender.connect(Slot.DEFAULT, Interface.AUDIO_REPAIR, new Endpoint("rs8m://0.0.0.0:10002"));
for (int i = 0; i < SINE_SAMPLES / BUFFER_SIZE; i++) {
Expand All @@ -238,7 +238,7 @@ public void testWrite() throws Exception {

@Test
public void testInvalidWrite() throws Exception {
try (RocSender sender = new RocSender(context, config)) {
try (RocSender sender = new RocSender(context, CONFIG)) {
// bind not implemented in roc 0.2.x yet
// assertThrows(IOException.class, () -> sender.write(samples)); // write before bind
// sender.bind();
Expand All @@ -250,7 +250,7 @@ public void testInvalidWrite() throws Exception {

@Test
public void testInvalidConnectAfterWrite() throws Exception {
try (RocSender sender = new RocSender(context, config)) {
try (RocSender sender = new RocSender(context, CONFIG)) {
sender.connect(Slot.DEFAULT, Interface.AUDIO_SOURCE, new Endpoint("rtp+rs8m://0.0.0.0:10001"));
sender.connect(Slot.DEFAULT, Interface.AUDIO_REPAIR, new Endpoint("rs8m://0.0.0.0:10002"));
sender.write(samples);
Expand Down

0 comments on commit ed5d19c

Please sign in to comment.