Skip to content

Commit 621339d

Browse files
committed
fix tests
Signed-off-by: wind57 <[email protected]>
1 parent 587c9ae commit 621339d

File tree

3 files changed

+78
-10
lines changed

3 files changed

+78
-10
lines changed

spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/election/Fabric8LeaderElectionInitiator.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ final class Fabric8LeaderElectionInitiator {
6161

6262
private volatile CompletableFuture<Void> podReadyFuture;
6363

64-
private volatile boolean destroyCalled = false;
65-
6664
private volatile CompletableFuture<?> leaderFuture;
6765

6866
Fabric8LeaderElectionInitiator(String candidateIdentity, String candidateNamespace,
@@ -119,7 +117,6 @@ void postConstruct() {
119117

120118
@PreDestroy
121119
void preDestroy() {
122-
destroyCalled = true;
123120
LOG.info(() -> "preDestroy called on the leader initiator : " + candidateIdentity);
124121

125122
if (!podReadyWaitingExecutor.isShutdown()) {
@@ -136,7 +133,6 @@ void preDestroy() {
136133
}
137134

138135
if (leaderFuture != null) {
139-
LOG.info(() -> "leaderFuture will be canceled for : " + candidateIdentity);
140136
// needed to release the lock, in case we are holding it.
141137
// fabric8 internally expects this one to be called
142138
LOG.debug(() -> "leaderFuture will be canceled for : " + candidateIdentity);
@@ -156,13 +152,12 @@ private void startLeaderElection() {
156152
leaderFuture.whenComplete((ok, error) -> {
157153

158154
if (error != null) {
155+
// only we have a reference to leaderFuture; and if it is canceled, it is
156+
// only possible
157+
// from our own preDestroy
159158
if (error instanceof CancellationException) {
160-
if (!destroyCalled) {
161-
LOG.warn(() -> "renewal failed for : " + candidateIdentity + ", will re-start it after : "
162-
+ leaderElectionProperties.waitAfterRenewalFailure().toSeconds() + " seconds");
163-
sleep(leaderElectionProperties);
164-
podReadyWaitingExecutor.execute(this::startLeaderElection);
165-
}
159+
LOG.info(() -> "cancel was called on the leader initiator : " + candidateIdentity);
160+
LOG.info(() -> "terminating leadership for : " + candidateIdentity);
166161
}
167162
else {
168163
LOG.warn(() -> "leader failed with : " + error.getMessage());

spring-cloud-kubernetes-integration-tests/spring-cloud-kubernetes-fabric8-leader-election/src/test/java/org/springframework/cloud/kubernetes/fabric8/leader/election/AbstractLeaderElection.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.cloud.kubernetes.integration.tests.commons.Commons;
3939
import org.springframework.context.annotation.Bean;
4040
import org.springframework.context.annotation.Primary;
41+
import org.springframework.test.annotation.DirtiesContext;
4142

4243
/**
4344
* @author wind57
@@ -50,6 +51,7 @@
5051
"logging.level.org.springframework.cloud.kubernetes.commons.leader.election=debug",
5152
"logging.level.org.springframework.cloud.kubernetes.fabric8.leader.election=debug" },
5253
classes = { App.class, AbstractLeaderElection.LocalConfiguration.class })
54+
@DirtiesContext
5355
abstract class AbstractLeaderElection {
5456

5557
private static K3sContainer container;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2013-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.fabric8.leader.election;
18+
19+
import org.junit.jupiter.api.AfterEach;
20+
import org.junit.jupiter.api.BeforeAll;
21+
import org.junit.jupiter.api.Test;
22+
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.boot.test.system.CapturedOutput;
25+
import org.springframework.test.context.TestPropertySource;
26+
27+
import static org.springframework.cloud.kubernetes.fabric8.leader.election.Assertions.assertAcquireAndRenew;
28+
import static org.springframework.cloud.kubernetes.integration.tests.commons.Awaitilities.awaitUntil;
29+
30+
/**
31+
* <pre>
32+
* - we acquire the leadership
33+
* - leadership feature fails
34+
* </pre>
35+
*
36+
* @author wind57
37+
*/
38+
@TestPropertySource(properties = { "spring.cloud.kubernetes.leader.election.wait-for-pod-ready=true",
39+
"spring.cloud.kubernetes.leader.election.restart-on-failure=true", "readiness.passes=true" })
40+
class Fabric8LeaderElectionCanceledAndNotRestartedIT extends AbstractLeaderElection {
41+
42+
private static final String NAME = "acquired-then-canceled";
43+
44+
@Autowired
45+
private Fabric8LeaderElectionInitiator initiator;
46+
47+
@BeforeAll
48+
static void beforeAll() {
49+
AbstractLeaderElection.beforeAll(NAME);
50+
}
51+
52+
@AfterEach
53+
void afterEach() {
54+
stopFutureAndDeleteLease(initiator);
55+
}
56+
57+
@Test
58+
void test(CapturedOutput output) {
59+
60+
assertAcquireAndRenew(output, this::getLease, NAME);
61+
62+
initiator.leaderFeature().cancel(true);
63+
64+
awaitUntil(10, 100, () -> output.getOut().contains("cancel was called on the leader initiator : " + NAME));
65+
66+
// lease is going to reset
67+
awaitUntil(10, 100, () -> getLease().getSpec().getHolderIdentity().isEmpty());
68+
69+
}
70+
71+
}

0 commit comments

Comments
 (0)