From fd686c0f2e860926bc07288a20b7ea37c147420a Mon Sep 17 00:00:00 2001 From: heck Date: Thu, 16 Apr 2020 21:34:57 +0200 Subject: [PATCH] Tests: improved Logging, prefix threadName Test: jni92, single vs multithreaded mass instantiation of Engine --- .../pEp/jniadapter/test/basic/TestMain.java | 1 - .../jniadapter/test/jni92/SyncCallbacks.java | 4 +- .../pEp/jniadapter/test/jni92/TestMain.java | 109 ++++++++++-------- .../pEp/jniadapter/test/utils/TestUtils.java | 12 +- 4 files changed, 69 insertions(+), 57 deletions(-) diff --git a/test/java/foundation/pEp/jniadapter/test/basic/TestMain.java b/test/java/foundation/pEp/jniadapter/test/basic/TestMain.java index a80e5b7..df44d03 100644 --- a/test/java/foundation/pEp/jniadapter/test/basic/TestMain.java +++ b/test/java/foundation/pEp/jniadapter/test/basic/TestMain.java @@ -25,7 +25,6 @@ class TestMain { public static void main(String[] args) { Engine engine; - TestUtils.log("fds"); // load try { engine = new Engine(); diff --git a/test/java/foundation/pEp/jniadapter/test/jni92/SyncCallbacks.java b/test/java/foundation/pEp/jniadapter/test/jni92/SyncCallbacks.java index 5496f25..69fe067 100644 --- a/test/java/foundation/pEp/jniadapter/test/jni92/SyncCallbacks.java +++ b/test/java/foundation/pEp/jniadapter/test/jni92/SyncCallbacks.java @@ -10,7 +10,7 @@ class SyncCallbacks implements Sync.MessageToSendCallback, Sync.NotifyHandshakeC TestUtils.log("To: " + message.getTo()); TestUtils.log("Subject: " + message.getShortmsg()); TestUtils.log("Attachement[0]: " + message.getAttachments().get(0).toString()); - TestUtils.logSectEnd(); + TestUtils.logSectEnd(""); } public void notifyHandshake(Identity myself, Identity partner, SyncHandshakeSignal signal) @@ -19,6 +19,6 @@ class SyncCallbacks implements Sync.MessageToSendCallback, Sync.NotifyHandshakeC TestUtils.log("Myself: " + myself); TestUtils.log("Partner: " + partner); TestUtils.log("Signal: " + signal); - TestUtils.logSectEnd(); + TestUtils.logSectEnd(""); } } \ No newline at end of file diff --git a/test/java/foundation/pEp/jniadapter/test/jni92/TestMain.java b/test/java/foundation/pEp/jniadapter/test/jni92/TestMain.java index 417a086..aaff4e9 100644 --- a/test/java/foundation/pEp/jniadapter/test/jni92/TestMain.java +++ b/test/java/foundation/pEp/jniadapter/test/jni92/TestMain.java @@ -13,71 +13,82 @@ https://pep.foundation/jira/browse/JNI-81 `engine.key_reset_identity` and `engine.setMessageToSendCallback` */ - - class TestThread extends Thread { - private String threadName = "TestThread-1"; - private Engine engine = null; - TestThread(String threadName) { - this.threadName = threadName; + Thread.currentThread().setName(threadName); } public void run() { - TestUtils.logH1(threadName + ": Starting thread"); - - try { - // load engine - try { - engine = new Engine(); - SyncCallbacks callbacks = new SyncCallbacks(); - engine.setMessageToSendCallback(callbacks); - engine.setNotifyHandshakeCallback(callbacks); - } catch (pEpException ex) { - TestUtils.log(threadName + ": cannot load"); - return; - } - TestUtils.log(threadName + ": Engine loaded"); - if(!engine.isSyncRunning()) { - engine.startSync(); - } - - } catch (Exception e) { - TestUtils.log("Exception in Thread " + threadName); - TestUtils.log(e.toString()); - } - TestUtils.sleep(20000); - if(engine.isSyncRunning()) { - engine.stopSync(); - } - TestUtils.log(threadName + ": DONE"); + TestUtils.logH1( "Thread Starting"); + TestMain.TestMainRun(2); } } - class TestMain { - public static void main(String[] args) throws Exception { - TestUtils.logH1("JNI-92 Starting"); - Vector tts = new Vector(); - int nrThreads = 3; - for(int i = 0; i < nrThreads; i++){ - tts.add(new TestThread("TestThread-" + i)); + public static Engine createNewEngine() throws pEpException { + Engine e; + TestUtils.logH2("Creating new Engine"); + e = new Engine(); + TestUtils.log("Engine created\n"); + return e; + } + + public static Vector createEngines(int nrEngines) throws pEpException { + Vector ev = new Vector(); + for(int i = 0; i < nrEngines; i++) { + ev.add(createNewEngine()); } + return ev; + } - tts.forEach( t -> { - t.start(); - TestUtils.sleep(2000); + public static void own_identities_retrieve_on_EngineVector(Vector ev) { + ev.forEach(e -> { + TestUtils.logH2("own_identities_retrieve()"); + e.own_identities_retrieve(); + TestUtils.log("\n"); }); + } + + + + public static void TestMainRun(int nrEngines) { + Vector engineVector = TestMain.createEngines(nrEngines); +// TestUtils.sleep(200); + TestMain.own_identities_retrieve_on_EngineVector(engineVector); + } - tts.forEach( t -> { - try { - t.join(); - } catch(Exception e ){ - TestUtils.log("Exception joining thread" + e.toString()); + public static void main(String[] args) throws Exception { + TestUtils.logH1("JNI-92 Starting"); + boolean multiThreaded = true; + int nrEngines = 3; + + if (!multiThreaded) { + // Single Threaded + TestMainRun(nrEngines); + } else { + // Mutli Threaded + Vector tts = new Vector(); + int nrThreads = nrEngines; + for (int i = 0; i < nrThreads; i++) { + tts.add(new TestThread("TestThread-" + i)); +// TestUtils.sleep(200); } - }); + + tts.forEach(t -> { + t.start(); +// TestUtils.sleep(2000); + }); + + tts.forEach(t -> { + try { + t.join(); + } catch (Exception e) { + TestUtils.log("Exception joining thread" + e.toString()); + } + }); + } } } diff --git a/test/java/foundation/pEp/jniadapter/test/utils/TestUtils.java b/test/java/foundation/pEp/jniadapter/test/utils/TestUtils.java index 84b4b70..83d7d75 100644 --- a/test/java/foundation/pEp/jniadapter/test/utils/TestUtils.java +++ b/test/java/foundation/pEp/jniadapter/test/utils/TestUtils.java @@ -23,17 +23,19 @@ public class TestUtils { } public static void log(String msg) { - System.out.println(msg); + String threadNameFmt = String.format("%-10s", Thread.currentThread().getName()); + String msgOut = threadNameFmt + ": " + msg; + System.out.println(msgOut); } public static void logH1(String msg) { String decorationStr = getDecoratorString(msg, "="); - System.out.println(decorationStr + " " + msg.toUpperCase() + " " + decorationStr); + log(decorationStr + " " + msg.toUpperCase() + " " + decorationStr); } public static void logH2(String msg) { String decorationStr = getDecoratorString(msg, "-"); - System.out.println(decorationStr + " " + msg + " " + decorationStr); + log(decorationStr + " " + msg + " " + decorationStr); } private static String getDecoratorString(String msg, String s) { @@ -46,7 +48,7 @@ public class TestUtils { return decorationStr; } - public static void logSectEnd() { - System.out.println(""); + public static void logSectEnd(String msg) { + log(msg + "\n"); } } \ No newline at end of file