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
,
StreamsContext.Type.STREAMING_ANALYTICS_SERVICE_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 | Field and Description |
---|---|
static java.lang.String |
TEST_TRACE_LEVEL
System property to set the trace level of application under tests.
|
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. |
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) |
void |
complete(StreamsContext<?> context)
Submit the topology for this tester and wait for it to complete.
|
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.
|
Condition<java.lang.Void> |
resetConsistentRegions(java.lang.Integer minimumResets)
Create a condition that randomly resets consistent regions.
|
<T extends com.ibm.streams.flow.handlers.StreamHandler<com.ibm.streams.operator.Tuple>> |
splHandler(SPLStream stream,
T handler)
Deprecated.
Since 1.11. In most distributed environments stream contents cannot be obtained
due to network isolation. An alternative is to use
stringTupleTester
to perform a per-tuple check. |
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)
Return a condition that evaluates if every tuple on
stream
evaluates to true with tester . |
Condition<java.util.List<com.ibm.streams.operator.Tuple>> |
tupleContents(SPLStream stream,
com.ibm.streams.operator.Tuple... values)
Return a condition that evaluates if
stream has submitted
at tuples matching values in the same order. |
Condition<java.lang.Long> |
tupleCount(TStream<?> stream,
long expectedCount)
Return a condition that evaluates if
stream has submitted
exactly expectedCount number of tuples. |
static final java.lang.String TEST_TRACE_LEVEL
StreamsContext.Type#STANDALONE_TESTER
,
StreamsContext.Type#DISTRIBUTED_TESTER
and
StreamsContext.Type#STREAMING_ANALYTICS_SERVICE_TESTER
contexts
setting this property overrides any default trace level or any trace
level set by a test.
The value should be one of the enumeration names of java.util.logging.Level
.
SEVERE
- IBM Streams error
.WARNING
- IBM Streams warn
.INFO
- IBM Streams info
.FINE
- IBM Streams debug
.FINEST
, ALL
- IBM Streams trace
.OFF
- IBM Streams off
.Topology getTopology()
<T extends com.ibm.streams.flow.handlers.StreamHandler<com.ibm.streams.operator.Tuple>> T splHandler(SPLStream stream, T handler)
stringTupleTester
to perform a per-tuple check.handler
to capture the output of stream
.
Not supported when testing using
STREAMING_ANALYTICS_SERVICE_TESTER
context.
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.
Note: Since 1.11 getResult()
from the returned Condition
is
deprecated and tests should not rely on it returning the tuple count seen on the stream.
The result
of the returned Condition
is the number of
tuples seen on stream
so far.
stream
- Stream to be tested.expectedCount
- Number of tuples expected on stream
.Condition<java.lang.Long> atLeastTupleCount(TStream<?> stream, long expectedCount)
stream
has submitted
at least expectedCount
number of tuples.
Note: Since 1.11 getResult()
from the returned Condition
is
deprecated and tests should not rely on it returning the tuple count seen on the stream.
The result
of the returned Condition
is the number of
tuples seen on stream
so far.
stream
- Stream to be tested.expectedCount
- Number of tuples expected on stream
.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.
Note: Since 1.11 getResult()
from the returned Condition
is
deprecated and tests should not rely on it returning the tuple count seen on the stream.
The result
of the returned Condition
is the
tuples seen on stream
so far.
stream
- Stream to be tested.values
- Expected tuples on stream
.Condition<java.util.List<com.ibm.streams.operator.Tuple>> tupleContents(SPLStream stream, com.ibm.streams.operator.Tuple... values)
stream
has submitted
at tuples matching values
in the same order.
Note: Since 1.11 getResult()
from the returned Condition
is
deprecated and tests should not rely on it returning the tuple count seen on the stream.
The result
of the returned Condition
is the
tuples seen on stream
so far.
stream
- Stream to be tested.values
- Expected tuples on stream
.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.
Note: Since 1.11 getResult()
from the returned Condition
is
deprecated and tests should not rely on it returning the tuple count seen on the stream.
The result
of the returned Condition
is the
tuples seen on stream
so far.
stream
- Stream to be tested.values
- Expected tuples on stream
.Condition<java.lang.String> stringTupleTester(TStream<java.lang.String> stream, Predicate<java.lang.String> tester)
stream
evaluates to true
with tester
.stream
- Stream to be tested.tester
- Predicate that will be executed against each tuple.Condition<java.lang.Void> resetConsistentRegions(java.lang.Integer minimumResets)
minimumResets
times
by the tester.
A region is reset by initiating a request though the Job Control Plane. The reset is not driven by any injected failure, such as a PE restart.
minimumResets
- - Minimum number of resets for each region, defaults to 10.java.lang.IllegalArgumentException
- minimumResets
less than zero.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