RaftActor extends AbstractUntypedPersistentActor { private void switchBehavior(final SwitchBehavior message) { if (!getRaftActorContext().getRaftPolicy().automaticElectionsEnabled()) { RaftState newState = message.getNewState(); if (newState == RaftState.Leader || newState == RaftState.Follower) { getRaftActorContext().getTermInformation().updateAndPersist(message.getNewTerm(), ""); switchBehavior(behaviorStateTracker.capture(getCurrentBehavior()), AbstractRaftActorBehavior.createBehavior(context, message.getNewState())); } else { LOG.warn("Switching to behavior : {} - not supported", newState); } } } protected  RaftActor(final String id, final Map<String, String> peerAddresses,
         final Optional<ConfigParams> configParams, final short payloadVersion); @Override void preStart(); @Override void postStop(); @Override long snapshotSequenceNr(); ActorSelection getLeader(); final DataPersistenceProvider persistence(); void setPersistence(final DataPersistenceProvider provider);  }
@Test public void testSwitchBehavior() { String persistenceId = factory.generateActorId("leader-"); DefaultConfigParamsImpl config = new DefaultConfigParamsImpl(); config.setCustomRaftPolicyImplementationClass(DisableElectionsRaftPolicy.class.getName()); config.setHeartBeatInterval(new FiniteDuration(1, TimeUnit.DAYS)); config.setIsolatedLeaderCheckInterval(new FiniteDuration(1, TimeUnit.DAYS)); config.setSnapshotBatchCount(5); DataPersistenceProvider dataPersistenceProvider = createProvider(); Map<String, String> peerAddresses = ImmutableMap.<String, String>builder().build(); TestActorRef<MockRaftActor> mockActorRef = factory.createTestActor( MockRaftActor.props(persistenceId, peerAddresses, config, dataPersistenceProvider), persistenceId); MockRaftActor leaderActor = mockActorRef.underlyingActor(); leaderActor.waitForRecoveryComplete(); leaderActor.handleCommand(new SwitchBehavior(RaftState.Follower, 100)); assertEquals(100, leaderActor.getRaftActorContext().getTermInformation().getCurrentTerm()); assertEquals(RaftState.Follower, leaderActor.getCurrentBehavior().state()); leaderActor.handleCommand(new SwitchBehavior(RaftState.Leader, 110)); assertEquals(110, leaderActor.getRaftActorContext().getTermInformation().getCurrentTerm()); assertEquals(RaftState.Leader, leaderActor.getCurrentBehavior().state()); leaderActor.handleCommand(new SwitchBehavior(RaftState.Candidate, 125)); assertEquals(110, leaderActor.getRaftActorContext().getTermInformation().getCurrentTerm()); assertEquals(RaftState.Leader, leaderActor.getCurrentBehavior().state()); leaderActor.handleCommand(new SwitchBehavior(RaftState.IsolatedLeader, 125)); assertEquals(110, leaderActor.getRaftActorContext().getTermInformation().getCurrentTerm()); assertEquals(RaftState.Leader, leaderActor.getCurrentBehavior().state()); }