Skip to content

Commit

Permalink
make the methods of TestProxyManager static and synchronized.
Browse files Browse the repository at this point in the history
  • Loading branch information
billwert committed Sep 27, 2023
1 parent 89ad48a commit 18fa938
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@

import com.azure.core.test.utils.TestProxyManager;
import com.azure.core.util.logging.ClientLogger;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.TestInfo;

import java.nio.file.Path;
import java.nio.file.Paths;

import static com.azure.core.test.utils.TestUtils.toURI;

/**
* Base class for running live and playback tests using test-proxy
*/
Expand All @@ -30,8 +24,6 @@ public TestProxyTestBase() {
super();
}

private static TestProxyManager testProxyManager;

/**
* Before tests are executed, determines the test mode by reading the {@code AZURE_TEST_MODE} environment variable.
* If it is not set, {@link TestMode#PLAYBACK}
Expand All @@ -40,20 +32,8 @@ public TestProxyTestBase() {
@BeforeAll
public static void setupTestProxy(TestInfo testInfo) {
testMode = initializeTestMode();
Path testClassPath = Paths.get(toURI(testInfo.getTestClass().get().getResource(testInfo.getTestClass().get().getSimpleName() + ".class")));
if (isTestProxyEnabled() && (testMode == TestMode.PLAYBACK || testMode == TestMode.RECORD)) {
testProxyManager = new TestProxyManager(testClassPath);
testProxyManager.startProxy();
}
}

/**
* Performs cleanup actions after all tests are executed.
*/
@AfterAll
public static void teardownTestProxy() {
if (testProxyManager != null) {
testProxyManager.stopProxy();
TestProxyManager.startProxy();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,42 @@
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.Semaphore;

import static com.azure.core.test.utils.TestUtils.toURI;

/**
* Manages running the test recording proxy server
*/
public class TestProxyManager {
public final class TestProxyManager {
private static final ClientLogger LOGGER = new ClientLogger(TestProxyManager.class);
private Process proxy;
private final Path testClassPath;
private static final Semaphore semaphore = new Semaphore(1);
private static Process proxy;
private static final Path TEST_CLASS_PATH = Paths.get(toURI(TestProxyManager.class.getResource(TestProxyManager.class.getSimpleName() + ".class")));

/**
* Construct a {@link TestProxyManager} for controlling the external test proxy.
* @param testClassPath the test class path
*/
public TestProxyManager(Path testClassPath) {
this.testClassPath = testClassPath;
// This is necessary to stop the proxy when the debugger is stopped.
Runtime.getRuntime().addShutdownHook(new Thread(this::stopProxy));
static {
Runtime.getRuntime().addShutdownHook(new Thread(TestProxyManager::stopProxy));
if (runningLocally()) {
TestProxyDownloader.installTestProxy(testClassPath);
TestProxyDownloader.installTestProxy(TEST_CLASS_PATH);
}
}

@Deprecated
private TestProxyManager() { }

/**
* Start an instance of the test proxy.
* @throws UncheckedIOException There was an issue communicating with the proxy.
* @throws RuntimeException There was an issue starting the proxy process.
*/
public void startProxy() {
public static synchronized void startProxy() {
try {
semaphore.acquire();
// if we're not running in CI we will check to see if someone has started the proxy, and start one if not.
if (runningLocally() && !checkAlive(1, Duration.ofSeconds(1))) {
String commandLine = Paths.get(TestProxyDownloader.getProxyDirectory().toString(),
TestProxyUtils.getProxyProcessName()).toString();

ProcessBuilder builder = new ProcessBuilder(commandLine,
"--storage-location",
TestUtils.getRepoRootResolveUntil(testClassPath, "eng").toString());
TestUtils.getRepoRootResolveUntil(TEST_CLASS_PATH, "eng").toString());
Map<String, String> environment = builder.environment();
environment.put("LOGGING__LOGLEVEL", "Information");
environment.put("LOGGING__LOGLEVEL__MICROSOFT", "Warning");
Expand All @@ -72,12 +68,10 @@ public void startProxy() {
throw LOGGER.logExceptionAsError(new UncheckedIOException(e));
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
semaphore.release();
}
}

private boolean checkAlive(int loops, Duration waitTime) throws InterruptedException {
private static boolean checkAlive(int loops, Duration waitTime) throws InterruptedException {
HttpURLConnectionHttpClient client = new HttpURLConnectionHttpClient();
HttpRequest request = new HttpRequest(HttpMethod.GET,
String.format("%s/admin/isalive", TestProxyUtils.getProxyUrl()));
Expand All @@ -99,7 +93,7 @@ private boolean checkAlive(int loops, Duration waitTime) throws InterruptedExcep
/**
* Stop the running instance of the test proxy.
*/
public void stopProxy() {
private static void stopProxy() {
if (proxy != null && proxy.isAlive()) {
proxy.destroy();
}
Expand All @@ -109,7 +103,7 @@ public void stopProxy() {
* Checks the environment variables commonly set in CI to determine if the run is local.
* @return True if the run is local.
*/
private boolean runningLocally() {
private static boolean runningLocally() {
return Configuration.getGlobalConfiguration().get("TF_BUILD") == null
&& Configuration.getGlobalConfiguration().get("CI") == null;
}
Expand Down

0 comments on commit 18fa938

Please sign in to comment.