-
Notifications
You must be signed in to change notification settings - Fork 619
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(atomix): do not append invalid entries
(cherry picked from commit 34c79d5)
- Loading branch information
1 parent
3a2d486
commit e02a798
Showing
3 changed files
with
101 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
atomix/cluster/src/test/java/io/atomix/raft/SingleRaftEntryValidationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright © 2020 camunda services GmbH (info@camunda.com) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.atomix.raft; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import io.atomix.raft.zeebe.EntryValidator; | ||
import io.atomix.raft.zeebe.ValidationResult; | ||
import io.atomix.raft.zeebe.ZeebeEntry; | ||
import java.util.List; | ||
import java.util.function.BiFunction; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
public class SingleRaftEntryValidationTest { | ||
|
||
private final TestEntryValidator entryValidator = new TestEntryValidator(); | ||
|
||
@Rule | ||
public RaftRule raftRule = RaftRule.withBootstrappedNodes(1).setEntryValidator(entryValidator); | ||
|
||
@Test | ||
public void shouldFailAppendOnInvalidEntry() { | ||
// given | ||
entryValidator.validation = (last, current) -> ValidationResult.failure("invalid"); | ||
|
||
// when - then expect | ||
assertThatThrownBy(() -> raftRule.appendEntry()).hasMessageContaining("invalid"); | ||
} | ||
|
||
@Test | ||
public void shouldNotAppendInvalidEntryToLog() throws Exception { | ||
// given | ||
entryValidator.validation = (last, current) -> ValidationResult.failure("invalid"); | ||
|
||
// when | ||
assertThatThrownBy(() -> raftRule.appendEntry()).hasMessageContaining("invalid"); | ||
entryValidator.validation = (last, current) -> ValidationResult.success(); | ||
raftRule.awaitNewLeader(); | ||
final var commitIndex = | ||
raftRule.appendEntry(); // append another entry to await the commit index | ||
|
||
// then | ||
raftRule.awaitCommit(commitIndex); | ||
raftRule.awaitSameLogSizeOnAllNodes(commitIndex); | ||
final var memberLog = raftRule.getMemberLogs(); | ||
|
||
final var logLength = memberLog.values().stream().map(List::size).findFirst().orElseThrow(); | ||
assertThat(logLength).withFailMessage(memberLog.toString()).isEqualTo(3); | ||
} | ||
|
||
private static class TestEntryValidator implements EntryValidator { | ||
BiFunction<ZeebeEntry, ZeebeEntry, ValidationResult> validation; | ||
|
||
@Override | ||
public ValidationResult validateEntry(final ZeebeEntry lastEntry, final ZeebeEntry entry) { | ||
return validation.apply(lastEntry, entry); | ||
} | ||
} | ||
} |