Browse Source

more more more tests for FsMQManager

JNI-96
heck 5 years ago
parent
commit
4c0a4d98df
  1. 6
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQIdentity.java
  2. 119
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/FsMQManager.java
  3. 4
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/Makefile.conf
  4. 40
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/ctx/FsMQManagerBaseTestContext.java
  5. 2
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/identities/Makefile
  6. 46
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/identities/TestMain.java
  7. 24
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/stateless_rxtx/Makefile
  8. 187
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/stateless_rxtx/TestMain.java
  9. 29
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/stateless_rxtx_mp/Makefile
  10. 81
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/stateless_rxtx_mp/TestAlice.java
  11. 20
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/utils/FsMQManagerTestUtils.java
  12. 2
      test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/FsMsgQueue.java

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

@ -9,6 +9,12 @@ public class FsMQIdentity implements java.io.Serializable {
this.qDir = qDir;
}
public FsMQIdentity(FsMQIdentity ident) {
this.address = ident.address;
this.qDir = ident.qDir;
}
public String getAddress() {
return address;
}

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

@ -15,10 +15,6 @@ public class FsMQManager {
private List<FsMQIdentity> identities = new ArrayList<>();
private Map<String, FsMsgQueue> identityAddressQueues = new HashMap<String, FsMsgQueue>();
private static String SYNMSG = "SYN";
private static String SYNACKMSG = "SYNACK";
private static String ACKMSG = "ACK";
public FsMQManager(FsMQIdentity self) throws NullPointerException {
if (self != null) {
this.self = self;
@ -113,51 +109,6 @@ public class FsMQManager {
return ret;
}
public void sendMessage(String address, String msg) throws UnknownIdentityException, IOException, NullPointerException {
if (address != null) {
if (msg != null) {
FsMQMessageInternal mqMsg = new FsMQMessageInternal(self, msg);
String serializedStr = mqMsg.serialize();
getQueueForIdentity(address).add(serializedStr);
} else {
throw new NullPointerException("msg cant be null");
}
} else {
throw new NullPointerException("address cant be null");
}
}
public void clearOwnQueue() {
getQueueForIdentity(self.getAddress()).clear();
}
public FsMQMessage receiveMessage() throws IOException, ClassNotFoundException, TimeoutException {
return receiveMessage(0);
}
// Blocks until timeout
public FsMQMessage receiveMessage(int timeoutSec) throws IOException, ClassNotFoundException, TimeoutException {
FsMQMessage ret = null;
FsMsgQueue onwQueue = getQueueForIdentity(self.getAddress());
FsMQMessageInternal 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 = FsMQMessageInternal.deserialize(serializedMsg);
} while (doHandshakeProtocol(mqMsg));
ret = mqMsg.toFsMQMessage();
return ret;
}
// True if existing
// False if not
// Exception on not unique
@ -177,7 +128,6 @@ public class FsMQManager {
return ret;
}
// Returns null if not existing
public FsMQIdentity getIdentityForAddress(String address) {
FsMQIdentity ret = null;
@ -225,15 +175,54 @@ public class FsMQManager {
return ret;
}
// public void handshake(FsMQIdentity ident) {
// String msg = "";
// sendSYN(ident);
// while (msg != SYNACKMSG + " " + ident.getAddress()) {
// log("Waiting for SYNACK from " + ident.getAddress());
// msg = waitForMsg();
// }
// sendACK(ident);
// }
public void clearOwnQueue() {
getQueueForIdentity(self.getAddress()).clear();
}
public void sendMessage(String address, String msg) throws UnknownIdentityException, IOException, NullPointerException {
if (address != null) {
if (msg != null) {
FsMQMessageInternal mqMsg = new FsMQMessageInternal(self, msg);
String serializedStr = mqMsg.serialize();
getQueueForIdentity(address).add(serializedStr);
} else {
throw new NullPointerException("msg cant be null");
}
} else {
throw new NullPointerException("address cant be null");
}
}
// Non blocking read
// Returns null if no messages available
public FsMQMessage receiveMessage() throws IOException, ClassNotFoundException, TimeoutException {
return receiveMessage(0);
}
// Blocking read
// Returns null if no messages available
public FsMQMessage receiveMessage(int timeoutSec) throws IOException, ClassNotFoundException, TimeoutException {
FsMQMessage ret = null;
FsMsgQueue onwQueue = getQueueForIdentity(self.getAddress());
FsMQMessageInternal mqMsg = null;
int pollInterval = 100;
int pollRepeats = timeoutSec * 1000 / pollInterval;
int pollCounter = 0;
do {
while (onwQueue.isEmpty()) {
TestUtils.sleep(pollInterval);
pollCounter++;
if (pollCounter >= pollRepeats) {
return ret;
}
}
String serializedMsg = onwQueue.remove();
mqMsg = FsMQMessageInternal.deserialize(serializedMsg);
} while (doHandshakeProtocol(mqMsg));
ret = mqMsg.toFsMQMessage();
return ret;
}
private boolean doHandshakeProtocol(FsMQMessageInternal msg) {
boolean ret = false;
@ -248,18 +237,6 @@ public class FsMQManager {
return ret;
}
// public void sendSYN(FsMQIdentity ident) {
// String msg = SYNMSG + " " + self.getAddress();
// log("Sending SYN to: " + ident.getAddress());
// sendMsgToIdentity(ident, msg);
// }
//
// public void sendACK(FsMQIdentity ident) {
// String msg = ACKMSG + " " + self.getAddress();
// log("Sending ACK to: " + ident.getAddress());
// sendMsgToIdentity(ident, msg);
// }
}
class FsMQMessageInternal extends FsMQMessage implements java.io.Serializable {

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

@ -9,3 +9,7 @@ JAVA_CLASSES_FSMSGQUEUE= \
../../FsMQMessage.class \
../../UnknownIdentityException.class \
../../../fsmsgqueue/FsMsgQueue.class
JAVA_CLASSES_TESTSCOMMON= \
../utils/FsMQManagerTestUtils.class \
../ctx/FsMQManagerBaseTestContext.class

40
test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/ctx/FsMQManagerBaseTestContext.java

@ -0,0 +1,40 @@
package foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.test.ctx;
import foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.*;
import foundation.pEp.jniadapter.test.framework.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FsMQManagerBaseTestContext extends AbstractTestContext {
private String qDirBase = "../resources/fsmsgqueue-test/";
private List<String> peerNames = null;
public Map<String, FsMQIdentity> peerMap = null;
public List<FsMQIdentity> peerList = null;
@Override
public void init() throws Throwable {
peerNames = new ArrayList<>();
peerNames.add("Alice");
peerNames.add("Bob");
peerNames.add("Carol");
createPeerMapAndPeerList();
}
private void createPeerMapAndPeerList() {
peerMap = new HashMap<>();
peerList = new ArrayList<>();
for (String addr : peerNames) {
FsMQIdentity ident = new FsMQIdentity(addr, getQDir(addr));
peerMap.put(addr, ident);
peerList.add(ident);
}
}
private String getQDir(String address) {
return qDirBase + "/" + address;
}
}

2
test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/regression/Makefile → test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/identities/Makefile

@ -1,6 +1,6 @@
include ../Makefile.conf
TEST_UNIT_NAME=regression
TEST_UNIT_NAME=identities
JAVA_CLASSES = \
TestMain.class \

46
test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/regression/TestMain.java → test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/identities/TestMain.java

@ -1,4 +1,4 @@
package foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.test.regression;
package foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.test.identities;
import static foundation.pEp.jniadapter.test.framework.TestLogger.*;
@ -222,48 +222,6 @@ class TestMain {
assert ctx.qm.getIdentityForAddress("UNKNOWN") == null : "Found an unknown address";
}).add();
new TestUnit<FsMQManagerTestContext>("ClearOwnQueue: " + testCtx.bobAddress, testCtx, ctx -> {
ctx.qm.clearOwnQueue();
}).add();
new TestUnit<FsMQManagerTestContext>("waitForMsg timeout", testCtx, ctx -> {
log("waitForMessage with timeout...");
try {
ctx.qm.receiveMessage(1);
} catch (IOException e) {
throw new RuntimeException(e.toString());
} catch (ClassNotFoundException e) {
throw new RuntimeException(e.toString());
} catch (TimeoutException e) {
return;
}
}).add();
new TestUnit<FsMQManagerTestContext>("sendMsgTo self " + testCtx.ownAddress, testCtx, ctx -> {
String msg = ctx.messages.get(0);
log("TX MSG: " + msg);
try {
ctx.qm.sendMessage(ctx.self.getAddress(), msg);
} catch (IOException e) {
throw new RuntimeException(e.toString());
}
}).add();
new TestUnit<FsMQManagerTestContext>("waitForMsg", testCtx, ctx -> {
FsMQMessage msg = null;
try {
msg = ctx.qm.receiveMessage(10);
} catch (Exception e) {
throw new RuntimeException(e.toString());
}
log("RX MSG: \n" + msg.toString());
assert msg.getMsg().equals(ctx.messages.get(0)) : "message content mismatch";
}).add();
TestSuite.run();
}
}
}

24
test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/stateless_rxtx/Makefile

@ -0,0 +1,24 @@
include ../Makefile.conf
TEST_UNIT_NAME=stateless_rxtx
JAVA_CLASSES = \
TestMain.class \
JAVA_CLASSES += $(JAVA_CLASSES_FSMSGQUEUE)
.PHONY: compile run test clean
all: compile
$(MAKE) run
run: compile
cd $(JAVA_CWD);$(JAVA) $(JAVA_PKG_BASENAME).$(TEST_UNIT_NAME).TestMain
compile: $(JAVA_CLASSES)
%.class: %.java
cd $(JAVA_CWD);pwd;javac $(JAVA_PKG_BASEPATH)/$(TEST_UNIT_NAME)/$<
clean:
rm -f $(JAVA_CLASSES)

187
test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/stateless_rxtx/TestMain.java

@ -0,0 +1,187 @@
package foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.test.stateless_rxtx;
import static foundation.pEp.jniadapter.test.framework.TestLogger.*;
import foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.*;
import foundation.pEp.jniadapter.test.framework.*;
import java.io.IOException;
import java.util.ArrayList;
class FsMQManagerTestContext extends AbstractTestContext {
Entity alice;
Entity bob;
Entity carol;
@Override
public void init() throws Throwable {
alice = new Entity("Alice");
bob = new Entity("Bob");
carol = new Entity("Carol");
alice.add(bob);
alice.add(carol);
}
class Entity {
public String name = "Undefined";
private String qDirBase = "../resources/fsmsgqueue-test/";
public FsMQIdentity ident = null;
public FsMQManager qm = null;
int msgCount = 10;
ArrayList<String> messages;
Entity(String name) {
log("Creating entity: " + name);
this.name = name;
String qDir = qDirBase + "/" + name;
ident = new FsMQIdentity(name, qDir);
qm = new FsMQManager(ident);
messages = createTestMessages(msgCount);
}
public void add(Entity ent) {
qm.addOrUpdateIdentity(ent.ident);
}
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 = ident.getAddress() + "TestMessage nr: " + i;
// msg += "\nLine 2 of " + msg;
log("Creating msg: " + msg);
messages.add(msg);
}
return messages;
}
}
}
class TestMain {
public static void main(String[] args) throws Exception {
TestSuite.setVerbose(true);
FsMQManagerTestContext testCtx = new FsMQManagerTestContext();
new TestUnit<FsMQManagerTestContext>("a/b/c ClearOwnQueue: ", testCtx, ctx -> {
ctx.alice.qm.clearOwnQueue();
ctx.bob.qm.clearOwnQueue();
ctx.carol.qm.clearOwnQueue();
}).add();
new TestUnit<FsMQManagerTestContext>("alice rx with timeout", testCtx, ctx -> {
log("waitForMessage with timeout...");
FsMQMessage msg = null;
try {
assert ctx.alice.qm.receiveMessage(1) == null;
assert ctx.bob.qm.receiveMessage(0) == null;
assert ctx.carol.qm.receiveMessage(0) == null;
} catch (Exception e) {
assert false : "Error receiving message";
}
}).add();
new TestUnit<FsMQManagerTestContext>("tx to null fails", testCtx, ctx -> {
try {
ctx.alice.qm.sendMessage(null, "");
} catch (Exception e) {
return;
}
assert false : "receiver cant be null";
}).add();
new TestUnit<FsMQManagerTestContext>("tx null msg fails", testCtx, ctx -> {
try {
ctx.alice.qm.sendMessage(ctx.bob.name, null);
} catch (Exception e) {
return;
}
assert false : "msg cant be null";
}).add();
new TestUnit<FsMQManagerTestContext>("a2a rx==tx seq", testCtx, ctx -> {
for (int i = 0; i < ctx.alice.msgCount; i++) {
String msg = ctx.alice.messages.get(i);
log("TX MSG: " + msg);
try {
ctx.alice.qm.sendMessage(ctx.alice.name, msg);
} catch (Exception e) {
throw new RuntimeException(e.toString());
}
}
FsMQMessage msgRx = null;
try {
int msgNr = 0;
while ((msgRx = ctx.alice.qm.receiveMessage()) != null) {
log("RX MSG: \n" + msgRx.toString());
assert msgRx != null : "null";
assert msgRx.getFrom().getAddress().equals(ctx.alice.name) : "msg from wrong";
assert msgRx.getMsg().equals(ctx.alice.messages.get(msgNr)) : "message content mismatch";
msgNr++;
}
log("No msgs available");
assert msgRx == null : "java is broken";
} catch (Exception e) {
throw new RuntimeException(e.toString());
}
}).add();
new TestUnit<FsMQManagerTestContext>("a2b rx==tx seq", testCtx, ctx -> {
for (int i = 0; i < ctx.alice.msgCount; i++) {
String msg = ctx.alice.messages.get(i);
log("TX MSG: " + msg);
try {
ctx.alice.qm.sendMessage(ctx.bob.name, msg);
} catch (Exception e) {
throw new RuntimeException(e.toString());
}
}
FsMQMessage msgRx = null;
try {
int msgNr = 0;
while ((msgRx = ctx.bob.qm.receiveMessage()) != null) {
log("RX MSG: \n" + msgRx.toString());
assert msgRx != null : "null";
assert msgRx.getFrom().getAddress().equals(ctx.alice.name) : "msg from wrong";
assert msgRx.getMsg().equals(ctx.alice.messages.get(msgNr)) : "message content mismatch";
msgNr++;
}
log("No msgs available");
assert msgNr == ctx.alice.msgCount : "msgcount wrong";
assert msgRx == null : "java is broken";
} catch (Exception e) {
throw new RuntimeException(e.toString());
}
}).add();
new TestUnit<FsMQManagerTestContext>("b2a not known", testCtx, ctx -> {
try {
ctx.bob.qm.sendMessage(ctx.alice.name, "WONT ARRIVE");
} catch (UnknownIdentityException e) {
return;
} catch (Exception e) {
}
assert false : "identity should not be known";
}).add();
new TestUnit<FsMQManagerTestContext>("b add a, tx again", testCtx, ctx -> {
ctx.bob.add(ctx.alice);
try {
ctx.bob.qm.sendMessage(ctx.alice.name, ctx.bob.messages.get(0));
} catch (UnknownIdentityException e) {
assert false : "should be known now";
} catch (Exception e) {
assert false : e.toString();
}
}).add();
TestSuite.run();
}
}

29
test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/stateless_rxtx_mp/Makefile

@ -0,0 +1,29 @@
include ../Makefile.conf
TEST_UNIT_NAME=stateless_rxtx_mp
JAVA_CLASSES = \
TestAlice.class \
../utils/FsMQManagerTestUtils.class
# TestBob.class
JAVA_CLASSES += $(JAVA_CLASSES_FSMSGQUEUE)
JAVA_CLASSES += $(JAVA_CLASSES_TESTSCOMMON)
.PHONY: all alice bob compile clean
all: alice compile
alice: compile
cd $(JAVA_CWD);$(JAVA) $(JAVA_PKG_BASENAME).$(TEST_UNIT_NAME).TestAlice
bob: compile
cd $(JAVA_CWD);$(JAVA) $(JAVA_PKG_BASENAME).$(TEST_UNIT_NAME).TestBob
compile: $(JAVA_CLASSES)
%.class: %.java
cd $(JAVA_CWD);pwd;javac $(JAVA_PKG_BASEPATH)/$(TEST_UNIT_NAME)/$<
clean:
rm -f $(JAVA_CLASSES)

81
test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/stateless_rxtx_mp/TestAlice.java

@ -0,0 +1,81 @@
package foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.test.stateless_rxtx_mp;
import static foundation.pEp.jniadapter.test.framework.TestLogger.*;
import foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.*;
import foundation.pEp.jniadapter.test.framework.*;
import foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.test.ctx.*;
import foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.test.utils.*;
import java.util.ArrayList;
import java.util.List;
class FsMQManagerTestContext extends FsMQManagerBaseTestContext {
private String selfAddress = null;
public FsMQIdentity self = null;
public FsMQManager qm;
private int MSG_COUNT = 10;
private List<String> messages;
public FsMQManagerTestContext(String selfAddress) {
this.selfAddress = selfAddress;
}
@Override
public void init() throws Throwable {
super.init();
defineSelfAndUpdatePeers();
qm = new FsMQManager(self);
qm.addIdentities(peerList);
messages = FsMQManagerTestUtils.createTestMessages(self.getAddress(), MSG_COUNT);
}
private void defineSelfAndUpdatePeers() {
self = peerMap.get(selfAddress);
if (self == null) {
throw new RuntimeException("selfAddress not found");
}
peerMap.remove(selfAddress);
peerList.removeIf(p -> p.getAddress().equals(self.getAddress()));
}
public List<String> getMessages() {
return messages;
}
}
class TestAlice {
public static void main(String[] args) throws Exception {
TestSuite.setVerbose(true);
String myself = "Alice";
FsMQManagerTestContext testCtx = new FsMQManagerTestContext(myself);
new TestUnit<FsMQManagerTestContext>("I am: " + myself, testCtx, ctx -> {
log("I am: " + ctx.self.getAddress());
assert ctx.self.getAddress().equals(myself);
}).add();
new TestUnit<FsMQManagerTestContext>("I know Bob and Carol", testCtx, ctx -> {
log("I know:");
log("QM");
for (FsMQIdentity ident : ctx.qm.getIdentities()) {
log(ident.toString());
}
log("PeerMap:");
for (String addr : ctx.peerMap.keySet()) {
log(addr);
}
log("PeerList:");
for (FsMQIdentity ident : ctx.peerList) {
log(ident.getAddress());
}
assert !ctx.peerMap.containsKey(myself) : "peers should not contain" + myself;
assert ctx.peerMap.containsKey("Bob") : "peers must contain Bob";
assert ctx.peerMap.containsKey("Carol") : "peers must contain Carol";
}).add();
TestSuite.run();
}
}

20
test/java/foundation/pEp/jniadapter/test/utils/transport/fsmqmanager/test/utils/FsMQManagerTestUtils.java

@ -0,0 +1,20 @@
package foundation.pEp.jniadapter.test.utils.transport.fsmqmanager.test.utils;
import java.util.ArrayList;
import java.util.List;
import static foundation.pEp.jniadapter.test.framework.TestLogger.log;
public class FsMQManagerTestUtils {
public static List<String> createTestMessages(String from, int count) {
log("Creating Test messages");
List<String> messages = new ArrayList<>();
for (int i = 0; i < count; i++) {
String msg = from + " says: 'TestMessage nr: [" + i + "]'";
// msg += "\nLine 2 of " + msg;
messages.add(msg);
log("Creating msg: " + msg);
}
return messages;
}
}

2
test/java/foundation/pEp/jniadapter/test/utils/transport/fsmsgqueue/FsMsgQueue.java

@ -239,7 +239,7 @@ public class FsMsgQueue implements Queue<String> {
File oldestFile = getOldestMsgFilename();
log("reading file:" + oldestFile.getName());
if (oldestFile == null) {
throw new NoSuchElementException("MNo .msg file in dir: " + qDir);
throw new NoSuchElementException("No .msg file in dir: " + qDir);
} else {
String fContent = null;
fContent = readFile(oldestFile.toPath(), fileEncoding);

Loading…
Cancel
Save