Record Class ContextExpectations

java.lang.Object
java.lang.Record
dev.restate.sdk.fake.ContextExpectations

@Experimental public record ContextExpectations(long randomSeed, String invocationId, Map<String,String> requestHeaders, Map<String,ContextExpectations.RunExpectation> runExpectations, BiPredicate<Duration,String> completeTimerIf, SerdeFactory serdeFactory) extends Record
Expectation configuration for FakeContext.

This record defines the expected behavior and configuration for a fake context used in testing. It controls various aspects including:

By default, the FakeContext will execute all ctx.run.

  • Constructor Details

    • ContextExpectations

      public ContextExpectations()
    • ContextExpectations

      public ContextExpectations(long randomSeed, String invocationId, Map<String,String> requestHeaders, Map<String,ContextExpectations.RunExpectation> runExpectations, BiPredicate<Duration,String> completeTimerIf, SerdeFactory serdeFactory)
      Creates an instance of a ContextExpectations record class.
      Parameters:
      randomSeed - the value for the randomSeed record component
      invocationId - the value for the invocationId record component
      requestHeaders - the value for the requestHeaders record component
      runExpectations - the value for the runExpectations record component
      completeTimerIf - the value for the completeTimerIf record component
      serdeFactory - the value for the serdeFactory record component
  • Method Details

    • withRandomSeed

      public ContextExpectations withRandomSeed(long randomSeed)
      Set the random seed to be used by Context.random().
      Parameters:
      randomSeed - the random seed to use
    • withInvocationId

      public ContextExpectations withInvocationId(String invocationId)
      Set the invocation id returned by ctx.request().invocationId().
      Parameters:
      invocationId - the invocation ID to use
    • withRequestHeaders

      public ContextExpectations withRequestHeaders(Map<String,String> requestHeaders)
      Set the request headers returned by ctx.request().headers().
      Parameters:
      requestHeaders - the request headers to use
    • executeRun

      public ContextExpectations executeRun(String runName)
      Specify that the run with the given name should be executed.

      The mocked context will try to execute the run, and in case of a failure, the given exception will be thrown as is.

      Parameters:
      runName - the name of the run that should be executed
    • dontRetryRun

      public ContextExpectations dontRetryRun(String runName)
      Specify that the run with the given name should not be retried.

      The mocked context will try to execute the run, and in case of a failure, the given exception will be converted to TerminalException.

      This is useful when unit testing a saga, and you want to simulate the "catch" branch.

      Parameters:
      runName - the name of the run that should not be retried
    • dontExecuteRun

      public ContextExpectations dontExecuteRun(String runName)
      Specify that the run with the given name should not be executed.

      The mocked context will not execute the run.

      This is useful when testing a flow where you either want to wait a ctx.run to complete, or another event (such as timers)

      Parameters:
      runName - the name of the run that should not be executed
    • completeAllTimersImmediately

      public ContextExpectations completeAllTimersImmediately()
      Specify that all timers immediately complete.
    • completeTimerNamed

      public ContextExpectations completeTimerNamed(String timerName)
      Specify that the timer with the given name complete as soon as they're created.
    • completeTimerLongerOrEqualThan

      public ContextExpectations completeTimerLongerOrEqualThan(Duration duration)
      Specify that all timers with duration longer than the given value complete as soon as they're created.
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • randomSeed

      public long randomSeed()
      Returns the value of the randomSeed record component.
      Returns:
      the value of the randomSeed record component
    • invocationId

      public String invocationId()
      Returns the value of the invocationId record component.
      Returns:
      the value of the invocationId record component
    • requestHeaders

      public Map<String,String> requestHeaders()
      Returns the value of the requestHeaders record component.
      Returns:
      the value of the requestHeaders record component
    • runExpectations

      public Map<String,ContextExpectations.RunExpectation> runExpectations()
      Returns the value of the runExpectations record component.
      Returns:
      the value of the runExpectations record component
    • completeTimerIf

      public BiPredicate<Duration,String> completeTimerIf()
      Returns the value of the completeTimerIf record component.
      Returns:
      the value of the completeTimerIf record component
    • serdeFactory

      public SerdeFactory serdeFactory()
      Returns the value of the serdeFactory record component.
      Returns:
      the value of the serdeFactory record component