Browse Source

FsMQManager regression test and other improvements

JNI-96
heck 5 years ago
parent
commit
fea2a175b8
  1. 6
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQIdentity.java
  2. 49
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQManager.java
  3. 7
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/UnknownIdentityException.java
  4. 2
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/Makefile.conf
  5. 116
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/regression/TestMain.java

6
test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQIdentity.java

@ -1,11 +1,10 @@
package foundation.pEp.jniadapter.test.utils.transport.fsmqmanager;
class FsMQIdentity implements java.io.Serializable {
public class FsMQIdentity implements java.io.Serializable {
private String address = null;
private String qDir = null;
FsMQIdentity(String address, String qDir) {
public FsMQIdentity(String address, String qDir) {
this.address = address;
this.qDir = qDir;
}
@ -17,5 +16,4 @@ class FsMQIdentity implements java.io.Serializable {
public String getqDir() {
return qDir;
}
}

49
test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQManager.java

@ -1,17 +1,18 @@
package foundation.pEp.jniadapter.test.utils.transport.fsmqmanager;
import foundation.pEp.jniadapter.Identity;
import foundation.pEp.jniadapter.test.framework.TestUtils;
import foundation.pEp.jniadapter.test.utils.transport.fsmsgqueue.FsMsgQueue;
import java.io.*;
import java.util.*;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import static foundation.pEp.jniadapter.test.framework.TestLogger.log;
public class FsMQManager {
private FsMQIdentity self = null;
private List<FsMQIdentity> identities = new ArrayList<>();
private Map<String, FsMsgQueue> identityAddressQueues = new HashMap<String, FsMsgQueue>();
@ -19,24 +20,27 @@ public class FsMQManager {
private static String SYNACKMSG = "SYNACK";
private static String ACKMSG = "ACK";
public FsMQManager(String ownAddr, String ownQueueDir) {
self = new FsMQIdentity(ownAddr, ownQueueDir);
public FsMQManager(FsMQIdentity self) {
this.self = self;
addOrUpdateIdentity(self);
}
// Identity address must be unique
public void addOrUpdateIdentity(FsMQIdentity ident) {
// Returns
// - true for added
// - false for updated
public boolean addOrUpdateIdentity(FsMQIdentity ident) {
try {
getIdentityForAddress(ident.getAddress());
} catch (UnknownIdentityException e) {
// Good, add new ident
addIdent(ident);
return;
return true;
}
// Ok, update ident
removeIdent(ident);
addIdent(ident);
return;
return false;
}
public void sendMsgToIdentity(FsMQIdentity ident, String msg) throws UnknownIdentityException, IOException {
@ -49,13 +53,24 @@ public class FsMQManager {
getQueueForIdentity(self).clear();
}
public String waitForMsg() throws UnknownIdentityException, IOException, ClassNotFoundException {
public String waitForMsg() throws UnknownIdentityException, IOException, ClassNotFoundException, TimeoutException {
return waitForMsg(0);
}
public String waitForMsg(int timeoutSec) throws UnknownIdentityException, IOException, ClassNotFoundException, TimeoutException {
String ret = null;
FsMsgQueue onwQueue = getQueueForIdentity(self);
FsMQMessage mqMsg = null;
int pollInterval = 100;
int pollRepeats = timeoutSec * 1000 / pollInterval;
int pollCounter = 0;
do {
while (onwQueue.isEmpty()) {
TestUtils.sleep(100);
pollCounter++;
if (pollCounter >= pollRepeats) {
throw new TimeoutException("");
}
}
String serializedMsg = onwQueue.remove();
mqMsg = FsMQMessage.deserialize(serializedMsg);
@ -70,7 +85,6 @@ public class FsMQManager {
createQueueForIdent(ident);
}
// undefined behaviour if already existing
// Removes the identity from identities and identityQueues by address
private void removeIdent(FsMQIdentity ident) {
identities.removeIf(i -> i.getAddress().equals(ident.getAddress()));
@ -85,13 +99,13 @@ public class FsMQManager {
private FsMsgQueue getQueueForIdentity(FsMQIdentity ident) throws UnknownIdentityException {
FsMsgQueue ret = null;
ret = identityAddressQueues.get(ident.getAddress());
if (ret != null) {
if (ret == null) {
throw new UnknownIdentityException("Unknown identity address: " + ident.getAddress());
}
return ret;
}
private FsMQIdentity getIdentityForAddress(String address) throws UnknownIdentityException, IllegalStateException {
public FsMQIdentity getIdentityForAddress(String address) throws UnknownIdentityException, IllegalStateException {
FsMQIdentity ret = null;
List<FsMQIdentity> matches = identities.stream().filter(i -> i.getAddress().equals(address)).collect(Collectors.toList());
if (matches.size() <= 0) {
@ -139,7 +153,7 @@ public class FsMQManager {
// log("Sending ACK to: " + ident.getAddress());
// sendMsgToIdentity(ident, msg);
// }
}
class FsMQMessage implements java.io.Serializable {
@ -147,8 +161,8 @@ class FsMQMessage implements java.io.Serializable {
FsMQHandshakeHeader header = null;
String msg = null;
FsMQMessage(FsMQIdentity from, String msg) throws IllegalStateException{
if(from == null || msg == null) {
FsMQMessage(FsMQIdentity from, String msg) throws IllegalStateException {
if (from == null || msg == null) {
throw new IllegalStateException("from and msg cant be null");
}
this.from = from;
@ -173,10 +187,10 @@ class FsMQMessage implements java.io.Serializable {
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
Object obj = ois.readObject();
ois.close();
if(!(obj instanceof FsMQMessage)) {
if (!(obj instanceof FsMQMessage)) {
throw new ClassNotFoundException("Unvalid serialized string");
} else {
ret = (FsMQMessage) obj;
ret = (FsMQMessage) obj;
}
return ret;
}
@ -197,8 +211,3 @@ class FsMQMessage implements java.io.Serializable {
}
class UnknownIdentityException extends RuntimeException {
UnknownIdentityException(String message) {
super(message);
}
}

7
test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/UnknownIdentityException.java

@ -0,0 +1,7 @@
package foundation.pEp.jniadapter.test.utils.transport.fsmqmanager;
public class UnknownIdentityException extends RuntimeException {
UnknownIdentityException(String message) {
super(message);
}
}

2
test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/Makefile.conf

@ -5,4 +5,6 @@ JAVA=java -enableassertions
JAVA_CLASSES_FSMSGQUEUE= \
../../FsMQManager.class \
../../FsMQIdentity.class \
../../UnknownIdentityException.class \
../../../fsmsgqueue/FsMsgQueue.class

116
test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/regression/TestMain.java

@ -1,21 +1,46 @@
package foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.test.regression;
import static foundation.pEp.jniadapter.test.framework.TestLogger.*;
import static foundation.pEp.jniadapter.test.framework.TestUtils.deleteRecursively;
import foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.*;
import foundation.pEp.jniadapter.test.framework.*;
import foundation.pEp.jniadapter.test.utils.transport.fsmsgqueue.FsMsgQueue;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.TimeoutException;
class FsMsgQueueTestContext extends AbstractTestContext {
String ownAddress = "Alice";
String ownQDir = "../resources/fsmsgqueue-test/alice";
String bobAddress = "Bob";
String bobQDir = "../resources/fsmsgqueue-test/bob";
FsMQIdentity bob = null;
int msgCount = 10;
ArrayList<String> messages;
FsMQManager qm;
FsMQIdentity self = null;
FsMQIdentity bob = null;
@Override
public void init() throws Throwable {
bob = new FsMQIdentity("Bob","../resources/fsmsgqueue-test/bob");
messages = createTestMessages(msgCount);
}
public java.util.ArrayList<String> createTestMessages(int count) {
log("Creating Test messages");
ArrayList<String> messages = new ArrayList<>();
for (int i = 0; i < count; i++) {
String msg = "TestMessage " + i;
// msg += "\nLine 2 of " + msg;
messages.add(msg);
log("Creating msg: " + msg);
}
return messages;
}
}
@ -24,18 +49,91 @@ class TestMain {
TestSuite.setVerbose(true);
FsMsgQueueTestContext testCtx = new FsMsgQueueTestContext();
new TestUnit<FsMsgQueueTestContext>("Constructor", testCtx, ctx -> {
log("Creating QM for: " + ctx.ownAddress);
ctx.qm = new FsMQManager(ctx.ownAddress, ctx.ownQDir);
new TestUnit<FsMsgQueueTestContext>("Create own ident: " + testCtx.ownAddress, testCtx, ctx -> {
ctx.self = new FsMQIdentity(ctx.ownAddress, ctx.ownQDir);
assert ctx.self != null : "null";
assert ctx.self.getAddress().equals(ctx.ownAddress): "Address mismatch";
assert ctx.self.getqDir().equals(ctx.ownQDir): "qDir mismatch";
}).add();
new TestUnit<FsMsgQueueTestContext>("Constructor with: " + testCtx.ownAddress, testCtx, ctx -> {
ctx.qm = new FsMQManager(ctx.self);
assert ctx.qm != null : "null";
}).add();
new TestUnit<FsMsgQueueTestContext>("Ident known: " + testCtx.ownAddress, testCtx, ctx -> {
FsMQIdentity self = ctx.qm.getIdentityForAddress(ctx.ownAddress);
assert self != null : "null";
assert self.equals(ctx.self) : "Obj mismatch";
log("Address: " + self.getAddress());
log("qDir: " + self.getqDir());
}).add();
new TestUnit<FsMsgQueueTestContext>("Create ident " + testCtx.bobAddress, testCtx, ctx -> {
ctx.bob = new FsMQIdentity(ctx.bobAddress, ctx.bobQDir);
assert ctx.bob != null : "null";
assert ctx.bob.getAddress().equals(ctx.bobAddress) : "Address mismatch";
assert ctx.bob.getqDir().equals(ctx.bobQDir) : "qDir mismatch";
}).add();
new TestUnit<FsMsgQueueTestContext>("Clear own queue", testCtx, ctx -> {
new TestUnit<FsMsgQueueTestContext>("Ident unknown: " + testCtx.bobAddress, testCtx, ctx -> {
try {
FsMQIdentity self = ctx.qm.getIdentityForAddress(ctx.bobAddress);
} catch (UnknownIdentityException e) {
return;
}
assert false : "Ident is known but shouldnt";
}).add();
new TestUnit<FsMsgQueueTestContext>("Add ident " + testCtx.bobAddress, testCtx, ctx -> {
assert ctx.qm.addOrUpdateIdentity(ctx.bob) : "Identity updated but should have been added";
}).add();
new TestUnit<FsMsgQueueTestContext>("Ident known: " + testCtx.bobAddress, testCtx, ctx -> {
FsMQIdentity bob = ctx.qm.getIdentityForAddress(ctx.bobAddress);
assert bob.equals(ctx.bob) : "Obj mismatch";
}).add();
new TestUnit<FsMsgQueueTestContext>("Update ident " + testCtx.bobAddress, testCtx, ctx -> {
assert !ctx.qm.addOrUpdateIdentity(ctx.bob) : "Ident got added but should have been updated";
}).add();
new TestUnit<FsMsgQueueTestContext>("ClearOwnQueue: " + testCtx.bobAddress, testCtx, ctx -> {
ctx.qm.clearOwnQueue();
}).add();
new TestUnit<FsMsgQueueTestContext>("Add ident bob", testCtx, ctx -> {
log("Adding ident: " + ctx.bob.getAddress());
ctx.qm.addOrUpdateIdentity(ctx.bob);
new TestUnit<FsMsgQueueTestContext>("waitForMsg timeout", testCtx, ctx -> {
log("waitForMessage with timeout...");
try {
ctx.qm.waitForMsg(3);
} catch(IOException e) {
throw new RuntimeException(e.toString());
} catch(ClassNotFoundException e) {
throw new RuntimeException(e.toString());
} catch (TimeoutException e) {
return;
}
}).add();
new TestUnit<FsMsgQueueTestContext>("sendMsgTo self " + testCtx.ownAddress, testCtx, ctx -> {
String msg = ctx.messages.get(0);
log("TX MSG: " + msg);
try {
ctx.qm.sendMsgToIdentity(ctx.self, msg);
} catch(IOException e) {
throw new RuntimeException(e.toString());
}
}).add();
new TestUnit<FsMsgQueueTestContext>("waitForMsg", testCtx, ctx -> {
String msg = null;
try {
msg = ctx.qm.waitForMsg(10);
} catch(Exception e) {
throw new RuntimeException(e.toString());
}
log("RX MSG: " + msg);
assert msg.equals(ctx.messages.get(0));
}).add();

Loading…
Cancel
Save