vaijosh commented on code in PR #3164:
URL: https://github.com/apache/hugegraph/pull/3164#discussion_r3896238294
##########
hugegraph-store/hg-store-test/src/main/java/org/apache/hugegraph/store/core/snapshot/HgSnapshotHandlerTest.java:
##########
@@ -42,13 +57,162 @@ public class HgSnapshotHandlerTest extends
StoreEngineTestBase {
private static HgSnapshotHandler hgSnapshotHandlerUnderTest;
+ @Rule
+ public TemporaryFolder tmpDir = new TemporaryFolder();
+
@Before
public void setUp() throws IOException {
hgSnapshotHandlerUnderTest = new
HgSnapshotHandler(createPartitionEngine(0));
FileUtils.forceMkdir(new File("/tmp/snapshot"));
FileUtils.forceMkdir(new File("/tmp/snapshot/data"));
}
+ // ── Fix 1: onSnapshotSave must throw when compaction is in progress
────────
+
+ /**
+ * Before the fix, onSnapshotSave silently returned when state == doing,
+ * causing JRaft to commit an empty snapshot dir with no data/.
+ * After the fix it must throw HgStoreException so JRaft retries instead.
+ */
+ @Test
+ public void testOnSnapshotSaveThrowsWhenCompactionInProgress() {
+ // Build a SnapshotHandler wired to a mock PartitionEngine whose
BusinessHandler
+ // reports state == doing (compaction active) for partition 0.
+ PartitionEngine mockEngine = mock(PartitionEngine.class);
+ HgStoreEngine mockStoreEngine = mock(HgStoreEngine.class);
+ BusinessHandler mockBusinessHandler = mock(BusinessHandler.class);
+
+ AtomicInteger doingState = new AtomicInteger(BusinessHandler.doing);
+
+ when(mockEngine.getGroupId()).thenReturn(0);
+ when(mockEngine.getStoreEngine()).thenReturn(mockStoreEngine);
+
when(mockStoreEngine.getBusinessHandler()).thenReturn(mockBusinessHandler);
+ when(mockBusinessHandler.getState(0)).thenReturn(doingState);
+
+ SnapshotHandler handler = new SnapshotHandler(mockEngine);
+
+ SnapshotWriter stubWriter = stubWriter("/tmp/snapshot");
+
+ HgStoreException ex = assertThrows(
+ "onSnapshotSave must throw when state == doing",
+ HgStoreException.class,
+ () -> handler.onSnapshotSave(stubWriter));
+
+ assertTrue("Exception message must mention the partition",
+ ex.getMessage().contains("0"));
+ assertTrue("Exception message must describe the cause",
+ ex.getMessage().contains("compaction in progress"));
+ }
+
+ /**
+ * When state is NOT doing (e.g. compactionDone), onSnapshotSave must not
throw.
+ */
+ @Test
+ public void testOnSnapshotSaveDoesNotThrowWhenNotBusy() throws Exception {
+ PartitionEngine mockEngine = mock(PartitionEngine.class);
+ HgStoreEngine mockStoreEngine = mock(HgStoreEngine.class);
+ BusinessHandler mockBusinessHandler = mock(BusinessHandler.class);
+
+ // state == compactionDone (not doing) — save should proceed normally
+ AtomicInteger doneState = new
AtomicInteger(BusinessHandler.compactionDone);
+
+ when(mockEngine.getGroupId()).thenReturn(0);
+ when(mockEngine.getStoreEngine()).thenReturn(mockStoreEngine);
+
when(mockStoreEngine.getBusinessHandler()).thenReturn(mockBusinessHandler);
+ when(mockBusinessHandler.getState(0)).thenReturn(doneState);
+
+ // saveSnapshot is a no-op via the mock, so we just need it not to
throw at the guard
+ SnapshotHandler handler = new SnapshotHandler(mockEngine);
+ SnapshotWriter stubWriter =
stubWriter(tmpDir.newFolder("snap-not-busy").getAbsolutePath());
+
+ // No exception should propagate from the state guard.
+ // (saveSnapshot will throw because the mock returns null for it —
that's fine,
+ // we only care the doing-check is not hit.)
+ try {
Review Comment:
Addressed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]