Skip to content

Commit 9de0290

Browse files
Deflake another test.
1 parent 02a2e79 commit 9de0290

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

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

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import static org.mockito.ArgumentMatchers.any;
1616
import static org.mockito.Mockito.atLeastOnce;
1717
import static org.mockito.Mockito.doAnswer;
18-
import static org.mockito.Mockito.doReturn;
1918
import static org.mockito.Mockito.times;
2019
import static org.mockito.Mockito.verify;
2120
import static org.mockito.Mockito.when;
@@ -27,6 +26,7 @@
2726
import io.kubernetes.client.openapi.ApiException;
2827
import java.net.HttpURLConnection;
2928
import java.time.Duration;
29+
import java.util.concurrent.Semaphore;
3030
import java.util.concurrent.atomic.AtomicReference;
3131
import org.junit.Test;
3232
import org.junit.runner.RunWith;
@@ -40,22 +40,15 @@ public class LeaderElectingControllerTest {
4040

4141
@Mock private Lock mockLock;
4242

43-
private final int stepCooldownIntervalInMillis = 2000;
44-
45-
private void cooldown() {
46-
try {
47-
Thread.sleep(stepCooldownIntervalInMillis);
48-
} catch (InterruptedException e) {
49-
e.printStackTrace();
50-
}
51-
}
52-
5343
@Test
54-
public void testLeaderElectingController() throws ApiException {
44+
public void testLeaderElectingController() throws ApiException, InterruptedException {
5545

5646
AtomicReference<LeaderElectionRecord> record = new AtomicReference<>();
5747
record.set(new LeaderElectionRecord());
5848

49+
Semaphore latch = new Semaphore(2);
50+
Semaphore controllerLatch = new Semaphore(2);
51+
5952
when(mockLock.identity()).thenReturn("foo");
6053
when(mockLock.get())
6154
.thenThrow(
@@ -65,12 +58,35 @@ public void testLeaderElectingController() throws ApiException {
6558
doAnswer(
6659
invocationOnMock -> {
6760
record.set(invocationOnMock.getArgument(0));
61+
latch.release();
6862
return true;
6963
})
7064
.when(mockLock)
7165
.create(any());
7266

73-
doReturn(false).when(mockLock).update(any());
67+
doAnswer(
68+
invocationOnMock -> {
69+
latch.release();
70+
return false;
71+
})
72+
.when(mockLock)
73+
.update(any());
74+
75+
doAnswer(
76+
invocationOnMock -> {
77+
controllerLatch.release();
78+
return null;
79+
})
80+
.when(mockController)
81+
.run();
82+
83+
doAnswer(
84+
invocationOnMock -> {
85+
controllerLatch.release();
86+
return null;
87+
})
88+
.when(mockController)
89+
.shutdown();
7490

7591
LeaderElectingController leaderElectingController =
7692
new LeaderElectingController(
@@ -82,14 +98,18 @@ public void testLeaderElectingController() throws ApiException {
8298
Duration.ofMillis(100))),
8399
mockController);
84100

101+
latch.acquire(2);
102+
controllerLatch.acquire(2);
103+
85104
Thread controllerThread = new Thread(leaderElectingController::run);
86105
controllerThread.start();
87-
cooldown();
106+
latch.acquire(2);
88107
controllerThread.interrupt();
89108

90109
verify(mockLock, times(1)).create(any());
91110
verify(mockLock, atLeastOnce()).update(any());
92111

112+
controllerLatch.acquire(2);
93113
verify(mockController, times(1)).run();
94114
verify(mockController, times(1)).shutdown();
95115
}

0 commit comments

Comments
 (0)