Skip to content

Commit

Permalink
[tests] adding packet counts to dummy server
Browse files Browse the repository at this point in the history
  • Loading branch information
truthbk committed Mar 20, 2020
1 parent 064b9ac commit b1e7aa4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/test/java/com/timgroup/statsd/DummyStatsDServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.LinkedBlockingQueue;

import jnr.unixsocket.UnixDatagramChannel;
Expand All @@ -18,6 +19,7 @@

class DummyStatsDServer {
private final List<String> messagesReceived = new ArrayList<String>();
private AtomicInteger packetsReceived = new AtomicInteger(0);

protected final DatagramChannel server;
protected volatile Boolean freeze = false;
Expand Down Expand Up @@ -51,6 +53,8 @@ public void run() {
((Buffer)packet).clear(); // Cast necessary to handle Java9 covariant return types
// see: https://jira.mongodb.org/browse/JAVA-2559 for ref.
server.receive(packet);
packetsReceived.addAndGet(1);

packet.flip();
for (String msg : StandardCharsets.UTF_8.decode(packet).toString().split("\n")) {
messagesReceived.add(msg.trim());
Expand Down Expand Up @@ -78,6 +82,10 @@ public List<String> messagesReceived() {
return new ArrayList<String>(messagesReceived);
}

public int packetsReceived() {
return packetsReceived.get();
}

public void freeze() {
freeze = true;
}
Expand All @@ -95,6 +103,7 @@ public void close() throws IOException {
}

public void clear() {
packetsReceived.set(0);
messagesReceived.clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

import java.io.IOException;
import java.net.SocketException;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import java.util.Random;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -65,6 +65,9 @@ public void run() {
} catch (InterruptedException ex) {}
}

log.info("Messages at server: " + messages);
log.info("Packets at server: " + server.packetsReceived());

assertEquals(testSize, server.messagesReceived().size());
}
}

0 comments on commit b1e7aa4

Please sign in to comment.