Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public class RaftEngine {
private volatile int alivePeerCount = -1;
private ScheduledExecutorService alivePeersRefresher;

public RaftEngine() {
private RaftEngine() {
this.stateMachine = new RaftStateMachine();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.hugegraph.pd.core.store;

import java.lang.reflect.Constructor;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -131,9 +132,9 @@ public void testWritesReachStoreOnlyThroughRaft() throws PDException {
}

@Test
public void testWriteOnNonLeaderIsRejectedAndNotApplied() throws PDException {
public void testWriteOnNonLeaderIsRejectedAndNotApplied() throws Exception {
// A RaftEngine that has not joined a group is never leader
RaftKVStore notLeader = new RaftKVStore(new RaftEngine(), this.store);
RaftKVStore notLeader = new RaftKVStore(newEngine(), this.store);

PDException e = Assert.assertThrows(PDException.class, () -> notLeader.put(KEY, VALUE));

Expand Down Expand Up @@ -236,4 +237,10 @@ private static void assertWriteFails(Write write) {
Assert.assertTrue(e.getMessage(),
e.getMessage().contains("Error code = " + Pdpb.ErrorType.UNKNOWN_VALUE));
}

private static RaftEngine newEngine() throws Exception {
Constructor<RaftEngine> constructor = RaftEngine.class.getDeclaredConstructor();
constructor.setAccessible(true);
return constructor.newInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.hugegraph.pd.raft;

import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -56,15 +57,21 @@ public class RaftEngineReadinessTest {
private RaftStateMachine stateMachine;

@Before
public void setUp() {
public void setUp() throws Exception {
// Other suites leave scheduled tasks reading the shared engine. Keep this mock
// private to the test so their isLeader() calls cannot affect interaction checks.
engine = new RaftEngine();
engine = newEngine();
mockNode = mock(Node.class);
stateMachine = Whitebox.getInternalState(engine, "stateMachine");
Whitebox.setInternalState(engine, "raftNode", mockNode);
}

private static RaftEngine newEngine() throws Exception {
Constructor<RaftEngine> constructor = RaftEngine.class.getDeclaredConstructor();
constructor.setAccessible(true);
return constructor.newInstance();
}

private static LeaderChangeContext ctx() {
return new LeaderChangeContext(LEADER, 5, Status.OK());
}
Expand Down
Loading