Skip to content

Commit

Permalink
Better Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Konloch committed Dec 15, 2023
1 parent 8ea8e3a commit 1aad8a0
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/test/java/com/konloch/TestSocketServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@
public class TestSocketServer
{
//change this value to increase the amount of concurrent clients
private static final int TEST_THREADS = 2;
private static final int CLIENT_TEST_THREADS = 2;

//change this value to adjust the time till timeout in ms
private static final int CLIENT_TIMEOUT = 7000;

//change this value to increase the amount of server processing threads
private static final int THREAD_POOL = 2;
private static final int SERVER_THREAD_POOL = 2;

//change this value to adjust the time till timeout in ms
private static final int SERVER_TIMEOUT = 30_000;

private static long lastDataTransferBPS;
private static AtomicLong totalAmountOfDataTransferred = new AtomicLong();
Expand All @@ -26,7 +32,8 @@ public class TestSocketServer
public static void main(String[] args)
{
Server server = setupServer();
testServer(server.getPort(), TEST_THREADS);
server.setTimeout(SERVER_TIMEOUT);
testServer(server.getPort(), CLIENT_TEST_THREADS);

while(true)
{
Expand All @@ -35,7 +42,7 @@ public static void main(String[] args)
Thread.sleep(10);

int connectedClientsAmount = 0;
for(int i = 0; i < THREAD_POOL; i++)
for(int i = 0; i < SERVER_THREAD_POOL; i++)
connectedClientsAmount += server.getClients(i).size();

System.out.println("Connected Clients: " + connectedClientsAmount + ", Total Bytes Transferred: " + totalAmountOfDataTransferred + ", Bytes Transferred Per Second: " + lastDataTransferBPS);
Expand All @@ -49,7 +56,7 @@ public static void main(String[] args)

private static Server setupServer()
{
Server server = new Server(1111, THREAD_POOL, null, client ->
Server server = new Server(1111, SERVER_THREAD_POOL, null, client ->
{
switch(client.getState())
{
Expand Down Expand Up @@ -127,8 +134,7 @@ private static void testConnection(int port) throws Exception
socket.getOutputStream().flush();

long readTimer = System.currentTimeMillis();
long giveUp = 7000; //change this value to adjust the time till it will 'give up'
while(socket.getInputStream().available() <= len && System.currentTimeMillis()-readTimer <= giveUp)
while(socket.getInputStream().available() <= len && System.currentTimeMillis()-readTimer <= CLIENT_TIMEOUT)
{
Thread.sleep(1);
}
Expand Down

0 comments on commit 1aad8a0

Please sign in to comment.