Class FaultyTransport

java.lang.Object
org.mqtt4j.hato.testkit.FaultyTransport
All Implemented Interfaces:
AutoCloseable, Transport

public final class FaultyTransport extends Object implements Transport
A wrapper that adds ordered receive delays, send delays, and disconnection to a transport. Neither direction is delayed until a delay is set. The scheduler measures the delays, so with ManualTimerScheduler, delayed data moves on when the test advances it. Use a long delay in either direction to simulate stopped traffic. A delay must fit in nanoseconds, and the scheduler must accept the resulting deadline. At most one send is outstanding; admission retains its array until onSent or the final onClosed. The input that a receive delay holds back is bounded: beyond the bound, the wrapper ends the connection with MqttCapacityException, so a long delay on a connection whose peer keeps sending can end it before the client's own deadlines. The wrapper owns and closes the delegate, but borrows the scheduler.
  • Constructor Details

    • FaultyTransport

      public FaultyTransport(Transport delegate, TimerScheduler scheduler)
      Wraps delegate, timing the delays with scheduler.
      Parameters:
      delegate - the transport to wrap, which the wrapper owns and closes
      scheduler - the scheduler of the delays, which the wrapper borrows
  • Method Details

    • receiveDelay

      public FaultyTransport receiveDelay(Duration value)
      Changes the delay of subsequently received chunks. Earlier chunks keep their deadlines; later chunks never overtake them even when the delay is reduced.
      Parameters:
      value - the receive delay
      Returns:
      this transport
      
      Throws:
      IllegalArgumentException -
      if `value` is negative
      
      ArithmeticException -
      if `value` does not fit in nanoseconds, about 292 years
      
    • sendDelay

      public FaultyTransport sendDelay(Duration value)
      Changes the delay of subsequently admitted sends. An admitted send keeps its deadline. Zero delegates immediately; a positive delay postpones delegation, not write completion. Closing before delegation discards the send; the final onClosed releases its array.
      Parameters:
      value - the send delay
      Returns:
      this transport
      
      Throws:
      IllegalArgumentException -
      if `value` is negative
      
      ArithmeticException -
      if `value` does not fit in nanoseconds, about 292 years
      
    • start

      public void start(URI endpoint, Duration timeout, Transport.Listener receiver)
      Specified by:
      start in interface Transport
    • send

      public Transport.SendResult send(long ticket, byte[] bytes)
      Specified by:
      send in interface Transport
    • disconnect

      public void disconnect(Throwable cause)
      Ends the connection with cause: later sends are rejected, delayed and later input is discarded, and the delegate is closed. A write the delegate has already completed is still reported through onSent before the final onClosed, which carries cause. No MQTT packet is sent, so the peer sees the connection fail, as on a network failure.
      Parameters:
      cause - the cause of the disconnection
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Transport