Skip to content

Deflake another test #2129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void tearDown() throws Exception {}

@Mock private Reconciler mockReconciler;

@Test
@Test(timeout = 90000)
public void testStartingStoppingController() throws InterruptedException {

DefaultController testController = new DefaultController("", mockReconciler, workQueue);
Expand Down Expand Up @@ -102,7 +102,7 @@ public Object answer(InvocationOnMock invocation) {
verify(mockReconciler, times(0)).reconcile(request2);
}

@Test
@Test(timeout = 90000)
public void testControllerWontStartBeforeReady() throws InterruptedException {

Request request1 = new Request("test1");
Expand Down Expand Up @@ -139,7 +139,7 @@ public Object answer(InvocationOnMock invocation) {
verify(mockReconciler, times(1)).reconcile(request1);
}

@Test
@Test(timeout = 90000)
public void testControllerKeepsWorkingWhenReconcilerAbortsWithRuntimeException()
throws InterruptedException {
AtomicBoolean aborts = new AtomicBoolean(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -27,6 +26,7 @@
import io.kubernetes.client.openapi.ApiException;
import java.net.HttpURLConnection;
import java.time.Duration;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -40,22 +40,15 @@ public class LeaderElectingControllerTest {

@Mock private Lock mockLock;

private final int stepCooldownIntervalInMillis = 2000;

private void cooldown() {
try {
Thread.sleep(stepCooldownIntervalInMillis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

@Test
public void testLeaderElectingController() throws ApiException {
@Test(timeout = 90000)
public void testLeaderElectingController() throws ApiException, InterruptedException {

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

Semaphore latch = new Semaphore(2);
Semaphore controllerLatch = new Semaphore(2);

when(mockLock.identity()).thenReturn("foo");
when(mockLock.get())
.thenThrow(
Expand All @@ -65,12 +58,35 @@ public void testLeaderElectingController() throws ApiException {
doAnswer(
invocationOnMock -> {
record.set(invocationOnMock.getArgument(0));
latch.release();
return true;
})
.when(mockLock)
.create(any());

doReturn(false).when(mockLock).update(any());
doAnswer(
invocationOnMock -> {
latch.release();
return false;
})
.when(mockLock)
.update(any());

doAnswer(
invocationOnMock -> {
controllerLatch.release();
return null;
})
.when(mockController)
.run();

doAnswer(
invocationOnMock -> {
controllerLatch.release();
return null;
})
.when(mockController)
.shutdown();

LeaderElectingController leaderElectingController =
new LeaderElectingController(
Expand All @@ -82,14 +98,18 @@ public void testLeaderElectingController() throws ApiException {
Duration.ofMillis(100))),
mockController);

latch.acquire(2);
controllerLatch.acquire(2);

Thread controllerThread = new Thread(leaderElectingController::run);
controllerThread.start();
cooldown();
latch.acquire(2);
controllerThread.interrupt();

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

controllerLatch.acquire(2);
verify(mockController, times(1)).run();
verify(mockController, times(1)).shutdown();
}
Expand Down