Skip to content

Commit

Permalink
Remove throws/try/catch from tests where possible (excluding manual t…
Browse files Browse the repository at this point in the history
…ests and he… (#81)

* Remove throws/try/catch where possible (excluding manual tests and helpers and classes that extends), remove SyslogFrameProcessorTest as that seems to be deprecated
  • Loading branch information
StrongestNumber9 authored Feb 26, 2024
1 parent 42a4074 commit 501dc35
Show file tree
Hide file tree
Showing 19 changed files with 227 additions and 430 deletions.
39 changes: 17 additions & 22 deletions src/test/java/com/teragrep/rlp_03/CloseByteConsumerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,18 @@
import com.teragrep.rlp_01.RelpBatch;
import com.teragrep.rlp_01.RelpConnection;
import com.teragrep.rlp_03.config.Config;
import org.junit.jupiter.api.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class CloseByteConsumerTest {
private static final Logger LOGGER = LoggerFactory.getLogger(CloseByteConsumerTest.class);

private final String hostname = "localhost";
private Server server;
private static int port = 1241;
Expand All @@ -78,53 +74,52 @@ public void accept(FrameContext relpFrameServerRX) {
}

@Override
public void close() throws Exception {
public void close() {
closed.set(true);
}
}

public void init() throws IOException, InterruptedException {
public void init() {
port = getPort();
Config config = new Config(port, 1);

ServerFactory serverFactory = new ServerFactory(config, new SyslogFrameProcessor(new AutoCloseableByteConsumer()));
server = serverFactory.create();
Assertions.assertAll(() -> {
server = serverFactory.create();

Thread serverThread = new Thread(server);
serverThread.start();
server.startup.waitForCompletion();
Thread serverThread = new Thread(server);
serverThread.start();
server.startup.waitForCompletion();
});
}

public void cleanup() throws InterruptedException {
public void cleanup() {
server.stop();
}

private synchronized int getPort() {
return ++port;
}




@Test
public void testSendMessage() throws IOException, TimeoutException, InterruptedException {
public void testSendMessage() {
init(); // starts server

RelpConnection relpSession = new RelpConnection();
relpSession.connect(hostname, port);
Assertions.assertAll(() -> relpSession.connect(hostname, port));
String msg = "<14>1 2020-05-15T13:24:03.603Z CFE-16 capsulated - - [CFE-16-metadata@48577 authentication_token=\"AUTH_TOKEN_11111\" channel=\"CHANNEL_11111\" time_source=\"generated\"][CFE-16-origin@48577] \"Hello, world!\"\n";
byte[] data = msg.getBytes(StandardCharsets.UTF_8);
RelpBatch batch = new RelpBatch();
long reqId = batch.insert(data);
relpSession.commit(batch);
Assertions.assertAll(() -> relpSession.commit(batch));
// verify successful transaction
Assertions.assertTrue(batch.verifyTransaction(reqId));
relpSession.disconnect();
Assertions.assertAll(relpSession::disconnect);

// message must equal to what was send
Assertions.assertEquals(msg, new String(messageList.get(0)));

Thread.sleep(100); // closure on the server-side is not synchronized to disconnect
Assertions.assertAll(() -> Thread.sleep(100)); // closure on the server-side is not synchronized to disconnect

cleanup(); // closes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,18 @@
import com.teragrep.rlp_01.RelpBatch;
import com.teragrep.rlp_01.RelpConnection;
import com.teragrep.rlp_03.config.Config;
import org.junit.jupiter.api.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class CloseRelpFrameServerRXConsumerTest {
private static final Logger LOGGER = LoggerFactory.getLogger(CloseRelpFrameServerRXConsumerTest.class);

private final String hostname = "localhost";
private Server server;
private Thread serverThread;
Expand All @@ -79,49 +75,48 @@ public void accept(FrameContext relpFrameServerRX) {
}

@Override
public void close() throws Exception {
public void close() {
closed.set(true);
}
}

private void init() throws InterruptedException, IOException {
private void init() {
port = getPort();
Config config = new Config(port, 1);
ServerFactory serverFactory = new ServerFactory(config, new SyslogFrameProcessor(new AutoCloseableRelpFrameServerRXConsumer()));
server = serverFactory.create();
Assertions.assertAll(() -> {
server = serverFactory.create();

serverThread = new Thread(server);
serverThread.start();
serverThread = new Thread(server);
serverThread.start();

server.startup.waitForCompletion();
server.startup.waitForCompletion();
});
}

private void cleanup() throws InterruptedException {
private void cleanup() {
server.stop();
serverThread.join();
Assertions.assertAll(()->serverThread.join());
}

private synchronized int getPort() {
return ++port;
}




@Test
public void testSendMessage() throws IOException, TimeoutException, InterruptedException {
public void testSendMessage() {
init(); // start server

RelpConnection relpSession = new RelpConnection();
relpSession.connect(hostname, port);
Assertions.assertAll(() -> relpSession.connect(hostname, port));
String msg = "<14>1 2020-05-15T13:24:03.603Z CFE-16 capsulated - - [CFE-16-metadata@48577 authentication_token=\"AUTH_TOKEN_11111\" channel=\"CHANNEL_11111\" time_source=\"generated\"][CFE-16-origin@48577] \"Hello, world!\"\n";
byte[] data = msg.getBytes(StandardCharsets.UTF_8);
RelpBatch batch = new RelpBatch();
long reqId = batch.insert(data);
relpSession.commit(batch);
Assertions.assertAll(() -> relpSession.commit(batch));
// verify successful transaction
Assertions.assertTrue(batch.verifyTransaction(reqId));
relpSession.disconnect();
Assertions.assertAll(relpSession::disconnect);

// message must equal to what was send
Assertions.assertEquals(msg, new String(messageList.get(0)));
Expand Down
33 changes: 14 additions & 19 deletions src/test/java/com/teragrep/rlp_03/ConnectionStormTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,33 @@

import com.teragrep.rlp_01.RelpConnection;
import com.teragrep.rlp_03.config.Config;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import org.junit.jupiter.api.*;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeoutException;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class ConnectionStormTest {

private static final Logger LOGGER = LoggerFactory.getLogger(ConnectionStormTest.class);

private final String hostname = "localhost";
private Server server;
private static int port = 1242;

private final List<byte[]> messageList = new LinkedList<>();

@BeforeAll
public void init() throws InterruptedException, IOException {
public void init() {
port = getPort();
Config config = new Config(port, 1);
ServerFactory serverFactory = new ServerFactory(config, new SyslogFrameProcessor((frame) -> messageList.add(frame.relpFrame().payload().toBytes())));
server = serverFactory.create();
Assertions.assertAll(() -> {
server = serverFactory.create();

Thread serverThread = new Thread(server);
serverThread.start();
Thread serverThread = new Thread(server);
serverThread.start();

server.startup.waitForCompletion();
server.startup.waitForCompletion();
});
}

@AfterAll
Expand All @@ -48,12 +41,14 @@ private synchronized int getPort() {
}

@Test
public void testOpenAndCloseSession() throws IOException, TimeoutException {
public void testOpenAndCloseSession() {
long count = 10000;
while (count > 0) {
RelpConnection relpSession = new RelpConnection();
relpSession.connect(hostname, port);
relpSession.disconnect();
Assertions.assertAll(() -> {
relpSession.connect(hostname, port);
relpSession.disconnect();
});
count--;
}
}
Expand Down
69 changes: 28 additions & 41 deletions src/test/java/com/teragrep/rlp_03/MultiClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,10 @@
import org.junit.jupiter.api.condition.DisabledOnJre;
import org.junit.jupiter.api.condition.JRE;


import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Deque;
import java.util.Random;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.TimeoutException;
import java.util.function.Supplier;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Expand All @@ -73,7 +70,7 @@ public class MultiClientTest extends Thread{


@Test
public void testMultiClient() throws InterruptedException, IllegalStateException {
public void testMultiClient() {
int n = 10;
Thread[] threads = new Thread[n];
for(int i=0; i<n; i++) {
Expand All @@ -82,54 +79,45 @@ public void testMultiClient() throws InterruptedException, IllegalStateException
threads[i] = thread;
}

for (int i=0; i<n; i++) {
threads[i].join();
}
Assertions.assertAll(() -> {
for (int i=0; i<n; i++) {
threads[i].join();
}
});
}

// testMultiClient executes this with new MultiClientTest() thread
public void run() {
Random random = new Random();

for(int i=0;i<3;i++) {
// Sleep to make the ordering unpredictable
try {
// Sleep to make the ordering unpredictable
Assertions.assertAll(() -> {
Thread.sleep(random.nextInt(100));
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
testSendBatch();
testSendMessage();
} catch (IOException | TimeoutException | IllegalStateException e) {
e.printStackTrace();
}
testSendBatch();
testSendMessage();
});
}
}

@BeforeAll
public void init() throws IOException, InterruptedException {

Supplier<FrameProcessor> frameProcessorSupplier = new Supplier<FrameProcessor>() {
@Override
public FrameProcessor get() {
return new SyslogFrameProcessor((frame) -> messageList.add(frame.relpFrame().payload().toBytes()));
}
};
public void init() {
Supplier<FrameProcessor> frameProcessorSupplier = () -> new SyslogFrameProcessor((frame) -> messageList.add(frame.relpFrame().payload().toBytes()));

port = getPort();
Config config = new Config(port, 4);
ServerFactory serverFactory = new ServerFactory(config, frameProcessorSupplier);
server = serverFactory.create();
Assertions.assertAll(() -> {
server = serverFactory.create();

Thread serverThread = new Thread(server);
serverThread.start();
Thread serverThread = new Thread(server);
serverThread.start();

server.startup.waitForCompletion();
server.startup.waitForCompletion();
});
}

@AfterAll
public void cleanup() throws InterruptedException {
public void cleanup() {
server.stop();

// 10 threads each: run 3 times 50 msgs of testSendBatch plus 3 times
Expand All @@ -141,33 +129,32 @@ private synchronized int getPort() {
return ++port;
}

private void testSendMessage() throws IOException, TimeoutException {
private void testSendMessage() {
RelpConnection relpSession = new RelpConnection();
relpSession.connect(hostname, port);
Assertions.assertAll(() -> relpSession.connect(hostname, port));
String msg = "<14>1 2020-05-15T13:24:03.603Z CFE-16 capsulated - - [CFE-16-metadata@48577 authentication_token=\"AUTH_TOKEN_11111\" channel=\"CHANNEL_11111\" time_source=\"generated\"][CFE-16-origin@48577] \"Hello, world!\"\n";
byte[] data = msg.getBytes(StandardCharsets.UTF_8);
RelpBatch batch = new RelpBatch();
long reqId = batch.insert(data);
relpSession.commit(batch);
Assertions.assertAll(() -> relpSession.commit(batch));
// verify successful transaction
Assertions.assertTrue(batch.verifyTransaction(reqId));
relpSession.disconnect();
Assertions.assertAll(relpSession::disconnect);
}

private void testSendBatch() throws IllegalStateException, IOException,
TimeoutException {
private void testSendBatch() {
RelpConnection relpSession = new RelpConnection();
relpSession.connect(hostname, port);
Assertions.assertAll(() -> relpSession.connect(hostname, port));
String msg = "Hello, world!";
byte[] data = msg.getBytes(StandardCharsets.UTF_8);
int n = 50;
RelpBatch batch = new RelpBatch();
for (int i = 0; i < n; i++) {
batch.insert(data);
}
relpSession.commit(batch);
Assertions.assertAll(() -> relpSession.commit(batch));
Assertions.assertTrue(batch.verifyTransactionAll());
relpSession.disconnect();
Assertions.assertAll(relpSession::disconnect);
}

}
Loading

0 comments on commit 501dc35

Please sign in to comment.