Search in sources :

Example 11 with BeaconTuple

use of com.ibm.streamsx.topology.tuple.BeaconTuple 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 12 with BeaconTuple

use of com.ibm.streamsx.topology.tuple.BeaconTuple in project streamsx.topology by IBMStreams.

the class SubscribeBeacon method main.

/**
     * Submit this application which subscribes to a topic.
     * @param args Command line arguments, accepts a single optional topic name.
     * @throws Exception Error running the application
     */
public static void main(String[] args) throws Exception {
    String topic = "/beacon";
    String type = "DISTRIBUTED";
    if (args.length >= 1)
        topic = "/" + args[0];
    if (args.length == 2)
        type = args[1];
    Topology topology = new Topology("SubscribeBeacon");
    TStream<BeaconTuple> beacon = topology.subscribe(topic, BeaconTuple.class);
    beacon.print();
    StreamsContextFactory.getStreamsContext(type).submit(topology);
}
Also used : BeaconTuple(com.ibm.streamsx.topology.tuple.BeaconTuple) Topology(com.ibm.streamsx.topology.Topology)

Example 13 with BeaconTuple

use of com.ibm.streamsx.topology.tuple.BeaconTuple in project streamsx.topology by IBMStreams.

the class ParallelTest method testParallelNonPartitioned.

@Test
public void testParallelNonPartitioned() throws Exception {
    checkUdpSupported();
    Topology topology = newTopology("testParallel");
    final int count = new Random().nextInt(1000) + 37;
    TStream<BeaconTuple> fb = BeaconStreams.beacon(topology, count);
    TStream<BeaconTuple> pb = fb.parallel(5);
    TStream<Integer> is = pb.transform(randomHashProducer());
    TStream<Integer> joined = is.endParallel();
    TStream<String> numRegions = joined.transform(uniqueIdentifierMap(count));
    Tester tester = topology.getTester();
    Condition<Long> expectedCount = tester.tupleCount(numRegions, 1);
    Condition<List<String>> regionCount = tester.stringContents(numRegions, "5");
    complete(tester, allConditions(regionCount, expectedCount), 10, TimeUnit.SECONDS);
    assertTrue(expectedCount.valid());
    assertTrue(regionCount.valid());
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Random(java.util.Random) BeaconTuple(com.ibm.streamsx.topology.tuple.BeaconTuple) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 14 with BeaconTuple

use of com.ibm.streamsx.topology.tuple.BeaconTuple in project streamsx.topology by IBMStreams.

the class ParallelTest method testParallelWidthSupplier.

@Test
public void testParallelWidthSupplier() throws Exception {
    checkUdpSupported();
    Topology topology = newTopology("testParallelWidthValue");
    final int count = new Random().nextInt(1000) + 37;
    String submissionWidthName = "width";
    final Integer submissionWidth = 5;
    TStream<BeaconTuple> fb = BeaconStreams.beacon(topology, count);
    TStream<BeaconTuple> pb = fb.parallel(() -> submissionWidth);
    TStream<Integer> is = pb.transform(randomHashProducer());
    TStream<Integer> joined = is.endParallel();
    TStream<String> numRegions = joined.transform(uniqueIdentifierMap(count));
    Tester tester = topology.getTester();
    Condition<Long> expectedCount = tester.tupleCount(numRegions, 1);
    Condition<List<String>> regionCount = tester.stringContents(numRegions, submissionWidth.toString());
    complete(tester, allConditions(regionCount, expectedCount), 10, TimeUnit.SECONDS);
    assertTrue(expectedCount.valid());
    assertTrue(regionCount.valid());
}
Also used : Tester(com.ibm.streamsx.topology.tester.Tester) Topology(com.ibm.streamsx.topology.Topology) TestTopology(com.ibm.streamsx.topology.test.TestTopology) Random(java.util.Random) BeaconTuple(com.ibm.streamsx.topology.tuple.BeaconTuple) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

BeaconTuple (com.ibm.streamsx.topology.tuple.BeaconTuple)14 Test (org.junit.Test)12 Topology (com.ibm.streamsx.topology.Topology)11 TestTopology (com.ibm.streamsx.topology.test.TestTopology)10 Tester (com.ibm.streamsx.topology.tester.Tester)9 Random (java.util.Random)9 ArrayList (java.util.ArrayList)6 List (java.util.List)6 HashMap (java.util.HashMap)2 JSONObject (com.ibm.json.java.JSONObject)1 StreamSchema (com.ibm.streams.operator.StreamSchema)1 Factory.getStreamSchema (com.ibm.streams.operator.Type.Factory.getStreamSchema)1 SPLStream (com.ibm.streamsx.topology.spl.SPLStream)1 File (java.io.File)1 IOException (java.io.IOException)1