public class FunctionTestHarness
extends java.lang.Object
FunctionTestHarness
provides a thin convenience wrapper around a StatefulFunction
to capture its results under test.
The harness captures all messages sent using the Context
from within the functions StatefulFunction.invoke(Context, Object)
method and returns
them. Values sent to an egress are also captured and can be queried via the getEgress(EgressIdentifier)
method.
ImportantThis test harness is intended strictly for basic unit tests of functions. As
such, Context.registerAsyncOperation(Object, CompletableFuture)
awaits all futures. If
you want to test in an asyncronous environment please consider using the the statefun-flink-harness
.
{@code @Test}
public void test() {
FunctionType type = new FunctionType("flink", "testfunc");
FunctionTestHarness harness = TestHarness.test(new TestFunctionProvider(), type, "my-id");
Assert.assertThat(
harness.invoke("ping"),
sent(
messagesTo(
new Address(new FunctionType("flink", "func"), "id"), equalTo("pong"));
}
Modifier and Type | Method and Description |
---|---|
<T> java.util.List<T> |
getEgress(EgressIdentifier<T> identifier) |
java.util.Map<Address,java.util.List<java.lang.Object>> |
invoke(Address from,
java.lang.Object message) |
java.util.Map<Address,java.util.List<java.lang.Object>> |
invoke(java.lang.Object message) |
static FunctionTestHarness |
test(StatefulFunctionProvider provider,
FunctionType type,
java.lang.String id)
Creates a test harness, pinning the function to a particular address.
|
static FunctionTestHarness |
test(StatefulFunctionProvider provider,
FunctionType type,
java.lang.String id,
java.time.Instant startTime)
Creates a test harness, pinning the function to a particular address.
|
java.util.Map<Address,java.util.List<java.lang.Object>> |
tick(java.time.Duration duration)
Advances the internal clock the harness and fires and pending timers.
|
public static FunctionTestHarness test(StatefulFunctionProvider provider, FunctionType type, java.lang.String id, java.time.Instant startTime)
provider
- A provider for the function under test.type
- The type of the function.id
- The static id of the function for the duration of the test.startTime
- The initial timestamp of the internal clock.public static FunctionTestHarness test(StatefulFunctionProvider provider, FunctionType type, java.lang.String id)
provider
- A provider for the function under test.type
- The type of the function.id
- The static id of the function for the duration of the test.public java.util.Map<Address,java.util.List<java.lang.Object>> invoke(java.lang.Object message)
message
- A message that will be sent to the function.Context.send(Address,
Object)
.public java.util.Map<Address,java.util.List<java.lang.Object>> invoke(Address from, java.lang.Object message)
message
- A message that will be sent to the function.from
- The address of the function that sent the message.Context.send(Address,
Object)
.public java.util.Map<Address,java.util.List<java.lang.Object>> tick(java.time.Duration duration)
duration
- the amount of time to advance for this tick.public <T> java.util.List<T> getEgress(EgressIdentifier<T> identifier)
T
- the data type consumed by the egress.identifier
- An egress identifierCopyright © 2014–2024 The Apache Software Foundation. All rights reserved.