
12 changed files with 443 additions and 117 deletions
@ -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; |
||||
|
} |
||||
|
} |
@ -1,6 +1,6 @@ |
|||||
include ../Makefile.conf |
include ../Makefile.conf |
||||
|
|
||||
TEST_UNIT_NAME=regression |
TEST_UNIT_NAME=identities |
||||
|
|
||||
JAVA_CLASSES = \
|
JAVA_CLASSES = \
|
||||
TestMain.class \
|
TestMain.class \
|
@ -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) |
@ -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(); |
||||
|
} |
||||
|
} |
@ -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) |
@ -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(); |
||||
|
} |
||||
|
} |
@ -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; |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue