Search in sources :

Example 56 with Tester

use of com.ibm.streamsx.topology.tester.Tester in project streamsx.topology by IBMStreams.

the class SimpleEmbeddedTest method testSimpleWithConditions.

@Test
public void testSimpleWithConditions() throws Exception {
    Topology topology = new Topology("testSimpleConditions");
    TStream<String> hw = topology.strings("Hello", "World!", "Test!!");
    Tester tester = topology.getTester();
    Condition<Long> expectedCount = tester.tupleCount(hw, 3);
    Condition<List<String>> expectedContents = tester.stringContents(hw, "Hello", "World!", "Test!!");
    StreamsContextFactory.getStreamsContext(StreamsContext.Type.EMBEDDED_TESTER).submit(topology).get();
    assertTrue(expectedCount.valid());
    assertTrue(expectedContents.valid());
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) LinkedList(java.util.LinkedList) List(java.util.List) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Test(org.junit.Test)

Example 57 with Tester

use of com.ibm.streamsx.topology.tester.Tester 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());
    // getConfig().put(ContextProperties.KEEP_ARTIFACTS, true);
    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" + " >");
    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));
    boolean b = rand.nextBoolean();
    opParamAdder.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);
    // 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));
    float f32 = rand.nextFloat();
    double f64 = rand.nextDouble();
    opParamAdder.put("f32", f32);
    opParamAdder.put("f64", f64);
    SPL.addToolkit(topology, new File(getTestRoot(), "spl/testtk"));
    SPLStream paramTuple = SPL.invokeSource(topology, "testgen::TypeLiteralTester", opParamAdder.getParams(), schema);
    // paramTuple.print();
    // paramTuple.filter(new AllowAll<Tuple>());
    Tester tester = topology.getTester();
    Condition<Long> expectedCount = tester.tupleCount(paramTuple, 1);
    MostRecent<Tuple> mr = tester.splHandler(paramTuple, new MostRecent<Tuple>());
    // getConfig().put(ContextProperties.KEEP_ARTIFACTS, true);
    complete(tester, expectedCount, 10, TimeUnit.SECONDS);
    assertTrue(expectedCount.toString(), expectedCount.valid());
    Tuple tuple = mr.getMostRecentTuple();
    // System.out.println("tuple: " + tuple);
    assertEquals(r, tuple.getString("r"));
    assertEquals(u, tuple.getString("u"));
    assertEquals(i8, tuple.getByte("i8"));
    assertEquals(i16, tuple.getShort("i16"));
    assertEquals(i32, tuple.getInt("i32"));
    assertEquals(i64, tuple.getLong("i64"));
    assertEquals(ui8, tuple.getByte("ui8"));
    assertEquals(ui16, tuple.getShort("ui16"));
    assertEquals(ui32, tuple.getInt("ui32"));
    assertEquals(ui64, tuple.getLong("ui64"));
    assertEquals(f32, tuple.getFloat("f32"), 0.001);
    assertEquals(f64, tuple.getDouble("f64"), 0.001);
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) StreamSchema(com.ibm.streams.operator.StreamSchema) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) Random(java.util.Random) File(java.io.File) Tuple(com.ibm.streams.operator.Tuple)

Example 58 with Tester

use of com.ibm.streamsx.topology.tester.Tester 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"));
    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]);
    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.ibm.json.java.JSONObject) List(java.util.List) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) File(java.io.File) SPLStream(com.ibm.streamsx.topology.spl.SPLStream) Test(org.junit.Test)

Example 59 with Tester

use of com.ibm.streamsx.topology.tester.Tester in project streamsx.topology by IBMStreams.

the class BeaconTest method testBeaconTuples.

@Test
public void testBeaconTuples() throws Exception {
    Topology topology = new Topology("testFixedCount");
    final int count = new Random().nextInt(1000) + 37;
    TStream<BeaconTuple> beacon = BeaconStreams.beacon(topology, count);
    TStream<JSONObject> json = JSONStreams.toJSON(beacon);
    TStream<String> strings = JSONStreams.serialize(json);
    Tester tester = topology.getTester();
    Condition<Long> expectedCount = tester.tupleCount(strings, count);
    @SuppressWarnings("serial") Condition<String> contents = tester.stringTupleTester(strings, new Predicate<String>() {

        private transient BeaconTuple lastTuple;

        @Override
        public boolean test(String tuple) {
            JSONObject json;
            try {
                json = (JSONObject) JSON.parse(tuple);
            } catch (NullPointerException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            BeaconTuple bt = new BeaconTuple((Long) json.get("sequence"), (Long) json.get("time"));
            boolean ok;
            if (lastTuple == null) {
                ok = bt.getSequence() == 0;
            } else {
                ok = lastTuple.compareTo(bt) < 0 && lastTuple.getTime() <= bt.getTime() && lastTuple.getSequence() + 1 == bt.getSequence();
            }
            ok = ok && bt.getTime() != 0;
            ok = ok && bt.getKey() == bt.getSequence();
            lastTuple = bt;
            return ok;
        }
    });
    complete(tester, expectedCount, 20, TimeUnit.SECONDS);
    assertTrue(expectedCount.valid());
    assertTrue(contents.toString(), contents.valid());
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) IOException(java.io.IOException) Random(java.util.Random) JSONObject(com.ibm.json.java.JSONObject) BeaconTuple(com.ibm.streamsx.topology.tuple.BeaconTuple) Test(org.junit.Test)

Example 60 with Tester

use of com.ibm.streamsx.topology.tester.Tester in project streamsx.topology by IBMStreams.

the class LowLatencyTest method testLowLatencySplit.

@Test
public void testLowLatencySplit() throws Exception {
    // lowLatency().split() is an interesting case because split()
    // has >1 oports.
    final Topology topology = newTopology("testLowLatencySplit");
    int splitWidth = 3;
    String[] strs = { "ch0", "ch1", "ch2" };
    TStream<String> s1 = topology.strings(strs);
    s1 = s1.isolate();
    s1 = s1.lowLatency();
    /////////////////////////////////////
    // assume that if s1.modify and the split().[modify()] are
    // in the same PE, that s1.split() is in the same too
    TStream<String> s2 = s1.modify(unaryGetPEId());
    List<TStream<String>> splits = s1.split(splitWidth, roundRobinSplitter());
    List<TStream<String>> splitChResults = new ArrayList<>();
    for (int i = 0; i < splits.size(); i++) {
        splitChResults.add(splits.get(i).modify(unaryGetPEId()));
    }
    TStream<String> splitChFanin = splitChResults.get(0).union(new HashSet<>(splitChResults.subList(1, splitChResults.size())));
    /////////////////////////////////////
    TStream<String> all = splitChFanin.endLowLatency();
    Tester tester = topology.getTester();
    Condition<Long> uCount = tester.tupleCount(all, strs.length);
    Condition<List<String>> contents = tester.stringContents(all, "");
    Condition<List<String>> s2contents = tester.stringContents(s2, "");
    complete(tester, uCount, 10, TimeUnit.SECONDS);
    Set<String> peIds = new HashSet<>();
    peIds.addAll(contents.getResult());
    peIds.addAll(s2contents.getResult());
    assertEquals("peIds: " + peIds, 1, peIds.size());
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) ArrayList(java.util.ArrayList) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) TStream(com.ibm.streamsx.topology.TStream) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Tester (com.ibm.streamsx.topology.tester.Tester)68 Topology (com.ibm.streamsx.topology.Topology)61 TestTopology (com.ibm.streamsx.topology.test.TestTopology)60 Test (org.junit.Test)60 List (java.util.List)50 ArrayList (java.util.ArrayList)28 SPLStream (com.ibm.streamsx.topology.spl.SPLStream)20 Random (java.util.Random)13 Tuple (com.ibm.streams.operator.Tuple)12 HashMap (java.util.HashMap)10 TStream (com.ibm.streamsx.topology.TStream)9 BeaconTuple (com.ibm.streamsx.topology.tuple.BeaconTuple)9 JSONObject (com.ibm.json.java.JSONObject)8 AllowAll (com.ibm.streamsx.topology.test.AllowAll)8 HashSet (java.util.HashSet)8 OutputTuple (com.ibm.streams.operator.OutputTuple)6 StreamSchema (com.ibm.streams.operator.StreamSchema)6 LinkedList (java.util.LinkedList)6 File (java.io.File)4 JSONArray (com.ibm.json.java.JSONArray)3