From 9ee569cf473bbad987d4fe38a1342876d0d4dfee Mon Sep 17 00:00:00 2001 From: heck Date: Mon, 5 Jul 2021 15:42:04 +0200 Subject: [PATCH] JNI-161: Test message alloc/dealloc is thread-safe --- .../pEp/jniadapter/test/jni161/Makefile | 34 +++++ .../pEp/jniadapter/test/jni161/TestAlice.java | 132 ++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 test/java/foundation/pEp/jniadapter/test/jni161/Makefile create mode 100644 test/java/foundation/pEp/jniadapter/test/jni161/TestAlice.java diff --git a/test/java/foundation/pEp/jniadapter/test/jni161/Makefile b/test/java/foundation/pEp/jniadapter/test/jni161/Makefile new file mode 100644 index 0000000..98958e7 --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/jni161/Makefile @@ -0,0 +1,34 @@ + include ../../../../../../../Makefile.conf +include ../Makefile.conf + +TEST_UNIT_NAME=jni161 + +JAVA_CLASSES+= \ + TestAlice.class + +.PHONY: pitytest compile alice test clean + +all: alice compile + +pitytest: + $(MAKE) -C $(PITYTEST_DIR) + +alice: compile clean-pep-home-alice + cd $(JAVA_CWD);pwd;HOME=$(JAVA_PEP_HOME_DIR_ALICE) $(JAVA) $(JAVA_PKG_BASENAME).$(TEST_UNIT_NAME).TestAlice + +compile: $(JAVA_CLASSES) pitytest + +%.class: %.java + cd $(JAVA_CWD);javac -cp $(CLASSPATH) $(JAVA_PKG_BASEPATH)/$(TEST_UNIT_NAME)/$< + +clean: + rm -f $(JAVA_CLASSES) + rm -f *.class + rm -f *.log + rm -Rf .gnupg + rm -Rf .lldb + +clean-pep-home: clean-pep-home-alice + +clean-pep-home-alice: + rm -rf $(PEP_HOME_DIR_ALICE)/.pEp diff --git a/test/java/foundation/pEp/jniadapter/test/jni161/TestAlice.java b/test/java/foundation/pEp/jniadapter/test/jni161/TestAlice.java new file mode 100644 index 0000000..bca15c0 --- /dev/null +++ b/test/java/foundation/pEp/jniadapter/test/jni161/TestAlice.java @@ -0,0 +1,132 @@ +package foundation.pEp.jniadapter.test.jni161; + +import foundation.pEp.jniadapter.Engine; +import foundation.pEp.jniadapter.Identity; +import foundation.pEp.jniadapter.Message; +import foundation.pEp.jniadapter.test.utils.AdapterTestUtils; +import foundation.pEp.jniadapter.test.utils.model.Role; +import foundation.pEp.jniadapter.test.utils.model.TestModel; +import foundation.pEp.jniadapter.test.utils.model.TestNode; +import foundation.pEp.jniadapter.test.utils.model.pEpTestIdentity; +import foundation.pEp.pitytest.AbstractTestContext; +import foundation.pEp.pitytest.TestSuite; +import foundation.pEp.pitytest.TestUnit; +import foundation.pEp.pitytest.utils.TestUtils; + +import java.util.Vector; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadPoolExecutor; + +import static foundation.pEp.pitytest.TestLogger.log; + + +class Jni161TestContext extends AbstractTestContext { + // Model + public TestModel> model = new TestModel(pEpTestIdentity::new, TestNode::new); + + // Basic + public Engine engine; + + // Identities + public Identity alice; + public Identity bob; + + public int blobSizeMB = 1; + public int blobCount = 3; + + public Vector messages = new Vector<>(); + + public Jni161TestContext init() throws Throwable { + engine = new Engine(); + // fetch data for the idents + alice = model.getIdent(Role.ALICE).pEpIdent; + bob = model.getIdent(Role.BOB).pEpIdent; + + // create keys etc.. + alice = engine.myself(alice); + bob = engine.myself(bob); + + return this; + } +} + +class TestAlice { + public static void main(String[] args) throws Exception { + TestSuite.getDefault().setVerbose(true); + TestSuite.getDefault().setTestColor(TestUtils.TermColor.GREEN); + + Jni161TestContext ctx161 = new Jni161TestContext(); + + new TestUnit("Proof leaking messages ", ctx161, ctx -> { + while (true) { + log("Thread: " + Thread.currentThread().getId() + " - Alloc"); + Message msg = AdapterTestUtils.makeNewTestMessage(ctx.alice, ctx.bob, Message.Direction.Outgoing); + msg.setAttachments(AdapterTestUtils.makeNewTestBlobList(ctx.blobSizeMB * 1024 * 1024, "dummyblob", "text/plain", ctx.blobCount)); + msg.setLongmsg(TestUtils.randomASCIIString(TestUtils.EASCIICharClassName.Alpha, ctx.blobSizeMB * 1024 * 1024)); +// msg.close(); + TestUtils.sleep(10); + } + }); + + new TestUnit("Proof leaking messages ", ctx161, ctx -> { + new Thread(() -> { + while (true) { + log("Thread: " + Thread.currentThread().getId() + " - Alloc"); + Message msg = AdapterTestUtils.makeNewTestMessage(ctx.alice, ctx.bob, Message.Direction.Outgoing); + msg.setAttachments(AdapterTestUtils.makeNewTestBlobList(ctx.blobSizeMB * 1024 * 1024, "dummyblob", "text/plain", ctx.blobCount)); + msg.setLongmsg(TestUtils.randomASCIIString(TestUtils.EASCIICharClassName.Alpha, ctx.blobSizeMB * 1024 * 1024)); + ctx.messages.add(msg); + } + }).start(); + + new Thread(() -> { + while (true) { +// log("Thread: " + Thread.currentThread().getId() + " - Dealloc"); + if(ctx.messages.size() > 0) { + log("Messages: " + ctx.messages.size()); + log("InstanceCount: " + Message.getInstanceCount()); + ctx.messages.remove(0).close(); + } + } + }).start(); + }).run(); + + + new TestUnit("Proof leaking messages ", ctx161, ctx -> { + ExecutorService allocPool = Executors.newFixedThreadPool(10); + ExecutorService deallocPool = Executors.newFixedThreadPool(10); + while (true) { + ThreadPoolExecutor allocExec = (ThreadPoolExecutor) allocPool; + if (allocExec.getActiveCount() < allocExec.getMaximumPoolSize()) { + allocPool.submit(() -> { + log("Thread: " + Thread.currentThread().getId() + " - Alloc"); + Message msg = AdapterTestUtils.makeNewTestMessage(ctx.alice, ctx.bob, Message.Direction.Outgoing); + msg.setAttachments(AdapterTestUtils.makeNewTestBlobList(ctx.blobSizeMB * 1024 * 1024, "dummyblob", "text/plain", ctx.blobCount)); + msg.setLongmsg(TestUtils.randomASCIIString(TestUtils.EASCIICharClassName.Alpha, ctx.blobSizeMB * 1024 * 1024)); + ctx.messages.add(msg); + log("MessagesADD: " + ctx.messages.size()); + }); + } + + if (ctx.messages.size() > 1) { + ThreadPoolExecutor deallocExec = (ThreadPoolExecutor) deallocPool; + if (deallocExec.getActiveCount() < deallocExec.getMaximumPoolSize()) { + deallocPool.submit(() -> { + log("Thread: " + Thread.currentThread().getId() + " - Dealloc"); + log("Messages: " + ctx.messages.size()); + log("InstanceCount: " + Message.getInstanceCount()); + for (Message msg : ctx.messages) { + msg.close(); + } + }); + } + } + TestUtils.sleep(10); + } + }); +// TestSuite.getDefault().run(); // comment this out + } +} + +