public interface Tester
Tester
adds the ability to test a topology in a test
framework such as JUnit.
The main feature is the ability to capture tuples from a
TStream
in order to perform some form of verification
on them. There are two mechanisms to perform verifications:
Condition
- Provides the ability to check if a common pattern is valid
, such as did the stream
produce the correct number of tuples
.StreamHandler
- Provides the ability
to add an arbitrary handler to a stream, that will be called for every tuple on the stream. A number of implementations of
StreamHandler
are in the com.ibm.streams.flow.handlers
provided by the IBM Streams Java Operator API.SPLStream
or TStream<String>
can have conditions
or handlers attached.
A Tester
only modifies its Topology
if the topology
is submitted to a tester StreamsContext
, of type
StreamsContext.Type.EMBEDDED_TESTER
,
StreamsContext.Type.STANDALONE_TESTER
,
StreamsContext.Type.DISTRIBUTED_TESTER
.
When running a test using StreamsContext.Type.STANDALONE_TESTER
or StreamsContext.Type.DISTRIBUTED_TESTER
a TCP server is setup
within the JVM running the test (e.g. the JVM running the JUnit tests} and the topology is modified to
send tuples from streams being tested to the TCP server. The port used by the TCP server is in the
ephemeral port range.
Modifier and Type | Method and Description |
---|---|
Condition<java.lang.Long> |
atLeastTupleCount(TStream<?> stream,
long expectedCount)
Return a condition that evaluates if
stream has submitted
at least expectedCount number of tuples. |
void |
complete(StreamsContext<?> context)
Submit the topology for this tester and wait for it to complete.
|
boolean |
complete(StreamsContext<?> context,
Condition<?> endCondition,
long timeout,
java.util.concurrent.TimeUnit unit)
Submit the topology for this tester and wait for it to complete,
or reach an end condition.
|
boolean |
complete(StreamsContext<?> context,
java.util.Map<java.lang.String,java.lang.Object> config,
Condition<?> endCondition,
long timeout,
java.util.concurrent.TimeUnit unit) |
Condition<java.util.List<java.lang.String>> |
completeAndTestStringOutput(StreamsContext<?> context,
java.util.Map<java.lang.String,java.lang.Object> config,
TStream<?> output,
long timeout,
java.util.concurrent.TimeUnit unit,
java.lang.String... contents) |
Condition<java.util.List<java.lang.String>> |
completeAndTestStringOutput(StreamsContext<?> context,
TStream<?> output,
long timeout,
java.util.concurrent.TimeUnit unit,
java.lang.String... contents) |
Topology |
getTopology()
Get the topology for this tester.
|
<T extends com.ibm.streams.flow.handlers.StreamHandler<com.ibm.streams.operator.Tuple>> |
splHandler(SPLStream stream,
T handler)
Adds
handler to capture the output of stream . |
Condition<java.util.List<java.lang.String>> |
stringContents(TStream<java.lang.String> stream,
java.lang.String... values)
Return a condition that evaluates if
stream has submitted
at tuples matching values in the same order. |
Condition<java.util.List<java.lang.String>> |
stringContentsUnordered(TStream<java.lang.String> stream,
java.lang.String... values)
Return a condition that evaluates if
stream has submitted
at tuples matching values in any order. |
Condition<java.lang.String> |
stringTupleTester(TStream<java.lang.String> stream,
Predicate<java.lang.String> tester) |
Condition<java.util.List<com.ibm.streams.operator.Tuple>> |
tupleContents(SPLStream stream,
com.ibm.streams.operator.Tuple... values) |
Condition<java.lang.Long> |
tupleCount(TStream<?> stream,
long expectedCount)
Return a condition that evaluates if
stream has submitted
exactly expectedCount number of tuples. |
Topology getTopology()
<T extends com.ibm.streams.flow.handlers.StreamHandler<com.ibm.streams.operator.Tuple>> T splHandler(SPLStream stream, T handler)
handler
to capture the output of stream
.stream
- Stream to have its tuples captured.handler
- StreamHandler
to capture tuples.handler
Condition<java.lang.Long> tupleCount(TStream<?> stream, long expectedCount)
stream
has submitted
exactly expectedCount
number of tuples. The function may be evaluated
after the
submit
call has returned.
result
of the returned Condition
is the number of
tuples seen on stream
so far.
Condition.valid()
and Condition.getResult()
may change as more tuples are seen on stream
.
stream
- Stream to be tested.expectedCount
- Number of tuples expected on stream
.expectedCount
number of
tuples, false otherwise.Condition<java.lang.Long> atLeastTupleCount(TStream<?> stream, long expectedCount)
stream
has submitted
at least expectedCount
number of tuples. The function may be evaluated
after the
submit
call has returned.
result
of the returned Condition
is the number of
tuples seen on stream
so far.
Condition.valid()
and Condition.getResult()
may change as more tuples are seen on stream
.
stream
- Stream to be tested.expectedCount
- Number of tuples expected on stream
.expectedCount
number of
tuples, false otherwise.Condition<java.util.List<java.lang.String>> stringContents(TStream<java.lang.String> stream, java.lang.String... values)
stream
has submitted
at tuples matching values
in the same order.
result
of the returned Condition
is the
tuples seen on stream
so far.
Condition.valid()
and Condition.getResult()
may change as more tuples are seen on stream
.
stream
- Stream to be tested.values
- Expected tuples on stream
.values
in
the same order, false otherwise.Condition<java.util.List<com.ibm.streams.operator.Tuple>> tupleContents(SPLStream stream, com.ibm.streams.operator.Tuple... values)
Condition<java.util.List<java.lang.String>> stringContentsUnordered(TStream<java.lang.String> stream, java.lang.String... values)
stream
has submitted
at tuples matching values
in any order.
result
of the returned Condition
is the
tuples seen on stream
so far.
Condition.valid()
and Condition.getResult()
may change as more tuples are seen on stream
.
stream
- Stream to be tested.values
- Expected tuples on stream
.values
in
the any order, false otherwise.Condition<java.lang.String> stringTupleTester(TStream<java.lang.String> stream, Predicate<java.lang.String> tester)
void complete(StreamsContext<?> context) throws java.lang.Exception
StreamsContext.Type.EMBEDDED_TESTER
and
StreamsContext.Type.STANDALONE_TESTER
.
A topology completes when the IBM Streams runtime determines
that there is no more processing to be performed, which is typically once
all sources have indicated there is no more data to be processed,
and all of the source tuples have been fully processed by the topology.
Note that many topologies will never complete, for example those
including polling or event sources. In this case a test case
should use complete(StreamsContext, Condition, long, TimeUnit)
.
context
- Context to be used for submission.java.lang.Exception
- Failure submitting or executing the topology.java.lang.IllegalStateException
- StreamsContext.getType()
is not supported.boolean complete(StreamsContext<?> context, Condition<?> endCondition, long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.Exception
timeout
then it is
terminated.
distributed
context.
End condition is usually a Condition
returned from
atLeastTupleCount(TStream, long)
or tupleCount(TStream, long)
so that this method returns once the stream has submitted a sufficient number of tuples.
Note that the condition will be only checked periodically up to timeout
,
so that if the condition is only valid for a brief period of time, then its
valid state may not be seen, and thus this method will wait for the timeout period.
context
- Context to be used for submission.endCondition
- Condition that will cause this method to return if it is true.timeout
- Maximum time to wait for the topology to complete or reach its end condition.unit
- Unit for timeout
.endCondition.valid()
.java.lang.Exception
- Failure submitting or executing the topology.java.lang.IllegalStateException
- StreamsContext.getType()
is not supported.complete(StreamsContext)
boolean complete(StreamsContext<?> context, java.util.Map<java.lang.String,java.lang.Object> config, Condition<?> endCondition, long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.Exception
java.lang.Exception
Condition<java.util.List<java.lang.String>> completeAndTestStringOutput(StreamsContext<?> context, TStream<?> output, long timeout, java.util.concurrent.TimeUnit unit, java.lang.String... contents) throws java.lang.Exception
java.lang.Exception
Condition<java.util.List<java.lang.String>> completeAndTestStringOutput(StreamsContext<?> context, java.util.Map<java.lang.String,java.lang.Object> config, TStream<?> output, long timeout, java.util.concurrent.TimeUnit unit, java.lang.String... contents) throws java.lang.Exception
java.lang.Exception