Skip to content

Commit 09dcf9c

Browse files
committed
fixes leader election test failure: windows thread behavior
Signed-off-by: yue9944882 <[email protected]>
1 parent 3a5f893 commit 09dcf9c

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

extended/src/main/java/io/kubernetes/client/extended/leaderelection/LeaderElector.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,11 @@ public void run(
137137
// Hook on start leading
138138
hookWorkers.submit(startLeadingHook);
139139
renewLoop();
140-
log.info("Failed to renew lease, lose leadership");
141-
// Hook on stop leading
142-
stopLeadingHook.run();
143140
} catch (Throwable t) {
141+
log.error("Leader elector stopped due to an exception", t);
142+
} finally {
143+
// Hook on stop leading
144+
log.info("Failed to renew lease, lose leadership");
144145
stopLeadingHook.run();
145146
}
146147
}

extended/src/test/java/io/kubernetes/client/extended/controller/LeaderElectingControllerTest.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public void testLeaderElectingController() throws ApiException, InterruptedExcep
4646
AtomicReference<LeaderElectionRecord> record = new AtomicReference<>();
4747
record.set(new LeaderElectionRecord());
4848

49-
Semaphore latch = new Semaphore(2);
50-
Semaphore controllerLatch = new Semaphore(2);
49+
Semaphore apiClientSem = new Semaphore(0);
50+
Semaphore controllerSem = new Semaphore(0);
5151

5252
when(mockLock.identity()).thenReturn("foo");
5353
when(mockLock.get())
@@ -58,31 +58,31 @@ public void testLeaderElectingController() throws ApiException, InterruptedExcep
5858
doAnswer(
5959
invocationOnMock -> {
6060
record.set(invocationOnMock.getArgument(0));
61-
latch.release();
61+
apiClientSem.release();
6262
return true;
6363
})
6464
.when(mockLock)
6565
.create(any());
6666

6767
doAnswer(
6868
invocationOnMock -> {
69-
latch.release();
69+
apiClientSem.release();
7070
return false;
7171
})
7272
.when(mockLock)
7373
.update(any());
7474

7575
doAnswer(
7676
invocationOnMock -> {
77-
controllerLatch.release();
77+
controllerSem.release();
7878
return null;
7979
})
8080
.when(mockController)
8181
.run();
8282

8383
doAnswer(
8484
invocationOnMock -> {
85-
controllerLatch.release();
85+
controllerSem.release();
8686
return null;
8787
})
8888
.when(mockController)
@@ -98,18 +98,15 @@ public void testLeaderElectingController() throws ApiException, InterruptedExcep
9898
Duration.ofMillis(100))),
9999
mockController);
100100

101-
latch.acquire(2);
102-
controllerLatch.acquire(2);
103-
104101
Thread controllerThread = new Thread(leaderElectingController::run);
105102
controllerThread.start();
106-
latch.acquire(2);
103+
apiClientSem.acquire(2);
107104
controllerThread.interrupt();
108105

109106
verify(mockLock, times(1)).create(any());
110107
verify(mockLock, atLeastOnce()).update(any());
111108

112-
controllerLatch.acquire(2);
109+
controllerSem.acquire(2);
113110
verify(mockController, times(1)).run();
114111
verify(mockController, times(1)).shutdown();
115112
}

0 commit comments

Comments
 (0)