@Before
  public void setUp() throws Exception {
    frameEncoder = new MessageLengthFrameEncoder();

    SimpleDirectory directory = new SimpleDirectory();
    engine =
        Engine.builder()
            .withAuthenticator(new SimpleAuthenticator().withDirectory(directory))
            .build();
    engine.open();

    reactor2 =
        EventReactor.builder()
            .withDispatcher(new ByteBufferDispatcher())
            .withPayloadAllocator(new ByteBufferPayload(2048))
            .build();
    reactor2.open().get();

    directory.add(userCredentials);

    messages = new byte[messageCount][];
    for (int i = 0; i < messageCount; ++i) {
      messages[i] = new byte[i];
      Arrays.fill(messages[i], (byte) i);
    }
  }
  @Test
  public void sendIdempotent() throws Exception {
    TestReceiver serverReceiver = new TestReceiver();
    Function<Transport, FixpSession> clientAcceptor =
        new Function<Transport, FixpSession>() {

          public FixpSession apply(Transport serverTransport) {
            try {
              FixpSession serverSession =
                  FixpSession.builder()
                      .withReactor(engine.getReactor())
                      .withTransport(serverTransport)
                      .withBufferSupplier(
                          new SingleBufferSupplier(
                              ByteBuffer.allocate(16 * 1024).order(ByteOrder.nativeOrder())))
                      .withMessageConsumer(serverReceiver)
                      .withOutboundFlow(FlowType.Idempotent)
                      .withOutboundKeepaliveInterval(keepAliveInterval)
                      .asServer()
                      .build();

              serverSession.open();

              return serverSession;
            } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
            return null;
          }
        };

    KeyStore ksKeys = Crypto.createKeyStore();
    Crypto.addKeyCertificateEntry(ksKeys, "exchange", "CN=trading, O=myorg, C=US", storePassphrase);

    KeyStore ksTrust = Crypto.createKeyStore();
    Crypto.addKeyCertificateEntry(
        ksTrust, "customer", "CN=Trader1, O=SomeFCM, C=US", storePassphrase);

    final InetSocketAddress serverAddress =
        new InetSocketAddress(InetAddress.getLoopbackAddress(), 7741);

    try (TlsTcpAcceptor tcpAcceptor =
        new TlsTcpAcceptor(
            engine.getIOReactor().getSelector(),
            serverAddress,
            ksKeys,
            ksTrust,
            storePassphrase,
            clientAcceptor)) {
      tcpAcceptor.open().get();

      Transport clientTransport =
          new TlsTcpConnectorTransport(
              engine.getIOReactor().getSelector(), serverAddress, ksTrust, ksKeys, storePassphrase);
      TestReceiver clientReceiver = new TestReceiver();
      UUID sessionId = SessionId.generateUUID();

      FixpSession clientSession =
          FixpSession.builder()
              .withReactor(reactor2)
              .withTransport(clientTransport)
              .withBufferSupplier(
                  new SingleBufferSupplier(
                      ByteBuffer.allocate(16 * 1024).order(ByteOrder.nativeOrder())))
              .withMessageConsumer(clientReceiver)
              .withOutboundFlow(FlowType.Idempotent)
              .withSessionId(sessionId)
              .withClientCredentials(userCredentials.getBytes())
              .withOutboundKeepaliveInterval(keepAliveInterval)
              .build();

      SessionReadyFuture readyFuture = new SessionReadyFuture(sessionId, reactor2);
      // Completes when transport is established or throws if IO error
      clientSession.open().get(1000, TimeUnit.MILLISECONDS);
      // Completes when FIXP session is established
      readyFuture.get(3000, TimeUnit.MILLISECONDS);

      ByteBuffer buf = ByteBuffer.allocate(8096).order(ByteOrder.nativeOrder());
      int bytesSent = 0;
      for (int i = 0; i < messageCount; ++i) {
        buf.clear();
        bytesSent += encodeApplicationMessageWithFrame(buf, messages[i]);
        clientSession.send(buf);
      }

      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {

      }
      assertEquals(messageCount, serverReceiver.getMsgsReceived());

      SessionTerminatedFuture terminatedFuture = new SessionTerminatedFuture(sessionId, reactor2);
      clientSession.close();
      terminatedFuture.get(1000, TimeUnit.MILLISECONDS);
    }
  }
 @After
 public void tearDown() throws Exception {
   engine.close();
   reactor2.close();
 }