Skip to content

Commit

Permalink
add a system level lock for DockerWebDriverTestCase
Browse files Browse the repository at this point in the history
  • Loading branch information
jumperchen committed Aug 24, 2023
1 parent 0b8e54e commit 3b31710
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=1.0.11.2
version=1.0.12

# dependencies
junitVersion=5.10.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;

import com.palantir.docker.compose.DockerComposeExtension;
import com.palantir.docker.compose.connection.waiting.HealthChecks;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.parallel.ResourceLock;

Expand All @@ -41,26 +46,61 @@ protected String getRemoteWebDriverUrl() {
return "http://localhost:" + externalPort + "/wd/hub";
}

protected FileLock globalLock;

private static final String tempDir = new SimpleDateFormat("yyyyMMdd").format(new java.util.Date());

// create a temp file for docker compose.yml
private static String exportResource(String file) {
private String exportResource(String file) {
InputStream resourceAsStream = ClassLoader.getSystemResourceAsStream(
"docker/docker-compose.yml");
try {
String dest = Files.createTempDirectory("zkWebdriver")
.resolve(file).toFile().getAbsolutePath();
Path path = Paths.get(dest);
Path path = Paths.get(System.getProperty("java.io.tmpdir"), tempDir, "zkWebdriver").resolve(file);
if (!Files.isDirectory(path.getParent())) {
Files.createDirectories(path.getParent());
}
Files.copy(resourceAsStream, path, StandardCopyOption.REPLACE_EXISTING);
return dest;
if (!path.toFile().exists()) {
Files.copy(resourceAsStream, path, StandardCopyOption.REPLACE_EXISTING);
}

// try to acquire a global lock for each DockerWebDriver
RandomAccessFile files = new RandomAccessFile(path.toFile(), "rw");
FileChannel channel = files.getChannel();
while (true) {
try {
globalLock = channel.tryLock();
if (globalLock.isValid()) {
break;
}
} catch (Throwable e) {
// File is already locked in this thread or virtual machine
}
try {
Thread.sleep(200);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
return path.toAbsolutePath().toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@AfterAll
public void unlockGlobalLock() {
if (globalLock != null) {
try {
globalLock.release();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

// remove the docker env. if the bug has fixed - https://tracker.zkoss.org/browse/ZK-5092
@RegisterExtension
public final static DockerComposeExtension docker = DockerComposeExtension.builder()
public final DockerComposeExtension docker = DockerComposeExtension.builder()
.file(exportResource("docker/docker-compose.yml"))
.useDockerComposeV2(Boolean.parseBoolean(System.getProperty("useDockerComposeV2", "true")))
.waitingForService("hub", HealthChecks.toRespondOverHttp(4444,
Expand Down

0 comments on commit 3b31710

Please sign in to comment.