Search in sources :

Example 1 with JLineInteractiveModeConsole

use of org.openksavi.sponge.standalone.interactive.JLineInteractiveModeConsole in project sponge by softelnet.

the class InteractiveModeStandaloneTest method testInteractive.

@Test
public void testInteractive() throws Exception {
    PipedInputStream in = new PipedInputStream();
    try (PipedOutputStream pipedOutputStream = new PipedOutputStream(in)) {
        outIn = pipedOutputStream;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        engine = StandaloneSpongeEngine.builder().commandLineArgs("-k", "examples/standalone/interactive.py", "-i").interactiveModeConsoleSupplier(() -> {
            JLineInteractiveModeConsole console = new JLineInteractiveModeConsole();
            console.setTerminalBuilder(TerminalBuilder.builder().streams(in, out));
            return console;
        }).build();
        SpongeUtils.executeConcurrentlyOnce(engine, () -> {
            engine.startup();
            engine.getInteractiveMode().loop();
        });
        await().atMost(10, TimeUnit.SECONDS).until(() -> engine != null && engine.isRunning() && engine.getInteractiveMode().isRunning());
        // Print the message.
        write("print 'Starting interactive mode tests.'");
        // Send the alarm.
        write("EPS.event(\"alarm\").send()");
        await().atMost(10, TimeUnit.SECONDS).until(() -> engine.getOperations().getVariable(Number.class, "alarms").intValue() >= 1);
        // Create trigger and send event.
        writeMulti("class T(Trigger):\\");
        writeMulti("    def onConfigure(self):\\");
        writeMulti("        self.event = \"notification\"\\");
        writeMulti("    def onRun(self, event):\\");
        writeMulti("        EPS.getVariable(\"notifications\").incrementAndGet()\\");
        writeMulti("        print \"Received the notification!\"");
        write("");
        write("EPS.enable(T)");
        write("EPS.event(\"notification\").send()");
        await().atMost(10, TimeUnit.SECONDS).until(() -> engine.getOperations().getVariable(Number.class, "notifications").intValue() >= 1);
        assertFalse(engine.isError());
    } finally {
        if (engine != null) {
            engine.shutdown();
        }
    }
}
Also used : JLineInteractiveModeConsole(org.openksavi.sponge.standalone.interactive.JLineInteractiveModeConsole) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 Test (org.junit.Test)1 JLineInteractiveModeConsole (org.openksavi.sponge.standalone.interactive.JLineInteractiveModeConsole)1