@Test
    public void testRecordsFlushedImmediatelyOnTransactionCompletion() throws Exception {
        try (Metrics m = new Metrics()) {
            int lingerMs = 50;
            SenderMetricsRegistry senderMetrics = new SenderMetricsRegistry(m);

            TransactionManager txnManager = new TransactionManager(logContext, "txnId", 6000, 100, apiVersions);
            setupWithTransactionState(txnManager, lingerMs);

            Sender sender = new Sender(logContext, client, metadata, this.accumulator, false, MAX_REQUEST_SIZE, ACKS_ALL,
                1, senderMetrics, time, REQUEST_TIMEOUT, RETRY_BACKOFF_MS, txnManager, apiVersions);

            // Begin a transaction and successfully add one partition to it.
            ProducerIdAndEpoch producerIdAndEpoch = new ProducerIdAndEpoch(123456L, (short) 0);
            doInitTransactions(txnManager, producerIdAndEpoch);
            txnManager.beginTransaction();
            addPartitionToTxn(sender, txnManager, tp0);

            // Send a couple records and assert that they are not sent immediately (due to linger).
            appendToAccumulator(tp0);
            appendToAccumulator(tp0);
            sender.runOnce();
            assertFalse(client.hasInFlightRequests());

            // Now begin the commit and assert that the Produce request is sent immediately
            // without waiting for the linger.
            TransactionalRequestResult commitResult = txnManager.beginCommit();
            runUntil(sender, client::hasInFlightRequests);

            // Respond to the produce request and wait for the EndTxn request to be sent.
            respondToProduce(tp0, Errors.NONE, 1L);
            runUntil(sender, txnManager::hasInFlightRequest);

            // Respond to the expected EndTxn request.
            respondToEndTxn(Errors.NONE);
            runUntil(sender, txnManager::isReady);

            assertTrue(commitResult.isSuccessful());
            commitResult.await();

            // Finally, we want to assert that the linger time is still effective
            // when the new transaction begins.
            txnManager.beginTransaction();
            addPartitionToTxn(sender, txnManager, tp0);

            appendToAccumulator(tp0);
            appendToAccumulator(tp0);
            time.sleep(lingerMs - 1);
            sender.runOnce();
            assertFalse(client.hasInFlightRequests());
            assertTrue(accumulator.hasUndrained());

            time.sleep(1);
            runUntil(sender, client::hasInFlightRequests);
            assertFalse(accumulator.hasUndrained());
        }
    }
Tests the code path where the target node to send FindCoordinator or InitProducerId
is not ready.
clients/src/test/java/org/apache/kafka/clients/producer/internals/SenderTest.java