Skip to content

Add macos to the matrix build. #2017

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 3 commits into from
Mar 10, 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
2 changes: 1 addition & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
matrix:
# Test against the LTS Java versions. TODO: add JDK18 when it becomes available.
java: [ 8.0.x, 11.0.x, 17.0.x ]
os: [ windows-latest, ubuntu-latest ]
os: [ macos-latest, windows-latest, ubuntu-latest ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2.4.0
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -201,24 +201,29 @@ public void testLeaderElectionWithRenewDeadline() throws InterruptedException {
List<String> electionHistory = new ArrayList<>();
List<String> leadershipHistory = new ArrayList<>();

CountDownLatch testLockAccessLatch = new CountDownLatch(9);
MockResourceLock mockLock = new MockResourceLock("mock");
mockLock.renewCountMax = 3;
mockLock.onCreate =
record -> {
electionHistory.add("create record");
leadershipHistory.add("get leadership");
testLockAccessLatch.countDown();
};
mockLock.onUpdate =
record -> {
electionHistory.add("update record");
testLockAccessLatch.countDown();
};
mockLock.onChange =
record -> {
electionHistory.add("change record");
testLockAccessLatch.countDown();
};
mockLock.onTryUpdate =
record -> {
electionHistory.add("try update record");
testLockAccessLatch.countDown();
};

LeaderElectionConfig leaderElectionConfig = new LeaderElectionConfig();
Expand All @@ -244,18 +249,16 @@ record -> {
});

testLeaderElectionLatch.await(10, SECONDS);
testLockAccessLatch.await(10, SECONDS);

assertHistory(
assertWildcardHistory(
electionHistory,
"create record",
"try update record",
"try update record+",
"update record",
"try update record",
"try update record+",
"update record",
"try update record",
"try update record",
"try update record",
"try update record");
"try update record+");
assertHistory(leadershipHistory, "get leadership", "start leading", "stop leading");
}

Expand All @@ -274,6 +277,36 @@ private void assertHistory(List<String> history, String... expected) {
}
}

// assertWildcardHistory allows for an arbitrary number of repeated entries for an
// comparison with a '+' suffix. This allows for a semantic rather than literal
Copy link
Member

@yue9944882 yue9944882 Mar 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: in the sense of regex, i think * is more accurate than + here according to the description

// comparison to avoid issues of timing.
private void assertWildcardHistory(List<String> history, String... expected) {
Assert.assertNotNull(expected);
Assert.assertNotNull(history);

// TODO: This code is too complicated and a little bit buggy, but it works
// for the current limited use case. Clean this up!
int expectedIx = 0;
for (int index = 0; index < history.size(); ++index) {
String compare = expected[expectedIx];
if (compare.endsWith("+")) {
compare = compare.substring(0, compare.length() - 1);
if (!history.get(index).equals(compare)) {
expectedIx++;
compare = expected[expectedIx];
expectedIx++;
}
} else {
expectedIx++;
}
Assert.assertEquals(
String.format(
"Not equal at index %d, expected %s, got %s", index, compare, history.get(index)),
compare,
history.get(index));
}
}

@Test
public void testLeaderElectionCaptureException() throws ApiException, InterruptedException {
RuntimeException expectedException = new RuntimeException("noxu");
Expand Down