/**
  * Large message version of {@link #setBody(int, ClientMessage)}.
  *
  * @param i
  * @param message
  */
 protected static void setLargeMessageBody(final int i, final ClientMessage message) {
   try {
     message.setBodyInputStream(ActiveMQTestBase.createFakeLargeStream(LARGE_MESSAGE_SIZE));
   } catch (Exception e) {
     throw new RuntimeException(e);
   }
 }
  @Test
  public void testLargeMessageFileLeak() throws Exception {
    OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();

    // only run this on *nix systems which will have the
    // com.sun.management.UnixOperatingSystemMXBean (needed to check open file count)
    Assume.assumeTrue(os instanceof UnixOperatingSystemMXBean);

    long fdBaseline = ((UnixOperatingSystemMXBean) os).getOpenFileDescriptorCount();
    final int SIZE = 2 * 1024;
    SimpleString dla = new SimpleString("DLA");
    SimpleString qName = new SimpleString("q1");
    SimpleString adName = new SimpleString("ad1");

    AddressSettings addressSettings =
        new AddressSettings().setMaxDeliveryAttempts(1).setDeadLetterAddress(dla);
    server.getAddressSettingsRepository().addMatch(adName.toString(), addressSettings);
    SimpleString dlq = new SimpleString("DLQ1");
    clientSession.createQueue(dla, dlq, null, false);
    clientSession.createQueue(adName, qName, null, false);
    for (int i = 0; i < 10; i++) {
      ClientProducer producer = clientSession.createProducer(adName);
      ClientMessage clientFile = clientSession.createMessage(true);
      clientFile.setBodyInputStream(ActiveMQTestBase.createFakeLargeStream(SIZE));
      producer.send(clientFile);
      clientSession.start();
      ClientConsumer clientConsumer = clientSession.createConsumer(qName);
      ClientMessage m = clientConsumer.receive(500);
      m.acknowledge();
      Assert.assertNotNull(m);

      // force a cancel
      clientSession.rollback();
      m = clientConsumer.receiveImmediate();
      Assert.assertNull(m);
      clientConsumer.close();
    }
    assertEquals(
        "File descriptors are leaking",
        0,
        ((UnixOperatingSystemMXBean) os).getOpenFileDescriptorCount() - fdBaseline);
  }