@Test(expected = IllegalArgumentException.class)
  public void shouldThrowExceptionWhenMessageTypeIdInvalid() {
    final int invalidMsgId = -1;
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[1024]);

    broadcastTransmitter.transmit(invalidMsgId, srcBuffer, 0, 32);
  }
  @Test
  public void shouldApplyPaddingWhenInsufficientSpaceAtEndOfBuffer() {
    long tail = CAPACITY - RECORD_ALIGNMENT;
    int recordOffset = (int) tail;
    final int length = RECORD_ALIGNMENT + 8;
    final int recordLength = length + HEADER_LENGTH;
    final int recordLengthAligned = align(recordLength, RECORD_ALIGNMENT);
    final int toEndOfBuffer = CAPACITY - recordOffset;

    when(buffer.getLong(TAIL_COUNTER_INDEX)).thenReturn(tail);

    final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[1024]);
    final int srcIndex = 0;

    broadcastTransmitter.transmit(MSG_TYPE_ID, srcBuffer, srcIndex, length);

    final InOrder inOrder = inOrder(buffer);
    inOrder.verify(buffer).getLong(TAIL_COUNTER_INDEX);

    inOrder
        .verify(buffer)
        .putLong(TAIL_INTENT_COUNTER_OFFSET, tail + recordLengthAligned + toEndOfBuffer);

    inOrder.verify(buffer).putInt(lengthOffset(recordOffset), toEndOfBuffer);
    inOrder.verify(buffer).putInt(typeOffset(recordOffset), PADDING_MSG_TYPE_ID);

    tail += toEndOfBuffer;
    recordOffset = 0;
    inOrder.verify(buffer).putInt(lengthOffset(recordOffset), recordLength);
    inOrder.verify(buffer).putInt(typeOffset(recordOffset), MSG_TYPE_ID);
    inOrder.verify(buffer).putBytes(msgOffset(recordOffset), srcBuffer, srcIndex, length);

    inOrder.verify(buffer).putLong(LATEST_COUNTER_INDEX, tail);
    inOrder.verify(buffer).putLongOrdered(TAIL_COUNTER_INDEX, tail + recordLengthAligned);
  }
  @Test(expected = IllegalArgumentException.class)
  public void shouldThrowExceptionWhenMaxMessageLengthExceeded() {
    final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[1024]);

    broadcastTransmitter.transmit(
        MSG_TYPE_ID, srcBuffer, 0, broadcastTransmitter.maxMsgLength() + 1);
  }
  @Test
  public void shouldTransmitIntoUsedBuffer() {
    final long tail = RECORD_ALIGNMENT * 3;
    final int recordOffset = (int) tail;
    final int length = 8;
    final int recordLength = length + HEADER_LENGTH;
    final int recordLengthAligned = align(recordLength, RECORD_ALIGNMENT);

    when(buffer.getLong(TAIL_COUNTER_INDEX)).thenReturn(tail);

    final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[1024]);
    final int srcIndex = 0;

    broadcastTransmitter.transmit(MSG_TYPE_ID, srcBuffer, srcIndex, length);

    final InOrder inOrder = inOrder(buffer);
    inOrder.verify(buffer).getLong(TAIL_COUNTER_INDEX);

    inOrder.verify(buffer).putLong(TAIL_INTENT_COUNTER_OFFSET, tail + recordLengthAligned);
    inOrder.verify(buffer).putInt(lengthOffset(recordOffset), recordLength);
    inOrder.verify(buffer).putInt(typeOffset(recordOffset), MSG_TYPE_ID);
    inOrder.verify(buffer).putBytes(msgOffset(recordOffset), srcBuffer, srcIndex, length);

    inOrder.verify(buffer).putLong(LATEST_COUNTER_INDEX, tail);
    inOrder.verify(buffer).putLongOrdered(TAIL_COUNTER_INDEX, tail + recordLengthAligned);
  }