[DEV] externalize esignal
This commit is contained in:
parent
a851ede714
commit
4f16dc25fb
59
.classpath
59
.classpath
@ -23,32 +23,7 @@
|
||||
<attribute name="test" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry combineaccessrules="false" kind="src" path="/atriasoft-io-gami">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry combineaccessrules="false" exported="true" kind="src" path="/atriasoft-ejson">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry combineaccessrules="false" exported="true" kind="src" path="/atriasoft-etk">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry combineaccessrules="false" exported="true" kind="src" path="/scenarium-logger">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry combineaccessrules="false" exported="true" kind="src" path="/atriasoft-gale">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry combineaccessrules="false" exported="true" kind="src" path="/atriasoft-exml">
|
||||
<classpathentry combineaccessrules="false" kind="src" path="/loader3d">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
@ -58,7 +33,37 @@
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry combineaccessrules="false" kind="src" path="/loader3d">
|
||||
<classpathentry combineaccessrules="false" exported="true" kind="src" path="/atriasoft-exml">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry combineaccessrules="false" exported="true" kind="src" path="/atriasoft-gale">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry combineaccessrules="false" exported="true" kind="src" path="/scenarium-logger">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry combineaccessrules="false" exported="true" kind="src" path="/atriasoft-etk">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry combineaccessrules="false" exported="true" kind="src" path="/atriasoft-ejson">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry combineaccessrules="false" kind="src" path="/atriasoft-io-gami">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry combineaccessrules="false" exported="true" kind="src" path="/atriasoft-esignal">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
|
40
.gitignore
vendored
40
.gitignore
vendored
@ -1,11 +1,28 @@
|
||||
/bin/
|
||||
/Operator/
|
||||
/DrawerProperties/
|
||||
*.pdfd
|
||||
*.dbc
|
||||
SchedulerConfig.txt
|
||||
scenicView.properties
|
||||
ScenariumConfig.txt
|
||||
# ---> Java
|
||||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.nar
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
*.class
|
||||
*~
|
||||
*.bck
|
||||
@ -15,3 +32,10 @@ build.number
|
||||
/.settings/
|
||||
/junit/
|
||||
/target/
|
||||
|
||||
*.pdfd
|
||||
*.dbc
|
||||
SchedulerConfig.txt
|
||||
scenicView.properties
|
||||
ScenariumConfig.txt
|
||||
*.blend*
|
||||
|
@ -16,8 +16,7 @@ open module org.atriasoft.ewol {
|
||||
exports org.atriasoft.ewol.widget;
|
||||
//exports org.atriasoft.ewol.widget.meta;
|
||||
|
||||
exports org.atriasoft.esignal;
|
||||
|
||||
requires transitive org.atriasoft.esignal;
|
||||
requires transitive org.atriasoft.iogami;
|
||||
requires transitive org.atriasoft.gale;
|
||||
requires transitive org.atriasoft.etk;
|
||||
|
@ -1,47 +0,0 @@
|
||||
package org.atriasoft.esignal;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
public class Connection implements AutoCloseable {
|
||||
protected WeakReference<ConnectionRemoveInterface> connection;
|
||||
|
||||
public void connectionIsRemovedBySignal() {
|
||||
connection = null;
|
||||
}
|
||||
|
||||
public void disconnect() {
|
||||
close();
|
||||
}
|
||||
|
||||
public Connection( final ConnectionRemoveInterface object ) {
|
||||
this.connection = new WeakReference<>(object);
|
||||
}
|
||||
|
||||
public Connection() {
|
||||
this.connection = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (this.connection == null) {
|
||||
return;
|
||||
}
|
||||
ConnectionRemoveInterface tmp = this.connection.get();
|
||||
if (tmp == null) {
|
||||
return;
|
||||
}
|
||||
tmp.disconnect(this);
|
||||
this.connection = null;
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
if (this.connection == null) {
|
||||
return false;
|
||||
}
|
||||
ConnectionRemoveInterface tmp = this.connection.get();
|
||||
if (tmp == null) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
package org.atriasoft.esignal;
|
||||
|
||||
public interface ConnectionRemoveInterface {
|
||||
void disconnect(final Connection connection);
|
||||
}
|
@ -1,172 +0,0 @@
|
||||
package org.atriasoft.esignal;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
class ConnectedElement<T> {
|
||||
protected final WeakReference<Consumer<T>> consumer;
|
||||
|
||||
public ConnectedElement(final Consumer<T> consumer) {
|
||||
this.consumer = new WeakReference<Consumer<T>>(consumer);
|
||||
}
|
||||
|
||||
public Consumer<T> getConsumer() {
|
||||
return this.consumer.get();
|
||||
}
|
||||
|
||||
public boolean isCompatibleWith(final Object elem) {
|
||||
/*
|
||||
Object out = this.reference.get();
|
||||
if (out == elem) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
*/
|
||||
return false;
|
||||
}
|
||||
|
||||
public void disconnect() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
class ConnectedElementDynamic<T> extends ConnectedElement<T> {
|
||||
protected final WeakReference<Object> linkedObject;
|
||||
|
||||
public ConnectedElementDynamic(Object linkedObject, final Consumer<T> consumer) {
|
||||
super(consumer);
|
||||
this.linkedObject = new WeakReference<Object>(linkedObject);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Consumer<T> getConsumer() {
|
||||
if (this.linkedObject.get() == null) {
|
||||
return null;
|
||||
}
|
||||
return this.consumer.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCompatibleWith(final Object elem) {
|
||||
if (super.isCompatibleWith(elem)) {
|
||||
return true;
|
||||
}
|
||||
Object obj = this.linkedObject.get();
|
||||
if (obj == elem) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect() {
|
||||
Object obj = this.linkedObject.get();
|
||||
if (obj == null) {
|
||||
return;
|
||||
}
|
||||
if (obj instanceof Connection tmp) {
|
||||
tmp.connectionIsRemovedBySignal();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class Signal<T> implements ConnectionRemoveInterface {
|
||||
List<ConnectedElement<T>> data = new ArrayList<>();
|
||||
|
||||
public void clear() {
|
||||
List<ConnectedElement<T>> data2 = data;
|
||||
synchronized(this.data) {
|
||||
this.data = new ArrayList<>();
|
||||
}
|
||||
final Iterator<ConnectedElement<T>> iterator = this.data2.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
final ConnectedElement<T> elem = iterator.next();
|
||||
elem.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
public void connect(final Consumer<T> function) {
|
||||
synchronized(this.data) {
|
||||
this.data.add(new ConnectedElement<T>(function));
|
||||
}
|
||||
}
|
||||
// public void disconnect(final Consumer<T> obj) {
|
||||
// synchronized(this.data) {
|
||||
// final Iterator<ConnectedElement<T>> iterator = this.data.iterator();
|
||||
// while (iterator.hasNext()) {
|
||||
// final ConnectedElement<T> elem = iterator.next();
|
||||
// if (elem.isCompatibleWith(obj)) {
|
||||
// iterator.remove();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
public Connection connectDynamic(final Consumer<T> function) {
|
||||
Connection out = new Connection(this);
|
||||
synchronized(this.data) {
|
||||
this.data.add(new ConnectedElementDynamic<T>(out, function));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
public void connectAutoRemoveObject(Object reference, final Consumer<T> function) {
|
||||
synchronized(this.data) {
|
||||
this.data.add(new ConnectedElementDynamic<T>(reference, function));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disconnect(final Connection connection) {
|
||||
synchronized(this.data) {
|
||||
final Iterator<ConnectedElement<T>> iterator = this.data.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
final ConnectedElement<T> elem = iterator.next();
|
||||
if (elem.isCompatibleWith(connection)) {
|
||||
elem.disconnect();
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void emit(final T value) {
|
||||
List<ConnectedElement<T>> tmp;
|
||||
// clean the list:
|
||||
synchronized(this.data) {
|
||||
final Iterator<ConnectedElement<T>> iterator = this.data.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
final ConnectedElement<T> elem = iterator.next();
|
||||
Object tmpObject = elem.getConsumer();
|
||||
if (tmpObject == null) {
|
||||
elem.disconnect();
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
// simple optimization:
|
||||
if (this.data.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
// clone the list to permit to have asynchronous remove call
|
||||
tmp = new ArrayList<>(this.data);
|
||||
}
|
||||
// real call elements
|
||||
{
|
||||
final Iterator<ConnectedElement<T>> iterator = tmp.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
final ConnectedElement<T> elem = iterator.next();
|
||||
Consumer<T> tmpObject = elem.getConsumer();
|
||||
if (tmpObject == null) {
|
||||
continue;
|
||||
}
|
||||
tmpObject.accept(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return this.data.size();
|
||||
}
|
||||
|
||||
}
|
@ -1,62 +0,0 @@
|
||||
package org.atriasoft.esignal;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
class ConnectedElementEmpty {
|
||||
private final WeakReference<Object> reference;
|
||||
private final Runnable runnable;
|
||||
|
||||
public ConnectedElementEmpty(final WeakReference<Object> reference, final Runnable runnable) {
|
||||
this.reference = reference;
|
||||
this.runnable = runnable;
|
||||
}
|
||||
|
||||
public WeakReference<Object> getReference() {
|
||||
return this.reference;
|
||||
}
|
||||
|
||||
public Runnable getRunner() {
|
||||
return this.runnable;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class SignalEmpty {
|
||||
|
||||
List<ConnectedElementEmpty> data = new ArrayList<>();
|
||||
|
||||
public void clear(final Object obj) {
|
||||
|
||||
}
|
||||
|
||||
public Connection connect(final Object reference, final Runnable runnable) {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void disconnect(final Connection connection) {
|
||||
|
||||
}
|
||||
|
||||
public void disconnect(final Object obj) {
|
||||
|
||||
}
|
||||
|
||||
public void emit() {
|
||||
final Iterator<ConnectedElementEmpty> iterator = this.data.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
final ConnectedElementEmpty elem = iterator.next();
|
||||
if (elem.getReference().get() == null) {
|
||||
iterator.remove();
|
||||
}
|
||||
elem.getRunner().run();
|
||||
}
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return this.data.size();
|
||||
}
|
||||
}
|
@ -1,59 +0,0 @@
|
||||
package test.atriasoft.esignal;
|
||||
|
||||
import io.scenarium.logger.LogLevel;
|
||||
import io.scenarium.logger.Logger;
|
||||
|
||||
public class Log {
|
||||
private static final String LIB_NAME = "esignal-test";
|
||||
private static final String LIB_NAME_DRAW = Logger.getDrawableName(LIB_NAME);
|
||||
private static final boolean PRINT_CRITICAL = Logger.getNeedPrint(LIB_NAME, LogLevel.CRITICAL);
|
||||
private static final boolean PRINT_ERROR = Logger.getNeedPrint(LIB_NAME, LogLevel.ERROR);
|
||||
private static final boolean PRINT_WARNING = Logger.getNeedPrint(LIB_NAME, LogLevel.WARNING);
|
||||
private static final boolean PRINT_INFO = Logger.getNeedPrint(LIB_NAME, LogLevel.INFO);
|
||||
private static final boolean PRINT_DEBUG = Logger.getNeedPrint(LIB_NAME, LogLevel.DEBUG);
|
||||
private static final boolean PRINT_VERBOSE = Logger.getNeedPrint(LIB_NAME, LogLevel.VERBOSE);
|
||||
private static final boolean PRINT_TODO = Logger.getNeedPrint(LIB_NAME, LogLevel.TODO);
|
||||
private static final boolean PRINT_PRINT = Logger.getNeedPrint(LIB_NAME, LogLevel.PRINT);
|
||||
|
||||
private Log() {}
|
||||
|
||||
public static void print(String data) {
|
||||
if (PRINT_PRINT)
|
||||
Logger.print(LIB_NAME_DRAW, data);
|
||||
}
|
||||
|
||||
public static void critical(String data) {
|
||||
if (PRINT_CRITICAL)
|
||||
Logger.critical(LIB_NAME_DRAW, data);
|
||||
}
|
||||
|
||||
public static void error(String data) {
|
||||
if (PRINT_ERROR)
|
||||
Logger.error(LIB_NAME_DRAW, data);
|
||||
}
|
||||
|
||||
public static void warning(String data) {
|
||||
if (PRINT_WARNING)
|
||||
Logger.warning(LIB_NAME_DRAW, data);
|
||||
}
|
||||
|
||||
public static void info(String data) {
|
||||
if (PRINT_INFO)
|
||||
Logger.info(LIB_NAME_DRAW, data);
|
||||
}
|
||||
|
||||
public static void debug(String data) {
|
||||
if (PRINT_DEBUG)
|
||||
Logger.debug(LIB_NAME_DRAW, data);
|
||||
}
|
||||
|
||||
public static void verbose(String data) {
|
||||
if (PRINT_VERBOSE)
|
||||
Logger.verbose(LIB_NAME_DRAW, data);
|
||||
}
|
||||
|
||||
public static void todo(String data) {
|
||||
if (PRINT_TODO)
|
||||
Logger.todo(LIB_NAME_DRAW, data);
|
||||
}
|
||||
}
|
@ -1,301 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Contributors:
|
||||
* Edouard DUPIN - initial API and implementation
|
||||
******************************************************************************/
|
||||
package test.atriasoft.esignal;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import io.scenarium.logger.Logger;
|
||||
|
||||
import org.atriasoft.esignal.Connection;
|
||||
import org.atriasoft.esignal.Signal;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
import org.junit.jupiter.api.Order;
|
||||
//import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
public class TestSignal {
|
||||
|
||||
class EmiterSimple {
|
||||
public Signal<String> signalEvent = new Signal<String>();
|
||||
public void sendEvent(String value) {
|
||||
signalEvent.emit(value);
|
||||
}
|
||||
}
|
||||
class ReceiverSimple {
|
||||
private String dataReceive = null;
|
||||
ReceiverSimple() {
|
||||
}
|
||||
public void connect1(EmiterSimple other) {
|
||||
WeakReference<ReceiverSimple> tmpp = new WeakReference<ReceiverSimple>(this);
|
||||
other.signalEvent.connect(data -> {
|
||||
tmpp.get().onData(data);
|
||||
});
|
||||
}
|
||||
public void connect2(EmiterSimple other) {
|
||||
// the solo lambda will not depend on the object => the remove must be done manually...
|
||||
other.signalEvent.connect(data -> {
|
||||
Log.error("lambda receive: " + data);
|
||||
});
|
||||
}
|
||||
public void connect3(EmiterSimple other) {
|
||||
// we reference the local object, then the lambda is alive while the object is alive...
|
||||
other.signalEvent.connect(data -> {
|
||||
Log.error("lambda receive: " + data);
|
||||
this.dataReceive = data;
|
||||
});
|
||||
}
|
||||
public void connect4(EmiterSimple other) {
|
||||
other.signalEvent.connect(data -> {
|
||||
onData(data);
|
||||
});
|
||||
}
|
||||
public void connect5(EmiterSimple other) {
|
||||
other.signalEvent.connect(this::onData);
|
||||
}
|
||||
// Does not work at all:
|
||||
// public void disconnect5(EmiterSimple other) {
|
||||
// other.signalEvent.disconnect(this::onData);
|
||||
// }
|
||||
|
||||
public void connect6(EmiterSimple other) {
|
||||
// the solo lambda will not depend on the object => the remove must be done manually...
|
||||
other.signalEvent.connectAutoRemoveObject(this, data -> {
|
||||
Log.error("lambda receive: " + data);
|
||||
});
|
||||
}
|
||||
private Connection tmpConnect = null;
|
||||
|
||||
public void connect7(EmiterSimple other) {
|
||||
tmpConnect = other.signalEvent.connectDynamic(this::onData);
|
||||
}
|
||||
|
||||
public void disconnect7(EmiterSimple other) {
|
||||
other.signalEvent.disconnect(tmpConnect);
|
||||
}
|
||||
|
||||
public void disconnect72() {
|
||||
tmpConnect.disconnect();
|
||||
}
|
||||
public boolean isConnected() {
|
||||
return tmpConnect.isConnected();
|
||||
}
|
||||
|
||||
public void onData(String data) {
|
||||
Log.error("Retrive data : " + data);
|
||||
dataReceive = data;
|
||||
}
|
||||
public String getDataAndClean() {
|
||||
String tmp = dataReceive;
|
||||
dataReceive = null;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
public void testConnectAndTransmit1() {
|
||||
Log.warning("Test 1 [BEGIN]");
|
||||
EmiterSimple sender = new EmiterSimple();
|
||||
ReceiverSimple receiver = new ReceiverSimple();
|
||||
receiver.connect1(sender);
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
String testData1 = "MUST receive this data...";
|
||||
sender.sendEvent(testData1);
|
||||
assertEquals(testData1, receiver.getDataAndClean());
|
||||
receiver = null;
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
System.gc();
|
||||
String testData2 = "MUST NOT receive this data...";
|
||||
sender.sendEvent(testData2);
|
||||
assertEquals(0, sender.signalEvent.size());
|
||||
Log.warning("Test 1 [ END ]");
|
||||
|
||||
}
|
||||
@Test
|
||||
@Order(2)
|
||||
public void testConnectAndTransmit2() {
|
||||
Log.warning("Test 2 [BEGIN]");
|
||||
EmiterSimple sender = new EmiterSimple();
|
||||
ReceiverSimple receiver = new ReceiverSimple();
|
||||
receiver.connect2(sender);
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
String testData1 = "MUST receive this data...";
|
||||
sender.sendEvent(testData1);
|
||||
// No data stored ... assertEquals(testData1, receiver.getDataAndClean());
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
receiver = null;
|
||||
System.gc();
|
||||
String testData2 = "Solo Lambda MUST receive this data...";
|
||||
sender.sendEvent(testData2);
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
Log.warning("Test 2 [ END ]");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
public void testConnectAndTransmit3() {
|
||||
Log.warning("Test 3 [BEGIN]");
|
||||
EmiterSimple sender = new EmiterSimple();
|
||||
ReceiverSimple receiver = new ReceiverSimple();
|
||||
receiver.connect3(sender);
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
String testData1 = "MUST receive this data...";
|
||||
sender.sendEvent(testData1);
|
||||
assertEquals(testData1, receiver.getDataAndClean());
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
receiver = null;
|
||||
System.gc();
|
||||
String testData2 = "MUST NOT receive this data...";
|
||||
sender.sendEvent(testData2);
|
||||
assertEquals(0, sender.signalEvent.size());
|
||||
Log.warning("Test 3 [ END ]");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
public void testConnectAndTransmit4() {
|
||||
Log.warning("Test 4 [BEGIN]");
|
||||
EmiterSimple sender = new EmiterSimple();
|
||||
ReceiverSimple receiver = new ReceiverSimple();
|
||||
receiver.connect4(sender);
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
String testData1 = "MUST receive this data...";
|
||||
sender.sendEvent(testData1);
|
||||
assertEquals(testData1, receiver.getDataAndClean());
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
receiver = null;
|
||||
System.gc();
|
||||
String testData2 = "MUST NOT receive this data...";
|
||||
sender.sendEvent(testData2);
|
||||
assertEquals(0, sender.signalEvent.size());
|
||||
Log.warning("Test 4 [ END ]");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
public void testConnectAndTransmit5() {
|
||||
Log.warning("Test 5 [BEGIN]");
|
||||
EmiterSimple sender = new EmiterSimple();
|
||||
ReceiverSimple receiver = new ReceiverSimple();
|
||||
//connect step 1
|
||||
receiver.connect5(sender);
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
String testData1 = "MUST receive this data... 111";
|
||||
sender.sendEvent(testData1);
|
||||
assertEquals(testData1, receiver.getDataAndClean());
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
// remove connection
|
||||
// receiver.disconnect5(sender);
|
||||
// assertEquals(0, sender.signalEvent.size());
|
||||
// System.gc();
|
||||
// String testData2 = "MUST NOT receive this data... 222";
|
||||
// sender.sendEvent(testData2);
|
||||
// assertEquals(null, receiver.getDataAndClean());
|
||||
// // reconnect (step 2
|
||||
// receiver.connect5(sender);
|
||||
// assertEquals(1, sender.signalEvent.size());
|
||||
// String testData3 = "MUST receive this data... 333";
|
||||
// sender.sendEvent(testData3);
|
||||
// assertEquals(testData3, receiver.getDataAndClean());
|
||||
// assertEquals(1, sender.signalEvent.size());
|
||||
// check auto remove...
|
||||
receiver = null;
|
||||
System.gc();
|
||||
String testData4 = "MUST NOT receive this data... 444";
|
||||
sender.sendEvent(testData4);
|
||||
assertEquals(0, sender.signalEvent.size());
|
||||
Log.warning("Test 5 [ END ]");
|
||||
|
||||
}
|
||||
@Test
|
||||
@Order(6)
|
||||
public void testConnectAndTransmit6() {
|
||||
Log.warning("Test 6 [BEGIN]");
|
||||
EmiterSimple sender = new EmiterSimple();
|
||||
ReceiverSimple receiver = new ReceiverSimple();
|
||||
receiver.connect6(sender);
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
String testData1 = "MUST receive this data...";
|
||||
sender.sendEvent(testData1);
|
||||
//assertEquals(testData1, receiver.getDataAndClean());
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
receiver = null;
|
||||
System.gc();
|
||||
String testData2 = "MUST NOT receive this data...";
|
||||
sender.sendEvent(testData2);
|
||||
assertEquals(0, sender.signalEvent.size());
|
||||
Log.warning("Test 6 [ END ]");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
public void testConnectAndTransmit7() {
|
||||
Log.warning("Test 7 [BEGIN]");
|
||||
EmiterSimple sender = new EmiterSimple();
|
||||
ReceiverSimple receiver = new ReceiverSimple();
|
||||
//connect step 1
|
||||
receiver.connect7(sender);
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
String testData1 = "MUST receive this data... 111";
|
||||
sender.sendEvent(testData1);
|
||||
assertEquals(testData1, receiver.getDataAndClean());
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
assertEquals(true, receiver.isConnected());
|
||||
// remove connection
|
||||
receiver.disconnect7(sender);
|
||||
assertEquals(false, receiver.isConnected());
|
||||
System.gc();
|
||||
String testData2 = "MUST NOT receive this data... 222";
|
||||
sender.sendEvent(testData2);
|
||||
assertEquals(0, sender.signalEvent.size());
|
||||
assertEquals(null, receiver.getDataAndClean());
|
||||
// reconnect (step 2
|
||||
receiver.connect7(sender);
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
String testData3 = "MUST receive this data... 333";
|
||||
sender.sendEvent(testData3);
|
||||
assertEquals(testData3, receiver.getDataAndClean());
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
assertEquals(true, receiver.isConnected());
|
||||
// remove connection
|
||||
receiver.disconnect72();
|
||||
assertEquals(false, receiver.isConnected());
|
||||
assertEquals(0, sender.signalEvent.size());
|
||||
System.gc();
|
||||
String testData4 = "MUST NOT receive this data... 444";
|
||||
sender.sendEvent(testData4);
|
||||
assertEquals(null, receiver.getDataAndClean());
|
||||
// reconnect (step 2
|
||||
receiver.connect7(sender);
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
String testData5 = "MUST receive this data... 555";
|
||||
sender.sendEvent(testData5);
|
||||
assertEquals(testData5, receiver.getDataAndClean());
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
// check auto remove...
|
||||
receiver = null;
|
||||
System.gc();
|
||||
String testData6 = "MUST NOT receive this data... 666";
|
||||
sender.sendEvent(testData6);
|
||||
assertEquals(0, sender.signalEvent.size());
|
||||
Log.warning("Test 7 [ END ]");
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,301 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*
|
||||
* Contributors:
|
||||
* Edouard DUPIN - initial API and implementation
|
||||
******************************************************************************/
|
||||
package test.atriasoft.esignal;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import io.scenarium.logger.Logger;
|
||||
|
||||
import org.atriasoft.esignal.Connection;
|
||||
import org.atriasoft.esignal.Signal;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
|
||||
import org.junit.jupiter.api.Order;
|
||||
//import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
@TestMethodOrder(OrderAnnotation.class)
|
||||
public class TestSignalType {
|
||||
|
||||
class EmiterSimple {
|
||||
public Signal<String> signalEvent = new Signal<String>();
|
||||
public void sendEvent(String value) {
|
||||
signalEvent.emit(value);
|
||||
}
|
||||
}
|
||||
class ReceiverSimple {
|
||||
private String dataReceive = null;
|
||||
ReceiverSimple() {
|
||||
}
|
||||
public void connect1(EmiterSimple other) {
|
||||
WeakReference<ReceiverSimple> tmpp = new WeakReference<ReceiverSimple>(this);
|
||||
other.signalEvent.connect(data -> {
|
||||
tmpp.get().onData(data);
|
||||
});
|
||||
}
|
||||
public void connect2(EmiterSimple other) {
|
||||
// the solo lambda will not depend on the object => the remove must be done manually...
|
||||
other.signalEvent.connect(data -> {
|
||||
Log.error("lambda receive: " + data);
|
||||
});
|
||||
}
|
||||
public void connect3(EmiterSimple other) {
|
||||
// we reference the local object, then the lambda is alive while the object is alive...
|
||||
other.signalEvent.connect(data -> {
|
||||
Log.error("lambda receive: " + data);
|
||||
this.dataReceive = data;
|
||||
});
|
||||
}
|
||||
public void connect4(EmiterSimple other) {
|
||||
other.signalEvent.connect(data -> {
|
||||
onData(data);
|
||||
});
|
||||
}
|
||||
public void connect5(EmiterSimple other) {
|
||||
other.signalEvent.connect(this::onData);
|
||||
}
|
||||
// Does not work at all:
|
||||
// public void disconnect5(EmiterSimple other) {
|
||||
// other.signalEvent.disconnect(this::onData);
|
||||
// }
|
||||
|
||||
public void connect6(EmiterSimple other) {
|
||||
// the solo lambda will not depend on the object => the remove must be done manually...
|
||||
other.signalEvent.connectAutoRemoveObject(this, data -> {
|
||||
Log.error("lambda receive: " + data);
|
||||
});
|
||||
}
|
||||
private Connection tmpConnect = null;
|
||||
|
||||
public void connect7(EmiterSimple other) {
|
||||
tmpConnect = other.signalEvent.connectDynamic(this::onData);
|
||||
}
|
||||
|
||||
public void disconnect7(EmiterSimple other) {
|
||||
other.signalEvent.disconnect(tmpConnect);
|
||||
}
|
||||
|
||||
public void disconnect72() {
|
||||
tmpConnect.disconnect();
|
||||
}
|
||||
public boolean isConnected() {
|
||||
return tmpConnect.isConnected();
|
||||
}
|
||||
|
||||
public void onData(String data) {
|
||||
Log.error("Retrive data : " + data);
|
||||
dataReceive = data;
|
||||
}
|
||||
public String getDataAndClean() {
|
||||
String tmp = dataReceive;
|
||||
dataReceive = null;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
public void testConnectAndTransmit1() {
|
||||
Log.warning("Test 1 [BEGIN]");
|
||||
EmiterSimple sender = new EmiterSimple();
|
||||
ReceiverSimple receiver = new ReceiverSimple();
|
||||
receiver.connect1(sender);
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
String testData1 = "MUST receive this data...";
|
||||
sender.sendEvent(testData1);
|
||||
assertEquals(testData1, receiver.getDataAndClean());
|
||||
receiver = null;
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
System.gc();
|
||||
String testData2 = "MUST NOT receive this data...";
|
||||
sender.sendEvent(testData2);
|
||||
assertEquals(0, sender.signalEvent.size());
|
||||
Log.warning("Test 1 [ END ]");
|
||||
|
||||
}
|
||||
@Test
|
||||
@Order(2)
|
||||
public void testConnectAndTransmit2() {
|
||||
Log.warning("Test 2 [BEGIN]");
|
||||
EmiterSimple sender = new EmiterSimple();
|
||||
ReceiverSimple receiver = new ReceiverSimple();
|
||||
receiver.connect2(sender);
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
String testData1 = "MUST receive this data...";
|
||||
sender.sendEvent(testData1);
|
||||
// No data stored ... assertEquals(testData1, receiver.getDataAndClean());
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
receiver = null;
|
||||
System.gc();
|
||||
String testData2 = "Solo Lambda MUST receive this data...";
|
||||
sender.sendEvent(testData2);
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
Log.warning("Test 2 [ END ]");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
public void testConnectAndTransmit3() {
|
||||
Log.warning("Test 3 [BEGIN]");
|
||||
EmiterSimple sender = new EmiterSimple();
|
||||
ReceiverSimple receiver = new ReceiverSimple();
|
||||
receiver.connect3(sender);
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
String testData1 = "MUST receive this data...";
|
||||
sender.sendEvent(testData1);
|
||||
assertEquals(testData1, receiver.getDataAndClean());
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
receiver = null;
|
||||
System.gc();
|
||||
String testData2 = "MUST NOT receive this data...";
|
||||
sender.sendEvent(testData2);
|
||||
assertEquals(0, sender.signalEvent.size());
|
||||
Log.warning("Test 3 [ END ]");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
public void testConnectAndTransmit4() {
|
||||
Log.warning("Test 4 [BEGIN]");
|
||||
EmiterSimple sender = new EmiterSimple();
|
||||
ReceiverSimple receiver = new ReceiverSimple();
|
||||
receiver.connect4(sender);
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
String testData1 = "MUST receive this data...";
|
||||
sender.sendEvent(testData1);
|
||||
assertEquals(testData1, receiver.getDataAndClean());
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
receiver = null;
|
||||
System.gc();
|
||||
String testData2 = "MUST NOT receive this data...";
|
||||
sender.sendEvent(testData2);
|
||||
assertEquals(0, sender.signalEvent.size());
|
||||
Log.warning("Test 4 [ END ]");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
public void testConnectAndTransmit5() {
|
||||
Log.warning("Test 5 [BEGIN]");
|
||||
EmiterSimple sender = new EmiterSimple();
|
||||
ReceiverSimple receiver = new ReceiverSimple();
|
||||
//connect step 1
|
||||
receiver.connect5(sender);
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
String testData1 = "MUST receive this data... 111";
|
||||
sender.sendEvent(testData1);
|
||||
assertEquals(testData1, receiver.getDataAndClean());
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
// remove connection
|
||||
// receiver.disconnect5(sender);
|
||||
// assertEquals(0, sender.signalEvent.size());
|
||||
// System.gc();
|
||||
// String testData2 = "MUST NOT receive this data... 222";
|
||||
// sender.sendEvent(testData2);
|
||||
// assertEquals(null, receiver.getDataAndClean());
|
||||
// // reconnect (step 2
|
||||
// receiver.connect5(sender);
|
||||
// assertEquals(1, sender.signalEvent.size());
|
||||
// String testData3 = "MUST receive this data... 333";
|
||||
// sender.sendEvent(testData3);
|
||||
// assertEquals(testData3, receiver.getDataAndClean());
|
||||
// assertEquals(1, sender.signalEvent.size());
|
||||
// check auto remove...
|
||||
receiver = null;
|
||||
System.gc();
|
||||
String testData4 = "MUST NOT receive this data... 444";
|
||||
sender.sendEvent(testData4);
|
||||
assertEquals(0, sender.signalEvent.size());
|
||||
Log.warning("Test 5 [ END ]");
|
||||
|
||||
}
|
||||
@Test
|
||||
@Order(6)
|
||||
public void testConnectAndTransmit6() {
|
||||
Log.warning("Test 6 [BEGIN]");
|
||||
EmiterSimple sender = new EmiterSimple();
|
||||
ReceiverSimple receiver = new ReceiverSimple();
|
||||
receiver.connect6(sender);
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
String testData1 = "MUST receive this data...";
|
||||
sender.sendEvent(testData1);
|
||||
//assertEquals(testData1, receiver.getDataAndClean());
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
receiver = null;
|
||||
System.gc();
|
||||
String testData2 = "MUST NOT receive this data...";
|
||||
sender.sendEvent(testData2);
|
||||
assertEquals(0, sender.signalEvent.size());
|
||||
Log.warning("Test 6 [ END ]");
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
public void testConnectAndTransmit7() {
|
||||
Log.warning("Test 7 [BEGIN]");
|
||||
EmiterSimple sender = new EmiterSimple();
|
||||
ReceiverSimple receiver = new ReceiverSimple();
|
||||
//connect step 1
|
||||
receiver.connect7(sender);
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
String testData1 = "MUST receive this data... 111";
|
||||
sender.sendEvent(testData1);
|
||||
assertEquals(testData1, receiver.getDataAndClean());
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
assertEquals(true, receiver.isConnected());
|
||||
// remove connection
|
||||
receiver.disconnect7(sender);
|
||||
assertEquals(false, receiver.isConnected());
|
||||
System.gc();
|
||||
String testData2 = "MUST NOT receive this data... 222";
|
||||
sender.sendEvent(testData2);
|
||||
assertEquals(0, sender.signalEvent.size());
|
||||
assertEquals(null, receiver.getDataAndClean());
|
||||
// reconnect (step 2
|
||||
receiver.connect7(sender);
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
String testData3 = "MUST receive this data... 333";
|
||||
sender.sendEvent(testData3);
|
||||
assertEquals(testData3, receiver.getDataAndClean());
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
assertEquals(true, receiver.isConnected());
|
||||
// remove connection
|
||||
receiver.disconnect72();
|
||||
assertEquals(false, receiver.isConnected());
|
||||
assertEquals(0, sender.signalEvent.size());
|
||||
System.gc();
|
||||
String testData4 = "MUST NOT receive this data... 444";
|
||||
sender.sendEvent(testData4);
|
||||
assertEquals(null, receiver.getDataAndClean());
|
||||
// reconnect (step 2
|
||||
receiver.connect7(sender);
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
String testData5 = "MUST receive this data... 555";
|
||||
sender.sendEvent(testData5);
|
||||
assertEquals(testData5, receiver.getDataAndClean());
|
||||
assertEquals(1, sender.signalEvent.size());
|
||||
// check auto remove...
|
||||
receiver = null;
|
||||
System.gc();
|
||||
String testData6 = "MUST NOT receive this data... 666";
|
||||
sender.sendEvent(testData6);
|
||||
assertEquals(0, sender.signalEvent.size());
|
||||
Log.warning("Test 7 [ END ]");
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user