Skip to content
Snippets Groups Projects
Unverified Commit a82e1091 authored by Sergii Kabashniuk's avatar Sergii Kabashniuk Committed by GitHub
Browse files

Do not use spy in ServerCheckerTest (#14975)


Signed-off-by: default avatarSergii Kabashniuk <skabashniuk@redhat.com>
parent 5d38d7a7
No related branches found
No related tags found
No related merge requests found
...@@ -12,11 +12,7 @@ ...@@ -12,11 +12,7 @@
package org.eclipse.che.api.workspace.server.hc; package org.eclipse.che.api.workspace.server.hc;
import static java.lang.String.format; import static java.lang.String.format;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
...@@ -24,6 +20,7 @@ import static org.testng.Assert.fail; ...@@ -24,6 +20,7 @@ import static org.testng.Assert.fail;
import java.util.Timer; import java.util.Timer;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.eclipse.che.api.workspace.server.spi.InfrastructureException; import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
...@@ -56,49 +53,47 @@ public class ServerCheckerTest { ...@@ -56,49 +53,47 @@ public class ServerCheckerTest {
@Test(timeOut = TEST_TIMEOUT_MS) @Test(timeOut = TEST_TIMEOUT_MS)
public void successfulCheckTest() throws Exception { public void successfulCheckTest() throws Exception {
checker = checker =
spy( new TestServerChecker(
new TestServerChecker( MACHINE_NAME,
MACHINE_NAME, SERVER_REF,
SERVER_REF, PERIOD_MS,
PERIOD_MS, CHECKER_TIMEOUT_MS,
CHECKER_TIMEOUT_MS, SUCCESS_THRESHOLD,
SUCCESS_THRESHOLD, TimeUnit.MILLISECONDS,
TimeUnit.MILLISECONDS, timer);
timer));
CompletableFuture<String> reportCompFuture = checker.getReportCompFuture(); CompletableFuture<String> reportCompFuture = checker.getReportCompFuture();
// not considered as available before start // not considered as available before start
assertFalse(reportCompFuture.isDone()); assertFalse(reportCompFuture.isDone());
// ensure server not available before start // ensure server not available before start
when(checker.isAvailable()).thenReturn(false); CountDownLatch isAvailableCountDownLatch = checker.setAvailable(false);
checker.start(); checker.start();
verify(checker, timeout((int) (PERIOD_MS * 2)).atLeastOnce()).isAvailable(); isAvailableCountDownLatch.await(PERIOD_MS * 2, TimeUnit.MILLISECONDS);
// not considered as available after check // not considered as available after check
assertFalse(reportCompFuture.isDone()); assertFalse(reportCompFuture.isDone());
// make server available // make server available
when(checker.isAvailable()).thenReturn(true); isAvailableCountDownLatch = checker.setAvailable(true);
assertEquals(reportCompFuture.get(), SERVER_REF); assertEquals(reportCompFuture.get(), SERVER_REF);
verify(checker, atLeast(2)).isAvailable(); isAvailableCountDownLatch.await(PERIOD_MS * 2, TimeUnit.MILLISECONDS);
} }
@Test(timeOut = TEST_TIMEOUT_MS) @Test(timeOut = TEST_TIMEOUT_MS)
public void checkTimeoutTest() throws Exception { public void checkTimeoutTest() throws Exception {
checker = checker =
spy( new TestServerChecker(
new TestServerChecker( MACHINE_NAME,
MACHINE_NAME, SERVER_REF,
SERVER_REF, PERIOD_MS,
PERIOD_MS, PERIOD_MS * 2,
PERIOD_MS * 2, SUCCESS_THRESHOLD,
SUCCESS_THRESHOLD, TimeUnit.MILLISECONDS,
TimeUnit.MILLISECONDS, timer);
timer));
// ensure server not available before start // ensure server not available before start
when(checker.isAvailable()).thenReturn(false); checker.setAvailable(false);
checker.start(); checker.start();
CompletableFuture<String> reportCompFuture = checker.getReportCompFuture(); CompletableFuture<String> reportCompFuture = checker.getReportCompFuture();
...@@ -119,6 +114,11 @@ public class ServerCheckerTest { ...@@ -119,6 +114,11 @@ public class ServerCheckerTest {
} }
private static class TestServerChecker extends ServerChecker { private static class TestServerChecker extends ServerChecker {
private boolean isAvailable;
private CountDownLatch isAvailableCountDownLatch = new CountDownLatch(1);
protected TestServerChecker( protected TestServerChecker(
String machineName, String machineName,
String serverRef, String serverRef,
...@@ -132,7 +132,14 @@ public class ServerCheckerTest { ...@@ -132,7 +132,14 @@ public class ServerCheckerTest {
@Override @Override
public boolean isAvailable() { public boolean isAvailable() {
return false; isAvailableCountDownLatch.countDown();
return isAvailable;
}
public CountDownLatch setAvailable(boolean isAvailable) {
this.isAvailable = isAvailable;
this.isAvailableCountDownLatch = new CountDownLatch(1);
return isAvailableCountDownLatch;
} }
} }
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment