Hello guys,

I've created a library to help narrow integration testing of Stateful Functions 
applications written in Java. It utilizes Stateful Functions' RequestReply 
protocol and provides Java DSL to test the function. Statefun Tsukuyomi sets up 
the function under test with the initial state, interacts, and validates 
outgoing messages and the state after invocation. Ex:

@Test
@Timeout(30)
void verifiesThatTheFunctionSendsMessagesInOrderTheyExpected() {
  // Define your envelopes (envelopes describe messages)
  Envelope envelope = incomingEnvelope();
  Envelope expectedToFunction = outgoingEnvelopeToFunction();
  Envelope expectedToEgress = outgoingEnvelopeToEgress();
  Envelope expectedToSelf = outgoingEnvelopeToSelf();
  Envelope expectedToFunctionDelayed = delayedEnvelopeToFunction();
  // Define function under test and its initial state
  GivenFunction testee = given(
      function(Testee.TYPE, new Testee()),
      withState(Testee.FOO, StateValue.empty()),
      withState(Testee.BAR, StateValue.havingValue(BAR))
  );

  // When function under test receives that envelope
  when(
      testee,
      receives(envelope)
  ).then(
      // Then expect it sends the following messages
      sendsInOrder(expectedToFunction),
      sendsInOrder(expectedToSelf),
      sendsInOrder(expectedToEgress),
      sendsInOrder(expectedToFunctionDelayed),
      // and has the following state value after invocation
      state(Testee.FOO, is("foo")) // Hamcrest matchers supported
  );
}

It runs Flink inside a Docker container and a function under test in Undertow. 
Undertow accepts Flink calls and forwards messages to the function as an actual 
remote module.

Since this approach utilizes actual Flink, it enables testing of:

• Serialization and deserialization of function's state
• Serialization and deserialization of incoming and outgoing messages
• Exchanging real state and messages with real Flink instance


I've built Statefun Tsukuyomi to help our internal development, but it's pretty 
generic and might be helpful for some of you. Please let me know if you find it 
useful.

Pay attention that Docker is required.
Kudos to the Flink team for the inspiration. Their approach to testing Stateful 
Functions inspired me to build this library.
Check out the project page for more information: 
https://github.com/4insyde/statefun-tsukuyomi

Best,
Tymur Yarosh

Reply via email to