Search in sources :

Example 81 with SPLStream

use of com.ibm.streamsx.topology.spl.SPLStream in project streamsx.topology by IBMStreams.

the class SPLOperatorsTest method testSPLOperator.

/**
 * Test we can invoke an SPL operator.
 */
@Test
public void testSPLOperator() throws Exception {
    Topology topology = new Topology("testSPLOperator");
    SPLStream tuples = SPLStreamsTest.testTupleStream(topology);
    // Filter on the vi attribute, passing the value 321.
    Map<String, Object> params = new HashMap<>();
    params.put("attr", tuples.getSchema().getAttribute("vi"));
    params.put("value", 321);
    SPL.addToolkit(tuples, new File(getTestRoot(), "spl/testtk"));
    SPL.addToolkitDependency(tuples, "com.ibm.streamsx.topology.testing.testtk", "0.9.9");
    SPLStream int32Filtered = SPL.invokeOperator("testspl::Int32Filter", tuples, tuples.getSchema(), params);
    Tester tester = topology.getTester();
    Condition<Long> expectedCount = tester.tupleCount(int32Filtered, 2);
    Condition<List<Tuple>> expectedTuples = tester.tupleContents(int32Filtered, SPLStreamsTest.TEST_TUPLES[0], SPLStreamsTest.TEST_TUPLES[2]);
    if (isStreamingAnalyticsRun())
        getConfig().put(ContextProperties.FORCE_REMOTE_BUILD, true);
    complete(tester, expectedCount, 10, TimeUnit.SECONDS);
    assertTrue(expectedCount.toString(), expectedCount.valid());
    assertTrue(expectedTuples.toString(), expectedTuples.valid());
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) HashMap(java.util.HashMap) JsonObject(com.google.gson.JsonObject) ArrayList(java.util.ArrayList) List(java.util.List) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) RString(com.ibm.streams.operator.types.RString) File(java.io.File) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) Test(org.junit.Test)

Example 82 with SPLStream

use of com.ibm.streamsx.topology.spl.SPLStream in project streamsx.topology by IBMStreams.

the class SPLOperatorsTest method testOpParamsOptionalTypes.

/**
 * Test we can invoke an SPL operator with various parameter types,
 * where the type is an optional type.
 */
private void testOpParamsOptionalTypes(String testName, OpParamAdder opParamAdder) throws Exception {
    Topology topology = new Topology(testName);
    opParamAdder.init(topology, getConfig());
    StreamSchema schema = Type.Factory.getStreamSchema("tuple<" + "rstring r" + ", optional<rstring> orv" + ", optional<rstring> ornv" + ", int32 i32" + ", optional<int32> oi32v" + ", optional<int32> oi32nv" + " >");
    Map<String, Object> expectedValues = new HashMap<>();
    Random rand = new Random();
    String r = "test    X\tY\"Lit\nerals\\nX\\tY " + rand.nextInt();
    opParamAdder.put("r", r);
    expectedValues.put("r", new RString(r));
    String orv = "test    X\tY\"Lit\nerals\\nX\\tY " + rand.nextInt();
    opParamAdder.put("orv", orv);
    // test setting optional type to null by using null in Map
    opParamAdder.put("ornv", null);
    expectedValues.put("orv", new RString(orv));
    expectedValues.put("ornv", null);
    int i32 = rand.nextInt();
    opParamAdder.put("i32", i32);
    int oi32v = rand.nextInt();
    opParamAdder.put("oi32v", oi32v);
    // test setting optional type to null by using createNullValue() in Map
    opParamAdder.put("oi32nv", SPL.createNullValue());
    expectedValues.put("i32", i32);
    expectedValues.put("oi32v", oi32v);
    expectedValues.put("oi32nv", null);
    SPL.addToolkit(topology, new File(getTestRoot(), "spl/testtkopt"));
    SPLStream paramTuple = SPL.invokeSource(topology, "testgen::TypeLiteralTester", opParamAdder.getParams(), schema);
    Tester tester = topology.getTester();
    Condition<Long> expectedCount = tester.tupleCount(paramTuple, 1);
    Condition<?> contents = tester.tupleContents(paramTuple, schema.getTuple(expectedValues));
    complete(tester, expectedCount.and(contents), 10, TimeUnit.SECONDS);
    assertTrue(contents.valid());
    assertTrue(expectedCount.valid());
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) HashMap(java.util.HashMap) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) RString(com.ibm.streams.operator.types.RString) StreamSchema(com.ibm.streams.operator.StreamSchema) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) Random(java.util.Random) JsonObject(com.google.gson.JsonObject) RString(com.ibm.streams.operator.types.RString) File(java.io.File)

Example 83 with SPLStream

use of com.ibm.streamsx.topology.spl.SPLStream in project streamsx.topology by IBMStreams.

the class SPLOperatorsTest method testOpParams.

/**
 * Test we can invoke an SPL operator with various parameter types.
 */
private void testOpParams(String testName, OpParamAdder opParamAdder) throws Exception {
    Topology topology = new Topology(testName);
    opParamAdder.init(topology, getConfig());
    StreamSchema schema = Type.Factory.getStreamSchema("tuple<" + "rstring r" + ", ustring u" + ", boolean b" + ", int8 i8, int16 i16, int32 i32, int64 i64" + ", uint8 ui8, uint16 ui16, uint32 ui32, uint64 ui64" + ", float32 f32, float64 f64" + " >");
    Map<String, Object> expectedValues = new HashMap<>();
    Random rand = new Random();
    String r = "test    X\tY\"Lit\nerals\\nX\\tY " + rand.nextInt();
    opParamAdder.put("r", r);
    String u = "test    X\tY\"Lit\nerals\\nX\\tY " + rand.nextInt();
    opParamAdder.put("u", SPL.createValue(u, MetaType.USTRING));
    expectedValues.put("r", new RString(r));
    expectedValues.put("u", u);
    boolean b = rand.nextBoolean();
    opParamAdder.put("b", b);
    expectedValues.put("b", b);
    byte i8 = (byte) rand.nextInt();
    short i16 = (short) rand.nextInt();
    int i32 = rand.nextInt();
    long i64 = rand.nextLong();
    opParamAdder.put("i8", i8);
    opParamAdder.put("i16", i16);
    opParamAdder.put("i32", i32);
    opParamAdder.put("i64", i64);
    expectedValues.put("i8", i8);
    expectedValues.put("i16", i16);
    expectedValues.put("i32", i32);
    expectedValues.put("i64", i64);
    // 255 => -1
    byte ui8 = (byte) 0xFF;
    // 65534 => -2
    short ui16 = (short) 0xFFFE;
    // 4294967293 => -3
    int ui32 = 0xFFFFFFFD;
    // 18446744073709551612 => -4
    long ui64 = 0xFFFFFFFFFFFFFFFCL;
    opParamAdder.put("ui8", SPL.createValue(ui8, MetaType.UINT8));
    opParamAdder.put("ui16", SPL.createValue(ui16, MetaType.UINT16));
    opParamAdder.put("ui32", SPL.createValue(ui32, MetaType.UINT32));
    opParamAdder.put("ui64", SPL.createValue(ui64, MetaType.UINT64));
    expectedValues.put("ui8", ui8);
    expectedValues.put("ui16", ui16);
    expectedValues.put("ui32", ui32);
    expectedValues.put("ui64", ui64);
    float f32 = 4.0f;
    double f64 = 32.0;
    opParamAdder.put("f32", f32);
    opParamAdder.put("f64", f64);
    expectedValues.put("f32", f32);
    expectedValues.put("f64", f64);
    SPL.addToolkit(topology, new File(getTestRoot(), "spl/testtk"));
    SPLStream paramTuple = SPL.invokeSource(topology, "testgen::TypeLiteralTester", opParamAdder.getParams(), schema);
    Tuple expectedTuple = schema.getTuple(expectedValues);
    Tester tester = topology.getTester();
    Condition<Long> expectedCount = tester.tupleCount(paramTuple, 1);
    Condition<?> contents = tester.tupleContents(paramTuple, expectedTuple);
    ;
    complete(tester, expectedCount.and(contents), 10, TimeUnit.SECONDS);
    assertTrue(contents.valid());
    assertTrue(expectedCount.valid());
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) HashMap(java.util.HashMap) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) RString(com.ibm.streams.operator.types.RString) StreamSchema(com.ibm.streams.operator.StreamSchema) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) Random(java.util.Random) JsonObject(com.google.gson.JsonObject) RString(com.ibm.streams.operator.types.RString) File(java.io.File) Tuple(com.ibm.streams.operator.Tuple)

Example 84 with SPLStream

use of com.ibm.streamsx.topology.spl.SPLStream in project streamsx.topology by IBMStreams.

the class SPLStreamsTest method testConversionFromSPLToString.

@Test
public void testConversionFromSPLToString() throws Exception {
    final Topology topology = new Topology();
    SPLStream splStream = testTupleStream(topology);
    TStream<String> strings = SPLStreams.toStringStream(splStream);
    assertEquals(String.class, strings.getTupleClass());
    completeAndValidate(strings, 10, "0", "1", "2", "3");
}
Also used : Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) Test(org.junit.Test)

Example 85 with SPLStream

use of com.ibm.streamsx.topology.spl.SPLStream in project streamsx.topology by IBMStreams.

the class StatefulAppCR method createApp.

public static TStream<Long> createApp(Topology topology, boolean fail) {
    Map<String, Object> params = new HashMap<>();
    params.put("period", 0.0005);
    params.put("iterations", 512_345);
    SPLStream b = SPL.invokeSource(topology, "spl.utility::Beacon", params, com.ibm.streams.operator.Type.Factory.getStreamSchema("tuple<int64 a>"));
    b.setConsistent(ConsistentRegionConfig.periodic(30));
    TStream<Long> s = b.map(t -> t.getLong(0));
    s = s.isolate().filter(new StatefulApp.Stateful.Filter(fail));
    s = s.isolate().map(new StatefulApp.Stateful.Map(fail));
    s.isolate().forEach(new StatefulApp.Stateful.ForEach(fail));
    return s;
}
Also used : HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) SPLStream(com.ibm.streamsx.topology.spl.SPLStream)

Aggregations

SPLStream (com.ibm.streamsx.topology.spl.SPLStream)86 Topology (com.ibm.streamsx.topology.Topology)66 Test (org.junit.Test)56 TestTopology (com.ibm.streamsx.topology.test.TestTopology)49 Tester (com.ibm.streamsx.topology.tester.Tester)40 List (java.util.List)38 HashMap (java.util.HashMap)33 StreamSchema (com.ibm.streams.operator.StreamSchema)25 OutputTuple (com.ibm.streams.operator.OutputTuple)20 Tuple (com.ibm.streams.operator.Tuple)19 Map (java.util.Map)17 TStream (com.ibm.streamsx.topology.TStream)15 SPL (com.ibm.streamsx.topology.spl.SPL)14 Condition (com.ibm.streamsx.topology.tester.Condition)14 TimeUnit (java.util.concurrent.TimeUnit)14 Constants (com.ibm.streamsx.kafka.test.utils.Constants)12 KafkaSPLStreamsUtils (com.ibm.streamsx.kafka.test.utils.KafkaSPLStreamsUtils)12 StreamsContext (com.ibm.streamsx.topology.context.StreamsContext)12 Type (com.ibm.streamsx.topology.context.StreamsContext.Type)12 StreamsContextFactory (com.ibm.streamsx.topology.context.StreamsContextFactory)12