@Test
    public void testSnapshotDuringTransaction() throws Exception {
        MockFaultHandler faultHandler = new MockFaultHandler("testSnapshotDuringTransaction");
        MockPublisher publisher = new MockPublisher("testSnapshotDuringTransaction");
        List<MockPublisher> publishers = singletonList(publisher);
        try (MetadataLoader loader = new MetadataLoader.Builder().
                setFaultHandler(faultHandler).
                setHighWaterMarkAccessor(() -> OptionalLong.of(0)).
                build()) {
            loader.installPublishers(publishers).get();
            loader.waitForAllEventsToBeHandled();

            loader.handleCommit(
                MockBatchReader.newSingleBatchReader(500, 100, Arrays.asList(
                    new ApiMessageAndVersion(new BeginTransactionRecord()
                        .setName("txn-1"), (short) 0),
                    new ApiMessageAndVersion(new TopicRecord()
                        .setName("foo")
                        .setTopicId(Uuid.fromString("HQSM3ccPQISrHqYK_C8GpA")), (short) 0)
                )));
            loader.waitForAllEventsToBeHandled();
            publisher.firstPublish.get(30, TimeUnit.SECONDS);
            assertNull(publisher.latestImage.topics().getTopic("foo"));

            // loading a snapshot discards any in-flight transaction
            loader.handleLoadSnapshot(MockSnapshotReader.fromRecordLists(
                new MetadataProvenance(600, 101, 4000), singletonList(
                    singletonList(new ApiMessageAndVersion(new TopicRecord().
                        setName("foo").
                        setTopicId(Uuid.fromString("Uum7sfhHQP-obSvfywmNUA")), (short) 0))
                )));
            loader.waitForAllEventsToBeHandled();
            assertEquals("Uum7sfhHQP-obSvfywmNUA",
                publisher.latestImage.topics().getTopic("foo").id().toString());
        }
        faultHandler.maybeRethrowFirstException();
    }
Test that loading a snapshot clears the previous state.
metadata/src/test/java/org/apache/kafka/image/loader/MetadataLoaderTest.java