Skip to content

Commit

Permalink
Buildify PingPong test helper (#132)
Browse files Browse the repository at this point in the history
* Buildify PingPong test helper

---------

Co-authored-by: Tilmann Zäschke <tilmann.zaeschke@inf.ethz.ch>
  • Loading branch information
tzaeschke and Tilmann Zäschke authored Oct 25, 2024
1 parent 9ba49b0 commit 850e63b
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 52 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Add a SHIM, required for #130 (topo file port range support).
[#130](https://github.com/scionproto-contrib/jpan/pull/130)

### Changed
- Buildified PingPong test helper. [#132](https://github.com/scionproto-contrib/jpan/pull/132)

## [0.3.1] - 2024-10-11

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ private void testExpired(BiConsumer<ScionDatagramChannel, Path> sendMethod, bool
String pong = Charset.defaultCharset().decode(response).toString();
assertEquals(PingPongChannelHelper.MSG, pong);
};
PingPongChannelHelper pph = new PingPongChannelHelper(1, 10, 5, connect);
PingPongChannelHelper pph = PingPongChannelHelper.newBuilder(1, 10, 5).connect(connect).build();
pph.runPingPong(serverFn, clientFn);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static void afterAll() {
void test() {
PingPongChannelHelper.Server serverFn = PingPongChannelHelper::defaultServer;
PingPongChannelHelper.Client clientFn = this::client;
PingPongChannelHelper pph = new PingPongChannelHelper(1, 20, 50, false);
PingPongChannelHelper pph = PingPongChannelHelper.newBuilder(1, 20, 50).connect(false).build();
pph.runPingPong(serverFn, clientFn);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static void afterAll() {
void test() {
PingPongChannelHelper.Server serverFn = PingPongChannelHelper::defaultServer;
PingPongChannelHelper.Client clientFn = this::client;
PingPongChannelHelper pph = new PingPongChannelHelper(1, 20, 50, false);
PingPongChannelHelper pph = PingPongChannelHelper.newBuilder(1, 20, 50).connect(false).build();
pph.runPingPong(serverFn, clientFn);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void afterAll() {
void test() {
PingPongChannelHelper.Server serverFn = PingPongChannelHelper::defaultServer;
PingPongChannelHelper.Client clientFn = this::client;
PingPongChannelHelper pph = new PingPongChannelHelper(1, 10, 10);
PingPongChannelHelper pph = PingPongChannelHelper.newBuilder(1, 10, 10).build();
pph.runPingPong(serverFn, clientFn);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void afterAll() {
void test() {
PingPongChannelHelper.Server serverFn = this::server;
PingPongChannelHelper.Client clientFn = this::client;
PingPongChannelHelper pph = new PingPongChannelHelper(1, 10, 10);
PingPongChannelHelper pph = PingPongChannelHelper.newBuilder(1, 10, 10).build();
pph.runPingPong(serverFn, clientFn);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public static void afterAll() {
void test() {
PingPongChannelHelper.Server serverFn = PingPongChannelHelper::defaultServer;
PingPongChannelHelper.Client clientFn = this::client;
PingPongChannelHelper pph = new PingPongChannelHelper(1, 2, 10);
pph.runPingPong(serverFn, clientFn, false);
PingPongChannelHelper pph = PingPongChannelHelper.newBuilder(1, 2, 10).build();
pph.runPingPong(serverFn, clientFn);
// TODO: This sometimes reports 22 i.o. 20.
assertEquals(
2 * 10,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ public static void afterAll() {
void test() {
PingPongChannelHelper.Server serverFn = this::server;
PingPongChannelHelper.Client clientFn = this::client;
PingPongChannelHelper pph = new PingPongChannelHelper(1, 2, 2);
pph.runPingPong(serverFn, clientFn, false);
PingPongChannelHelper pph =
PingPongChannelHelper.newBuilder(1, 2, 2).checkCounters(false).build();
pph.runPingPong(serverFn, clientFn);
assertEquals(2 * 2 * 2 * N_BULK, MockNetwork.getAndResetForwardCount());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ private void testExpired(BiConsumer<ScionDatagramSocket, Path> sendMethod) throw
String pong = Charset.defaultCharset().decode(response).toString();
assertEquals(PingPongSocketHelper.MSG, pong);
};
PingPongSocketHelper pph = new PingPongSocketHelper(1, 10, 5);
PingPongSocketHelper pph = PingPongSocketHelper.newBuilder(1, 10, 5).build();
pph.runPingPong(serverFn, clientFn);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ void test() throws IOException {
PingPongSocketHelper.Server receiverFn = this::receiver;
PingPongSocketHelper.Server senderFn = this::sender;
PingPongSocketHelper.Client clientFn = this::client;
PingPongSocketHelper pph = new PingPongSocketHelper(2, 5, 100);
pph.runPingPongSharedServerSocket(receiverFn, senderFn, clientFn, false);
PingPongSocketHelper pph = PingPongSocketHelper.newBuilder(2, 5, 100).build();
pph.runPingPongSharedServerSocket(receiverFn, senderFn, clientFn);
assertEquals(2 * 5 * 100, MockNetwork.getAndResetForwardCount());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public static void afterAll() {
void test() {
PingPongSocketHelper.Server serverFn = this::server;
PingPongSocketHelper.Client clientFn = this::client;
PingPongSocketHelper pph = new PingPongSocketHelper(1, 10, 10);
pph.runPingPong(serverFn, clientFn, false);
PingPongSocketHelper pph = PingPongSocketHelper.newBuilder(1, 10, 10).build();
pph.runPingPong(serverFn, clientFn);
assertEquals(2 * 10 * 10, MockNetwork.getAndResetForwardCount());
}

Expand Down
23 changes: 18 additions & 5 deletions src/test/java/org/scion/jpan/internal/ShimTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,29 @@ private Path createDummyPath(int serverPort) {

@Test
void testForwardingUDP() {
test();
PingPongChannelHelper.Server serverFn = this::server;
PingPongChannelHelper.Client clientFn = this::client;
// we are circumventing the daemon! -> checkCounters(false)
PingPongChannelHelper pph =
PingPongChannelHelper.newBuilder(1, 2, 10).checkCounters(false).build();
pph.runPingPong(serverFn, clientFn);
assertTrue(Shim.isInstalled());
assertEquals(2 * 2 * 10, shimForwardingCounter.getAndSet(0));
}

private void test() {
@Disabled
@Test
void testForwardingUDP_LocalAS() {
PingPongChannelHelper.Server serverFn = this::server;
PingPongChannelHelper.Client clientFn = this::client;
PingPongChannelHelper pph = new PingPongChannelHelper(1, 1, 10);
pph.runPingPong(serverFn, clientFn, false);
PingPongChannelHelper pph =
PingPongChannelHelper.newBuilder(1, 2, 10)
.checkCounters(false)
.serverIsdAs(MockNetwork.TINY_CLIENT_ISD_AS)
.build();
pph.runPingPong(serverFn, clientFn);
assertTrue(Shim.isInstalled());
assertEquals(2 * 10, shimForwardingCounter.getAndSet(0));
assertEquals(2 * 2 * 10, shimForwardingCounter.getAndSet(0));
}

public void client(ScionDatagramChannel channel, Path serverAddress, int id) throws IOException {
Expand Down
12 changes: 8 additions & 4 deletions src/test/java/org/scion/jpan/testutil/MockNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public class MockNetwork {
public static final int TINY_SRV_PORT_1 = 22233;
public static final String TINY_SRV_ISD_AS = "1-ff00:0:112";
public static final String TINY_SRV_NAME_1 = "server.as112.test";
public static final String TINY_CLIENT_ISD_AS = "1-ff00:0:110";
public static final String TINY_CLIENT_TOPOFILE = MockBootstrapServer.TOPOFILE_TINY_110;
static final AtomicInteger nForwardTotal = new AtomicInteger();
static final AtomicIntegerArray nForwards = new AtomicIntegerArray(20);
static final AtomicInteger dropNextPackets = new AtomicInteger();
Expand Down Expand Up @@ -139,12 +141,10 @@ private static synchronized void startTiny(String localIP, String remoteIP, Mode
MockDNS.install(TINY_SRV_ISD_AS, TINY_SRV_NAME_1, TINY_SRV_ADDR_1);

if (mode == Mode.NAPTR || mode == Mode.BOOTSTRAP) {
topoServer =
MockBootstrapServer.start(MockBootstrapServer.TOPOFILE_TINY_110, mode == Mode.NAPTR);
topoServer = MockBootstrapServer.start(TINY_CLIENT_TOPOFILE, mode == Mode.NAPTR);
controlServer = MockControlServer.start(topoServer.getControlServerPort());
} else if (mode == Mode.AS_ONLY) {
AsInfo asInfo =
JsonFileParser.parseTopologyFile(Paths.get(MockBootstrapServer.TOPOFILE_TINY_110));
AsInfo asInfo = JsonFileParser.parseTopologyFile(Paths.get(TINY_CLIENT_TOPOFILE));
controlServer = MockControlServer.start(asInfo.getControlServerPort());
}

Expand Down Expand Up @@ -204,6 +204,10 @@ public static int getAndResetForwardCount() {
return nForwardTotal.getAndSet(0);
}

public static int getForwardCount() {
return nForwardTotal.get();
}

/**
* Set the routers to drop the next n packets.
*
Expand Down
37 changes: 27 additions & 10 deletions src/test/java/org/scion/jpan/testutil/PingPongChannelHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@

public class PingPongChannelHelper extends PingPongHelperBase {

public PingPongChannelHelper(int nServers, int nClients, int nRounds) {
this(nServers, nClients, nRounds, true);
private PingPongChannelHelper(
int nServers,
int nClients,
int nRounds,
boolean connect,
boolean resetCounters,
String serverIsdAs) {
super(nServers, nClients, nRounds, connect, resetCounters, serverIsdAs);
}

public PingPongChannelHelper(int nServers, int nClients, int nRounds, boolean connect) {
super(nServers, nClients, nRounds, connect);
public static Builder newBuilder(int nServers, int nClients, int nRounds) {
return new Builder(nServers, nClients, nRounds);
}

private abstract class AbstractChannelEndpoint extends AbstractEndpoint {
Expand Down Expand Up @@ -118,14 +124,9 @@ public interface Server {
}

public void runPingPong(Server serverFn, Client clientFn) {
runPingPong(serverFn, clientFn, true);
}

public void runPingPong(Server serverFn, Client clientFn, boolean reset) {
runPingPong(
(id, nRounds) -> new ServerEndpoint(serverFn, id, nRounds),
(id, path, nRounds) -> new ClientEndpoint(clientFn, id, path, nRounds, connectClients),
reset);
(id, path, nRounds) -> new ClientEndpoint(clientFn, id, path, nRounds, connectClients));
}

public static void defaultClient(ScionDatagramChannel channel, Path serverAddress, int id)
Expand Down Expand Up @@ -160,4 +161,20 @@ public static void defaultServer(ScionDatagramChannel channel) throws IOExceptio
channel.send(request, responseAddress);
// System.out.println("SERVER: Sent: " + responseAddress);
}

public static class Builder extends PingPongHelperBase.Builder<PingPongChannelHelper.Builder> {

protected Builder(int nServers, int nClients, int nRounds) {
super(nServers, nClients, nRounds, true);
}

protected Builder(int nServers, int nClients, int nRounds, boolean connect) {
super(nServers, nClients, nRounds, connect);
}

public PingPongChannelHelper build() {
return new PingPongChannelHelper(
nServers, nClients, nRounds, connectClients, checkCounters, serverIA);
}
}
}
56 changes: 51 additions & 5 deletions src/test/java/org/scion/jpan/testutil/PingPongHelperBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,29 @@ public class PingPongHelperBase {
protected final int nServers;
private final int nRounds;
protected final boolean connectClients;
private final boolean checkCounters;
private final String serverIsdAs;

final CountDownLatch startUpBarrierClient;
final CountDownLatch startUpBarrierServer;
protected final AtomicInteger nRoundsClient = new AtomicInteger();
protected final AtomicInteger nRoundsServer = new AtomicInteger();
protected final ConcurrentLinkedQueue<Throwable> exceptions = new ConcurrentLinkedQueue<>();

protected PingPongHelperBase(int nServers, int nClients, int nRounds, boolean connect) {
protected PingPongHelperBase(
int nServers,
int nClients,
int nRounds,
boolean connect,
boolean checkCounters,
String serverIsdAs) {
this.nClients = nClients;
this.nServers = nServers;
this.nRounds = nRounds;
this.connectClients = connect;
this.checkCounters = checkCounters;
this.serverIsdAs = serverIsdAs;

startUpBarrierClient = new CountDownLatch(nClients);
startUpBarrierServer = new CountDownLatch(nServers);
shutDownBarrier = new CountDownLatch(nClients + nServers);
Expand Down Expand Up @@ -91,7 +102,7 @@ interface ServerFactory {
AbstractEndpoint create(int id, int nRounds);
}

void runPingPong(ServerFactory serverFactory, ClientFactory clientFactory, boolean reset) {
void runPingPong(ServerFactory serverFactory, ClientFactory clientFactory) {
try {
MockNetwork.startTiny();

Expand All @@ -106,7 +117,7 @@ void runPingPong(ServerFactory serverFactory, ClientFactory clientFactory, boole
throw new RuntimeException("Server startup failed: " + startUpBarrierServer);
}
InetSocketAddress serverAddress = servers[0].getLocalAddress();
MockDNS.install(MockNetwork.TINY_SRV_ISD_AS, serverAddress.getAddress());
MockDNS.install(serverIsdAs, serverAddress.getAddress());
Path requestPath = Scion.defaultService().lookupAndGetPath(serverAddress, null);

Thread[] clients = new Thread[nClients];
Expand Down Expand Up @@ -144,8 +155,8 @@ void runPingPong(ServerFactory serverFactory, ClientFactory clientFactory, boole
checkExceptions();
}

if (reset) {
assertEquals(nRounds * nClients * 2, MockNetwork.getAndResetForwardCount());
if (checkCounters) {
assertEquals(nRounds * nClients * 2, MockNetwork.getForwardCount());
}
assertEquals(nRounds * nClients, nRoundsClient.get());
// For now, we assume that every request is handles by every server thread.
Expand All @@ -164,4 +175,39 @@ private void checkExceptions() {
assertEquals(0, exceptions.size());
exceptions.clear();
}

protected abstract static class Builder<T extends PingPongHelperBase.Builder<T>> {
protected final int nClients;
protected final int nServers;
protected final int nRounds;
protected boolean connectClients = true;
protected boolean checkCounters = true;
protected String serverIA = MockNetwork.TINY_SRV_ISD_AS;

protected Builder(int nServers, int nClients, int nRounds, boolean connect) {
this.nClients = nClients;
this.nServers = nServers;
this.nRounds = nRounds;
this.connectClients = connect;
}

public T resetCounters(boolean resetCounters) {
return (T) this;
}

public T serverIsdAs(String serverIsdAs) {
this.serverIA = serverIsdAs;
return (T) this;
}

public T connect(boolean connectClients) {
this.connectClients = connectClients;
return (T) this;
}

public T checkCounters(boolean checkCounters) {
this.checkCounters = checkCounters;
return (T) this;
}
}
}
4 changes: 2 additions & 2 deletions src/test/java/org/scion/jpan/testutil/PingPongHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public static void afterAll() {
void testChannel() {
PingPongChannelHelper.Server serverFn = PingPongChannelHelper::defaultServer;
PingPongChannelHelper.Client clientFn = PingPongChannelHelper::defaultClient;
PingPongChannelHelper pph = new PingPongChannelHelper(1, 1, 10);
PingPongChannelHelper pph = PingPongChannelHelper.newBuilder(1, 1, 10).build();
pph.runPingPong(serverFn, clientFn);
}

@Test
void testSocket() {
PingPongSocketHelper.Server serverFn = PingPongSocketHelper::defaultServer;
PingPongSocketHelper.Client clientFn = PingPongSocketHelper::defaultClient;
PingPongSocketHelper pph = new PingPongSocketHelper(1, 1, 10);
PingPongSocketHelper pph = PingPongSocketHelper.newBuilder(1, 1, 10).build();
pph.runPingPong(serverFn, clientFn);
}
}
Loading

0 comments on commit 850e63b

Please sign in to comment.