|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license |
| 3 | + * agreements. See the NOTICE file distributed with this work for additional information regarding |
| 4 | + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the |
| 5 | + * "License"); you may not use this file except in compliance with the License. You may obtain a |
| 6 | + * copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 11 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 12 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 13 | + * the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package org.apache.geode.gradle; |
| 17 | + |
| 18 | +import static java.nio.file.StandardCopyOption.COPY_ATTRIBUTES; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.io.UncheckedIOException; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.util.concurrent.atomic.AtomicLong; |
| 25 | + |
| 26 | +import org.gradle.api.Action; |
| 27 | +import org.gradle.api.internal.tasks.testing.TestFramework; |
| 28 | +import org.gradle.api.internal.tasks.testing.WorkerTestClassProcessorFactory; |
| 29 | +import org.gradle.api.internal.tasks.testing.detection.TestFrameworkDetector; |
| 30 | +import org.gradle.api.tasks.testing.TestFrameworkOptions; |
| 31 | +import org.gradle.process.internal.JavaExecHandleBuilder; |
| 32 | +import org.gradle.process.internal.worker.WorkerProcessBuilder; |
| 33 | + |
| 34 | +/** |
| 35 | + * Wraps a test framework to run each test worker in a separate working directory. |
| 36 | + */ |
| 37 | +public class RunInSubdirectoryTestFramework implements TestFramework { |
| 38 | + private static final String GEMFIRE_PROPERTIES = "gemfire.properties"; |
| 39 | + private final AtomicLong workerId = new AtomicLong(); |
| 40 | + private final TestFramework delegate; |
| 41 | + |
| 42 | + public RunInSubdirectoryTestFramework(TestFramework delegate) { |
| 43 | + this.delegate = delegate; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public TestFrameworkDetector getDetector() { |
| 48 | + return delegate.getDetector(); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public TestFrameworkOptions getOptions() { |
| 53 | + return delegate.getOptions(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public WorkerTestClassProcessorFactory getProcessorFactory() { |
| 58 | + return delegate.getProcessorFactory(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Return an action that configures the test worker builder to run the test worker in a unique |
| 63 | + * subdirectory of the task's working directory. |
| 64 | + */ |
| 65 | + @Override |
| 66 | + public Action<WorkerProcessBuilder> getWorkerConfigurationAction() { |
| 67 | + return workerProcessBuilder -> { |
| 68 | + delegate.getWorkerConfigurationAction().execute(workerProcessBuilder); |
| 69 | + JavaExecHandleBuilder javaCommand = workerProcessBuilder.getJavaCommand(); |
| 70 | + |
| 71 | + Path taskWorkingDir = javaCommand.getWorkingDir().toPath(); |
| 72 | + String workerWorkingDirName = String.format("test-worker-%06d", workerId.incrementAndGet()); |
| 73 | + Path workerWorkingDir = taskWorkingDir.resolve(workerWorkingDirName); |
| 74 | + |
| 75 | + createWorkingDir(workerWorkingDir); |
| 76 | + copyGemFirePropertiesFile(taskWorkingDir, workerWorkingDir); |
| 77 | + |
| 78 | + javaCommand.setWorkingDir(workerWorkingDir); |
| 79 | + }; |
| 80 | + } |
| 81 | + |
| 82 | + private void copyGemFirePropertiesFile(Path taskWorkingDir, Path workerWorkingDir) { |
| 83 | + Path taskPropertiesFile = taskWorkingDir.resolve(GEMFIRE_PROPERTIES); |
| 84 | + if (!Files.exists(taskPropertiesFile)) { |
| 85 | + return; |
| 86 | + } |
| 87 | + Path workerPropertiesFile = workerWorkingDir.resolve(taskPropertiesFile.getFileName()); |
| 88 | + try { |
| 89 | + Files.copy(taskPropertiesFile, workerPropertiesFile, COPY_ATTRIBUTES); |
| 90 | + } catch (IOException e) { |
| 91 | + throw new UncheckedIOException(e); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private void createWorkingDir(Path workerWorkingDir) { |
| 96 | + try { |
| 97 | + Files.createDirectories(workerWorkingDir); |
| 98 | + } catch (IOException e) { |
| 99 | + throw new UncheckedIOException(e); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments