From c5d845e7ff4cb043200786aed7d35c51c796cb5f Mon Sep 17 00:00:00 2001 From: ebner Date: Mon, 21 Mar 2016 15:47:04 +0100 Subject: [PATCH] added simulated epics server for testing --- build.gradle | 2 +- .../java/ch/psi/fda/aq/ManipulatorTest.java | 6 + .../java/ch/psi/fda/core/CAJTestServer.java | 144 ++++++++++++++++++ .../core/loops/cr/CrlogicLoopStreamTest.java | 2 +- 4 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 src/test/java/ch/psi/fda/core/CAJTestServer.java diff --git a/build.gradle b/build.gradle index 920694b..8afe043 100644 --- a/build.gradle +++ b/build.gradle @@ -30,7 +30,7 @@ dependencies { compile 'org.glassfish.jersey.containers:jersey-container-grizzly2-http:2.5.1' compile 'org.glassfish.jersey.media:jersey-media-sse:2.5.1' compile 'org.glassfish.jersey.media:jersey-media-json-jackson:2.5.1' - compile 'org.zeromq:jeromq:0.3.5' + compile 'org.zeromq:jeromq:0.3.4' compile 'org.apache.commons:cli:1.2' // For reading/writing Matlab files compile 'com.jmatio:jmatio:0.2u2psi1' diff --git a/src/test/java/ch/psi/fda/aq/ManipulatorTest.java b/src/test/java/ch/psi/fda/aq/ManipulatorTest.java index fe09436..4583c22 100644 --- a/src/test/java/ch/psi/fda/aq/ManipulatorTest.java +++ b/src/test/java/ch/psi/fda/aq/ManipulatorTest.java @@ -38,6 +38,7 @@ import com.google.common.eventbus.Subscribe; import ch.psi.fda.core.TestChannels; import ch.psi.fda.aq.Manipulator; +import ch.psi.fda.core.CAJTestServer; import ch.psi.fda.core.Manipulation; import ch.psi.fda.core.manipulator.JythonManipulation; import ch.psi.fda.core.scripting.JythonParameterMapping; @@ -59,16 +60,21 @@ public class ManipulatorTest { private EventBus bus; private ChannelService cservice; + private CAJTestServer server; @Before public void setUp() throws Exception { bus = new EventBus(); cservice = new DefaultChannelService(); + + server = new CAJTestServer(); + server.runInSeparateThread(); } @After public void tearDown() throws Exception { cservice.destroy(); + server.destroy(); } @Test diff --git a/src/test/java/ch/psi/fda/core/CAJTestServer.java b/src/test/java/ch/psi/fda/core/CAJTestServer.java new file mode 100644 index 0000000..8b47c7b --- /dev/null +++ b/src/test/java/ch/psi/fda/core/CAJTestServer.java @@ -0,0 +1,144 @@ +package ch.psi.fda.core; + + +import gov.aps.jca.CAException; +import gov.aps.jca.JCALibrary; +import gov.aps.jca.cas.ServerContext; +import gov.aps.jca.dbr.DBR_Double; +import gov.aps.jca.dbr.DBR_Enum; +import gov.aps.jca.dbr.DBR_Int; +import gov.aps.jca.dbr.DBR_String; + +import com.cosylab.epics.caj.cas.util.DefaultServerImpl; +import com.cosylab.epics.caj.cas.util.MemoryProcessVariable; +import com.cosylab.epics.caj.cas.util.examples.CounterProcessVariable; + +public class CAJTestServer { + + /** + * JCA server context. + */ + private volatile ServerContext context = null; + + /** + * Initialize JCA context. + * @throws CAException throws on any failure. + */ + private void initialize() throws CAException { + + // Get the JCALibrary instance. + JCALibrary jca = JCALibrary.getInstance(); + + // Create server implementation + DefaultServerImpl server = new DefaultServerImpl(); + + // Create a context with default configuration values. + context = jca.createServerContext(JCALibrary.CHANNEL_ACCESS_SERVER_JAVA, server); + + // register process variables + registerProcessVariables(server); + } + + /** + * Register process variables. + * @param server + */ + private void registerProcessVariables(DefaultServerImpl server) { + + // simple in-memory PV + server.createMemoryProcessVariable(TestChannels.BINARY_OUT, DBR_Int.TYPE, new int[]{0}); + server.createMemoryProcessVariable(TestChannels.BINARY_OUT_TWO, DBR_Int.TYPE, new int[]{0}); + server.createMemoryProcessVariable(TestChannels.ANALOG_OUT, DBR_Double.TYPE, new double[] {0.0}); + server.createMemoryProcessVariable(TestChannels.STRING_OUT, DBR_String.TYPE, new String[]{"hello"}); + + + + // simple in-memory 1MB array + final double[] arrayValue = new double[1024*1024]; + for (int i = 0; i < arrayValue.length; i++) + arrayValue[i] = i; + server.createMemoryProcessVariable(TestChannels.DOUBLE_WAVEFORM, DBR_Double.TYPE, arrayValue); + } + + /** + * Destroy JCA server context. + */ + public void destroy() { + + try { + + // Destroy the context, check if never initialized. + if (context != null) + context.destroy(); + + } catch (Throwable th) { + th.printStackTrace(); + } + } + + /** + * @param channelName + */ + public void execute() { + + try { + + // initialize context + initialize(); + + // Display basic information about the context. + System.out.println(context.getVersion().getVersionString()); + context.printInfo(); System.out.println(); + + System.out.println("Running server..."); + + // run server + context.run(0); + + System.out.println("Done."); + + } catch (Throwable th) { + th.printStackTrace(); + } + finally { + // always finalize + destroy(); + } + + } + + public void runInSeparateThread() + { + try { + + // initialize context + initialize(); + + // run server + new Thread(new Runnable() { + @Override + public void run() { + try { + context.run(0); + } catch(Throwable th) { + th.printStackTrace(); + } + } + }).start(); + + } catch (Throwable th) { + throw new RuntimeException("Failed to start CA server.", th); + } + } + + + /** + * Program entry point. + * @param args command-line arguments + */ + public static void main(String[] args) { + // execute + new CAJTestServer().execute(); + } + +} \ No newline at end of file diff --git a/src/test/java/ch/psi/fda/core/loops/cr/CrlogicLoopStreamTest.java b/src/test/java/ch/psi/fda/core/loops/cr/CrlogicLoopStreamTest.java index 196e231..99edffa 100644 --- a/src/test/java/ch/psi/fda/core/loops/cr/CrlogicLoopStreamTest.java +++ b/src/test/java/ch/psi/fda/core/loops/cr/CrlogicLoopStreamTest.java @@ -39,7 +39,7 @@ public class CrlogicLoopStreamTest { private static final Logger logger = Logger.getLogger(CrlogicLoopStreamTest.class.getName()); - + @Ignore @Test(timeout=60000) public void testExecute() throws InterruptedException, ChannelException, TimeoutException, ExecutionException{