//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void candidateShouldWinElectionAndBecomeLeader() throws Exception //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void CandidateShouldWinElectionAndBecomeLeader() { // given FakeClock fakeClock = Clocks.fakeClock(); TimerService timeouts = new OnDemandTimerService(fakeClock); RaftMachine raft = (new RaftMachineBuilder(_myself, 3, RaftTestMemberSetBuilder.INSTANCE)).outbound(_outbound).timerService(timeouts).clock(fakeClock).build(); raft.InstallCoreState(new RaftCoreState(new MembershipEntry(0, asSet(_myself, _member1, _member2)))); raft.PostRecoveryActions(); timeouts.Invoke(RaftMachine.Timeouts.ELECTION); // when raft.Handle(voteResponse().from(_member1).term(1).grant().build()); raft.Handle(voteResponse().from(_member2).term(1).grant().build()); // then assertEquals(1, raft.Term()); assertEquals(LEADER, raft.CurrentRole()); /* * We require atLeast here because RaftMachine has its own scheduled service, which can spuriously wake up and * send empty entries. These are fine and have no bearing on the correctness of this test, but can cause it * fail if we expect exactly 2 of these messages */ verify(_outbound, atLeast(1)).send(eq(_member1), isA(typeof(Org.Neo4j.causalclustering.core.consensus.RaftMessages_AppendEntries_Request))); verify(_outbound, atLeast(1)).send(eq(_member2), isA(typeof(Org.Neo4j.causalclustering.core.consensus.RaftMessages_AppendEntries_Request))); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void candidateShouldLoseElectionAndRemainCandidate() throws Exception //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void CandidateShouldLoseElectionAndRemainCandidate() { // Note the etcd implementation seems to diverge from the paper here, since the paper suggests that it should // remain as a candidate // given FakeClock fakeClock = Clocks.fakeClock(); TimerService timeouts = new OnDemandTimerService(fakeClock); RaftMachine raft = (new RaftMachineBuilder(_myself, 3, RaftTestMemberSetBuilder.INSTANCE)).outbound(_outbound).timerService(timeouts).clock(fakeClock).build(); raft.InstallCoreState(new RaftCoreState(new MembershipEntry(0, asSet(_myself, _member1, _member2)))); raft.PostRecoveryActions(); timeouts.Invoke(RaftMachine.Timeouts.ELECTION); // when raft.Handle(voteResponse().from(_member1).term(1).deny().build()); raft.Handle(voteResponse().from(_member2).term(1).deny().build()); // then assertEquals(1, raft.Term()); assertEquals(CANDIDATE, raft.CurrentRole()); verify(_outbound, never()).send(eq(_member1), isA(typeof(Org.Neo4j.causalclustering.core.consensus.RaftMessages_AppendEntries_Request))); verify(_outbound, never()).send(eq(_member2), isA(typeof(Org.Neo4j.causalclustering.core.consensus.RaftMessages_AppendEntries_Request))); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldNotCacheInFlightEntriesUntilAfterRecovery() throws Exception //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldNotCacheInFlightEntriesUntilAfterRecovery() { // given FakeClock fakeClock = Clocks.fakeClock(); InFlightCache inFlightCache = new ConsecutiveInFlightCache(10, 10000, InFlightCacheMonitor.VOID, false); OnDemandTimerService timerService = new OnDemandTimerService(fakeClock); RaftMachine raft = (new RaftMachineBuilder(_myself, 3, RaftTestMemberSetBuilder.INSTANCE)).timerService(timerService).clock(fakeClock).raftLog(_raftLog).inFlightCache(inFlightCache).build(); _raftLog.append(new RaftLogEntry(0, new MemberIdSet(asSet(_myself, _member1, _member2)))); // when raft.Handle(appendEntriesRequest().from(_member1).prevLogIndex(0).prevLogTerm(0).leaderTerm(0).logEntry(new RaftLogEntry(0, _data1)).build()); // then assertEquals(_data1, readLogEntry(_raftLog, 1).content()); assertNull(inFlightCache.Get(1L)); // when raft.PostRecoveryActions(); raft.Handle(appendEntriesRequest().from(_member1).prevLogIndex(1).prevLogTerm(0).leaderTerm(0).logEntry(new RaftLogEntry(0, _data2)).build()); // then assertEquals(_data2, readLogEntry(_raftLog, 2).content()); assertEquals(_data2, inFlightCache.Get(2L).content()); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldPersistAtSpecifiedLogIndex() throws Exception //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldPersistAtSpecifiedLogIndex() { // given FakeClock fakeClock = Clocks.fakeClock(); OnDemandTimerService timerService = new OnDemandTimerService(fakeClock); RaftMachine raft = (new RaftMachineBuilder(_myself, 3, RaftTestMemberSetBuilder.INSTANCE)).timerService(timerService).clock(fakeClock).raftLog(_raftLog).build(); _raftLog.append(new RaftLogEntry(0, new MemberIdSet(asSet(_myself, _member1, _member2)))); // when raft.Handle(appendEntriesRequest().from(_member1).prevLogIndex(0).prevLogTerm(0).leaderTerm(0).logEntry(new RaftLogEntry(0, _data1)).build()); // then assertEquals(1, _raftLog.appendIndex()); assertEquals(_data1, readLogEntry(_raftLog, 1).content()); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void candidateShouldVoteForTheSameCandidateInTheSameTerm() throws Exception //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void CandidateShouldVoteForTheSameCandidateInTheSameTerm() { // given FakeClock fakeClock = Clocks.fakeClock(); TimerService timeouts = new OnDemandTimerService(fakeClock); RaftMachine raft = (new RaftMachineBuilder(_myself, 3, RaftTestMemberSetBuilder.INSTANCE)).outbound(_outbound).timerService(timeouts).clock(fakeClock).build(); raft.InstallCoreState(new RaftCoreState(new MembershipEntry(0, asSet(_myself, _member1, _member2)))); // when raft.Handle(voteRequest().from(_member1).candidate(_member1).term(1).build()); raft.Handle(voteRequest().from(_member1).candidate(_member1).term(1).build()); // then verify(_outbound, times(2)).send(_member1, voteResponse().term(1).grant().build()); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldNotBecomeLeaderByVotesFromOldTerm() throws Exception //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldNotBecomeLeaderByVotesFromOldTerm() { // Given FakeClock fakeClock = Clocks.fakeClock(); OnDemandTimerService timerService = new OnDemandTimerService(fakeClock); RaftMachine raft = (new RaftMachineBuilder(_myself, 3, RaftTestMemberSetBuilder.INSTANCE)).timerService(timerService).clock(fakeClock).build(); raft.InstallCoreState(new RaftCoreState(new MembershipEntry(0, asSet(_myself, _member1, _member2)))); raft.PostRecoveryActions(); timerService.Invoke(ELECTION); // When raft.Handle(voteResponse().from(_member1).term(0).grant().build()); raft.Handle(voteResponse().from(_member2).term(0).grant().build()); // Then assertThat(raft.Leader, @is(false)); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldVoteFalseForCandidateInOldTerm() throws Exception //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldVoteFalseForCandidateInOldTerm() { // Given FakeClock fakeClock = Clocks.fakeClock(); OnDemandTimerService timerService = new OnDemandTimerService(fakeClock); OutboundMessageCollector messages = new OutboundMessageCollector(); RaftMachine raft = (new RaftMachineBuilder(_myself, 3, RaftTestMemberSetBuilder.INSTANCE)).timerService(timerService).clock(fakeClock).outbound(messages).build(); raft.InstallCoreState(new RaftCoreState(new MembershipEntry(0, asSet(_myself, _member1, _member2)))); raft.PostRecoveryActions(); // When raft.Handle(voteRequest().from(_member1).term(-1).candidate(_member1).lastLogIndex(0).lastLogTerm(-1).build()); // Then assertThat(messages.SentTo(_member1).Count, equalTo(1)); assertThat(messages.SentTo(_member1), hasItem(voteResponse().from(_myself).term(0).deny().build())); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldAppendNewLeaderBarrierAfterBecomingLeader() throws Exception //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldAppendNewLeaderBarrierAfterBecomingLeader() { // Given FakeClock fakeClock = Clocks.fakeClock(); OnDemandTimerService timerService = new OnDemandTimerService(fakeClock); OutboundMessageCollector messages = new OutboundMessageCollector(); InMemoryRaftLog raftLog = new InMemoryRaftLog(); RaftMachine raft = (new RaftMachineBuilder(_myself, 3, RaftTestMemberSetBuilder.INSTANCE)).timerService(timerService).clock(fakeClock).outbound(messages).raftLog(raftLog).build(); raft.InstallCoreState(new RaftCoreState(new MembershipEntry(0, asSet(_myself, _member1, _member2)))); raft.PostRecoveryActions(); // When timerService.Invoke(ELECTION); raft.Handle(voteResponse().from(_member1).term(1).grant().build()); // Then assertEquals(new NewLeaderBarrier(), readLogEntry(raftLog, raftLog.AppendIndex()).content()); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void leaderShouldSendHeartBeatsOnHeartbeatTimeout() throws Exception //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void LeaderShouldSendHeartBeatsOnHeartbeatTimeout() { // Given FakeClock fakeClock = Clocks.fakeClock(); OnDemandTimerService timerService = new OnDemandTimerService(fakeClock); OutboundMessageCollector messages = new OutboundMessageCollector(); RaftMachine raft = (new RaftMachineBuilder(_myself, 3, RaftTestMemberSetBuilder.INSTANCE)).timerService(timerService).outbound(messages).clock(fakeClock).build(); raft.InstallCoreState(new RaftCoreState(new MembershipEntry(0, asSet(_myself, _member1, _member2)))); raft.PostRecoveryActions(); timerService.Invoke(ELECTION); raft.Handle(voteResponse().from(_member1).term(1).grant().build()); // When timerService.Invoke(RaftMachine.Timeouts.Heartbeat); // Then assertTrue(last(messages.SentTo(_member1)) is RaftMessages_Heartbeat); assertTrue(last(messages.SentTo(_member2)) is RaftMessages_Heartbeat); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldMonitorLeaderNotFound() throws Exception //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldMonitorLeaderNotFound() { // Given FakeClock fakeClock = Clocks.fakeClock(); OnDemandTimerService timerService = new OnDemandTimerService(fakeClock); RaftMachine raft = (new RaftMachineBuilder(_myself, 3, RaftTestMemberSetBuilder.INSTANCE)).timerService(timerService).build(); raft.InstallCoreState(new RaftCoreState(new MembershipEntry(0, asSet(_myself, _member1, _member2)))); try { // When // There is no leader raft.Leader; fail("Should have thrown exception"); } // Then catch (NoLeaderFoundException) { // expected } }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void newMembersShouldBeIncludedInHeartbeatMessages() throws Exception //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void NewMembersShouldBeIncludedInHeartbeatMessages() { // Given DirectNetworking network = new DirectNetworking(); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final org.neo4j.causalclustering.identity.MemberId newMember = member(99); MemberId newMember = member(99); DirectNetworking.Inbound <RaftMessages_RaftMessage> newMemberInbound = new Org.Neo4j.causalclustering.core.consensus.DirectNetworking.Inbound <RaftMessages_RaftMessage>(network, newMember); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final OutboundMessageCollector messages = new OutboundMessageCollector(); OutboundMessageCollector messages = new OutboundMessageCollector(); newMemberInbound.RegisterHandler((Org.Neo4j.causalclustering.messaging.Inbound_MessageHandler <RaftMessages_RaftMessage>)message => messages.send(newMember, message)); FakeClock fakeClock = Clocks.fakeClock(); OnDemandTimerService timerService = new OnDemandTimerService(fakeClock); RaftMachine raft = (new RaftMachineBuilder(_myself, 3, RaftTestMemberSetBuilder.INSTANCE)).timerService(timerService).outbound(messages).clock(fakeClock).build(); raft.InstallCoreState(new RaftCoreState(new MembershipEntry(0, asSet(_myself, _member1, _member2)))); raft.PostRecoveryActions(); // We make ourselves the leader timerService.Invoke(ELECTION); raft.Handle(voteResponse().from(_member1).term(1).grant().build()); // When raft.TargetMembershipSet = asSet(_myself, _member1, _member2, newMember); network.ProcessMessages(); timerService.Invoke(RaftMachine.Timeouts.Heartbeat); network.ProcessMessages(); // Then assertEquals(typeof(RaftMessages_AppendEntries_Request), messages.SentTo(newMember)[0].GetType()); }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldRequestVotesOnElectionTimeout() throws Exception //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldRequestVotesOnElectionTimeout() { // Given FakeClock fakeClock = Clocks.fakeClock(); OnDemandTimerService timerService = new OnDemandTimerService(fakeClock); OutboundMessageCollector messages = new OutboundMessageCollector(); RaftMachine raft = (new RaftMachineBuilder(_myself, 3, RaftTestMemberSetBuilder.INSTANCE)).timerService(timerService).electionTimeout(_electionTimeout).clock(fakeClock).outbound(messages).build(); raft.InstallCoreState(new RaftCoreState(new MembershipEntry(0, asSet(_myself, _member1, _member2)))); raft.PostRecoveryActions(); // When timerService.Invoke(ELECTION); // Then assertThat(messages.SentTo(_myself).Count, equalTo(0)); assertThat(messages.SentTo(_member1).Count, equalTo(1)); assertThat(messages.SentTo(_member1)[0], instanceOf(typeof(RaftMessages_Vote_Request))); assertThat(messages.SentTo(_member2).Count, equalTo(1)); assertThat(messages.SentTo(_member2)[0], instanceOf(typeof(RaftMessages_Vote_Request))); }