void snapshotSession(final ClusterSession session)
    {
        final String responseChannel = session.responseChannel();
        final int length = MessageHeaderEncoder.ENCODED_LENGTH + ClusterSessionEncoder.BLOCK_LENGTH +
            ClusterSessionEncoder.responseChannelHeaderLength() + responseChannel.length();

        if (length <= publication.maxPayloadLength())
        {
            idleStrategy.reset();
            while (true)
            {
                final long result = publication.tryClaim(length, bufferClaim);
                if (result > 0)
                {
                    encodeSession(session, responseChannel, bufferClaim.buffer(), bufferClaim.offset());
                    bufferClaim.commit();
                    break;
                }

                checkResultAndIdle(result);
            }
        }
        else
        {
            final int offset = 0;
            encodeSession(session, responseChannel, offerBuffer, offset);
            offer(offerBuffer, offset, length);
        }
    }
@Test
    void snapshotSessionShouldUseTryClaimIfDataFitsIntoMaxPayloadSize()
    {
        final int offset = 10;
        final String responseChannel = "aeron:ipc";
        final int length = MessageHeaderEncoder.ENCODED_LENGTH + ClusterSessionEncoder.BLOCK_LENGTH +
            ClusterSessionEncoder.responseChannelHeaderLength() + responseChannel.length();
        final ClusterSession clusterSession = new ClusterSession(556, 42, responseChannel);
        clusterSession.loadSnapshotState(
            13, 1024, 800, CloseReason.CLIENT_ACTION);
        when(publication.maxPayloadLength()).thenReturn(length);
        when(publication.tryClaim(eq(length), any()))
            .thenReturn(BACK_PRESSURED, ADMIN_ACTION)
            .thenAnswer(mockTryClaim(offset));

        snapshotTaker.snapshotSession(clusterSession);

        final InOrder inOrder = inOrder(idleStrategy, publication);
        inOrder.verify(publication).maxPayloadLength();
        inOrder.verify(idleStrategy).reset();
        inOrder.verify(publication).tryClaim(anyInt(), any());
        inOrder.verify(idleStrategy).idle();
        inOrder.verify(publication).tryClaim(anyInt(), any());
        inOrder.verify(idleStrategy).idle();
        inOrder.verify(publication).tryClaim(anyInt(), any());
        inOrder.verifyNoMoreInteractions();

        clusterSessionDecoder.wrapAndApplyHeader(buffer, offset + HEADER_LENGTH, messageHeaderDecoder);
        assertEquals(clusterSession.id(), clusterSessionDecoder.clusterSessionId());
        assertEquals(clusterSession.correlationId(), clusterSessionDecoder.correlationId());
        assertEquals(clusterSession.openedLogPosition(), clusterSessionDecoder.openedLogPosition());
        assertEquals(Aeron.NULL_VALUE, clusterSessionDecoder.timeOfLastActivity());
        assertEquals(clusterSession.closeReason(), clusterSessionDecoder.closeReason());
        assertEquals(clusterSession.responseStreamId(), clusterSessionDecoder.responseStreamId());
        assertEquals(responseChannel, clusterSessionDecoder.responseChannel());
    }