Skip to content

Commit f392fd6

Browse files
authored
Merge pull request #138 from lerna-stack/test-state-after-append-entries-handling
Test state after AppendEntries handling
2 parents 14a6cc2 + f3c1b03 commit f392fd6

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

src/test/scala/lerna/akka/entityreplication/raft/RaftActorCandidateSpec.scala

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,10 @@ class RaftActorCandidateSpec extends TestKit(ActorSystem()) with RaftActorSpecBa
309309
candidate ! createAppendEntries(shardId, term2, anotherMemberIndex)
310310
expectMsg(AppendEntriesSucceeded(term2, LogEntryIndex(0), candidateMemberIndex))
311311

312-
getState(candidate).stateName should be(Follower)
312+
val state = getState(candidate)
313+
state.stateName should be(Follower)
314+
state.stateData.currentTerm should be(term2)
315+
state.stateData.leaderMember should contain(anotherMemberIndex)
313316
}
314317

315318
"AppendEntries が同じ Term を持っているときは(先に別のリーダーが当選したということなので)Follower に降格" in {
@@ -324,7 +327,9 @@ class RaftActorCandidateSpec extends TestKit(ActorSystem()) with RaftActorSpecBa
324327
candidate ! createAppendEntries(shardId, term, anotherMemberIndex)
325328
expectMsg(AppendEntriesSucceeded(term, LogEntryIndex(0), candidateMemberIndex))
326329

327-
getState(candidate).stateName should be(Follower)
330+
val state = getState(candidate)
331+
state.stateName should be(Follower)
332+
state.stateData.leaderMember should contain(anotherMemberIndex)
328333
}
329334

330335
"コマンドは保留し、フォロワーに降格した場合はリーダーに転送する" in {
@@ -440,6 +445,10 @@ class RaftActorCandidateSpec extends TestKit(ActorSystem()) with RaftActorSpecBa
440445
setState(candidate, Candidate, candidateData)
441446
candidate ! createAppendEntries(shardId, term1, leaderMemberIndex, index3, term1)
442447
expectMsg(AppendEntriesFailed(term1, candidateMemberIndex))
448+
449+
val state = getState(candidate)
450+
state.stateName should be(Follower)
451+
state.stateData.leaderMember should contain(leaderMemberIndex)
443452
}
444453

445454
"prevLogIndex の Term が prevLogTerm に一致するログエントリでない場合は AppendEntriesFailed を返す" in {
@@ -497,6 +506,7 @@ class RaftActorCandidateSpec extends TestKit(ActorSystem()) with RaftActorSpecBa
497506
val state = getState(candidate)
498507
state.stateName should be(Follower)
499508
state.stateData.currentTerm should be(leaderTerm)
509+
state.stateData.leaderMember should contain(leaderMemberIndex)
500510
}
501511

502512
"leaderCommit > commitIndex となる場合、 commitIndex に min(leaderCommit, 新規エントリの最後のインデックス) を設定" in {

src/test/scala/lerna/akka/entityreplication/raft/RaftActorFollowerSpec.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ class RaftActorFollowerSpec extends TestKit(ActorSystem()) with RaftActorSpecBas
207207
val leaderMemberIndex = candidateMemberIndex
208208
follower ! createAppendEntries(shardId, term, leaderMemberIndex)
209209
expectMsg(AppendEntriesSucceeded(term, LogEntryIndex(0), followerMemberIndex))
210+
211+
val state = getState(follower)
212+
state.stateName should be(Follower)
213+
state.stateData.leaderMember should contain(leaderMemberIndex)
210214
}
211215

212216
"自分が持っている Term より新しい場合は AppendEntries を成功させる" in {
@@ -227,6 +231,11 @@ class RaftActorFollowerSpec extends TestKit(ActorSystem()) with RaftActorSpecBas
227231
val leaderMemberIndex = candidateMemberIndex
228232
follower ! createAppendEntries(shardId, term2, leaderMemberIndex)
229233
expectMsg(AppendEntriesSucceeded(term2, LogEntryIndex(0), followerMemberIndex))
234+
235+
val state = getState(follower)
236+
state.stateName should be(Follower)
237+
state.stateData.currentTerm should be(term2)
238+
state.stateData.leaderMember should contain(leaderMemberIndex)
230239
}
231240

232241
"自分が持っている Term より古い場合は AppendEntries を失敗させる" in {
@@ -494,6 +503,10 @@ class RaftActorFollowerSpec extends TestKit(ActorSystem()) with RaftActorSpecBas
494503
logEntries,
495504
)
496505
expectMsg(AppendEntriesFailed(term1, followerMemberIndex))
506+
507+
val state = getState(follower)
508+
state.stateName should be(Follower)
509+
state.stateData.leaderMember should contain(leaderMemberIndex)
497510
}
498511

499512
"agree to a term if it receives AppendEntries which includes log entries that cannot be merged and newer Term" in {
@@ -598,6 +611,10 @@ class RaftActorFollowerSpec extends TestKit(ActorSystem()) with RaftActorSpecBas
598611
followerLogEntries.last.term.next(),
599612
)
600613
expectMsg(AppendEntriesFailed(term1, followerMemberIndex))
614+
615+
val state = getState(follower)
616+
state.stateName should be(Follower)
617+
state.stateData.leaderMember should contain(leaderMemberIndex)
601618
}
602619

603620
}

src/test/scala/lerna/akka/entityreplication/raft/RaftActorLeaderSpec.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ class RaftActorLeaderSpec extends TestKit(ActorSystem()) with RaftActorSpecBase
6161
leader ! createAppendEntries(shardId, term2, anotherMemberIndex)
6262
expectMsg(AppendEntriesSucceeded(term2, LogEntryIndex(0), leaderMemberIndex))
6363

64-
getState(leader).stateName should be(Follower)
64+
val state = getState(leader)
65+
state.stateName should be(Follower)
66+
state.stateData.currentTerm should be(term2)
67+
state.stateData.leaderMember should contain(anotherMemberIndex)
6568
}
6669

6770
"コマンドを ReplicationActor に転送する" ignore {}
@@ -107,6 +110,11 @@ class RaftActorLeaderSpec extends TestKit(ActorSystem()) with RaftActorSpecBase
107110

108111
leader ! createAppendEntries(shardId, term2, newLeaderMemberIndex, index3, term1, logEntries)
109112
expectMsg(AppendEntriesFailed(term2, leaderMemberIndex))
113+
114+
val state = getState(leader)
115+
state.stateName should be(Follower)
116+
state.stateData.currentTerm should be(term2)
117+
state.stateData.leaderMember should contain(newLeaderMemberIndex)
110118
}
111119

112120
"prevLogIndex の Term が prevLogTerm に一致するログエントリでない場合は AppendEntriesFailed を返す" in {

0 commit comments

Comments
 (0)