From 3e5a22cefa902df9ab78d509d5b8a1052f3d54e8 Mon Sep 17 00:00:00 2001 From: dengziming Date: Wed, 9 Dec 2020 02:27:11 +0800 Subject: [PATCH] KAFKA-10756; Add missing unit test for `UnattachedState` (#9635) This patch adds a unit test for `UnattachedState`, similar to `ResignedStateTest` and `VotedStateTest`. Reviewers: Jason Gustafson --- .../kafka/raft/UnattachedStateTest.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 raft/src/test/java/org/apache/kafka/raft/UnattachedStateTest.java diff --git a/raft/src/test/java/org/apache/kafka/raft/UnattachedStateTest.java b/raft/src/test/java/org/apache/kafka/raft/UnattachedStateTest.java new file mode 100644 index 00000000000..bd00baaf22b --- /dev/null +++ b/raft/src/test/java/org/apache/kafka/raft/UnattachedStateTest.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.kafka.raft; + +import org.apache.kafka.common.utils.MockTime; +import org.apache.kafka.common.utils.Utils; +import org.junit.jupiter.api.Test; + +import java.util.Optional; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class UnattachedStateTest { + + private final MockTime time = new MockTime(); + + @Test + public void testElectionTimeout() { + Set voters = Utils.mkSet(1, 2, 3); + int epoch = 1; + int electionTimeoutMs = 10000; + + UnattachedState state = new UnattachedState( + time, + epoch, + voters, + Optional.empty(), + electionTimeoutMs + ); + + assertEquals(epoch, state.epoch()); + + assertEquals(ElectionState.withUnknownLeader(epoch, voters), state.election()); + assertEquals(electionTimeoutMs, state.remainingElectionTimeMs(time.milliseconds())); + assertFalse(state.hasElectionTimeoutExpired(time.milliseconds())); + + time.sleep(electionTimeoutMs / 2); + assertEquals(electionTimeoutMs / 2, state.remainingElectionTimeMs(time.milliseconds())); + assertFalse(state.hasElectionTimeoutExpired(time.milliseconds())); + + time.sleep(electionTimeoutMs / 2); + assertEquals(0, state.remainingElectionTimeMs(time.milliseconds())); + assertTrue(state.hasElectionTimeoutExpired(time.milliseconds())); + } +}